所以,我尝试在我的 Discord 机器人上制作石头、纸、剪刀命令。 这段代码的工作方式是用户对他/她选择的表情符号之一做出反应,机器人随机选择其响应并发送它。 代码如下:
module.exports = {
name: 'rps-game',
description: 'play a rps game',
async execute(message, args, Discord, client) {
const rockemoji = '?'
const paperemoji = '?'
const scissorsemoji = '✂'
const rpsembed = new Discord.MessageEmbed()
.setTitle('A game of RPS')
.setDescription('React to one of the emojis below!')
const messageEmbed = await message.channel.send(rpsembed)
messageEmbed.react(rockemoji)
messageEmbed.react(paperemoji)
messageEmbed.react(scissorsemoji)
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch()
if (reaction.partial) await reaction.fetch()
if (user.bot) return
if (!reaction.message.guild) return
const rpschoices = ['?', '?', '✂']
const unexchoice = rpschoices[Math.floor(Math.random() * rpschoices.length)]
if (reaction.emoji.name === rockemoji && unexchoice === "?") {
const unexwin1 = new Discord.MessageEmbed()
.setTitle('RPS results')
.addFields(
{ name: 'Your Choice', value: '?' },
{ name: 'Unex\'s Choice', value: '?' }
)
message.channel.send(unexwin1)
message.channel.send('Unex wins!')
return
}
else if (reaction.emoji.name === paperemoji && unexchoice === "✂") {
const unexwin2 = new Discord.MessageEmbed()
.setTitle('RPS results')
.addFields(
{ name: 'Your Choice', value: '?' },
{ name: 'Unex\'s Choice', value: '✂' }
)
message.channel.send(unexwin2)
message.channel.send('Unex wins!')
return
}
else if (reaction.emoji.name === scissorsemoji && unexchoice === "?") {
const unexwin3 = new Discord.MessageEmbed()
.setTitle('RPS results')
.addFields(
{ name: 'Your Choice', value: '✂' },
{ name: 'Unex\'s Choice', value: '?' }
)
message.channel.send(unexwin3)
message.channel.send('Unex wins!')
return
}
else if (reaction.emoji.name === rockemoji && unexchoice === "?") {
const unextie1 = new Discord.MessageEmbed()
.setTitle('RPS results')
.addFields(
{ name: 'Your Choice', value: '?' },
{ name: 'Unex\'s Choice', value: '?' }
)
message.channel.send(unextie1)
message.channel.send('Its a tie!')
return
}
else if (reaction.emoji.name === paperemoji && unexchoice === "?") {
const unextie2 = new Discord.MessageEmbed()
.setTitle('RPS results')
.addFields(
{ name: 'Your Choice', value: '?' },
{ name: 'Unex\'s Choice', value: '?' }
)
message.channel.send(unextie2)
message.channel.send('Its a tie!')
return
}
else if (reaction.emoji.name === scissorsemoji && unexchoice === "✂") {
const unextie3 = new Discord.MessageEmbed()
.setTitle('RPS results')
.addFields(
{ name: 'Your Choice', value: '✂' },
{ name: 'Unex\'s Choice', value: '✂' }
)
message.channel.send(unextie3)
message.channel.send('Its a tie!')
return
}
else if (reaction.emoji.name === rockemoji && unexchoice === "✂") {
const userwin1 = new Discord.MessageEmbed()
.setTitle('RPS results')
.addFields(
{ name: 'Your Choice', value: '?' },
{ name: 'Unex\'s Choice', value: '✂' }
)
message.channel.send(userwin1)
message.channel.send('You win!')
return
}
else if (reaction.emoji.name === paperemoji && unexchoice === "?") {
const userwin2 = new Discord.MessageEmbed()
.setTitle('RPS results')
.addFields(
{ name: 'Your Choice', value: '?' },
{ name: 'Unex\'s Choice', value: '?' }
)
message.channel.send(userwin2)
message.channel.send('You win!')
return
}
else if (reaction.emoji.name === scissorsemoji && unexchoice === "?") {
const userwin1 = new Discord.MessageEmbed()
.setTitle('RPS results')
.addFields(
{ name: 'Your Choice', value: '✂' },
{ name: 'Unex\'s Choice', value: '?' }
)
message.channel.send(userwin1)
message.channel.send('You win!')
return
}
})
}
}
问题: 第一次使用时,机器人返回一条消息,第二次使用时,机器人返回两条消息,依此类推
如你所见,我已经尝试过返回,但它不会工作。
答案 0 :(得分:0)
发生这种情况的原因是您在命令中添加了 messageReactionAdd
事件侦听器。每次调用您的命令时,都会创建另一个事件侦听器并将再次执行代码。您可以使用 awaitReactions
,而不是将事件侦听器放在您的命令中。这些将允许您在消息发送后的一段时间内检查对消息的反应。例如:
const filter = (reaction, user) => {
const rpschoices = ['?', '?', '✂']
return rpschoises.includes(reaction.emoji.name) && user.id === message.author.id;
};
messageEmbed.awaitReactions(filter, { /*options*/ })
.then(collected => {}) //your handler code here
您可以在 the docs 中阅读有关 awaitReactions 的选项和方法的更多信息。