我正在尝试在discord.js 中为我的机器人发出命令。它应该做什么,每当服务器的管理员使用它时,它都会改变状态。我尝试制作它,但它不起作用。这是我的代码:
client.on('message', message => {
if (message.content.startsWith('!status')) {
const prefix = '!';
const args = message.content.slice(prefix.length).trim().split(' ');
if (member.hasPermission('ADMINISTRATOR')) {
const activity = args.splice(2);
client.user.setActivity(activity);
}
else {
message.channel.send('You\'re not the admin of the server or bot!')
}
}
});
最佳答案
第一个问题是member
未定义,如果你想让ti成为消息作者,可以使用message.member
代替。
第二个问题是activity
是一个数组,它needs to be a string或 ActivityOptions
目的。您可以使用 args
的第一个元素作为状态,或者如果您想接受多个单词,请通过空格连接
它们。
检查下面的代码,我也添加了一些注释:
const prefix = '!';
client.on('message', (message) => {
// if the message doesn't start with the prefix or the author is a bot, exit early
if (!message.content.startsWith(prefix) || message.author.bot) return;
// create an args variable that slices off the prefix and splits it into an array
const args = message.content.slice(prefix.length).split(/ +/);
// create a command variable by taking the first element in the array
// and removing it from args
const command = args.shift().toLowerCase();
if (command === 'status') {
// if the message author has no permission, send a message and exit
if (!message.member.hasPermission('ADMINISTRATOR'))
return message.channel.send('Your not the Admin of the server or bot!');
// if there are more than one words as the status, join them again
const activity = args.join(' ');
client.user.setActivity(activity);
}
});
关于javascript - 如何在discord.js 中创建使状态发生变化的命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67534879/