如何使某些角色运行discord.js命令



我的discord.js bot中有一个命令,我只想使用某些角色。但是,我一运行代码就出现错误。

const Discord = require('discord.js');
const keepAlive = require('./server');
const client = new Discord.Client();
checkRole = member.roles.cache.has('725812531637125120');
client.on('message', message => {
if (message.toString().includes('[PING]')) {
if (checkRole){
message.channel.send(`> <@&727633811709362187> :arrow_up:`);
}
else {
const embed = new Discord.MessageEmbed()
.setTitle(`Invalid Command`)
.setColor('#0099ff')
.setDescription(`${message.author}, the command that you have used is invalid:nThis command is only for managment.`)
message.author.send(embed);
}
}
});
keepAlive();
client.login(process.env.TOKEN);

这是错误。

ReferenceError: member is not defined

如果有人能给我一个解决方案,那就太好了!

如果您想访问GuildMember,您需要通过消息变量来访问它,该变量只能在事件回调函数内部访问
您还应该检查message.member是否为空(可能是通过DM发送消息而不是在公会频道上(,message.member.user是否不是机器人程序(只是为了确保您的机器人程序不会对来自其他机器人程序和自身的消息做出反应(类似这样的东西:

client.on('message', message => {
if (message.author !== null && message.author.bot) return;
if (message.toString().includes('[PING]')) {
if (message.member !== null && message.member.roles.cache.has('725812531637125120');){
message.channel.send(`> <@&727633811709362187> :arrow_up:`);
}
else {
const embed = new Discord.MessageEmbed()
.setTitle(`Invalid Command`)
.setColor('#0099ff')
.setDescription(`${message.author}, the command that you have used is invalid:nThis command is only for managment.`)
message.author.send(embed);
}
}
});

如果你想阅读更多:(https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=member)

最新更新