Exemple #1
0
        public static async Task OnCommandError(CommandErrorEventArgs e)
        {
            if (e.Command == null)
            {
                return;
            }

            if (e.Context.Guild == null || (e.Context.Channel.PermissionsFor(e.Context.Member) & Permissions.SendMessages) != 0)
            {
                switch (e.Exception)
                {
                case ArgumentException _:
                    await e.Context.RespondAsync($"{Emojis.ErrorEmoji} Invalid input.");

                    return;

                case ChecksFailedException checks:
                    switch (checks.FailedChecks.FirstOrDefault())
                    {
                    case RequirePermissionsAttribute attr:
                        string perms = null;
                        Stringify.RolePermissions(attr.Permissions).ForEach((x) => perms += $"- **{x}**\n");

                        await e.Context.RespondAsync($"{Emojis.ErrorEmoji} The command \"`{e.Command.Name}`\" requires elevated permissions.", embed : new DiscordEmbedBuilder()
                                                     .WithDescription(perms)
                                                     .WithTitle("Required permissions")
                                                     .Build());

                        break;

                    case RequireDirectMessageAttribute _:
                        await e.Context.RespondAsync($"{Emojis.ErrorEmoji} The command \"`{e.Command.Name}`\" is restricted to direct messages only.");

                        break;

                    case RequireGuildAttribute _:
                        await e.Context.RespondAsync($"{Emojis.ErrorEmoji} The command \"`{e.Command.Name}`\" is restricted to servers only.");

                        break;

                    case RequireNsfwAttribute _:
                        await e.Context.RespondAsync($"{Emojis.ErrorEmoji} The command \"`{e.Command.Name}`\" cannot be executed outside of a channel set as NSFW one.");

                        break;

                    case RequireRolesAttribute attr:
                        await e.Context.RespondAsync($"{Emojis.ErrorEmoji} The command \"`{e.Command.Name}`\" requires you to possess {(attr.RoleNames.Count == 1 ? "a " : "")}certain role{(attr.RoleNames.Count > 1 ? "s" : "")}: {attr.RoleNames.Aggregate((a, b) => a + ", " + b)}");

                        break;

                    default:
                        break;
                    }

                    return;

                default:
                    break;
                }
            }

            e.Context.Client.DebugLogger.LogMessage(LogLevel.Error, "Commands", $"The command \"{e.Command.Name}\" executed by the user {e.Context.User.Username}#{e.Context.User.Discriminator} (ID: {e.Context.User.Id}) in channel \"{e.Context.Channel.Id}\" encountered an error.", DateTime.Now, e.Exception);
            if (e.Context.Guild == null || (e.Context.Channel.PermissionsFor(e.Context.Member) & Permissions.SendMessages) != 0)
            {
                await e.Context.RespondAsync("An error occurred.");
            }
        }