private async Task ChecksFailedError(CommandsNextExtension c, CommandErrorEventArgs e) { if (e.Exception is ChecksFailedException checksFailed) { IReadOnlyList <CheckBaseAttribute> failedChecks = checksFailed.FailedChecks; string DetermineMessage() { if (failedChecks.Any(x => x is RequireBotPermissionsAttribute)) { return("I don't have the permissions necessary"); } if (failedChecks.Any(x => x is RequireUserPermissionsAttribute)) { return("you don't have the permissions necessary"); } if (failedChecks.Any(x => x is CooldownAttribute)) { CooldownAttribute cooldown = failedChecks.First(x => x is CooldownAttribute) as CooldownAttribute; return($"this command is on cooldown for {cooldown.GetRemainingCooldown(e.Context):hh\\:mm\\:ss}"); } if (failedChecks.Any(x => x is RequireOwnerAttribute)) { return("this command can only be used by the Bot's owner"); } return("The check failed is unknown"); } await e.Context.RespondAsync($"You can't use `{e.Command.QualifiedName}` because {DetermineMessage()}."); e.Handled = true; } }
private string ParseFailedCheck(CheckBaseAttribute attr) { return(attr switch { CooldownAttribute _ => "You cannot do that so often!", RequireOwnerAttribute _ => "Only the server owner can use that command!", RequirePermissionsAttribute _ => "You don't have permission to do that!", RequireRolesAttribute _ => "You do not have a required role!", RequireUserPermissionsAttribute _ => "You don't have permission to do that!", RequireNsfwAttribute _ => "This command can only be used in an NSFW channel!", _ => "Unknown Discord API error occured, please try again later." });
private static string TranslateFailedCheck(CheckBaseAttribute check, CommandContext ctx) { return(check switch { RequireOwnerAttribute => "You must be the bot's owner to use this command", RequireGuildAttribute => "The command can only be used in a Guild channel (not in a DM)", RequireDirectMessageAttribute => "The command can only be used in a Direct Message", RequireBotPermissionsAttribute botperm => $"The Bot doesn't have the required permissions. It needs: {botperm.Permissions}", RequireUserPermissionsAttribute userPerm => $"You don't have the required permissions. You need: {userPerm.Permissions}", RequirePermissionsAttribute perms => $"You or the bot don't the required permissions: {perms.Permissions}", RequireRolesAttribute roles => $"You need the following role(s) to use this command: {string.Join(", ", roles.RoleNames)}", RequireNsfwAttribute => $"This command can only be used in a nsfw channel!", CooldownAttribute cooldown => $"This command has a cooldown. Please wait {cooldown.GetRemainingCooldown(ctx).Humanize(culture: new("en-GB"))} before you can use it again.", _ => $"{check.TypeId} failed" });