node.js - 所以我试图使用 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 的内容都可以正常工作
解决方案
好吧,在文档中很清楚,days
需要是 0 到 7 之间的值。
如果要删除较旧的消息,则需要手动获取它们,然后使用Message#delete()
or TextChannel#bulkDelete()
。
推荐阅读
- python - 如何使用具有张量流属性的所有核心中的一些(数字)核心
- json - 有没有办法自动翻译整个反应原生应用程序
- java - android studio中选项卡式活动中的选项菜单?
- scala - 如何获取 BOX 服务帐户用户 ID/邮件以将其作为协作者添加到文件中?
- html - 烧瓶项目,设置价格字段
- reactjs - 为什么 destroyOnClose={true} 在 React 中不起作用
- solr - Solr 中的 Dsolr.clustering.enabled 是什么?
- php - 如何将友好的 url 与 htdocs 一起使用?
- java - 来自文件的响应创建弹性搜索响应期间出错
- mysql - 仅当在其他表中找到某个值时才更新字段