首页 > 解决方案 > 所以我试图使用 discord.js V12 发出禁令命令,无论如何我总是遇到同样的错误

问题描述

禁令命令听起来很简单,我知道我可以在互联网上复制一个,但我想用时间等参数制作更具体的禁令命令,看起来像这样:

const client = new Discord.Client();
client.client;
//message is an array of strings from  the message the user send
//and args is everything after the !ban so the mention is args[0]

module.exports = {
    name : 'ban',
    description : 'bans any user for as long as you want(unit in days)',
    execute(message, args) {
        if(!message.mentions.users.first()) {
            return message.channel.send('please specify a target');
        }
        // target is who will be banned
        const target = message.guild.members.cache.get(message.mentions.users.first().id);
        // time is how long will the user be banned (in days) it's mutiplied by 1 to convert it from a stirng to  NaN or a number parseInt() works too :)
        const time = args[1] * 1;
        // checks if there are any arguments
        if(args.length === 1) {
            message.channel.send('you have not input a reason and a time for the ban');
            return;
        }
        else if(!isNaN(time)) {
            // this is where the problem is
            try {
                target.send('you were banned, dummy accout'); // << this works
                target.ban({ reason:args.slice(2).join(' '), days:time }); // << but this doesn't
                /* (node:10484) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body
delete_message_days: int value should be less than or equal to 7. */
            }
            catch(error) {
                // this code does not execute
                console.error(error);
                message.reply('there was an issue executing the command');
            }
            return;
        }
// this one works as well only when i dont give a time for the ban (if the if statement above returns false )
        else if(typeof args[1] === 'string') {
            target.ban({ reason:args.slice(1).join(' ') });
            return;
        }
    },

};

我已经设置了一个命令处理程序,并且我拥有的每个命令都可以正常工作,但是在这个命令上我不断收到此错误(节点:10484) UnhandledPromiseRejectionWarning:DiscordAPIError:无效的表单正文 delete_message_days:int 值应该小于或等于 7。而且很有趣够了,如果我输入 !ban @user 然后任何小于 7 的内容都可以正常工作

标签: node.jsdiscord.js

解决方案


好吧,在文档中很清楚,days需要是 0 到 7 之间的值。
如果要删除较旧的消息,则需要手动获取它们,然后使用Message#delete()or TextChannel#bulkDelete()


推荐阅读