mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-05-06 05:56:29 +03:00
general code refactor and bug fixes
This commit is contained in:
parent
4d838e5af3
commit
04facc3de2
15 changed files with 197 additions and 132 deletions
|
@ -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() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue