This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
OctobotStealth/Boyfriend/CommandHandler.cs

82 lines
3.6 KiB
C#
Raw Normal View History

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 {
public static readonly Command[] Commands = {
new BanCommand(), new ClearCommand(), new HelpCommand(),
2022-01-30 11:43:15 +03:00
new KickCommand(), new MuteCommand(), new PingCommand(),
new SettingsCommand(), new UnbanCommand(), new UnmuteCommand()
};
public static async Task HandleCommand(SocketUserMessage message) {
2021-12-15 09:19:14 +03:00
var context = new SocketCommandContext(Boyfriend.Client, message);
foreach (var command in Commands) {
2022-01-30 11:43:15 +03:00
var regex = new Regex(Regex.Escape(Boyfriend.GetGuildConfig(context.Guild).Prefix!));
2022-02-02 16:14:26 +03:00
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);
2022-02-02 16:14:26 +03:00
} catch (Exception e) {
var signature = e switch {
ApplicationException => ":x:",
UnauthorizedAccessException => ":no_entry_sign:",
_ => ":stop_sign:"
};
2022-02-02 16:14:26 +03:00
var stacktrace = e.StackTrace;
var toSend = $"{signature} `{e.Message}`";
if (stacktrace != null && e is not ApplicationException && e is not UnauthorizedAccessException)
toSend += $"{Environment.NewLine}{Utils.Wrap(stacktrace)}";
await context.Channel.SendMessageAsync(toSend);
2022-01-30 11:43:15 +03:00
throw;
}
break;
2021-12-15 09:19:14 +03:00
}
}
public static async Task CheckPermissions(IGuildUser user, GuildPermission toCheck,
GuildPermission forBot = GuildPermission.StartEmbeddedActivities) {
2022-02-02 16:14:26 +03:00
var me = await user.Guild.GetCurrentUserAsync();
2021-12-15 09:19:14 +03:00
if (forBot == GuildPermission.StartEmbeddedActivities) forBot = toCheck;
2022-02-02 16:14:26 +03:00
if (user.Id != user.Guild.OwnerId
&& (!me.GuildPermissions.Has(GuildPermission.Administrator)
|| !user.GuildPermissions.Has(GuildPermission.Administrator))) {
if (!me.GuildPermissions.Has(forBot))
throw new UnauthorizedAccessException(Messages.CommandNoPermissionBot);
if (!user.GuildPermissions.Has(toCheck))
throw new UnauthorizedAccessException(Messages.CommandNoPermissionUser);
}
2021-12-15 09:19:14 +03:00
}
public static async Task CheckInteractions(IGuildUser actor, IGuildUser target) {
var me = await target.Guild.GetCurrentUserAsync();
2022-02-02 16:14:26 +03:00
if (actor.Guild != target.Guild)
throw new Exception(Messages.InteractionsDifferentGuilds);
2021-12-15 09:19:14 +03:00
if (actor.Id == actor.Guild.OwnerId) return;
2022-02-02 16:14:26 +03:00
2021-12-15 09:19:14 +03:00
if (target.Id == target.Guild.OwnerId)
throw new UnauthorizedAccessException(Messages.InteractionsOwner);
2021-12-15 09:19:14 +03:00
if (actor == target)
throw new UnauthorizedAccessException(Messages.InteractionsYourself);
2021-12-15 09:19:14 +03:00
if (target == me)
throw new UnauthorizedAccessException(Messages.InteractionsMe);
2021-12-15 09:19:14 +03:00
if (me.Hierarchy <= target.Hierarchy)
throw new UnauthorizedAccessException(Messages.InteractionsFailedBot);
if (actor.Hierarchy <= target.Hierarchy)
throw new UnauthorizedAccessException(Messages.InteractionsFailedUser);
2021-12-15 09:19:14 +03:00
}
2022-01-30 11:43:15 +03:00
}