mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-05-06 14:06:29 +03:00
Async message handling, CommandHandler rewrite and rename
This commit is contained in:
parent
53f13d88a5
commit
ac63719a0b
19 changed files with 1061 additions and 711 deletions
|
@ -1,74 +1,44 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class BanCommand : Command {
|
||||
public override string[] Aliases { get; } = { "ban", "бан" };
|
||||
public override int ArgsLengthRequired => 2;
|
||||
|
||||
public override async Task Run(SocketCommandContext context, string[] args) {
|
||||
var toBan = Utils.ParseUser(args[0]);
|
||||
public override async Task Run(CommandProcessor cmd, string[] args) {
|
||||
var toBan = cmd.GetUser(args, 0, "ToBan");
|
||||
if (toBan == null || !cmd.HasPermission(GuildPermission.BanMembers)) return;
|
||||
|
||||
if (toBan == null) {
|
||||
Error(Messages.UserDoesntExist, false);
|
||||
return;
|
||||
}
|
||||
var memberToBan = cmd.GetMember(toBan, null);
|
||||
if (memberToBan != null && !cmd.CanInteractWith(memberToBan, "Ban")) return;
|
||||
|
||||
var guild = context.Guild;
|
||||
var author = (SocketGuildUser)context.User;
|
||||
var duration = CommandProcessor.GetTimeSpan(args, 1);
|
||||
var reason = cmd.GetRemaining(args, duration.TotalSeconds < 1 ? 1 : 2, "BanReason");
|
||||
if (reason == null) return;
|
||||
|
||||
var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.BanMembers);
|
||||
if (permissionCheckResponse is not "") {
|
||||
Error(permissionCheckResponse, true);
|
||||
return;
|
||||
}
|
||||
|
||||
var reason = Utils.JoinString(ref args, 2);
|
||||
var memberToBan = Utils.ParseMember(guild, args[0]);
|
||||
|
||||
if (memberToBan != null) {
|
||||
var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref memberToBan);
|
||||
if (interactionCheckResponse is not "") {
|
||||
Error(interactionCheckResponse, true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var duration = Utils.GetTimeSpan(ref args[1]) ?? TimeSpan.FromMilliseconds(-1);
|
||||
if (duration.TotalSeconds < 0) {
|
||||
Warn(Messages.DurationParseFailed);
|
||||
reason = Utils.JoinString(ref args, 1);
|
||||
|
||||
if (reason is "") {
|
||||
Error(Messages.ReasonRequired, false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await BanUser(guild, author, toBan, duration, reason);
|
||||
await BanUser(cmd, toBan, duration, reason);
|
||||
}
|
||||
|
||||
public static async Task BanUser(SocketGuild guild, SocketGuildUser author, SocketUser toBan, TimeSpan duration,
|
||||
string reason) {
|
||||
var guildBanMessage = $"({author}) {reason}";
|
||||
|
||||
public static async Task BanUser(CommandProcessor cmd, SocketUser toBan, TimeSpan duration, string reason) {
|
||||
var author = cmd.Context.User;
|
||||
var guild = cmd.Context.Guild;
|
||||
await Utils.SendDirectMessage(toBan,
|
||||
string.Format(Messages.YouWereBanned, author.Mention, guild.Name, Utils.Wrap(reason)));
|
||||
|
||||
var guildBanMessage = $"({author}) {reason}";
|
||||
await guild.AddBanAsync(toBan, 0, guildBanMessage);
|
||||
|
||||
var feedback = string.Format(Messages.FeedbackUserBanned, toBan.Mention,
|
||||
Utils.GetHumanizedTimeOffset(ref duration), Utils.Wrap(reason));
|
||||
Success(feedback, author.Mention, false, false);
|
||||
await Utils.SendFeedback(feedback, guild.Id, author.Mention, true);
|
||||
Utils.GetHumanizedTimeOffset(duration), Utils.Wrap(reason));
|
||||
cmd.Reply(feedback, ":hammer: ");
|
||||
cmd.Audit(feedback);
|
||||
|
||||
if (duration.TotalSeconds > 0) {
|
||||
var _ = async () => {
|
||||
await Task.Delay(duration);
|
||||
await UnbanCommand.UnbanUser(guild, guild.CurrentUser, toBan, Messages.PunishmentExpired);
|
||||
await UnbanCommand.UnbanUser(cmd, toBan.Id, Messages.PunishmentExpired);
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,46 +1,23 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class ClearCommand : Command {
|
||||
public override string[] Aliases { get; } = { "clear", "purge", "очистить", "стереть" };
|
||||
public override int ArgsLengthRequired => 1;
|
||||
|
||||
public override async Task Run(SocketCommandContext context, string[] args) {
|
||||
var user = (SocketGuildUser)context.User;
|
||||
public override async Task Run(CommandProcessor cmd, string[] args) {
|
||||
if (cmd.Context.Channel is not SocketTextChannel channel) throw new Exception();
|
||||
|
||||
if (context.Channel is not SocketTextChannel channel) throw new Exception();
|
||||
if (!cmd.HasPermission(GuildPermission.ManageMessages)) return;
|
||||
|
||||
var permissionCheckResponse = CommandHandler.HasPermission(ref user, GuildPermission.ManageMessages);
|
||||
if (permissionCheckResponse is not "") {
|
||||
Error(permissionCheckResponse, true);
|
||||
return;
|
||||
}
|
||||
var toDelete = cmd.GetNumberRange(args, 0, 1, 200, "ClearAmount");
|
||||
if (toDelete == null) return;
|
||||
var messages = await channel.GetMessagesAsync((int)(toDelete + 1)).FlattenAsync();
|
||||
|
||||
if (!int.TryParse(args[0], out var toDelete)) {
|
||||
Error(Messages.ClearInvalidAmountSpecified, false);
|
||||
return;
|
||||
}
|
||||
var user = (SocketGuildUser)cmd.Context.User;
|
||||
await channel.DeleteMessagesAsync(messages, Utils.GetRequestOptions(user.ToString()!));
|
||||
|
||||
switch (toDelete) {
|
||||
case < 1:
|
||||
Error(Messages.ClearNegativeAmount, false);
|
||||
break;
|
||||
case > 200:
|
||||
Error(Messages.ClearAmountTooLarge, false);
|
||||
break;
|
||||
default:
|
||||
var messages = await channel.GetMessagesAsync(toDelete + 1).FlattenAsync();
|
||||
|
||||
await channel.DeleteMessagesAsync(messages, Utils.GetRequestOptions(user.ToString()!));
|
||||
|
||||
await Utils.SendFeedback(
|
||||
string.Format(Messages.FeedbackMessagesCleared, (toDelete + 1).ToString(), channel.Mention),
|
||||
context.Guild.Id, user.Mention);
|
||||
|
||||
break;
|
||||
}
|
||||
cmd.Audit(string.Format(Messages.FeedbackMessagesCleared, (toDelete + 1).ToString()));
|
||||
}
|
||||
}
|
|
@ -1,31 +1,7 @@
|
|||
using System.Text;
|
||||
using Discord.Commands;
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public abstract class Command {
|
||||
public abstract string[] Aliases { get; }
|
||||
|
||||
public abstract int ArgsLengthRequired { get; }
|
||||
public abstract Task Run(SocketCommandContext context, string[] args);
|
||||
|
||||
protected static void Output(ref StringBuilder message) {
|
||||
CommandHandler.StackedReplyMessage.Append(message).AppendLine();
|
||||
}
|
||||
|
||||
protected static void Success(string message, string userMention, bool sendPublicFeedback = false,
|
||||
bool sendPrivateFeedback = true) {
|
||||
CommandHandler.StackedReplyMessage.Append(":white_check_mark: ").AppendLine(message);
|
||||
if (sendPrivateFeedback)
|
||||
Utils.StackFeedback(ref message, ref userMention, sendPublicFeedback);
|
||||
}
|
||||
|
||||
protected static void Warn(string message) {
|
||||
CommandHandler.StackedReplyMessage.Append(":warning: ").AppendLine(message);
|
||||
}
|
||||
|
||||
protected static void Error(string message, bool accessDenied) {
|
||||
var symbol = accessDenied ? ":no_entry_sign: " : ":x: ";
|
||||
CommandHandler.StackedReplyMessage.Append(symbol).AppendLine(message);
|
||||
}
|
||||
public abstract Task Run(CommandProcessor cmd, string[] args);
|
||||
}
|
|
@ -1,20 +1,18 @@
|
|||
using Discord.Commands;
|
||||
using Humanizer;
|
||||
using Humanizer;
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class HelpCommand : Command {
|
||||
public override string[] Aliases { get; } = {"help", "помощь", "справка"};
|
||||
public override int ArgsLengthRequired => 0;
|
||||
public override string[] Aliases { get; } = { "help", "помощь", "справка" };
|
||||
|
||||
public override Task Run(SocketCommandContext context, string[] args) {
|
||||
var prefix = Boyfriend.GetGuildConfig(context.Guild.Id)["Prefix"];
|
||||
public override Task Run(CommandProcessor cmd, string[] args) {
|
||||
var prefix = Boyfriend.GetGuildConfig(cmd.Context.Guild.Id)["Prefix"];
|
||||
var toSend = Boyfriend.StringBuilder.Append(Messages.CommandHelp);
|
||||
|
||||
foreach (var command in CommandHandler.Commands)
|
||||
foreach (var command in CommandProcessor.Commands)
|
||||
toSend.Append(
|
||||
$"\n`{prefix}{command.Aliases[0]}`: {Utils.GetMessage($"CommandDescription{command.Aliases[0].Titleize()}")}");
|
||||
Output(ref toSend);
|
||||
cmd.Reply(toSend.ToString(), ":page_facing_up: ");
|
||||
toSend.Clear();
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
|
|
@ -1,49 +1,31 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class KickCommand : Command {
|
||||
public override string[] Aliases { get; } = { "kick", "кик", "выгнать" };
|
||||
public override int ArgsLengthRequired => 2;
|
||||
|
||||
public override async Task Run(SocketCommandContext context, string[] args) {
|
||||
var author = (SocketGuildUser)context.User;
|
||||
public override async Task Run(CommandProcessor cmd, string[] args) {
|
||||
var toKick = cmd.GetMember(args, 0, "ToKick");
|
||||
if (toKick == null || !cmd.HasPermission(GuildPermission.KickMembers)) return;
|
||||
|
||||
var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.KickMembers);
|
||||
if (permissionCheckResponse is not "") {
|
||||
Error(permissionCheckResponse, true);
|
||||
return;
|
||||
}
|
||||
if (!cmd.CanInteractWith(toKick, "Kick")) return;
|
||||
|
||||
var toKick = Utils.ParseMember(context.Guild, args[0]);
|
||||
|
||||
if (toKick == null) {
|
||||
Error(Messages.UserNotInGuild, false);
|
||||
return;
|
||||
}
|
||||
|
||||
var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref toKick);
|
||||
if (interactionCheckResponse is not "") {
|
||||
Error(interactionCheckResponse, true);
|
||||
return;
|
||||
}
|
||||
|
||||
await KickMember(context.Guild, author, toKick, Utils.JoinString(ref args, 1));
|
||||
|
||||
Success(
|
||||
string.Format(Messages.FeedbackMemberKicked, toKick.Mention,
|
||||
Utils.Wrap(Utils.JoinString(ref args, 1))), author.Mention);
|
||||
await KickMember(cmd, toKick, cmd.GetRemaining(args, 1, "KickReason"));
|
||||
}
|
||||
|
||||
private static async Task KickMember(IGuild guild, SocketUser author, SocketGuildUser toKick, string reason) {
|
||||
var authorMention = author.Mention;
|
||||
var guildKickMessage = $"({author}) {reason}";
|
||||
private static async Task KickMember(CommandProcessor cmd, SocketGuildUser toKick, string? reason) {
|
||||
if (reason == null) return;
|
||||
var guildKickMessage = $"({cmd.Context.User}) {reason}";
|
||||
|
||||
await Utils.SendDirectMessage(toKick,
|
||||
string.Format(Messages.YouWereKicked, authorMention, guild.Name, Utils.Wrap(reason)));
|
||||
string.Format(Messages.YouWereKicked, cmd.Context.User.Mention, cmd.Context.Guild.Name,
|
||||
Utils.Wrap(reason)));
|
||||
|
||||
await toKick.KickAsync(guildKickMessage);
|
||||
var format = string.Format(Messages.FeedbackMemberKicked, toKick.Mention, Utils.Wrap(reason));
|
||||
cmd.Reply(format, ":police_car: ");
|
||||
cmd.Audit(format);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.Net;
|
||||
using Discord.WebSocket;
|
||||
|
||||
|
@ -7,72 +6,45 @@ namespace Boyfriend.Commands;
|
|||
|
||||
public class MuteCommand : Command {
|
||||
public override string[] Aliases { get; } = { "mute", "timeout", "заглушить", "мут" };
|
||||
public override int ArgsLengthRequired => 2;
|
||||
|
||||
public override async Task Run(SocketCommandContext context, string[] args) {
|
||||
var toMute = Utils.ParseMember(context.Guild, args[0]);
|
||||
var reason = Utils.JoinString(ref args, 2);
|
||||
public override async Task Run(CommandProcessor cmd, string[] args) {
|
||||
var toMute = cmd.GetMember(args, 0, "ToMute");
|
||||
if (toMute == null) return;
|
||||
|
||||
var duration = Utils.GetTimeSpan(ref args[1]) ?? TimeSpan.FromMilliseconds(-1);
|
||||
if (duration.TotalSeconds < 0) {
|
||||
Warn(Messages.DurationParseFailed);
|
||||
reason = Utils.JoinString(ref args, 1);
|
||||
}
|
||||
|
||||
|
||||
if (reason is "") {
|
||||
Error(Messages.ReasonRequired, false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (toMute == null) {
|
||||
Error(Messages.UserNotInGuild, false);
|
||||
return;
|
||||
}
|
||||
|
||||
var guild = context.Guild;
|
||||
var role = Utils.GetMuteRole(ref guild);
|
||||
var duration = CommandProcessor.GetTimeSpan(args, 1);
|
||||
var reason = cmd.GetRemaining(args, duration.TotalSeconds < 1 ? 1 : 2, "MuteReason");
|
||||
if (reason == null) return;
|
||||
var role = Utils.GetMuteRole(cmd.Context.Guild);
|
||||
|
||||
if (role != null) {
|
||||
if (toMute.Roles.Contains(role) || (toMute.TimedOutUntil != null &&
|
||||
toMute.TimedOutUntil.Value.ToUnixTimeMilliseconds() >
|
||||
DateTimeOffset.Now.ToUnixTimeMilliseconds())) {
|
||||
Error(Messages.MemberAlreadyMuted, false);
|
||||
cmd.Reply(Messages.MemberAlreadyMuted, ":x: ");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var rolesRemoved = Boyfriend.GetRemovedRoles(context.Guild.Id);
|
||||
var rolesRemoved = Boyfriend.GetRemovedRoles(cmd.Context.Guild.Id);
|
||||
|
||||
if (rolesRemoved.ContainsKey(toMute.Id)) {
|
||||
foreach (var roleId in rolesRemoved[toMute.Id]) await toMute.AddRoleAsync(roleId);
|
||||
rolesRemoved.Remove(toMute.Id);
|
||||
CommandHandler.ConfigWriteScheduled = true;
|
||||
Warn(Messages.RolesReturned);
|
||||
cmd.ConfigWriteScheduled = true;
|
||||
cmd.Reply(Messages.RolesReturned, ":warning: ");
|
||||
}
|
||||
|
||||
var author = (SocketGuildUser)context.User;
|
||||
if (!cmd.HasPermission(GuildPermission.ModerateMembers) || !cmd.CanInteractWith(toMute, "Mute")) return;
|
||||
|
||||
var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.ModerateMembers);
|
||||
if (permissionCheckResponse is not "") {
|
||||
Error(permissionCheckResponse, true);
|
||||
return;
|
||||
}
|
||||
|
||||
var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref toMute);
|
||||
if (interactionCheckResponse is not "") {
|
||||
Error(interactionCheckResponse, true);
|
||||
return;
|
||||
}
|
||||
|
||||
await MuteMember(guild, author, toMute, duration, reason);
|
||||
await MuteMember(cmd, toMute, duration, reason);
|
||||
}
|
||||
|
||||
private static async Task MuteMember(SocketGuild guild, SocketUser author, SocketGuildUser toMute,
|
||||
private static async Task MuteMember(CommandProcessor cmd, SocketGuildUser toMute,
|
||||
TimeSpan duration, string reason) {
|
||||
var guild = cmd.Context.Guild;
|
||||
var config = Boyfriend.GetGuildConfig(guild.Id);
|
||||
var requestOptions = Utils.GetRequestOptions($"({author}) {reason}");
|
||||
var role = Utils.GetMuteRole(ref guild);
|
||||
var requestOptions = Utils.GetRequestOptions($"({cmd.Context.User}) {reason}");
|
||||
var role = Utils.GetMuteRole(guild);
|
||||
var hasDuration = duration.TotalSeconds > 0;
|
||||
|
||||
if (role != null) {
|
||||
|
@ -84,30 +56,30 @@ public class MuteCommand : Command {
|
|||
await toMute.RemoveRoleAsync(role);
|
||||
rolesRemoved.Add(userRole.Id);
|
||||
} catch (HttpException e) {
|
||||
Warn(string.Format(Messages.RoleRemovalFailed, $"<@&{userRole}>", Utils.Wrap(e.Reason)));
|
||||
cmd.Reply(string.Format(Messages.RoleRemovalFailed, $"<@&{userRole}>", Utils.Wrap(e.Reason)),
|
||||
":warning: ");
|
||||
}
|
||||
|
||||
Boyfriend.GetRemovedRoles(guild.Id).Add(toMute.Id, rolesRemoved.AsReadOnly());
|
||||
CommandHandler.ConfigWriteScheduled = true;
|
||||
|
||||
if (hasDuration) {
|
||||
var copy = duration;
|
||||
var _ = async () => {
|
||||
await Task.Delay(copy);
|
||||
await UnmuteCommand.UnmuteMember(guild, guild.CurrentUser, toMute, Messages.PunishmentExpired);
|
||||
};
|
||||
}
|
||||
cmd.ConfigWriteScheduled = true;
|
||||
}
|
||||
|
||||
await toMute.AddRoleAsync(role, requestOptions);
|
||||
|
||||
if (hasDuration) {
|
||||
var _ = async () => {
|
||||
await Task.Delay(duration);
|
||||
await UnmuteCommand.UnmuteMember(cmd, toMute, Messages.PunishmentExpired);
|
||||
};
|
||||
}
|
||||
} else {
|
||||
if (!hasDuration || duration.TotalDays > 28) {
|
||||
Error(Messages.DurationRequiredForTimeOuts, false);
|
||||
cmd.Reply(Messages.DurationRequiredForTimeOuts, ":x: ");
|
||||
return;
|
||||
}
|
||||
|
||||
if (toMute.IsBot) {
|
||||
Error(Messages.CannotTimeOutBot, false);
|
||||
cmd.Reply(Messages.CannotTimeOutBot, ":x: ");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -115,9 +87,9 @@ public class MuteCommand : Command {
|
|||
}
|
||||
|
||||
var feedback = string.Format(Messages.FeedbackMemberMuted, toMute.Mention,
|
||||
Utils.GetHumanizedTimeOffset(ref duration),
|
||||
Utils.GetHumanizedTimeOffset(duration),
|
||||
Utils.Wrap(reason));
|
||||
Success(feedback, author.Mention, false, false);
|
||||
await Utils.SendFeedback(feedback, guild.Id, author.Mention, true);
|
||||
cmd.Reply(feedback, ":mute: ");
|
||||
cmd.Audit(feedback);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,17 +1,16 @@
|
|||
using Discord.Commands;
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class PingCommand : Command {
|
||||
public override string[] Aliases { get; } = {"ping", "latency", "pong", "пинг", "задержка", "понг"};
|
||||
public override int ArgsLengthRequired => 0;
|
||||
public override string[] Aliases { get; } = { "ping", "latency", "pong", "пинг", "задержка", "понг" };
|
||||
|
||||
public override Task Run(SocketCommandContext context, string[] args) {
|
||||
public override Task Run(CommandProcessor cmd, string[] args) {
|
||||
var builder = Boyfriend.StringBuilder;
|
||||
|
||||
builder.Append(Utils.GetBeep()).Append(Boyfriend.Client.Latency).Append(Messages.Milliseconds);
|
||||
builder.Append(Utils.GetBeep())
|
||||
.Append(Math.Abs(DateTimeOffset.Now.Subtract(cmd.Context.Message.Timestamp).TotalMilliseconds))
|
||||
.Append(Messages.Milliseconds);
|
||||
|
||||
Output(ref builder);
|
||||
cmd.Reply(builder.ToString(), ":signal_strength: ");
|
||||
builder.Clear();
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
|
|
@ -1,23 +1,14 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class SettingsCommand : Command {
|
||||
public override string[] Aliases { get; } = { "settings", "config", "настройки", "конфиг" };
|
||||
public override int ArgsLengthRequired => 0;
|
||||
|
||||
public override Task Run(SocketCommandContext context, string[] args) {
|
||||
var author = (SocketGuildUser)context.User;
|
||||
public override Task Run(CommandProcessor cmd, string[] args) {
|
||||
if (!cmd.HasPermission(GuildPermission.ManageGuild)) return Task.CompletedTask;
|
||||
|
||||
var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.ManageGuild);
|
||||
if (permissionCheckResponse is not "") {
|
||||
Error(permissionCheckResponse, true);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
var guild = context.Guild;
|
||||
var guild = cmd.Context.Guild;
|
||||
var config = Boyfriend.GetGuildConfig(guild.Id);
|
||||
|
||||
if (args.Length == 0) {
|
||||
|
@ -48,7 +39,7 @@ public class SettingsCommand : Command {
|
|||
.AppendFormat(format, currentValue).AppendLine();
|
||||
}
|
||||
|
||||
Output(ref currentSettings);
|
||||
cmd.Reply(currentSettings.ToString(), ":gear: ");
|
||||
currentSettings.Clear();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
@ -66,19 +57,20 @@ public class SettingsCommand : Command {
|
|||
}
|
||||
|
||||
if (!exists) {
|
||||
Error(Messages.SettingDoesntExist, false);
|
||||
cmd.Reply(Messages.SettingDoesntExist, ":x: ");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
string value;
|
||||
string? value;
|
||||
|
||||
if (args.Length >= 2) {
|
||||
value = Utils.JoinString(ref args, 1);
|
||||
value = cmd.GetRemaining(args, 1, "Setting");
|
||||
if (value == null) return Task.CompletedTask;
|
||||
if (selectedSetting is "EventStartedReceivers") {
|
||||
value = value.Replace(" ", "").ToLower();
|
||||
if (value.StartsWith(",") || value.Count(x => x == ',') > 1 ||
|
||||
(!value.Contains("interested") && !value.Contains("role"))) {
|
||||
Error(Messages.InvalidSettingValue, false);
|
||||
cmd.Reply(Messages.InvalidSettingValue, ":x: ");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
@ -91,7 +83,7 @@ public class SettingsCommand : Command {
|
|||
_ => value
|
||||
};
|
||||
if (!IsBool(value)) {
|
||||
Error(Messages.InvalidSettingValue, false);
|
||||
cmd.Reply(Messages.InvalidSettingValue, ":x: ");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
@ -124,22 +116,23 @@ public class SettingsCommand : Command {
|
|||
config[selectedSetting] = Boyfriend.DefaultConfig[selectedSetting];
|
||||
} else {
|
||||
if (value == config[selectedSetting]) {
|
||||
Error(string.Format(Messages.SettingsNothingChanged, localizedSelectedSetting, formattedValue), false);
|
||||
cmd.Reply(string.Format(Messages.SettingsNothingChanged, localizedSelectedSetting, formattedValue),
|
||||
":x: ");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
if (selectedSetting is "Lang" && value is not "ru" and not "en") {
|
||||
Error(Messages.LanguageNotSupported, false);
|
||||
cmd.Reply(Messages.LanguageNotSupported, ":x: ");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
if (selectedSetting.EndsWith("Channel") && guild.GetTextChannel(mention) == null) {
|
||||
Error(Messages.InvalidChannel, false);
|
||||
cmd.Reply(Messages.InvalidChannel, ":x: ");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
if (selectedSetting.EndsWith("Role") && guild.GetRole(mention) == null) {
|
||||
Error(Messages.InvalidRole, false);
|
||||
cmd.Reply(Messages.InvalidRole, ":x: ");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
@ -153,10 +146,11 @@ public class SettingsCommand : Command {
|
|||
localizedSelectedSetting = Utils.GetMessage($"Settings{selectedSetting}");
|
||||
}
|
||||
|
||||
CommandHandler.ConfigWriteScheduled = true;
|
||||
cmd.ConfigWriteScheduled = true;
|
||||
|
||||
Success(string.Format(Messages.FeedbackSettingsUpdated, localizedSelectedSetting, formattedValue),
|
||||
author.Mention);
|
||||
var replyFormat = string.Format(Messages.FeedbackSettingsUpdated, localizedSelectedSetting, formattedValue);
|
||||
cmd.Reply(replyFormat, ":control_knobs: ");
|
||||
cmd.Audit(replyFormat, false);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
@ -167,4 +161,4 @@ public class SettingsCommand : Command {
|
|||
private static bool IsBool(string value) {
|
||||
return value is "true" or "false";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,45 +1,27 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class UnbanCommand : Command {
|
||||
public override string[] Aliases { get; } = { "unban", "разбан" };
|
||||
public override int ArgsLengthRequired => 2;
|
||||
|
||||
public override async Task Run(SocketCommandContext context, string[] args) {
|
||||
var author = (SocketGuildUser)context.User;
|
||||
public override async Task Run(CommandProcessor cmd, string[] args) {
|
||||
if (!cmd.HasPermission(GuildPermission.BanMembers)) return;
|
||||
|
||||
var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.BanMembers);
|
||||
if (permissionCheckResponse is not "") {
|
||||
Error(permissionCheckResponse, true);
|
||||
return;
|
||||
}
|
||||
var id = cmd.GetBan(args, 0);
|
||||
if (id == null) return;
|
||||
var reason = cmd.GetRemaining(args, 1, "UnbanReason");
|
||||
if (reason == null) return;
|
||||
|
||||
var toUnban = Utils.ParseUser(args[0]);
|
||||
|
||||
if (toUnban == null) {
|
||||
Error(Messages.UserDoesntExist, false);
|
||||
return;
|
||||
}
|
||||
|
||||
var reason = Utils.JoinString(ref args, 1);
|
||||
|
||||
await UnbanUser(context.Guild, author, toUnban, reason);
|
||||
await UnbanUser(cmd, id.Value, reason);
|
||||
}
|
||||
|
||||
public static async Task UnbanUser(SocketGuild guild, SocketGuildUser author, SocketUser toUnban, string reason) {
|
||||
if (guild.GetBanAsync(toUnban.Id) == null) {
|
||||
Error(Messages.UserNotBanned, false);
|
||||
return;
|
||||
}
|
||||
public static async Task UnbanUser(CommandProcessor cmd, ulong id, string reason) {
|
||||
var requestOptions = Utils.GetRequestOptions($"({cmd.Context.User}) {reason}");
|
||||
await cmd.Context.Guild.RemoveBanAsync(id, requestOptions);
|
||||
|
||||
var requestOptions = Utils.GetRequestOptions($"({author}) {reason}");
|
||||
await guild.RemoveBanAsync(toUnban, requestOptions);
|
||||
|
||||
var feedback = string.Format(Messages.FeedbackUserUnbanned, toUnban.Mention, Utils.Wrap(reason));
|
||||
Success(feedback, author.Mention, false, false);
|
||||
await Utils.SendFeedback(feedback, guild.Id, author.Mention, true);
|
||||
var feedback = string.Format(Messages.FeedbackUserUnbanned, $"<@{id.ToString()}>", Utils.Wrap(reason));
|
||||
cmd.Reply(feedback);
|
||||
cmd.Audit(feedback);
|
||||
}
|
||||
}
|
|
@ -1,59 +1,39 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace Boyfriend.Commands;
|
||||
|
||||
public class UnmuteCommand : Command {
|
||||
public override string[] Aliases { get; } = { "unmute", "размут" };
|
||||
public override int ArgsLengthRequired => 2;
|
||||
|
||||
public override async Task Run(SocketCommandContext context, string[] args) {
|
||||
var author = (SocketGuildUser)context.User;
|
||||
public override async Task Run(CommandProcessor cmd, string[] args) {
|
||||
if (!cmd.HasPermission(GuildPermission.ModerateMembers)) return;
|
||||
|
||||
var permissionCheckResponse = CommandHandler.HasPermission(ref author, GuildPermission.ModerateMembers);
|
||||
if (permissionCheckResponse is not "") {
|
||||
Error(permissionCheckResponse, true);
|
||||
return;
|
||||
}
|
||||
|
||||
var toUnmute = Utils.ParseMember(context.Guild, args[0]);
|
||||
|
||||
if (toUnmute == null) {
|
||||
Error(Messages.UserDoesntExist, false);
|
||||
return;
|
||||
}
|
||||
|
||||
var interactionCheckResponse = CommandHandler.CanInteract(ref author, ref toUnmute);
|
||||
if (interactionCheckResponse is not "") {
|
||||
Error(interactionCheckResponse, true);
|
||||
return;
|
||||
}
|
||||
|
||||
var reason = Utils.JoinString(ref args, 1);
|
||||
await UnmuteMember(context.Guild, author, toUnmute, reason);
|
||||
var toUnmute = cmd.GetMember(args, 0, "ToUnmute");
|
||||
var reason = cmd.GetRemaining(args, 1, "UnmuteReason");
|
||||
if (toUnmute == null || reason == null || !cmd.CanInteractWith(toUnmute, "Unmute")) return;
|
||||
await UnmuteMember(cmd, toUnmute, reason);
|
||||
}
|
||||
|
||||
public static async Task UnmuteMember(SocketGuild guild, SocketGuildUser author, SocketGuildUser toUnmute,
|
||||
public static async Task UnmuteMember(CommandProcessor cmd, SocketGuildUser toUnmute,
|
||||
string reason) {
|
||||
var requestOptions = Utils.GetRequestOptions($"({author}) {reason}");
|
||||
var role = Utils.GetMuteRole(ref guild);
|
||||
var requestOptions = Utils.GetRequestOptions($"({cmd.Context.User}) {reason}");
|
||||
var role = Utils.GetMuteRole(cmd.Context.Guild);
|
||||
|
||||
if (role != null && toUnmute.Roles.Contains(role)) {
|
||||
var rolesRemoved = Boyfriend.GetRemovedRoles(guild.Id);
|
||||
var rolesRemoved = Boyfriend.GetRemovedRoles(cmd.Context.Guild.Id);
|
||||
|
||||
if (rolesRemoved.ContainsKey(toUnmute.Id)) {
|
||||
await toUnmute.AddRolesAsync(rolesRemoved[toUnmute.Id]);
|
||||
rolesRemoved.Remove(toUnmute.Id);
|
||||
CommandHandler.ConfigWriteScheduled = true;
|
||||
cmd.ConfigWriteScheduled = true;
|
||||
}
|
||||
|
||||
await toUnmute.RemoveRoleAsync(role, requestOptions);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (toUnmute.TimedOutUntil == null || toUnmute.TimedOutUntil.Value.ToUnixTimeMilliseconds() <
|
||||
DateTimeOffset.Now.ToUnixTimeMilliseconds()) {
|
||||
Error(Messages.MemberNotMuted, false);
|
||||
cmd.Reply(Messages.MemberNotMuted, ":x: ");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -61,7 +41,7 @@ public class UnmuteCommand : Command {
|
|||
}
|
||||
|
||||
var feedback = string.Format(Messages.FeedbackMemberUnmuted, toUnmute.Mention, Utils.Wrap(reason));
|
||||
Success(feedback, author.Mention, false, false);
|
||||
await Utils.SendFeedback(feedback, guild.Id, author.Mention, true);
|
||||
cmd.Reply(feedback, ":loud_sound: ");
|
||||
cmd.Audit(feedback);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue