private async Task OnMessageRecievedAsync(SocketMessage msg) { if (msg is not SocketUserMessage { Source : MessageSource.User } message) { return; } int argumentPos = 0; if (!message.HasStringPrefix(_prefix, ref argumentPos) && !message.HasMentionPrefix(_client.CurrentUser, ref argumentPos)) { return; } SocketCommandContext context = new(_client, message); IResult result = await _commands.ExecuteAsync(context, argumentPos, _services); if (!result.IsSuccess && result.Error.HasValue) { if (result.Error.Value == CommandError.UnknownCommand) { await message.AddReactionAsync(new Emoji("❔")); } else { await context.Channel.SendMessageAsync(result.ErrorReason); } } }
/// <summary> /// Handles text command success and failures. /// </summary> /// <param name="command"></param> /// <param name="context"></param> /// <param name="result"></param> /// <returns></returns> public async Task CommandExecutedAsync(Optional <CommandInfo> command, ICommandContext context, Discord.Commands.IResult result) { // if a command isn't found, log that info to console and exit this method if (!command.IsSpecified) { System.Console.WriteLine($"Command not found"); return; } // log success to the console and exit this method if (result.IsSuccess) { System.Console.WriteLine($"Command Executed."); return; } else if (result.ErrorReason.Contains("Missing Permissions")) { await context.Channel.SendMessageAsync($"I do not have permission to carry out this action. I need permission to Send Messages, Embed Links, Attach Files, Add Reactions, Read Message History, and Manage Messages in this channel."); return; } else if (result.ErrorReason.Contains("Cannot reply without permission to read message history")) { await context.Channel.SendMessageAsync("I need permission to read message history in order to reply to a message and carry out the command."); return; } else if (result.ErrorReason.Contains("timed out")) { await context.Channel.SendMessageAsync($"Your command timed out. This may be because the bot is overloaded. Please try again later."); //report to owner anyway. } else { // failure scenario, let's let the user know await context.Channel.SendMessageAsync($"Sorry, Something went wrong..."); } //notify owner if desired: if (notifyOwnerOnError && !string.IsNullOrEmpty(_config["OwnerID"])) { string error = Format.Bold("Error:") + "\n" + result.Error + "\n" + Format.Code(result.ErrorReason) + "\n\n" + Format.Bold("Command:") + "\n" + Format.BlockQuote(context.Message.ToString()); if (error.Length > 2000) { error = error.Substring(0, 2000); } await Discord.UserExtensions.SendMessageAsync(_client.GetUser(ulong.Parse(_config["OwnerID"])), error); } }
private static async Task CommandExecuted(Optional <CommandInfo> command, ICommandContext context, IResult result) { //Only pay attention to commands which fail due to an exception if (result.IsSuccess || result.Error is not CommandError.Exception) { return; } if (result is ExecuteResult er) { Console.WriteLine(er.Exception); } await context.Channel.SendMessageAsync("Command Exception! " + result.ErrorReason); }
public async Task CommandExecutedAsync(Optional <CommandInfo> command, ICommandContext context, IResult result) { Logger.LogInformation("User {user} attempted to use command {command}", context.User, command.Value.Name); if (!command.IsSpecified || result.IsSuccess) { return; } await context.Channel.SendMessageAsync($"Error: {result}"); }