問題描述
我希望 discord.js 機器人在執(zhí)行命令時發(fā)送消息,對其做出反應,然后捕獲用戶的所有反應以執(zhí)行某些操作,例如響應他們.反應不應有時間限制.我還應該提到我正在使用命令處理程序,并且消息是從 index.js 以外的另一個文件發(fā)送的(甚至應該在其中發(fā)生收集).
I want the discord.js bot to send a message when a command is executed, react on it and then catch all reactions from users to do certain stuff, like respond to them. No time limit should be applied to reactions. I should also mention that I'm using a command handler and the message is being sent from another file than index.js (in which the collect even should occur).
我嘗試做一個收集器,但無論我怎么做,收集器變量都沒有定義或不起作用.我知道它應該是一個事實的收集器.
I tried doing a collector but no matter how I do it, the collector variable is not defined or not working. I know it should be a collector for a fact though.
為復選標記表情符號制作過濾器:
Making a filter for the checkmark emoji:
return reaction.emoji.name === `?` && user.id === call.message.author.id;
}
試圖捕捉命令塊本身之外的反應(因為如果我在塊中這樣做,事件將不起作用:
Trying to catch reactions outside the command block itself (because if I do it IN the block the event won't work:
collector.on(`collect`, (reaction, reactionCollector) => {
console.log(`Caught ${reaction.emoji.name}`);
});
我得到的錯誤幾乎是意料之中的,但我想解決這個問題:
The errors I get are pretty much expected but I want to solve that:
collector.on(`collect`, (reaction, reactionCollector) => {
^
ReferenceError: collector is not defined
也許是一個全局變量或設置消息 id 然后獲取?幫幫我.
Maybe a global variable or setting the message id and then fetching? Help me.
推薦答案
為什么不在你的 command.js 中包含 Collector?
Why not include the Collector in your command.js?
const time = 60000 //amount of time to collect for in milliseconds
receivedMessage.channel.send("Hello World")
.then(async function (message) {
await message.react('?')
const filter = (reaction, user) => {
return //YOUR FILTER HERE;
};
const collector = message.createReactionCollector(filter, { time: time });
collector.on('collect', (reaction, reactionCollector) => {
//do stuff
});
});
這篇關(guān)于Discord.js:如何發(fā)送消息并從中收集反應的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!