我正在使机器人来管理我的多个Discord公会。我想建立确认系统,例如:
由于我不知道该如何构建这样的系统。
答案 0 :(得分:2)
添加和设置事件监听器
首先,我们先定义discord.js并添加事件监听器:
const Discord = require('discord.js'); //defining discord
const client = new Discord.Client(); //getting your bot
client.on('message', async message => { //when a user sends a message
}
然后,您需要告诉该漫游器之后它做什么:
const Discord = require('discord.js'); //defining discord
const client = new Discord.Client(); //getting your bot
client.on('message', async message => { //when a user sends a message
if (message.author.bot) return; //If a bot sent the message we want to ignore it.
if (message.content.toLowerCase() === 'what message your looking for' { //what your looking for (make sure this string is all in lower case)
//what you want the bot to do after the message you are looking for has been sent
}
}
现在,如果您希望机器人将响应添加到消息中,请执行以下操作:
const Discord = require('discord.js'); //defining discord
const client = new Discord.Client(); //getting your bot
client.on('message', async message => { //when a user sends a message
if (message.author.bot) return; //If a bot sent the message we want to ignore it.
if (message.content.toLowerCase() === 'what message your looking for' { //what your looking for (make sure this string is all in lower case)
await message.react('?'); //reacting to the message with a thumbs up emoji
await message.react('?'); //reacting to the message with a thumbs down emoji
}
}
如果您希望机器人回复邮件,请使用:
const Discord = require('discord.js'); //defining discord
const client = new Discord.Client(); //getting your bot
client.on('message', async message => { //when a user sends a message
if (message.author.bot) return; //If a bot sent the message we want to ignore it.
if (message.content.toLowerCase() === 'what message your looking for' { //what your looking for (make sure this string is all in lower case)
message.channel.send('The bots message here') //what you want the bot to reply with
}
}
等待反应
在这里,这完全取决于您是否要等待对机器人消息或用户消息的反应。
如果您想等待来自机器人消息的响应,请使用:
const Discord = require('discord.js'); //defining discord
const client = new Discord.Client(); //getting your bot
client.on('message', async message => { //when a user sends a message
if (message.author.bot) return; //If a bot sent the message we want to ignore it.
if (message.content.toLowerCase() === 'what message your looking for' { //what your looking for (make sure this string is all in lower case)
message = await message.channel.send('The bots message here') //waiting for the message to be sent
const filter = (reaction, user) => { //filtering the reactions from the user
return (
['?', '?'].includes(reaction.emoji.name) && user.id === message.author.id
);
}
message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] }) //awaiting the reactions - remember the time is in milliseconds
.then((collected) => {
const reaction = collected.first();
if (reaction.emoji.name === '?') { //if the reaction was a thumbs up
//A action
reaction.users.remove(message.author.id) //If you wanted to remove the reaction
} else { //if the reaction was a thumbs down
//B action
reaction.users.remove(message.author.id) //If you wanted to remove the reaction
}
}).catch((collected) => { //when time is up
//C action
});
}
}
如果要等待用户消息中的消息,除了更改之外,您将做同样的事情:
if (message.content.toLowerCase() === 'what message your looking for' { //what your looking for (make sure this string is all in lower case)
message.channel.send('The bots message here') //sending the message but not awaiting reactions from it