1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-05-04 21:16:29 +03:00

general code refactor and bug fixes

This commit is contained in:
l1ttleO 2022-02-02 18:14:26 +05:00
parent 4d838e5af3
commit 04facc3de2
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
15 changed files with 197 additions and 132 deletions

View file

@ -9,42 +9,49 @@ namespace Boyfriend.Commands;
public class BanCommand : Command {
public override async Task Run(SocketCommandContext context, string[] args) {
var toBan = await Utils.ParseUser(args[0]);
var reason = Utils.JoinString(args, 1);
TimeSpan duration;
try {
duration = Utils.GetTimeSpan(args[1]);
reason = Utils.JoinString(args, 2);
}
catch (Exception e) when (e is ArgumentNullException or FormatException or OverflowException) {
} catch (Exception e) when (e is ArgumentNullException or FormatException or OverflowException) {
duration = TimeSpan.FromMilliseconds(-1);
}
var author = context.Guild.GetUser(context.User.Id);
await CommandHandler.CheckPermissions(author, GuildPermission.BanMembers);
var memberToBan = context.Guild.GetUser(toBan.Id);
if (memberToBan != null)
await CommandHandler.CheckInteractions(author, memberToBan);
await BanUser(context.Guild, context.Channel as ITextChannel, context.Guild.GetUser(context.User.Id),
toBan, duration, reason);
await Utils.ParseUser(args[0]), duration, reason);
}
public static async Task BanUser(IGuild guild, ITextChannel? channel, IGuildUser author, IUser toBan,
TimeSpan duration, string reason) {
var authorMention = author.Mention;
var guildBanMessage = $"({Utils.GetNameAndDiscrim(author)}) {reason}";
var memberToBan = await guild.GetUserAsync(toBan.Id);
var notification = string.Format(Messages.UserBanned, authorMention, toBan.Mention, Utils.WrapInline(reason));
await CommandHandler.CheckPermissions(author, GuildPermission.BanMembers);
if (memberToBan != null)
await CommandHandler.CheckInteractions(author, memberToBan);
await Utils.SendDirectMessage(toBan, string.Format(Messages.YouWereBanned, author.Mention, guild.Name,
Utils.WrapInline(reason)));
var guildBanMessage = $"({author.Username}#{author.Discriminator}) {reason}";
await guild.AddBanAsync(toBan, 0, guildBanMessage);
var notification = string.Format(Messages.UserBanned, authorMention, toBan.Mention, Utils.WrapInline(reason));
await Utils.SilentSendAsync(channel, string.Format(Messages.BanResponse, toBan.Mention,
Utils.WrapInline(reason)));
await Utils.SilentSendAsync(await guild.GetSystemChannelAsync(), notification);
await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(guild), notification);
var task = new Task(() => UnbanCommand.UnbanUser(guild, null, guild.GetCurrentUserAsync().Result, toBan,
Messages.PunishmentExpired));
await Utils.StartDelayed(task, duration, () => guild.GetBanAsync(toBan).Result != null);
async void UnbanWhenExpires() {
try {
await UnbanCommand.UnbanUser(guild, null, await guild.GetCurrentUserAsync(), toBan,
Messages.PunishmentExpired);
} catch (ApplicationException) {}
}
await Utils.StartDelayed(new Task(UnbanWhenExpires), duration);
}
public override List<string> GetAliases() {
@ -58,4 +65,4 @@ public class BanCommand : Command {
public override string GetSummary() {
return "Банит пользователя";
}
}
}

View file

@ -9,16 +9,19 @@ namespace Boyfriend.Commands;
public class ClearCommand : Command {
public override async Task Run(SocketCommandContext context, string[] args) {
var user = context.User;
int toDelete;
try {
toDelete = Convert.ToInt32(args[0]);
}
catch (Exception e) when (e is FormatException or OverflowException) {
} catch (Exception e) when (e is FormatException or OverflowException) {
throw new ApplicationException(Messages.ClearInvalidAmountSpecified);
}
if (context.Channel is not ITextChannel channel) return;
await CommandHandler.CheckPermissions(context.Guild.GetUser(context.User.Id), GuildPermission.ManageMessages);
await CommandHandler.CheckPermissions(context.Guild.GetUser(user.Id), GuildPermission.ManageMessages);
switch (toDelete) {
case < 1:
throw new ApplicationException(Messages.ClearNegativeAmount);
@ -26,9 +29,11 @@ public class ClearCommand : Command {
throw new ApplicationException(Messages.ClearAmountTooLarge);
default: {
var messages = await channel.GetMessagesAsync(toDelete + 1).FlattenAsync();
await channel.DeleteMessagesAsync(messages);
await channel.DeleteMessagesAsync(messages, Utils.GetRequestOptions(Utils.GetNameAndDiscrim(user)));
await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(context.Guild),
string.Format(Messages.MessagesDeleted, context.User.Mention, toDelete + 1,
string.Format(Messages.MessagesDeleted, user.Mention, toDelete + 1,
Utils.MentionChannel(context.Channel.Id)));
break;
}
@ -46,4 +51,4 @@ public class ClearCommand : Command {
public override string GetSummary() {
return "Очищает сообщения";
}
}
}

View file

@ -8,11 +8,11 @@ namespace Boyfriend.Commands;
public class HelpCommand : Command {
public override async Task Run(SocketCommandContext context, string[] args) {
var nl = Environment.NewLine;
var toSend = string.Format(Messages.CommandHelp, nl);
var prefix = Boyfriend.GetGuildConfig(context.Guild).Prefix;
var toSend = string.Format(Messages.CommandHelp, nl);
toSend = CommandHandler.Commands.Aggregate(toSend,
(current, command) => current + $"`{prefix}{command.GetAliases()[0]}`: {command.GetSummary()}{nl}");
await context.Channel.SendMessageAsync(toSend);
}
@ -27,4 +27,4 @@ public class HelpCommand : Command {
public override string GetSummary() {
return "Показывает эту справку";
}
}
}

View file

@ -9,24 +9,27 @@ namespace Boyfriend.Commands;
public class KickCommand : Command {
public override async Task Run(SocketCommandContext context, string[] args) {
var reason = Utils.JoinString(args, 1);
var author = context.Guild.GetUser(context.User.Id);
var toKick = await Utils.ParseMember(context.Guild, args[0]);
await CommandHandler.CheckPermissions(author, GuildPermission.KickMembers);
await CommandHandler.CheckInteractions(author, toKick);
KickMember(context.Guild, context.Channel as ITextChannel, context.Guild.GetUser(context.User.Id), toKick,
reason);
await KickMember(context.Guild, context.Channel as ITextChannel, author, toKick, Utils.JoinString(args, 1));
}
private static async void KickMember(IGuild guild, ITextChannel? channel, IUser author, IGuildUser toKick,
private static async Task KickMember(IGuild guild, ITextChannel? channel, IUser author, IGuildUser toKick,
string reason) {
var authorMention = author.Mention;
await Utils.SendDirectMessage(toKick, string.Format(Messages.YouWereKicked, authorMention, guild.Name,
Utils.WrapInline(reason)));
var guildKickMessage = $"({author.Username}#{author.Discriminator}) {reason}";
await toKick.KickAsync(guildKickMessage);
var guildKickMessage = $"({Utils.GetNameAndDiscrim(author)}) {reason}";
var notification = string.Format(Messages.MemberKicked, authorMention, toKick.Mention,
Utils.WrapInline(reason));
await Utils.SendDirectMessage(toKick, string.Format(Messages.YouWereKicked, authorMention, guild.Name,
Utils.WrapInline(reason)));
await toKick.KickAsync(guildKickMessage);
await Utils.SilentSendAsync(channel, string.Format(Messages.KickResponse, toKick.Mention,
Utils.WrapInline(reason)));
await Utils.SilentSendAsync(await guild.GetSystemChannelAsync(), notification);
@ -44,4 +47,4 @@ public class KickCommand : Command {
public override string GetSummary() {
return "Выгоняет участника";
}
}
}

View file

@ -1,5 +1,6 @@
using Discord;
using Discord.Commands;
using Discord.Net;
// ReSharper disable UnusedType.Global
// ReSharper disable UnusedMember.Global
@ -9,27 +10,29 @@ namespace Boyfriend.Commands;
public class MuteCommand : Command {
public override async Task Run(SocketCommandContext context, string[] args) {
TimeSpan duration;
var author = context.Guild.GetUser(context.User.Id);
var config = Boyfriend.GetGuildConfig(context.Guild);
var reason = Utils.JoinString(args, 1);
var role = Utils.GetMuteRole(context.Guild);
var rolesRemoved = config.RolesRemovedOnMute!;
var toMute = await Utils.ParseMember(context.Guild, args[0]);
TimeSpan duration;
try {
duration = Utils.GetTimeSpan(args[1]);
reason = Utils.JoinString(args, 2);
}
catch (Exception e) when (e is ArgumentNullException or FormatException or OverflowException) {
} catch (Exception e) when (e is ArgumentNullException or FormatException or OverflowException) {
duration = TimeSpan.FromMilliseconds(-1);
}
var author = context.Guild.GetUser(context.User.Id);
var toMute = await Utils.ParseMember(context.Guild, args[0]);
if (toMute == null)
throw new ApplicationException(Messages.UserNotInGuild);
var role = Utils.GetMuteRole(context.Guild);
if (role != null && toMute.RoleIds.Any(x => x == role.Id) ||
toMute.TimedOutUntil != null && toMute.TimedOutUntil.Value.ToUnixTimeMilliseconds()
> DateTimeOffset.Now.ToUnixTimeMilliseconds())
throw new ApplicationException(Messages.MemberAlreadyMuted);
var config = Boyfriend.GetGuildConfig(context.Guild);
var rolesRemoved = config.RolesRemovedOnMute!;
if (rolesRemoved.ContainsKey(toMute.Id)) {
foreach (var roleId in rolesRemoved[toMute.Id]) await toMute.AddRoleAsync(roleId);
rolesRemoved.Remove(toMute.Id);
@ -40,44 +43,53 @@ public class MuteCommand : Command {
await CommandHandler.CheckPermissions(author, GuildPermission.ModerateMembers, GuildPermission.ManageRoles);
await CommandHandler.CheckInteractions(author, toMute);
MuteMember(context.Guild, context.Channel as ITextChannel, context.Guild.GetUser(context.User.Id), toMute,
await MuteMember(context.Guild, context.Channel as ITextChannel, context.Guild.GetUser(context.User.Id), toMute,
duration, reason);
}
private static async void MuteMember(IGuild guild, ITextChannel? channel, IGuildUser author, IGuildUser toMute,
private static async Task MuteMember(IGuild guild, ITextChannel? channel, IGuildUser author, IGuildUser toMute,
TimeSpan duration, string reason) {
await CommandHandler.CheckPermissions(author, GuildPermission.ManageMessages, GuildPermission.ManageRoles);
var authorMention = author.Mention;
var role = Utils.GetMuteRole(guild);
var config = Boyfriend.GetGuildConfig(guild);
if (config.RemoveRolesOnMute.GetValueOrDefault(false) && role != null) {
var rolesRemoved = new List<ulong>();
try {
var requestOptions = Utils.GetRequestOptions($"({Utils.GetNameAndDiscrim(author)}) {reason}");
var role = Utils.GetMuteRole(guild);
if (role != null) {
if (config.RemoveRolesOnMute.GetValueOrDefault(false)) {
var rolesRemoved = new List<ulong>();
foreach (var roleId in toMute.RoleIds) {
if (roleId == guild.Id) continue;
await toMute.RemoveRoleAsync(roleId);
rolesRemoved.Add(roleId);
try {
if (roleId == guild.Id) continue;
if (roleId == role.Id) continue;
await toMute.RemoveRoleAsync(roleId);
rolesRemoved.Add(roleId);
} catch (HttpException) {}
}
config.RolesRemovedOnMute!.Add(toMute.Id, rolesRemoved);
await config.Save();
}
catch (NullReferenceException) { }
config.RolesRemovedOnMute!.Add(toMute.Id, rolesRemoved);
await config.Save();
}
if (role != null)
await toMute.AddRoleAsync(role);
else
await toMute.SetTimeOutAsync(duration);
await toMute.AddRoleAsync(role, requestOptions);
} else
await toMute.SetTimeOutAsync(duration, requestOptions);
var notification = string.Format(Messages.MemberMuted, authorMention, toMute.Mention, Utils.WrapInline(reason));
await Utils.SilentSendAsync(channel, string.Format(Messages.MuteResponse, toMute.Mention,
Utils.WrapInline(reason)));
await Utils.SilentSendAsync(await guild.GetSystemChannelAsync(), notification);
await Utils.SilentSendAsync(await Utils.GetAdminLogChannel(guild), notification);
var task = new Task(() => UnmuteCommand.UnmuteMember(guild, null, guild.GetCurrentUserAsync().Result, toMute,
Messages.PunishmentExpired));
async void UnmuteWhenExpires() {
try {
await UnmuteCommand.UnmuteMember(guild, null, await guild.GetCurrentUserAsync(), toMute,
Messages.PunishmentExpired);
} catch (ApplicationException) {}
}
if (role != null)
await Utils.StartDelayed(task, duration, () => toMute.RoleIds.Any(x => x == role.Id));
await Utils.StartDelayed(new Task(UnmuteWhenExpires), duration);
}
public override List<string> GetAliases() {

View file

@ -9,9 +9,11 @@ namespace Boyfriend.Commands;
public class SettingsCommand : Command {
public override async Task Run(SocketCommandContext context, string[] args) {
await CommandHandler.CheckPermissions(context.Guild.GetUser(context.User.Id), GuildPermission.ManageGuild);
var config = Boyfriend.GetGuildConfig(context.Guild);
var guild = context.Guild;
await CommandHandler.CheckPermissions(context.Guild.GetUser(context.User.Id), GuildPermission.ManageGuild);
if (args.Length == 0) {
var nl = Environment.NewLine;
var adminLogChannel = guild.GetTextChannel(config.AdminLogChannel.GetValueOrDefault(0));
@ -43,8 +45,9 @@ public class SettingsCommand : Command {
}
var setting = args[0].ToLower();
var value = "";
var shouldDefault = false;
var value = "";
if (args.Length >= 2)
value = args[1].ToLower();
else

View file

@ -9,20 +9,25 @@ namespace Boyfriend.Commands;
public class UnbanCommand : Command {
public override async Task Run(SocketCommandContext context, string[] args) {
var toUnban = await Utils.ParseUser(args[0]);
if (context.Guild.GetBanAsync(toUnban.Id) == null)
throw new ApplicationException(Messages.UserNotBanned);
UnbanUser(context.Guild, context.Channel as ITextChannel, context.Guild.GetUser(context.User.Id), toUnban,
Utils.JoinString(args, 1));
await UnbanUser(context.Guild, context.Channel as ITextChannel, context.Guild.GetUser(context.User.Id),
await Utils.ParseUser(args[0]), Utils.JoinString(args, 1));
}
public static async void UnbanUser(IGuild guild, ITextChannel? channel, IGuildUser author, IUser toUnban,
public static async Task UnbanUser(IGuild guild, ITextChannel? channel, IGuildUser author, IUser toUnban,
string reason) {
await CommandHandler.CheckPermissions(author, GuildPermission.BanMembers);
var authorMention = author.Mention;
var notification = string.Format(Messages.UserUnbanned, authorMention, toUnban.Mention,
Utils.WrapInline(reason));
await guild.RemoveBanAsync(toUnban);
var requestOptions = Utils.GetRequestOptions($"({Utils.GetNameAndDiscrim(author)}) {reason}");
await CommandHandler.CheckPermissions(author, GuildPermission.BanMembers);
if (guild.GetBanAsync(toUnban.Id) == null)
throw new ApplicationException(Messages.UserNotBanned);
await guild.RemoveBanAsync(toUnban, requestOptions);
await Utils.SilentSendAsync(channel, string.Format(Messages.UnbanResponse, toUnban.Mention,
Utils.WrapInline(reason)));
await Utils.SilentSendAsync(await guild.GetSystemChannelAsync(), notification);

View file

@ -9,49 +9,45 @@ namespace Boyfriend.Commands;
public class UnmuteCommand : Command {
public override async Task Run(SocketCommandContext context, string[] args) {
var toUnmute = await Utils.ParseMember(context.Guild, args[0]);
var author = context.Guild.GetUser(context.User.Id);
await CommandHandler.CheckPermissions(author, GuildPermission.ManageMessages, GuildPermission.ManageRoles);
await UnmuteMember(context.Guild, context.Channel as ITextChannel, context.Guild.GetUser(context.User.Id),
await Utils.ParseMember(context.Guild, args[0]), Utils.JoinString(args, 1));
}
public static async Task UnmuteMember(IGuild guild, ITextChannel? channel, IGuildUser author, IGuildUser toUnmute,
string reason) {
await CommandHandler.CheckPermissions(author, GuildPermission.ModerateMembers, GuildPermission.ManageRoles);
await CommandHandler.CheckInteractions(author, toUnmute);
var role = Utils.GetMuteRole(context.Guild);
var authorMention = author.Mention;
var config = Boyfriend.GetGuildConfig(guild);
var notification = string.Format(Messages.MemberUnmuted, authorMention, toUnmute.Mention,
Utils.WrapInline(reason));
var requestOptions = Utils.GetRequestOptions($"({Utils.GetNameAndDiscrim(author)}) {reason}");
var role = Utils.GetMuteRole(guild);
if (role != null) {
if (toUnmute.RoleIds.All(x => x != role.Id)) {
var config = Boyfriend.GetGuildConfig(context.Guild);
var rolesRemoved = config.RolesRemovedOnMute;
foreach (var roleId in rolesRemoved![toUnmute.Id]) await toUnmute.AddRoleAsync(roleId);
await toUnmute.AddRolesAsync(rolesRemoved![toUnmute.Id]);
rolesRemoved.Remove(toUnmute.Id);
await config.Save();
throw new ApplicationException(Messages.RolesReturned);
}
}
if (role != null && toUnmute.RoleIds.All(x => x != role.Id) ||
toUnmute.TimedOutUntil == null || toUnmute.TimedOutUntil.Value.ToUnixTimeMilliseconds()
< DateTimeOffset.Now.ToUnixTimeMilliseconds())
throw new ApplicationException(Messages.MemberNotMuted);
UnmuteMember(context.Guild, context.Channel as ITextChannel, context.Guild.GetUser(context.User.Id),
toUnmute, Utils.JoinString(args, 1));
}
public static async void UnmuteMember(IGuild guild, ITextChannel? channel, IGuildUser author, IGuildUser toUnmute,
string reason) {
await CommandHandler.CheckPermissions(author, GuildPermission.ManageMessages, GuildPermission.ManageRoles);
var authorMention = author.Mention;
var notification = string.Format(Messages.MemberUnmuted, authorMention, toUnmute.Mention,
Utils.WrapInline(reason));
var role = Utils.GetMuteRole(guild);
if (role != null) {
await toUnmute.RemoveRoleAsync(role);
var config = Boyfriend.GetGuildConfig(guild);
if (toUnmute.RoleIds.All(x => x != role.Id))
throw new ApplicationException(Messages.MemberNotMuted);
await toUnmute.RemoveRoleAsync(role, requestOptions);
if (config.RolesRemovedOnMute!.ContainsKey(toUnmute.Id)) {
foreach (var roleId in config.RolesRemovedOnMute[toUnmute.Id]) await toUnmute.AddRoleAsync(roleId);
await toUnmute.AddRolesAsync(config.RolesRemovedOnMute[toUnmute.Id]);
config.RolesRemovedOnMute.Remove(toUnmute.Id);
await config.Save();
}
} else {
if (toUnmute.TimedOutUntil == null || toUnmute.TimedOutUntil.Value.ToUnixTimeMilliseconds()
< DateTimeOffset.Now.ToUnixTimeMilliseconds())
throw new ApplicationException(Messages.MemberNotMuted);
await toUnmute.RemoveTimeOutAsync();
}