就我所知,我试图发出锁定命令,这很好,但是出了点问题。
const Discord = require('discord.js');
const client = new Discord.Client();
module.exports = async (msg,client,args) => {
const channel = msg.channel;
const roles = msg.guild.roles;
if(!msg.guild) return;
if(!msg.member.hasPermission("MANAGE_CHANNELS"))
msg.channel.send("Você não tem permissão para executar esse comando.")
else{
roles.forEach((roles) => {
channel.overwritePermissions (roles ,{
SEND_MESSSAGES: false,
ADD_REACTIONS: false
})
})
msg.channel.send("Canal bloqueado com sucesso.")
}
}```
**error recived: TypeError: Cannot read property 'roles' of undefined**
答案 0 :(得分:0)
在主文件中,您将参数传递为:(客户端,味精),因此在命令文件中,您将需要以正确的顺序获取参数,
所以来自:
module.exports = async (msg, client, args) => {
}
对此:
module.exports = async (client, msg, args) => {
}
您也不会在主文件中传递args,因此请更改:
if (commands[args[0]]) commands[args[0]](client,msg);
else if(args[0].split("")[0] == config.prefix) unknownCommand(client,msg);
收件人:
const first = args[0];
if(commands[first]) {
commands[first](client, msg, args);
} else if(first[0] === config.prefix) {
unknownCommand(client, msg, args);
}
下一个改进是在您的foreach语句中:
roles.forEach((roles) => {
//code
});
这里有一组角色,然后传递的变量也称为角色,您应该将其称为角色或其他名称,
v12中的<Message>.guild.roles
也是Manager
而非集合,因此您需要添加`.cache
您还需要更改channel.overwritePermission
中的参数或切换到channel.updateOverwrite
:
roles.cache.forEach(role => {
channel.updateOverwrite(role, {
SEND_MESSSAGES: false,
ADD_REACTIONS: false
})
})
或
roles.cache.forEach(role => {
channel.overwritePermissions([
{
id: role.id,
deny: ["SEND_MESSAGE", "ADD_REACTIONS"]
}
]);
})
您可能还想研究一些更好的命令处理程序,据我所知,目前您的命令处理程序没有任何配置选项