Exemple #1
0
        private string ParseFailedCheck(CheckBaseAttribute attr)
        {
            switch (attr)
            {
            case CooldownAttribute _:
                return("You cannot use do that so often!");

            case RequireOwnerAttribute _:
                return("Only the server owner can use that command!");

            case RequirePermissionsAttribute _:
                return("You don't have permission to do that!");

            case RequireRolesAttributeAttribute _:
                return("You do not have a required role!");

            case RequireUserPermissionsAttribute _:
                return("You don't have permission to do that!");

            case RequireNsfwAttribute _:
                return("This command can only be used in an NSFW channel!");

            default:
                return("Unknown Discord API error occured, please try again later.");
            }
        }
 /// <summary>
 /// Adds a pre-execution check to this command.
 /// </summary>
 /// <param name="check">Pre-execution check to add to this command.</param>
 /// <returns>This builder.</returns>
 public CommandBuilder WithExecutionCheck(CheckBaseAttribute check)
 {
     if (!this.ExecutionCheckList.Contains(check))
     {
         this.ExecutionCheckList.Add(check);
     }
     return(this);
 }
Exemple #3
0
        private static string ToCleanReason(this CheckBaseAttribute check)
        {
            if (check is DescriptiveCheckBaseAttribute descriptiveCheck)
            {
                return(descriptiveCheck.FailureResponse);
            }

            return(check.ToString());
        }
Exemple #4
0
 private static string ParseFailedCheck(CheckBaseAttribute attr)
 {
     return(attr switch
     {
         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.",
         RequirePollStartChannelAttribute => ", you cannot start a poll in this channel.",
         _ => ", an unknown Discord API error has occurred, contact the developer and please try again later."
     });
Exemple #5
0
 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"
     });
Exemple #6
0
        /// <summary>
        ///     Render the error message for an Attribute
        /// </summary>
        /// <param name="checkBase">The attribute</param>
        /// <param name="lang">The language</param>
        /// <param name="isinguild">Was the command executed in a guild or in direct messages</param>
        /// <param name="e">Gives the raw command error arguments</param>
        /// <returns>A <see cref="string" /> containing the error message</returns>
        private static string RenderErrorMessageForAttribute(CheckBaseAttribute checkBase, Language lang,
                                                             bool isinguild, CommandErrorEventArgs e)
        {
            var type = checkBase.GetType();

            if (type == typeof(RequireDjAttribute))
            {
                return(lang.RequireDJCheckFailed);
            }
            if (type == typeof(RequireGuildAttribute))
            {
                return(lang.RequireGuildCheckFailed);
            }
            if (type == typeof(RequireNsfwAttribute))
            {
                return(lang.RequireNsfwCheckFailed);
            }
            if (type == typeof(RequireOwnerAttribute))
            {
                return(lang.RequireOwnerCheckFailed);
            }
            return(checkBase switch
            {
                RequireRolesAttribute requireRolesAttribute when requireRolesAttribute.RoleNames.Count == 1 => string.Format(lang.RequireRolesCheckFailedSG, requireRolesAttribute.RoleNames[0]),
                RequireRolesAttribute requireRolesAttribute => string.Format(lang.RequireRolesCheckFailedPL, requireRolesAttribute.RoleNames.Humanize()),
                RequireBotPermissionsAttribute requireBotPermissions when !(requireBotPermissions.IgnoreDms && isinguild) => lang.RequireGuildCheckFailed,
                RequireBotPermissionsAttribute requireBotPermissions when Enum.IsDefined(requireBotPermissions.Permissions) &&
                requireBotPermissions.Permissions != Permissions.All => string.Format(lang.RequireBotPermisionsCheckFailedSG,
                                                                                      requireBotPermissions.Permissions.Humanize(LetterCasing.LowerCase)),
                RequireBotPermissionsAttribute requireBotPermissions => string.Format(lang.RequireBotPermisionsCheckFailedPL,
                                                                                      requireBotPermissions.Permissions.Humanize(LetterCasing.LowerCase)),
                RequireUserPermissionsAttribute userPermissions when !(userPermissions.IgnoreDms && isinguild) => lang.RequireGuildCheckFailed,
                RequireUserPermissionsAttribute userPermissions when Enum.IsDefined(userPermissions.Permissions) && userPermissions.Permissions != Permissions.All => string.Format(lang.RequireUserPermisionsCheckFailedSG,
                                                                                                                                                                                    userPermissions.Permissions.Humanize(LetterCasing.LowerCase)),
                RequireUserPermissionsAttribute userPermissions => string.Format(lang.RequireUserPermisionsCheckFailedPL,
                                                                                 userPermissions.Permissions.Humanize(LetterCasing.LowerCase)),
                RequirePermissionsAttribute userAndBotPermissions when !(userAndBotPermissions.IgnoreDms && isinguild) => lang.RequireGuildCheckFailed,
                RequirePermissionsAttribute userAndBotPermissions when Enum.IsDefined(userAndBotPermissions.Permissions) &&
                userAndBotPermissions.Permissions != Permissions.All => string.Format(lang.RequireBotAndUserPermisionsCheckFailedSG,
                                                                                      userAndBotPermissions.Permissions.Humanize(LetterCasing.LowerCase)),
                RequirePermissionsAttribute userAndBotPermissions => string.Format(lang.RequireBotAndUserPermisionsCheckFailedPL,
                                                                                   userAndBotPermissions.Permissions.Humanize(LetterCasing.LowerCase)),
                RequireAttachmentAttribute attachmentAttribute when e.Context.Message.Attachments.Count > attachmentAttribute.AttachmentCount => (string)typeof(Language).GetProperty(attachmentAttribute.MoreThenLang)?.GetValue(lang),
                RequireAttachmentAttribute attachmentAttribute => (string)typeof(Language).GetProperty(attachmentAttribute.LessThenLang)?.GetValue(lang),
                _ => string.Format(lang.CheckFailed, type.Name.RemoveStringFromEnd("Attribute").Humanize()),
            });
Exemple #7
0
        private async Task CommandErroredAsync(CommandErrorEventArgs eventArgs)
        {
            switch (eventArgs.Exception)
            {
            case CommandNotFoundException _:
                break;

            case ChecksFailedException e:
                CheckBaseAttribute check = e.FailedChecks[0];
                switch (check)
                {
                case RequirePermissionsAttribute attr:
                    Console.WriteLine(attr.Permissions);
                    break;

                case RequireUserPermissionsAttribute attr:
                    Console.WriteLine(attr.Permissions);
                    break;
                }
                break;

            case ArgumentException _:
                GuildsModel guild = eventArgs.Context.Channel.IsPrivate ? null : new GuildsModel {
                    ID = eventArgs.Context.Guild.Id
                };
                Locale locale = new LocaleExtension().GetLocale(guild);
                DiscordEmbedBuilder builder = new DiscordEmbedBuilder {
                    Color = new DiscordColor(ColorConstant.embedColor),
                    Title = (await new StringsDAO().LoadAsync(new StringsModel {
                        Locale = locale,
                        Identifier = "ArgumentError"
                    })).String
                };
                await eventArgs.Context.Channel.SendMessageAsync(embed : await new CommandUseExtension().GetCommandUseAsync(builder, eventArgs.Context.Command, locale, new PrefixExtension().GetPrefix(guild)));

                break;

            default:
                eventArgs.Context.Client.DebugLogger.LogMessage(LogLevel.Error, "Handler", eventArgs.Exception.StackTrace, DateTime.Now);
                break;
            }
        }
        /// <summary>
        /// Converts a <see cref="CheckBaseAttribute"/> to a human-readable format.
        /// </summary>
        /// <param name="attribute">The attribute to describe.</param>
        /// <returns>A description of <paramref name="attribute"/></returns>
        public static string ToDescription(this CheckBaseAttribute attribute)
        {
            //TODO: Add localization support.
#pragma warning disable IDE0011 // Add braces
            switch (attribute)
            {
            case RequireBotPermissionsAttribute botPermissionsAttribute:
                return("Requires the bot to have the following permissions" + botPermissionsAttribute.Permissions.ToPermissionString());

            case RequireDirectMessageAttribute _:
                return("Requires to be run inside direct messages");

            case RequireGuildAttribute _:
                return("Requires to be run inside a server");

            case RequireNsfwAttribute _:
                return("Requires to be run inside a NSFW channel");

            case RequireOwnerAttribute _:
            case RequireUserGroupAttribute _:
                return("Requires to be run by the owner of this bot");

            case RequirePermissionsAttribute permissionsAttribute:
                return("Requires the following permissions: " + permissionsAttribute.Permissions.ToPermissionString());

            case RequirePrefixesAttribute prefixesAttribute:
                return("Requires to be run with one of following prefixes: " + string.Join(", ", prefixesAttribute.Prefixes));

            case RequireRolesAttribute rolesAttribute:
                return("Requires the user to have following roles: " + string.Join(", ", rolesAttribute.RoleNames));

            case RequireUserPermissionsAttribute userPermissionsAttribute:
                return("Requires the user to have following permissions: " + userPermissionsAttribute.Permissions.ToPermissionString());
            }
#pragma warning restore IDE0011 // Add braces

            return("Description not available");
        }
Exemple #9
0
 private static string?GetFailedCheckMessage(CheckBaseAttribute failedCheck, CommandContext context) =>
 failedCheck switch
 {
Exemple #10
0
 /// <summary>
 /// Adds a pre-execution check to this command.
 /// </summary>
 /// <param name="check">Pre-execution check to add to this command.</param>
 /// <returns>This builder.</returns>
 public CommandBuilder WithExecutionCheck(CheckBaseAttribute check)
 {
     this.ExecutionCheckList.Add(check);
     return(this);
 }