/// <summary>
        /// Not currently used
        /// </summary>
        /// <param name="arg1"></param>
        /// <param name="arg2"></param>
        /// <param name="arg3"></param>
        /// <returns></returns>
        private Task ContextCommandExecuted(ContextCommandInfo arg1, Discord.IInteractionContext arg2, Discord.Interactions.IResult arg3)
        {
            if (!arg3.IsSuccess)
            {
                switch (arg3.Error)
                {
                case InteractionCommandError.UnmetPrecondition:
                    // implement
                    break;

                case InteractionCommandError.UnknownCommand:
                    // implement
                    break;

                case InteractionCommandError.BadArgs:
                    // implement
                    break;

                case InteractionCommandError.Exception:
                    // implement
                    break;

                case InteractionCommandError.Unsuccessful:
                    // implement
                    break;

                default:
                    break;
                }
            }

            return(Task.CompletedTask);
        }
        /// <summary>
        /// Handle an executed component
        /// Used for buttons, drop downs, etc.
        /// </summary>
        /// <param name="arg1"></param>
        /// <param name="arg2"></param>
        /// <param name="arg3"></param>
        /// <returns></returns>
        private Task ComponentCommandExecuted(ComponentCommandInfo arg1, Discord.IInteractionContext arg2, Discord.Interactions.IResult arg3)
        {
            if (!arg3.IsSuccess)
            {
                // Defer if not already done:
                try
                {
                    arg2.Interaction.DeferAsync(true).GetAwaiter().GetResult();
                }
                catch
                {
                    // ignore
                }

                switch (arg3.Error)
                {
                case InteractionCommandError.UnmetPrecondition:
                    // Check for userperm error:
                    if (arg3.ErrorReason.Contains("UserPerm"))
                    {
                        arg2.Interaction.FollowupAsync("You do not have permission to execute this command.", ephemeral: true);
                        break;
                    }

                    arg2.Interaction.FollowupAsync("Action Failed\n" + arg3.ErrorReason, ephemeral: true);
                    break;

                case InteractionCommandError.UnknownCommand:
                    arg2.Interaction.FollowupAsync("Unknown action. It may have been recently removed or changed.", ephemeral: true);
                    break;

                case InteractionCommandError.BadArgs:
                    arg2.Interaction.FollowupAsync("The provided values are invalid. (BadArgs)", ephemeral: true);
                    break;

                case InteractionCommandError.Exception:
                    //notify owner if desired:
                    if (arg3.ErrorReason.Contains("Invalid Form Body"))
                    {
                        arg2.Interaction.FollowupAsync("Invalid form body. Please check to ensure that all of your parameters are correct.", ephemeral: true);
                        break;
                    }
                    if (notifyOwnerOnError && !string.IsNullOrEmpty(_config["OwnerID"]))
                    {
                        string error = Format.Bold("Error:") + "\n" + Format.Code(arg3.ErrorReason) + "\n\n" + Format.Bold("Command:") + "\n" + Format.BlockQuote(arg1.Name + " " + DiscordTools.SlashParamToString(arg2));
                        if (error.Length > 2000)
                        {
                            error = error.Substring(0, 2000);
                        }
                        UserExtensions.SendMessageAsync(_client.GetUser(ulong.Parse(_config["OwnerID"])), error);
                    }
                    arg2.Interaction.FollowupAsync("Sorry, Something went wrong...", ephemeral: true);
                    break;

                case InteractionCommandError.Unsuccessful:
                    //notify owner if desired:
                    if (notifyOwnerOnError && !string.IsNullOrEmpty(_config["OwnerID"]))
                    {
                        string error = Format.Bold("Error:") + "\n" + Format.Code(arg3.ErrorReason) + "\n\n" + Format.Bold("Command:") + "\n" + Format.BlockQuote(arg1.Name + " " + DiscordTools.SlashParamToString(arg2));
                        if (error.Length > 2000)
                        {
                            error = error.Substring(0, 2000);
                        }
                        UserExtensions.SendMessageAsync(_client.GetUser(ulong.Parse(_config["OwnerID"])), error);
                    }
                    arg2.Interaction.FollowupAsync("Sorry, Something went wrong...", ephemeral: true);
                    break;

                default:
                    //notify owner if desired:
                    if (notifyOwnerOnError && !string.IsNullOrEmpty(_config["OwnerID"]))
                    {
                        string error = Format.Bold("Error:") + "\n" + Format.Code(arg3.ErrorReason) + "\n\n" + Format.Bold("Command:") + "\n" + Format.BlockQuote(arg1.Name + " " + DiscordTools.SlashParamToString(arg2));
                        if (error.Length > 2000)
                        {
                            error = error.Substring(0, 2000);
                        }
                        UserExtensions.SendMessageAsync(_client.GetUser(ulong.Parse(_config["OwnerID"])), error);
                    }
                    arg2.Interaction.FollowupAsync("Sorry, Something went wrong...");
                    break;
                }
            }

            return(Task.CompletedTask);
        }
Example #3
0
        async Task SlashCommandExecuted(SlashCommandInfo arg1, Discord.IInteractionContext arg2, Discord.Interactions.IResult arg3)
        {
            if (!arg3.IsSuccess)
            {
                switch (arg3.Error)
                {
                case InteractionCommandError.UnmetPrecondition:
                    await arg2.Interaction.RespondAsync($"Unmet Precondition: {arg3.ErrorReason}", ephemeral : true);

                    break;

                case InteractionCommandError.UnknownCommand:
                    await arg2.Interaction.RespondAsync("Unknown command", ephemeral : true);

                    break;

                case InteractionCommandError.BadArgs:
                    await arg2.Interaction.RespondAsync("Invalid number or arguments", ephemeral : true);

                    break;

                case InteractionCommandError.Exception:
                    Console.WriteLine("Command Error:");
                    Console.WriteLine(arg3.ErrorReason);
                    await arg2.Interaction.RespondAsync($"Command exception: {arg3.ErrorReason}. If this message persists, please let us know in the support server (https://discord.gg/xyzMyJH) !", ephemeral : true);

                    break;

                case InteractionCommandError.Unsuccessful:
                    await arg2.Interaction.RespondAsync("Command could not be executed", ephemeral : true);

                    break;

                default:
                    break;
                }
            }
        }
        private async Task SlashCommandExecutedAsync(SlashCommandInfo command, IInteractionContext context, Discord.Interactions.IResult result)
        {
            // the command was successful, we don't care about this result, unless we want to log that a command succeeded.
            if (result.IsSuccess)
            {
                return;
            }

            await context.Interaction.RespondAsync(result.ErrorReason, ephemeral : true);
        }
Example #5
0
        private async Task CmdErroredHandler(SlashCommandInfo info, IInteractionContext context, Discord.Interactions.IResult result)
        {
            if (!result.IsSuccess)
            {
                if (result is ExecuteResult eResult)
                {
                    if (eResult.Exception is BaseAPIException)
                    {
                        _logger.LogError($"Command '{info.Name}' invoked by '{context.User.Username}#{context.User.Discriminator}' failed: {(eResult.Exception as BaseAPIException).Error}");

                        using var scope = _serviceProvider.CreateScope();
                        Translator translator = scope.ServiceProvider.GetRequiredService <Translator>();
                        if (context.Guild != null)
                        {
                            await translator.SetContext(context.Guild.Id);
                        }

                        string errorCode = "#" + ((int)(eResult.Exception as BaseAPIException).Error).ToString("D4");

                        EmbedBuilder builder = new EmbedBuilder()
                                               .WithTitle(translator.T().SomethingWentWrong())
                                               .WithColor(Color.Red)
                                               .WithDescription(translator.T().Enum((eResult.Exception as BaseAPIException).Error))
                                               .WithCurrentTimestamp()
                                               .WithFooter($"{translator.T().Code()} {errorCode}");

                        try
                        {
                            await context.Interaction.RespondAsync(embed : builder.Build());
                        }
                        catch (TimeoutException)
                        {
                            await context.Channel.SendMessageAsync(embed : builder.Build());
                        }
                    }
                    else
                    {
                        _logger.LogError($"Command '{info.Name}' invoked by '{context.User.Username}#{context.User.Discriminator}' failed: " + eResult.Exception.Message + "\n" + eResult.Exception.StackTrace);
                    }
                }
                else
                {
                    _logger.LogError($"Command '{info.Name}' invoked by '{context.User.Username}#{context.User.Discriminator}' failed due to {result.Error}.");
                }
            }
        }