2022-01-18 20:38:15 +03:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using Boyfriend.Commands;
|
|
|
|
|
using Discord;
|
2021-12-15 09:19:14 +03:00
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
|
|
|
|
|
namespace Boyfriend;
|
|
|
|
|
|
|
|
|
|
public static class CommandHandler {
|
2022-01-18 20:38:15 +03:00
|
|
|
|
public static readonly Command[] Commands = {
|
|
|
|
|
new BanCommand(), new ClearCommand(), new HelpCommand(),
|
|
|
|
|
new KickCommand(), new MuteCommand()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public static async Task HandleCommand(SocketUserMessage message) {
|
2021-12-15 09:19:14 +03:00
|
|
|
|
var context = new SocketCommandContext(Boyfriend.Client, message);
|
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
foreach (var command in Commands) {
|
|
|
|
|
var regex = new Regex(Regex.Escape(Boyfriend.GetGuildConfig(context.Guild).Prefix));
|
|
|
|
|
if (!command.GetAliases().Contains(regex.Replace(message.Content, "", 1).Split()[0])) continue;
|
|
|
|
|
|
|
|
|
|
var args = message.Content.Split().Skip(1).ToArray();
|
|
|
|
|
try {
|
|
|
|
|
if (command.GetArgumentsAmountRequired() > args.Length)
|
|
|
|
|
throw new ApplicationException(string.Format(Messages.NotEnoughArguments,
|
|
|
|
|
command.GetArgumentsAmountRequired(), args.Length));
|
|
|
|
|
await command.Run(context, args);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
var signature = e switch {
|
|
|
|
|
ApplicationException => ":x:",
|
|
|
|
|
UnauthorizedAccessException => ":no_entry_sign:",
|
|
|
|
|
_ => ":stop_sign:"
|
|
|
|
|
};
|
|
|
|
|
await context.Channel.SendMessageAsync($"{signature} `{e.Message}`");
|
|
|
|
|
if (e.StackTrace != null && e is not ApplicationException or UnauthorizedAccessException)
|
|
|
|
|
await context.Channel.SendMessageAsync(Utils.Wrap(e.StackTrace));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
2021-12-15 09:19:14 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task CheckPermissions(IGuildUser user, GuildPermission toCheck,
|
|
|
|
|
GuildPermission forBot = GuildPermission.StartEmbeddedActivities) {
|
|
|
|
|
if (forBot == GuildPermission.StartEmbeddedActivities) forBot = toCheck;
|
|
|
|
|
if (!(await user.Guild.GetCurrentUserAsync()).GuildPermissions.Has(forBot))
|
2022-01-18 20:38:15 +03:00
|
|
|
|
throw new UnauthorizedAccessException(Messages.CommandNoPermissionBot);
|
2021-12-15 09:19:14 +03:00
|
|
|
|
if (!user.GuildPermissions.Has(toCheck))
|
2022-01-18 20:38:15 +03:00
|
|
|
|
throw new UnauthorizedAccessException(Messages.CommandNoPermissionUser);
|
2021-12-15 09:19:14 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task CheckInteractions(IGuildUser actor, IGuildUser target) {
|
|
|
|
|
if (actor.Guild != target.Guild)
|
2022-01-18 20:38:15 +03:00
|
|
|
|
throw new UnauthorizedAccessException(Messages.InteractionsDifferentGuilds);
|
2021-12-15 09:19:14 +03:00
|
|
|
|
var me = await target.Guild.GetCurrentUserAsync();
|
|
|
|
|
if (actor.Id == actor.Guild.OwnerId) return;
|
|
|
|
|
if (target.Id == target.Guild.OwnerId)
|
2022-01-18 20:38:15 +03:00
|
|
|
|
throw new UnauthorizedAccessException(Messages.InteractionsOwner);
|
2021-12-15 09:19:14 +03:00
|
|
|
|
if (actor == target)
|
2022-01-18 20:38:15 +03:00
|
|
|
|
throw new UnauthorizedAccessException(Messages.InteractionsYourself);
|
2021-12-15 09:19:14 +03:00
|
|
|
|
if (target == me)
|
2022-01-18 20:38:15 +03:00
|
|
|
|
throw new UnauthorizedAccessException(Messages.InteractionsMe);
|
2021-12-15 09:19:14 +03:00
|
|
|
|
if (me.Hierarchy <= target.Hierarchy)
|
2022-01-18 20:38:15 +03:00
|
|
|
|
throw new UnauthorizedAccessException(Messages.InteractionsFailedBot);
|
|
|
|
|
if (actor.Hierarchy <= target.Hierarchy)
|
|
|
|
|
throw new UnauthorizedAccessException(Messages.InteractionsFailedUser);
|
2021-12-15 09:19:14 +03:00
|
|
|
|
}
|
|
|
|
|
}
|