1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 09:09:00 +03:00
Octobot/Boyfriend/CommandHandler.cs

110 lines
4.5 KiB
C#
Raw Normal View History

2022-05-14 16:12:24 +03:00
using System.Text;
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()
};
2022-05-14 16:12:24 +03:00
private static readonly Dictionary<string, Regex> RegexCache = new();
public static readonly StringBuilder StackedReplyMessage = new();
public static readonly StringBuilder StackedPublicFeedback = new();
public static readonly StringBuilder StackedPrivateFeedback = new();
#pragma warning disable CA2211
public static bool ConfigWriteScheduled = false; // HOW IT CAN BE PRIVATE????
#pragma warning restore CA2211
public static async Task HandleCommand(SocketUserMessage message) {
2022-05-14 16:12:24 +03:00
StackedReplyMessage.Clear();
StackedPrivateFeedback.Clear();
StackedPublicFeedback.Clear();
2021-12-15 09:19:14 +03:00
var context = new SocketCommandContext(Boyfriend.Client, message);
2022-05-14 16:12:24 +03:00
var guild = context.Guild;
var config = Boyfriend.GetGuildConfig(guild.Id);
2021-12-15 09:19:14 +03:00
2022-05-14 16:12:24 +03:00
Regex regex;
if (RegexCache.ContainsKey(config["Prefix"])) { regex = RegexCache[config["Prefix"]]; } else {
2022-05-14 16:12:24 +03:00
regex = new Regex(Regex.Escape(config["Prefix"]));
RegexCache.Add(config["Prefix"], regex);
}
var list = message.Content.Split("\n");
var currentLine = 0;
foreach (var line in list) {
currentLine++;
foreach (var command in Commands) {
if (!command.Aliases.Contains(regex.Replace(line, "", 1).ToLower().Split()[0]))
continue;
await context.Channel.TriggerTypingAsync();
var args = line.Split().Skip(1).ToArray();
if (command.ArgsLengthRequired <= args.Length)
await command.Run(context, args);
else
StackedReplyMessage.AppendFormat(Messages.NotEnoughArguments, command.ArgsLengthRequired.ToString(),
args.Length.ToString());
2022-05-14 16:12:24 +03:00
if (currentLine != list.Length) continue;
if (ConfigWriteScheduled) await Boyfriend.WriteGuildConfig(guild.Id);
await message.ReplyAsync(StackedReplyMessage.ToString(), false, null, AllowedMentions.None);
2022-05-14 16:12:24 +03:00
var adminChannel = Utils.GetAdminLogChannel(guild.Id);
var systemChannel = guild.SystemChannel;
if (StackedPrivateFeedback.Length > 0 && adminChannel != null && adminChannel.Id != message.Channel.Id)
2022-05-14 16:12:24 +03:00
await Utils.SilentSendAsync(adminChannel, StackedPrivateFeedback.ToString());
if (StackedPublicFeedback.Length > 0 && systemChannel != null && systemChannel.Id != adminChannel?.Id
&& systemChannel.Id != message.Channel.Id)
2022-05-14 16:12:24 +03:00
await Utils.SilentSendAsync(systemChannel, StackedPublicFeedback.ToString());
}
2021-12-15 09:19:14 +03:00
}
}
2022-05-14 16:12:24 +03:00
public static string HasPermission(ref SocketGuildUser user, GuildPermission toCheck,
2021-12-15 09:19:14 +03:00
GuildPermission forBot = GuildPermission.StartEmbeddedActivities) {
2022-05-14 16:12:24 +03:00
var me = user.Guild.CurrentUser;
if (user.Id == user.Guild.OwnerId || (me.GuildPermissions.Has(GuildPermission.Administrator) &&
user.GuildPermissions.Has(GuildPermission.Administrator))) return "";
2021-12-15 09:19:14 +03:00
if (forBot == GuildPermission.StartEmbeddedActivities) forBot = toCheck;
2022-02-02 16:14:26 +03:00
2022-05-14 16:12:24 +03:00
if (!me.GuildPermissions.Has(forBot))
return Messages.CommandNoPermissionBot;
2021-12-15 09:19:14 +03:00
2022-05-14 16:12:24 +03:00
return !user.GuildPermissions.Has(toCheck) ? Messages.CommandNoPermissionUser : "";
}
2022-02-02 16:14:26 +03:00
2022-05-14 16:12:24 +03:00
public static string CanInteract(ref SocketGuildUser actor, ref SocketGuildUser target) {
2022-02-02 16:14:26 +03:00
if (actor.Guild != target.Guild)
2022-05-14 16:12:24 +03:00
return Messages.InteractionsDifferentGuilds;
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)
2022-05-14 16:12:24 +03:00
return Messages.InteractionsOwner;
2021-12-15 09:19:14 +03:00
if (actor == target)
2022-05-14 16:12:24 +03:00
return Messages.InteractionsYourself;
var me = target.Guild.CurrentUser;
2021-12-15 09:19:14 +03:00
if (target == me)
2022-05-14 16:12:24 +03:00
return Messages.InteractionsMe;
2021-12-15 09:19:14 +03:00
if (me.Hierarchy <= target.Hierarchy)
2022-05-14 16:12:24 +03:00
return Messages.InteractionsFailedBot;
return actor.Hierarchy <= target.Hierarchy ? Messages.InteractionsFailedUser : "";
2021-12-15 09:19:14 +03:00
}
2022-01-30 11:43:15 +03:00
}