![](/img/trans.png)
[英]When user reacts to message, bot sends another message in discord.js
[英]Bot confirmation after user reacts not displaying in Discord.js
我希望用户使用反应来回答“是或否”的问题。 但是,当被标记的用户对问题做出反应时,机器人不会发送关于被标记的用户是否想要协商的消息。 下面是我的代码。
const yesEmoji = '✅';
const noEmoji = '❌';
client.on('message', (negotiate) => {
const listen = negotiate.content;
const userID = negotiate.author.id;
var prefix = '!';
var negotiating = false;
let mention = negotiate.mentions.users.first();
if(listen.toUpperCase().startsWith(prefix + 'negotiate with '.toUpperCase()) && (mention)) {
negotiate.channel.send(`<@${mention.id}>, do you want to negotiate with ` + `<@${userID}>`)
.then(async (m) => {
await m.react(yesEmoji);
await m.react(noEmoji);
//get an answer from the mentioned user
const filter = (reaction, user) => user.id === mention.id;
const collector = negotiate.createReactionCollector(filter);
collector.on('collect', (reaction) => {
if (reaction.emoji.name === yesEmoji) {
negotiate.channel.send('The mentioned user is okay to negotiate with you!');
}
else {
negotiate.channel.send('The mentioned user is not okay to negotiate with you...')
}
})
})
negotiating = true;
}
})
So far, the code displays the reaction but it does not make the bot send a message whether the tagged user is ok or not ok to negotiate with the user that tagged them.
更新:我设法让机器人发送一条消息,无论标记的用户是否可以与标记他们的用户协商。 现在有一个错误,在 10 秒(指定时间)后显示。 以下是更新后的代码:
const yesEmoji = '✅';
const noEmoji = '❌';
client.on("message", async negotiate => {
const listen = negotiate.content;
let mention = negotiate.mentions.users.first();
if(listen.toUpperCase().startsWith(prefix + 'negotiate with '.toUpperCase()) && (mention)) {
let mention = negotiate.mentions.users.first();
let msg = await negotiate.channel.send(`${mention} do you want to negotiate with ${negotiate.author}`);
var negotiating = false;
await msg.react(yesEmoji);
await msg.react(noEmoji);
const filter = (reaction, member) => {
return reaction.emoji.name === yesEmoji || reaction.emoji.name === noEmoji && member.id === mention.id;
};
msg.awaitReactions(filter, { max: 1, time: 10000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === yesEmoji) {
negotiating = true;
negotiate.reply('The mentioned user agreed to negotiate with you!');
}
else return negotiate.reply('The mentioned user did not agree to negotiate with you.')
})
}
})
对于您的问题,我有一个更简单的解决方案:
const yesEmoji = '✅';
const noEmoji = '❌';
let mention = negotiate.mentions.users.first();
if(mention.id === negotiate.author.id) return message.channel.send("You cannot tag yourself!");
let msg = await negotiate.channel.send(`${mention} do you want to negotiate with ${negotiate.author}`);
var negotiating = false;
await msg.react(yesEmoji);
await msg.react(noEmoji);
const filter = (reaction, member) => {
return (member.id === mention.id && reaction.emoji.name === yesEmoji) || (member.id === mention.id && reaction.emoji.name === noEmoji);
};
msg.awaitReactions(filter, { max: 1, time: 10000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === yesEmoji) {
negotiating = true;
negotiate.channel.send('The mentioned user is okay to negotiate with you!');
}
else if (reaction.emoji.name === noEmoji) return negotiate.channel.send('The mentioned user is not okay to negotiate with you...')
}).catch(err => {
if(err) return message.channel.send(`${mention} did not react within the 10 seconds!`);
})
所以首先我们得到了两个表情符号,我们希望用户做出反应。 mention
是我们提到的用户, msg
是“是或否”的问题, negotiating
默认设置为假。 起初,我们用表情符号对问题做出反应。 在此示例中,我使用awaitReactions
,因为它使用起来非常简单。 为此,我们需要一个过滤器。 在这种情况下,我也将变量命名为filter
。 filter
检查反应是yesEmoji
还是noEmoji
以及反应的用户是否被mention
(我们提到的用户)。 然后在awaitReactions
中,我们只需要1 个反应(是或否),我将时间设置为 10 秒,但您可以根据需要更改它。 在awaitReactions
设置之后,我们想要收集我们的反应。 这是在.then()
中完成的。 collected
给了我们反应,我们只想要第一个,所以我们将collected.first()
存储在reaction
中。 现在我们有了一个非常简单的 if-else 语句。 如果反应的 emoji 是yesEmoji
, negotiating
将被设置为 true 并且一条消息被发送到通道中,否则它只会发送一条消息并返回。
如果用户对yesEmoji
做出反应,将negotiating
设置为true
很重要。 在您的代码中,即使什么也没发生也是true
,因为当您运行命令时,该命令代码中的所有内容都将被执行。 最后一行是negotiating = true;
. 我认为这不是你想做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.