forked from TeamInklings/Octobot
Async command handling
This commit is contained in:
parent
51c24c1e23
commit
53f13d88a5
7 changed files with 54 additions and 40 deletions
|
@ -12,11 +12,15 @@ public static class Boyfriend {
|
|||
|
||||
private static readonly DiscordSocketConfig Config = new() {
|
||||
MessageCacheSize = 250,
|
||||
GatewayIntents = GatewayIntents.All
|
||||
GatewayIntents = GatewayIntents.All,
|
||||
AlwaysDownloadUsers = true,
|
||||
AlwaysResolveStickers = false,
|
||||
AlwaysDownloadDefaultStickers = false,
|
||||
LargeThreshold = 500
|
||||
};
|
||||
|
||||
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();
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<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.ru" Version="2.14.1"/>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.2-beta1"/>
|
||||
|
|
|
@ -22,10 +22,14 @@ public static class CommandHandler {
|
|||
public static readonly StringBuilder StackedPrivateFeedback = new();
|
||||
|
||||
#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
|
||||
|
||||
private static bool _handlerBusy;
|
||||
|
||||
public static async Task HandleCommand(SocketUserMessage message) {
|
||||
while (_handlerBusy) await Task.Delay(200);
|
||||
_handlerBusy = true;
|
||||
StackedReplyMessage.Clear();
|
||||
StackedPrivateFeedback.Clear();
|
||||
StackedPublicFeedback.Clear();
|
||||
|
@ -43,6 +47,22 @@ public static class CommandHandler {
|
|||
var currentLine = 0;
|
||||
foreach (var line in list) {
|
||||
currentLine++;
|
||||
await RunCommands(line, regex, context, currentLine == list.Length);
|
||||
}
|
||||
|
||||
if (ConfigWriteScheduled) await Boyfriend.WriteGuildConfig(guild.Id);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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]))
|
||||
|
@ -53,24 +73,16 @@ public static class CommandHandler {
|
|||
var args = line.Split().Skip(1).ToArray();
|
||||
|
||||
if (command.ArgsLengthRequired <= args.Length)
|
||||
if (shouldAwait)
|
||||
await command.Run(context, args);
|
||||
else
|
||||
_ = command.Run(context, args);
|
||||
else
|
||||
StackedReplyMessage.AppendFormat(Messages.NotEnoughArguments, command.ArgsLengthRequired.ToString(),
|
||||
args.Length.ToString());
|
||||
|
||||
if (currentLine != list.Length) continue;
|
||||
if (ConfigWriteScheduled) await Boyfriend.WriteGuildConfig(guild.Id);
|
||||
if (StackedReplyMessage.Length > 0)
|
||||
await message.ReplyAsync(StackedReplyMessage.ToString(), false, null, AllowedMentions.None);
|
||||
|
||||
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());
|
||||
}
|
||||
args.Length.ToString()).AppendLine();
|
||||
if (StackedReplyMessage.Length <= 1675 && !shouldAwait) continue;
|
||||
await context.Message.ReplyAsync(StackedReplyMessage.ToString(), false, null, AllowedMentions.None);
|
||||
StackedReplyMessage.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -65,12 +65,10 @@ public class BanCommand : Command {
|
|||
await Utils.SendFeedback(feedback, guild.Id, author.Mention, true);
|
||||
|
||||
if (duration.TotalSeconds > 0) {
|
||||
async void DelayUnban() {
|
||||
var _ = async () => {
|
||||
await Task.Delay(duration);
|
||||
await UnbanCommand.UnbanUser(guild, guild.CurrentUser, toBan, Messages.PunishmentExpired);
|
||||
}
|
||||
|
||||
new Task(DelayUnban).Start();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
|
@ -91,12 +91,11 @@ public class MuteCommand : Command {
|
|||
CommandHandler.ConfigWriteScheduled = true;
|
||||
|
||||
if (hasDuration) {
|
||||
async void DelayUnmute() {
|
||||
await Task.Delay(duration);
|
||||
var copy = duration;
|
||||
var _ = async () => {
|
||||
await Task.Delay(copy);
|
||||
await UnmuteCommand.UnmuteMember(guild, guild.CurrentUser, toMute, Messages.PunishmentExpired);
|
||||
}
|
||||
|
||||
new Task(DelayUnmute).Start();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -83,7 +83,7 @@ public class EventHandler {
|
|||
(message.Content.Contains(prev) || message.Content.Contains(prevFailsafe))))
|
||||
return;
|
||||
|
||||
await CommandHandler.HandleCommand(message);
|
||||
_ = CommandHandler.HandleCommand(message);
|
||||
}
|
||||
|
||||
private static async Task MessageUpdatedEvent(Cacheable<IMessage, ulong> messageCached, SocketMessage messageSocket,
|
||||
|
|
|
@ -118,6 +118,7 @@ public static class Utils {
|
|||
}
|
||||
}
|
||||
|
||||
numberBuilder.Clear();
|
||||
return new TimeSpan(days, hours, minutes, seconds);
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue