如何让机器人在收到特定消息的反应后将消息发送到特定频道
How to make a bot send a message to a specific channel after receiving a reaction from a certain message
所以我正在尝试为一个非常小的项目开发一个机器人(我不是程序员或任何东西,只需要做一件事)。我需要它做的就是收集我发送的特定消息的反应,并在检测到反应后立即向频道发送另一条消息。该消息将包含反应堆的标签和一些文本。我需要它一直积极收集反应,没有任何限制。我尝试查看文档,但我真的不知道如何实施 .awaitmessageReactions 或它所谓的任何内容。你能帮帮我吗?
您可以为此使用方法 createReactionCollector
。但是当机器人宕机时,这个收集器就会停止。
const Discord = require('discord.js');
const bot = new Discord.Client();
let targetChannelId = '1232132132131231';
bot.on('ready', () => {
console.log(`${bot.user.tag} is ready on ${bot.guilds.cache.size} servers!`);
});
bot.on('message', (message) => {
if (message.content === 'test') {
message.channel.send(`i\`m await of reactions on this message`).then((msg) => {
const filter = (reaction, user) => !user.bot;
const collector = msg.createReactionCollector(filter);
collector.on('collect', (reaction, user) => {
let channel = message.guild.channels.cache.get(targetChannelId);
if (channel) {
let embed = new Discord.MessageEmbed();
embed.setAuthor(
user.tag,
user.displayAvatarURL({
dynamic: true,
format: 'png',
}),
);
}
embed.setDescription(`${user} (${user.tag}) has react a: ${reaction.emoji}`);
channel.send(embed);
});
collector.on('end', (reaction, reactionCollector) => {
msg.reactions.removeAll();
});
});
}
});
bot.login('token');
或者您可以使用发射器 messageReactionAdd
并处理对特定消息的反应。
const Discord = require('discord.js')
const token = require('./token.json').token
const bot = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });
bot.once('ready', () => {
console.log(`${bot.user.tag} is ready on ${bot.guilds.cache.size} guilds`)
})
let targetChannelId = '668497133011337224'
bot.on('messageReactionAdd', async (reaction, user) => {
if (reaction.partial) {
// If the message this reaction belongs to was removed the fetching might result in an API error, which we need to handle
try {
await reaction.fetch();
} catch (error) {
console.log('Something went wrong when fetching the message: ', error);
// Return as `reaction.message.author` may be undefined/null
return;
}
}
if (reaction.message.id === '730000158745559122') {
let channel = reaction.message.guild.channels.cache.get(targetChannelId);
console.log(reaction.emoji)
if (channel) {
let embed = new Discord.MessageEmbed();
embed.setAuthor(
user.tag,
user.displayAvatarURL({
dynamic: true,
format: 'png',
}),
);
embed.setDescription(`${user} has react a: ${reaction.emoji}`);
channel.send(embed);
}
}
});
bot.login(token)
所以我正在尝试为一个非常小的项目开发一个机器人(我不是程序员或任何东西,只需要做一件事)。我需要它做的就是收集我发送的特定消息的反应,并在检测到反应后立即向频道发送另一条消息。该消息将包含反应堆的标签和一些文本。我需要它一直积极收集反应,没有任何限制。我尝试查看文档,但我真的不知道如何实施 .awaitmessageReactions 或它所谓的任何内容。你能帮帮我吗?
您可以为此使用方法 createReactionCollector
。但是当机器人宕机时,这个收集器就会停止。
const Discord = require('discord.js');
const bot = new Discord.Client();
let targetChannelId = '1232132132131231';
bot.on('ready', () => {
console.log(`${bot.user.tag} is ready on ${bot.guilds.cache.size} servers!`);
});
bot.on('message', (message) => {
if (message.content === 'test') {
message.channel.send(`i\`m await of reactions on this message`).then((msg) => {
const filter = (reaction, user) => !user.bot;
const collector = msg.createReactionCollector(filter);
collector.on('collect', (reaction, user) => {
let channel = message.guild.channels.cache.get(targetChannelId);
if (channel) {
let embed = new Discord.MessageEmbed();
embed.setAuthor(
user.tag,
user.displayAvatarURL({
dynamic: true,
format: 'png',
}),
);
}
embed.setDescription(`${user} (${user.tag}) has react a: ${reaction.emoji}`);
channel.send(embed);
});
collector.on('end', (reaction, reactionCollector) => {
msg.reactions.removeAll();
});
});
}
});
bot.login('token');
或者您可以使用发射器 messageReactionAdd
并处理对特定消息的反应。
const Discord = require('discord.js')
const token = require('./token.json').token
const bot = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });
bot.once('ready', () => {
console.log(`${bot.user.tag} is ready on ${bot.guilds.cache.size} guilds`)
})
let targetChannelId = '668497133011337224'
bot.on('messageReactionAdd', async (reaction, user) => {
if (reaction.partial) {
// If the message this reaction belongs to was removed the fetching might result in an API error, which we need to handle
try {
await reaction.fetch();
} catch (error) {
console.log('Something went wrong when fetching the message: ', error);
// Return as `reaction.message.author` may be undefined/null
return;
}
}
if (reaction.message.id === '730000158745559122') {
let channel = reaction.message.guild.channels.cache.get(targetChannelId);
console.log(reaction.emoji)
if (channel) {
let embed = new Discord.MessageEmbed();
embed.setAuthor(
user.tag,
user.displayAvatarURL({
dynamic: true,
format: 'png',
}),
);
embed.setDescription(`${user} has react a: ${reaction.emoji}`);
channel.send(embed);
}
}
});
bot.login(token)