javascript - 当用户对消息做出反应时,机器人会在 discord.js 中发送另一条消息
问题描述
我想知道我该怎么做,如果有人对特定的表情符号做出反应,机器人会发送另一条消息。这是一个帮助命令。我自己完成了命令,机器人对他自己的消息做出反应,我还为其他消息添加了嵌入,我只是不知道如何让它工作。
这是我的代码:
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();
}
}
解决方案
您可以使用反应收集器。不过,我会将命令存储在对象数组中,因此您可以使用更少的代码。
我创建了一个categories
数组,其中包含基本类别、表情符号、名称、颜色、标题、描述和命令。这样,您可以在任何需要的时候循环这个数组(例如当机器人发送第一反应时等)。
我在下面添加了一些评论:
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.`);
});
});
},
};
推荐阅读
- c - 使用 scanf() 函数的 C 编程中的意外输出
- javascript - 转换对象数组中的对象值?
- winforms - 当 TableLayoutPanel 激活滚动时,第一行变得太大
- c# - c# NLog如何只获取一个以longdate为文件名的日志文件
- jakarta-mail - 在邮件服务器中获取消息正文很少会导致错误的消息编号,因此返回不同消息的正文
- google-tag-manager - 对象 .... 在 GTM 变量中。如何?
- python - 每个键使用不同的值和每个键的不同颜色进行绘图
- git - 查找对当前分支引入某些更改的合并提交
- tkinter - tkinter:如何使用标签和条目在画布中显示任意大小的字典,并从输入输入操作字典
- python - Tensorflow XLA 编译器 - bazel 构建错误 - 找不到包含文件“.../hlo_ops_base.td”