[英]When user reacts to message, bot sends another message in discord.js
I want to know how I can do it that if someone reacts to a specific emoji, the bot sends another message.我想知道如何做到这一点,如果有人对特定的表情符号做出反应,机器人会发送另一条消息。 It is for a help command.
它用于帮助命令。 I have done the command itself and that the bot reacts to his own message and I also have added embeds for the other messages, I just don't know how to make it work.
我已经完成了命令本身并且机器人对他自己的消息做出了反应,并且我还为其他消息添加了嵌入,我只是不知道如何使其工作。
Here is my code:这是我的代码:
const { DiscordAPIError } = require("discord.js");
const Discord = require('discord.js');
const client = new Discord.Client();
module.exports = {
name: 'help',
description: "Overview of all commands!",
execute(message, args) {
const embed = new Discord.MessageEmbed()
.setColor("#171b20")
.setTitle("Help")
.setDescription("React to the message to see commands of a specific category!")
.addFields({
name: ":red_square:" + " Moderation",
value: '\u200b'
}, {
name: ":orange_square:" + " Interaction",
value: '\u200b'
}, {
name: ":yellow_square:" + " Fun",
value: '\u200b'
}, {
name: ":purple_square:" + " Games",
value: '\u200b'
}, {
name: ":brown_square:" + " NSFW",
value: '\u200b'
}, {
name: ":green_square:" + " Information",
value: '\u200b'
})
.setFooter(message.author.username)
.setTimestamp();
message.channel.send(embed).then(embedMsg => {
embedMsg.react("🟥")
.then(reaction => embedMsg.react('🟧'))
.then(reaction => embedMsg.react('🟨'))
.then(reaction => embedMsg.react('🟪'))
.then(reaction => embedMsg.react('🟫'))
.then(reaction => embedMsg.react('🟩'))
})
const moderation = new Discord.MessageEmbed()
.setColor("#ff0000")
.setTitle("Help")
.setDescription("Below you can see the moderation commands.")
.addFields({
name: "test",
value: "this is a test"
})
.setFooter(message.author.username)
.setTimestamp();
const interaction = new Discord.MessageEmbed()
.setColor("#ffa500")
.setTitle("Help")
.setDescription("Below you can see the interaction commands.")
.addFields({
name: "test",
value: "this is a test"
})
.setFooter(message.author.username)
.setTimestamp();
const fun = new Discord.MessageEmbed()
.setColor("#ffff00")
.setTitle("Help")
.setDescription("Below you can see the fun commands.")
.addFields({
name: "test",
value: "this is a test"
})
.setFooter(message.author.username)
.setTimestamp();
const games = new Discord.MessageEmbed()
.setColor("#e600e6")
.setTitle("Help")
.setDescription("Below you can see the games commands.")
.addFields({
name: "test",
value: "this is a test"
})
.setFooter(message.author.username)
.setTimestamp();
const nsfw = new Discord.MessageEmbed()
.setColor("#4d0000")
.setTitle("Help")
.setDescription("Below you can see the nsfw commands.")
.addFields({
name: "test",
value: "this is a test"
})
.setFooter(message.author.username)
.setTimestamp();
const information = new Discord.MessageEmbed()
.setColor("#008000")
.setTitle("Help")
.setDescription("Below you can see the information commands.")
.addFields({
name: "test",
value: "this is a test"
})
.setFooter(message.author.username)
.setTimestamp();
}
}
You can use reaction collectors .您可以使用反应收集器。 I would store the commands in an array of objects though, so you can use a lot less code.
不过,我会将命令存储在一个对象数组中,因此您可以使用更少的代码。
I've created a categories
array that contains the basic categories, their emojis, name, color, title, description, and commands.我创建了一个
categories
数组,其中包含基本类别、它们的表情符号、名称、颜色、标题、描述和命令。 This way, you can loop over this array any time needed (eg when the bot sends first reaction, etc).这样,您可以在任何需要的时候循环遍历这个数组(例如,当机器人发送第一反应时等)。
I've added a few comments below:我在下面添加了一些评论:
const categories = [
{
emoji: '🟥',
name: 'Moderation',
color: '#ff0000',
title: 'Help',
description: 'Below you can see the moderation commands.',
commands: [
{
name: 'Moderation 1',
value: 'to moderate stuff',
},
{
name: 'Moderation 2',
value: 'to moderate other stuff',
},
],
},
{
emoji: '🟧',
name: 'Interaction',
color: '#ffa500',
title: 'Help',
description: 'Below you can see the interaction commands.',
commands: [
{
name: 'Interaction 1',
value: 'to interact 1',
},
{
name: 'Interaction 2',
value: 'to interact 2',
},
],
},
{
emoji: '🟨',
name: 'Fun',
color: '#ffff00',
title: 'Help',
description: 'Below you can see the fun commands.',
commands: [
{
name: 'Fun 1',
value: 'have fun 1',
},
{
name: 'Fun 2',
value: 'have fun 2',
},
],
},
{
emoji: '🟪',
name: 'Games',
color: '#e600e6',
title: 'Help',
description: 'Below you can see the games commands.',
commands: [
{
name: 'Game 1',
value: 'to play game 1',
},
{
name: 'Game 2',
value: 'to play game 2',
},
{
name: 'Game 3',
value: 'to play game 3',
},
],
},
{
emoji: '🟫',
name: 'NSFW',
color: '#4d0000',
title: 'Help',
description: 'Below you can see the NSFW commands.',
commands: [
{
name: 'NSFW 1',
value: 'nsfw 1',
},
{
name: 'NSFW 2',
value: 'nsfw 2',
},
],
},
{
emoji: '🟩',
name: 'Information',
color: '#008000',
title: 'Help',
description: 'Below you can see the information commands.',
commands: [
{
name: 'Info 1',
value: 'to get info 1',
},
],
},
];
module.exports = {
name: 'help',
description: 'Overview of all commands!',
execute(message, args) {
const embed = new Discord.MessageEmbed()
.setColor('#171b20')
.setTitle('Help')
.setDescription('React to the message to see commands of a specific category!')
.addFields(
// add fields for each category
categories.map((cat) => ({
name: `${cat.emoji} ${cat.name}`,
value: '\u200b',
}))
)
.setFooter(message.author.username)
.setTimestamp();
message.channel.send(embed).then((embedMsg) => {
// send reactions for each emojis
const emojis = categories.map((cat) => cat.emoji);
emojis.forEach((emoji) => embedMsg.react(emoji));
// the filter checks if the reaction emoji is in the categories
// it also checks if the person who reacted shares the same id
// as the author of the original message
const filter = (reaction, user) =>
emojis.includes(reaction.emoji.name) && user.id === message.author.id;
const collector = embedMsg.createReactionCollector(filter, {
// max number of reactions is the number of categories
max: emojis.length,
// it won't accept reactions after 60 seconds
// optional, you can remove/change it
time: 60000,
});
collector.on('collect', (reaction, user) => {
// find the category by its emoji
const selectedCategory = categories.find(
(category) => category.emoji === reaction.emoji.name,
);
if (!selectedCategory) {
return message.channel.send('Oops, there was an error... Try again?!');
}
const embed = new Discord.MessageEmbed()
.setColor(selectedCategory.color)
.setTitle(selectedCategory.title)
.setDescription(selectedCategory.description)
.addFields(selectedCategory.commands)
.setFooter(message.author.username)
.setTimestamp();
message.channel.send(embed);
});
collector.on('end', (collected, reason) => {
// reactions are no longer collected
// if the user clicked on every available emoji
if (reason === 'limit')
return message.channel.send(`You've checked every emoji, ${message.author}. I won't accept any more reactions.`);
// if it's timeout
return message.channel.send(`It's been a minute now, so I won't accept any more reactions.`);
});
});
},
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.