我想执行例如eat之类的命令,而我想进行机器人ping操作,这是一个示例:D
bot.on('message', (message) => {
const parts = message.content.split('');
if (parts [0] == '!eat'){
if (parts [1] == 'The member that i want to be pinged by the bot') {
message.channel.send('The user that i pinged on command has been eated!')
}
};
}
答案 0 :(得分:2)
我做了一些更改:
prefix
变量,以便可以轻松更改前缀。if
语句替换为switch
语句。我强烈建议您阅读Discord.js guide,因为它非常适合初学者,并且可以使您编写比此答案更好的代码。此答案中的许多代码均来自Discord.js指南。
const prefix = '!';
bot.on('message', (message) => {
if (message.author.bot || !message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).trim().split(/\s+/g);
const command = args.shift().toLowerCase();
switch (command) {
case 'eat':
const member = message.mentions.members.first();
if (!member) return message.reply('Could not find the mentioned user.');
message.channel.send(`${member.user.username} has been eaten!`);
break;
default:
message.reply(`The command \`${command}\` was not recognized`);
}
});
设置命令前缀:
const prefix = '!';
如果消息是由漫游器发送的,或者不是以前缀开头的,则该函数退出:
if (message.author.bot || !message.content.startsWith(prefix)) return;
从邮件开头删除前缀,并在所有空白处拆分邮件:
const args = message.content.slice(prefix.length).trim().split(/\s+/g);
第一个参数被删除,转换为小写并存储在command
变量中:
const command = args.shift().toLowerCase();
找到第一个提到的用户:
const member = message.mentions.members.first();
如果没有提到的用户,则返回警告消息:
if (!member) return message.reply('Could not find the mentioned user.');
一条消息发送到服务器,说明“ [用户]已被吃掉!” :
message.channel.send(`${member.user.username} has been eaten!`);
答案 1 :(得分:0)
<@ $ {message.member.id}>这将仅使用(``)而不是('')ping邮件的作者,并将ping作者