public static async Task ProcessCommandAsync(ICommandContext context, Guild sguild) { var prefix = MessageTools.GetPrefixFromCommand(context.Message.Content, SkuldApp.Configuration.Prefix, SkuldApp.Configuration.AltPrefix, sguild?.Prefix); if (prefix is not null) { await DispatchCommandAsync(context, prefix).ConfigureAwait(false); } }
internal static async Task CommandService_CommandExecuted( Optional <CommandInfo> arg1, ICommandContext arg2, IResult arg3 ) { CommandInfo cmd = null; string name = ""; if (arg1.IsSpecified) { cmd = arg1.Value; name = cmd.Module.GetModulePath(); if (cmd.Name != null) { name += "." + cmd.Name; } name = name .ToLowerInvariant() .Replace(" ", "-") .Replace("/", "."); } if (arg3.IsSuccess) { using var Database = new SkuldDbContextFactory().CreateDbContext(); if (arg1.IsSpecified) { var cont = arg2 as ShardedCommandContext; DogStatsd.Increment("commands.total.threads", 1, 1, new[] { $"module:{cmd.Module.Name.ToLowerInvariant()}", $"cmd:{name}" }); DogStatsd.Histogram("commands.latency", watch.ElapsedMilliseconds, 0.5, new[] { $"module:{cmd.Module.Name.ToLowerInvariant()}", $"cmd:{name}" }); var usr = await Database.InsertOrGetUserAsync(cont.User).ConfigureAwait(false); await InsertCommandAsync(cmd, usr).ConfigureAwait(false); DogStatsd.Increment("commands.processed", 1, 1, new[] { $"module:{cmd.Module.Name.ToLowerInvariant()}", $"cmd:{name}" }); } } else { bool displayerror = true; if (arg3.ErrorReason.Contains("few parameters")) { var prefix = MessageTools.GetPrefixFromCommand(arg2.Message.Content, SkuldApp.Configuration.Prefix, SkuldApp.Configuration.AltPrefix); string cmdName = ""; if (arg2.Guild != null) { using var Database = new SkuldDbContextFactory().CreateDbContext(); prefix = MessageTools.GetPrefixFromCommand(arg2.Message.Content, SkuldApp.Configuration.Prefix, SkuldApp.Configuration.AltPrefix, (await Database.InsertOrGetGuildAsync(arg2.Guild).ConfigureAwait(false)).Prefix ); } if (cmd != null && cmd.Module.Group != null) { string pfx = ""; ModuleInfo mod = cmd.Module; while (mod.Group != null) { pfx += $"{mod.Group} "; if (mod.IsSubmodule) { mod = cmd.Module.Parent; } } cmdName = $"{pfx}{cmd.Name}"; var cmdembed = await SkuldApp.CommandService.GetCommandHelpAsync(arg2, cmdName, prefix).ConfigureAwait(false); await arg2.Channel.SendMessageAsync( "You seem to be missing a parameter or 2, here's the help", embed : cmdembed.Build() ) .ConfigureAwait(false); displayerror = false; } } if (arg3.ErrorReason.Contains("Timeout")) { var hourglass = new Emoji("⏳"); if (!arg2.Message.Reactions.Any(x => x.Key == hourglass && x.Value.IsMe)) { await arg2.Message.AddReactionAsync(hourglass).ConfigureAwait(false); } displayerror = false; } if (arg3.Error != CommandError.UnknownCommand && displayerror) { Log.Error(Key, "Error with command, Error is: " + arg3, arg2 as ShardedCommandContext); await EmbedExtensions.FromError(arg3.ErrorReason, arg2) .QueueMessageAsync(arg2) .ConfigureAwait(false); } switch (arg3.Error) { case CommandError.UnmetPrecondition: DogStatsd.Increment("commands.errors", 1, 1, new[] { "err:unm-precon", $"mod:{cmd.Module.Name}", $"cmd:{name}" } ); break; case CommandError.Unsuccessful: DogStatsd.Increment("commands.errors", 1, 1, new[] { "err:generic", $"mod:{cmd.Module.Name}", $"cmd:{name}" } ); break; case CommandError.MultipleMatches: DogStatsd.Increment("commands.errors", 1, 1, new[] { "err:multiple", $"mod:{cmd.Module.Name}", $"cmd:{name}" } ); break; case CommandError.BadArgCount: DogStatsd.Increment("commands.errors", 1, 1, new[] { "err:incorr-args", $"mod:{cmd.Module.Name}", $"cmd:{name}" } ); break; case CommandError.ParseFailed: DogStatsd.Increment("commands.errors", 1, 1, new[] { "err:parse-fail", $"mod:{cmd.Module.Name}", $"cmd:{name}" } ); break; case CommandError.Exception: DogStatsd.Increment("commands.errors", 1, 1, new[] { "err:exception", $"mod:{cmd.Module.Name}", $"cmd:{name}" } ); break; case CommandError.UnknownCommand: DogStatsd.Increment("commands.errors", 1, 1, new[] { "err:unk-cmd" } ); break; } } watch = new Stopwatch(); try { var message = arg2.Message as SocketUserMessage; using var Database = new SkuldDbContextFactory().CreateDbContext(); User suser = await Database.InsertOrGetUserAsync(message.Author).ConfigureAwait(false); { var keys = Database.DonatorKeys.AsAsyncEnumerable().Where(x => x.Redeemer == suser.Id); if (await keys.AnyAsync().ConfigureAwait(false)) { bool hasChanged = false; var current = DateTime.Now; await keys.ForEachAsync(x => { if (current > x.RedeemedWhen.FromEpoch().AddDays(365)) { Database.DonatorKeys.Remove(x); hasChanged = true; } }).ConfigureAwait(false); if (hasChanged) { if (!await Database.DonatorKeys.AsAsyncEnumerable().Where(x => x.Redeemer == suser.Id).AnyAsync().ConfigureAwait(false)) { suser.Flags -= DiscordUtilities.BotDonator; } await Database.SaveChangesAsync().ConfigureAwait(false); } } } if (!suser.IsUpToDate(message.Author as SocketUser)) { suser.AvatarUrl = message.Author.GetAvatarUrl() ?? message.Author.GetDefaultAvatarUrl(); suser.Username = message.Author.Username; await Database.SaveChangesAsync().ConfigureAwait(false); } } catch (Exception ex) { Log.Error(Key, ex.Message, arg2, ex); } }
private static async Task HandleMessageAsync(SocketMessage arg) { DogStatsd.Increment("messages.recieved"); if (arg.Author.IsBot || arg.Author.IsWebhook || arg.Author.DiscriminatorValue == 0 || arg is not SocketUserMessage message) { return; } ShardedCommandContext context = new ShardedCommandContext( SkuldApp.DiscordClient, message ); Guild sguild = null; if (context.Guild != null) { if (!await CheckPermissionToSendMessageAsync(context.Channel as ITextChannel).ConfigureAwait(false)) { return; } if (SkuldApp.DiscordClient.GetShardFor(context.Guild).ConnectionState != ConnectionState.Connected) { return; } _ = Task.Run(() => HandleSideTasksAsync(context)); var guser = await (context.Guild as IGuild).GetCurrentUserAsync() .ConfigureAwait(false); var guildMem = await (context.Guild as IGuild).GetUserAsync(message.Author.Id) .ConfigureAwait(false); if (!guser.GetPermissions(context.Channel as IGuildChannel).SendMessages) { return; } if (!MessageTools.IsEnabledChannel(guildMem, context.Channel as ITextChannel)) { return; } using var Database = new SkuldDbContextFactory().CreateDbContext(); sguild = await Database.InsertOrGetGuildAsync(context.Guild) .ConfigureAwait(false); if (sguild.Name != context.Guild.Name) { sguild.Name = context.Guild.Name; await Database.SaveChangesAsync().ConfigureAwait(false); } if (sguild.IconUrl != context.Guild.IconUrl) { sguild.IconUrl = context.Guild.IconUrl; await Database.SaveChangesAsync().ConfigureAwait(false); } } if (!MessageTools.HasPrefix(message, SkuldApp.Configuration.Prefix, SkuldApp.Configuration.AltPrefix, sguild?.Prefix)) { return; } try { using var Database = new SkuldDbContextFactory().CreateDbContext(); User suser = await Database.InsertOrGetUserAsync(message.Author).ConfigureAwait(false); if (suser != null && suser.Flags.IsBitSet(DiscordUtilities.Banned) && (!suser.Flags.IsBitSet(DiscordUtilities.BotCreator) || !suser.Flags.IsBitSet(DiscordUtilities.BotAdmin)) ) { return; } var prefix = MessageTools.GetPrefixFromCommand(context.Message.Content, SkuldApp.Configuration.Prefix, SkuldApp.Configuration.AltPrefix, sguild?.Prefix); if (prefix != null) { await DispatchCommandAsync(context, prefix).ConfigureAwait(false); } } catch (Exception ex) { Log.Critical(Key, ex.Message, context, ex); } }