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

Async command handling

This commit is contained in:
Octol1ttle 2022-08-29 21:24:38 +05:00
parent 51c24c1e23
commit 53f13d88a5
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF
7 changed files with 54 additions and 40 deletions

View file

@ -12,11 +12,15 @@ public static class Boyfriend {
private static readonly DiscordSocketConfig Config = new() { private static readonly DiscordSocketConfig Config = new() {
MessageCacheSize = 250, MessageCacheSize = 250,
GatewayIntents = GatewayIntents.All GatewayIntents = GatewayIntents.All,
AlwaysDownloadUsers = true,
AlwaysResolveStickers = false,
AlwaysDownloadDefaultStickers = false,
LargeThreshold = 500
}; };
public static readonly DiscordSocketClient Client = new(Config); public static readonly DiscordSocketClient Client = new(Config);
private static readonly Game Activity = new("Retrospecter - Genocide", ActivityType.Listening); private static readonly Game Activity = new("Toby Fox - The World Revolving", ActivityType.Listening);
private static readonly Dictionary<ulong, Dictionary<string, string>> GuildConfigDictionary = new(); private static readonly Dictionary<ulong, Dictionary<string, string>> GuildConfigDictionary = new();

View file

@ -15,7 +15,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Discord.Net" Version="3.7.2"/> <PackageReference Include="Discord.Net" Version="3.8.0"/>
<PackageReference Include="Humanizer.Core" Version="2.14.1"/> <PackageReference Include="Humanizer.Core" Version="2.14.1"/>
<PackageReference Include="Humanizer.Core.ru" Version="2.14.1"/> <PackageReference Include="Humanizer.Core.ru" Version="2.14.1"/>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2-beta1"/> <PackageReference Include="Newtonsoft.Json" Version="13.0.2-beta1"/>

View file

@ -22,10 +22,14 @@ public static class CommandHandler {
public static readonly StringBuilder StackedPrivateFeedback = new(); public static readonly StringBuilder StackedPrivateFeedback = new();
#pragma warning disable CA2211 #pragma warning disable CA2211
public static bool ConfigWriteScheduled = false; // HOW IT CAN BE PRIVATE???? public static bool ConfigWriteScheduled = false; // Can't be private
#pragma warning restore CA2211 #pragma warning restore CA2211
private static bool _handlerBusy;
public static async Task HandleCommand(SocketUserMessage message) { public static async Task HandleCommand(SocketUserMessage message) {
while (_handlerBusy) await Task.Delay(200);
_handlerBusy = true;
StackedReplyMessage.Clear(); StackedReplyMessage.Clear();
StackedPrivateFeedback.Clear(); StackedPrivateFeedback.Clear();
StackedPublicFeedback.Clear(); StackedPublicFeedback.Clear();
@ -43,34 +47,42 @@ public static class CommandHandler {
var currentLine = 0; var currentLine = 0;
foreach (var line in list) { foreach (var line in list) {
currentLine++; currentLine++;
foreach (var command in Commands) { await RunCommands(line, regex, context, currentLine == list.Length);
var lineNoMention = MentionRegex.Replace(line, "", 1); }
if (!command.Aliases.Contains(regex.Replace(lineNoMention, "", 1).Trim().ToLower().Split()[0]))
continue;
await context.Channel.TriggerTypingAsync(); if (ConfigWriteScheduled) await Boyfriend.WriteGuildConfig(guild.Id);
var args = line.Split().Skip(1).ToArray(); var adminChannel = Utils.GetAdminLogChannel(guild.Id);
var systemChannel = guild.SystemChannel;
if (StackedPrivateFeedback.Length > 0 && adminChannel != null && adminChannel.Id != message.Channel.Id)
await Utils.SilentSendAsync(adminChannel, StackedPrivateFeedback.ToString());
if (StackedPublicFeedback.Length > 0 && systemChannel != null && systemChannel.Id != adminChannel?.Id
&& systemChannel.Id != message.Channel.Id)
await Utils.SilentSendAsync(systemChannel, StackedPublicFeedback.ToString());
_handlerBusy = false;
}
if (command.ArgsLengthRequired <= args.Length) private static async Task RunCommands(string line, Regex regex, SocketCommandContext context, bool shouldAwait) {
foreach (var command in Commands) {
var lineNoMention = MentionRegex.Replace(line, "", 1);
if (!command.Aliases.Contains(regex.Replace(lineNoMention, "", 1).Trim().ToLower().Split()[0]))
continue;
await context.Channel.TriggerTypingAsync();
var args = line.Split().Skip(1).ToArray();
if (command.ArgsLengthRequired <= args.Length)
if (shouldAwait)
await command.Run(context, args); await command.Run(context, args);
else else
StackedReplyMessage.AppendFormat(Messages.NotEnoughArguments, command.ArgsLengthRequired.ToString(), _ = command.Run(context, args);
args.Length.ToString()); else
StackedReplyMessage.AppendFormat(Messages.NotEnoughArguments, command.ArgsLengthRequired.ToString(),
if (currentLine != list.Length) continue; args.Length.ToString()).AppendLine();
if (ConfigWriteScheduled) await Boyfriend.WriteGuildConfig(guild.Id); if (StackedReplyMessage.Length <= 1675 && !shouldAwait) continue;
if (StackedReplyMessage.Length > 0) await context.Message.ReplyAsync(StackedReplyMessage.ToString(), false, null, AllowedMentions.None);
await message.ReplyAsync(StackedReplyMessage.ToString(), false, null, AllowedMentions.None); StackedReplyMessage.Clear();
var adminChannel = Utils.GetAdminLogChannel(guild.Id);
var systemChannel = guild.SystemChannel;
if (StackedPrivateFeedback.Length > 0 && adminChannel != null && adminChannel.Id != message.Channel.Id)
await Utils.SilentSendAsync(adminChannel, StackedPrivateFeedback.ToString());
if (StackedPublicFeedback.Length > 0 && systemChannel != null && systemChannel.Id != adminChannel?.Id
&& systemChannel.Id != message.Channel.Id)
await Utils.SilentSendAsync(systemChannel, StackedPublicFeedback.ToString());
}
} }
} }

View file

@ -65,12 +65,10 @@ public class BanCommand : Command {
await Utils.SendFeedback(feedback, guild.Id, author.Mention, true); await Utils.SendFeedback(feedback, guild.Id, author.Mention, true);
if (duration.TotalSeconds > 0) { if (duration.TotalSeconds > 0) {
async void DelayUnban() { var _ = async () => {
await Task.Delay(duration); await Task.Delay(duration);
await UnbanCommand.UnbanUser(guild, guild.CurrentUser, toBan, Messages.PunishmentExpired); await UnbanCommand.UnbanUser(guild, guild.CurrentUser, toBan, Messages.PunishmentExpired);
} };
new Task(DelayUnban).Start();
} }
} }
} }

View file

@ -91,12 +91,11 @@ public class MuteCommand : Command {
CommandHandler.ConfigWriteScheduled = true; CommandHandler.ConfigWriteScheduled = true;
if (hasDuration) { if (hasDuration) {
async void DelayUnmute() { var copy = duration;
await Task.Delay(duration); var _ = async () => {
await Task.Delay(copy);
await UnmuteCommand.UnmuteMember(guild, guild.CurrentUser, toMute, Messages.PunishmentExpired); await UnmuteCommand.UnmuteMember(guild, guild.CurrentUser, toMute, Messages.PunishmentExpired);
} };
new Task(DelayUnmute).Start();
} }
} }

View file

@ -83,7 +83,7 @@ public class EventHandler {
(message.Content.Contains(prev) || message.Content.Contains(prevFailsafe)))) (message.Content.Contains(prev) || message.Content.Contains(prevFailsafe))))
return; return;
await CommandHandler.HandleCommand(message); _ = CommandHandler.HandleCommand(message);
} }
private static async Task MessageUpdatedEvent(Cacheable<IMessage, ulong> messageCached, SocketMessage messageSocket, private static async Task MessageUpdatedEvent(Cacheable<IMessage, ulong> messageCached, SocketMessage messageSocket,
@ -176,4 +176,4 @@ public class EventHandler {
await channel.SendMessageAsync(string.Format(Messages.EventCompleted, Utils.Wrap(scheduledEvent.Name), await channel.SendMessageAsync(string.Format(Messages.EventCompleted, Utils.Wrap(scheduledEvent.Name),
Utils.Wrap(scheduledEvent.StartTime.Subtract(DateTimeOffset.Now).Negate().ToString()))); Utils.Wrap(scheduledEvent.StartTime.Subtract(DateTimeOffset.Now).Negate().ToString())));
} }
} }

View file

@ -118,6 +118,7 @@ public static class Utils {
} }
} }
numberBuilder.Clear();
return new TimeSpan(days, hours, minutes, seconds); return new TimeSpan(days, hours, minutes, seconds);
} }
@ -168,4 +169,4 @@ public static class Utils {
public static void SetCurrentLanguage(ulong guildId) { public static void SetCurrentLanguage(ulong guildId) {
Messages.Culture = CultureInfoCache[Boyfriend.GetGuildConfig(guildId)["Lang"]]; Messages.Culture = CultureInfoCache[Boyfriend.GetGuildConfig(guildId)["Lang"]];
} }
} }