private async Task HandleCommandFinishedGlobalAsync(Command command, CommandResult result, LittleBigBotExecutionContext context) { if (result.IsSuccessful) { CommandSuccesses++; } else { CommandFailures++; } var baseResult = result.Cast <BaseResult>(); if (baseResult.Content != null && !string.IsNullOrWhiteSpace(baseResult.Content)) { await context.Channel.SendMessageAsync(baseResult.Content); } foreach (var embed in baseResult.Embeds) { await context.Channel.SendMessageAsync(string.Empty, false, embed.Build()); } if (result is FailedBaseResult fbr) { LogCommandRuntimeFailure(context, command, fbr); } else { LogCommandSuccess(context, command); } }
private async Task HandleMessageAsync(SocketMessage incomingMessage) { if (!(incomingMessage is SocketUserMessage message) || incomingMessage.Author is SocketWebhookUser) { return; // Ignore web-hooks or system messages } if (incomingMessage.Author.IsBot) { return; } // Ignore bots var argPos = 0; if (!message.HasStringPrefix(_config.LittleBigBot.Prefix, ref argPos) && !message.HasMentionPrefix(_client.CurrentUser, ref argPos)) { return; } var context = new LittleBigBotExecutionContext(message, _services); try { var result = await _commandService.ExecuteAsync( message.Content.Substring(argPos) /* Remove prefix from string */, context, _services); if (result.IsSuccessful) { return; } Command command; switch (result) { case CommandNotFoundResult _: await context.Message.AddReactionAsync(UnknownCommandReaction); return; case ChecksFailedResult cfr: command = cfr.Command; await context.Channel.SendMessageAsync( $"The following check{(cfr.FailedChecks.Count == 0 ? "" : "s")} failed, so I couldn't execute the command: \n" + string.Join("\n", cfr.FailedChecks.Select(a => $"- {a.Error}"))); break; case ParseFailedResult pfr: command = pfr.Command; if (pfr.ParseFailure == ParseFailure.TooFewArguments) { await context.Channel.SendMessageAsync( $"Sorry, but you didn't supply enough information for this command! Here is the command listing for `{pfr.Command.Aliases.First()}`:", false, HelpModule.CreateCommandEmbed(pfr.Command, context)); break; } await context.Channel.SendMessageAsync( $"Parsing of your input failed: {pfr.ParseFailure.Humanize()}."); break; case TypeParserFailedResult tpfr: command = tpfr.Parameter.Command; await context.Channel.SendMessageAsync( $"Sorry, but \"{tpfr.Value}\" is not a valid form of {tpfr.Parameter.GetFriendlyName()}! Here is the command listing for `{tpfr.Parameter.Command.Aliases.First()}`:", false, HelpModule.CreateCommandEmbed(tpfr.Parameter.Command, context)); break; case ExecutionFailedResult _: return; case CommandOnCooldownResult cdr: command = cdr.Command; var msg = new StringBuilder(); msg.AppendLine("This command is on cooldown!"); foreach (var cdv in cdr.Cooldowns) { msg.AppendLine(); msg.AppendLine($"**Cooldown type:** {cdv.Cooldown.BucketType.Cast<CooldownType>()}"); msg.AppendLine($"**Limit:** {cdv.Cooldown.Amount} requests per {cdv.Cooldown.Per:g}"); msg.AppendLine($"**Retry after:** {cdv.RetryAfter:g}"); } await context.Channel.SendMessageAsync(msg.ToString()); break; case CommandResult _: return; default: await context.Channel.SendMessageAsync($"Generic failure: {result}"); return; } LogCommandGeneralFailure(context, command, result as FailedResult); } catch (Exception) { // Ignored - caught through CommandErrored event! // Should (theoretically) never happen, but Qmmands is an early library and errors could occur. // I don't know though. } }
private void LogCommandRuntimeFailure(LittleBigBotExecutionContext context, Command command, FailedBaseResult fbr) { _commandsTracking.LogInformation(GenerateLogString(context, command) + $" unsuccessfully, with run-time reason \"{fbr.Content}\""); }
private void LogCommandGeneralFailure(LittleBigBotExecutionContext context, Command command, FailedResult failure) { _commandsTracking.LogInformation(GenerateLogString(context, command) + $" unsuccessfully, with pre-run reason \"{failure.Reason}\""); }
private void LogCommandSuccess(LittleBigBotExecutionContext context, Command command) { _commandsTracking.LogInformation(GenerateLogString(context, command) + " successfully"); }
private string GenerateLogString(LittleBigBotExecutionContext context, Command command) { return ($"Executed command '{command.Aliases.First()}' for {context.Invoker} (ID {context.Invoker.Id}) in {context.Channel.Name} (ID {context.Channel.Id}){(context.Guild != null ? $" in guild {context.Guild.Name} (ID {context.Guild.Id})" : "")}"); }
public EvaluationHelper(LittleBigBotExecutionContext context, IServiceProvider provider) { Services = provider; Context = context; }