我正在尝试为我的一个机器人提供建议功能。我已经在网上搜索过,但没有什么真正的帮助。建议部分正在工作,但我想添加一个功能,如果我对日志通道中的消息作出反应,它将对原始消息进行反应。这是我的代码:
bot.on('message', message => {
if (message.content.startsWith("/suggest")){
message.reply(`Your response has been recorded.`)
var yes = message.content
const channel1 = bot.channels.cache.get('722590953017442335');
channel1.send(`${message.author} suggests: ${yes.slice(9)}`)
if (chanell1.reaction.emoji.name === '✅'){
const channel2 = bot.channels.cache.get('722595812462297139');
channell2.react.author.message('✅')
}
}
})
我正在使用节点的v12。
答案 0 :(得分:1)
请阅读https://discord.js.org/#/docs/main/v12/general/welcome上的官方文档以获取v12帮助。您应该使用Client#messageReactionAdd
事件来跟踪反应-您的代码距离不太远,但是缺少该关键事件。请注意,如果要使反应在重新启动后起作用,则需要跟踪存储来跟踪反应。或者,您可以尝试等待反应,或者仅在短期内尝试使用反应收集器。
尝试以下方法:
const { Client } = require('discord.js');
const bot = new Client({ partials: ['REACTION', 'USER'] });
const prefix = '/';
const suggestionsCache = {};
bot.on('message', async message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.split(' '),
command = args.shift().slice(prefix.length);
if (command == 'suggest') {
const suggestion = args.join(' '),
suggestionMessage = `${message.author} suggests: ${suggestion}`,
suggestionChannel = bot.channels.cache.get('722590953017442335'),
logChannel = bot.channels.cache.get('722595812462297139');
if (!suggestionChannel || !logChannel) return message.reply('Oops! Something went wrong.');
try {
const suggestionMessage = await suggestionChannel.send(suggestionMessage);
const logMessage = await logChannel.send(suggestionMessage);
suggestionsCache[logMessage.id] = suggestionMessage.id; // Save the ID for later.
message.reply('Your response has been recorded.');
} catch {
message.reply('Oops! Something went wrong.');
};
};
});
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.partial) {
try {
await reaction.fetch();
} catch {}
}
const messageID = suggestionsCache[reaction.message.id];
if (!messageID || reaction.emoji.name !== '✅') return; // If not found in cache, ignore and if the emoji isn't the check, ignore it.
try {
const channel = await client.channels.fetch('722590953017442335');
if (!channel) return;
const message = channel.messages.fetch(messageID);
if (!message) return; // Message deleted.
message.react('✅');
} catch {
return;
};
});
请注意,我是v12的新手,通常使用v11!上面的代码未经测试,因此可能包含错误。请随时编辑/更新代码。
答案 1 :(得分:1)
您可以使用awaitReactions()
函数:
import axios from "axios";
import { sampleSize } from "lodash";
export default async () => {
return new Promise((resolve, reject) => {
axios.get("https://picsum.photos/list").then(res => {
console.log(res.data[0]);
return resolve(sampleSize(res.data, 50));
});
});
};