试图让我的机器人处理对一条消息的多种反应。
如果我只检查一种反应,我可以得到一个这样的版本:
reaction, user = await bot.wait_for('reaction_add', timeout=5, check=checkR)
但是当我检查多个反应(如纸和剪刀)时,该代码根本无法工作。
我到处都在搜索有关此方面的帮助,但找不到Discord后重写的任何内容。
任何帮助表示赞赏!
# test rps
@bot.command()
async def test(ctx):
eb = await getEmbed(ctx, "Rock, Paper, Scissors", "", {}, "", "Choose one:", discord.Colour.gold())
msg = await ctx.message.channel.send(embed = eb)
channel = msg.channel
for emoji in ('?', '?', "✂"):
await msg.add_reaction(emoji)
# now check for response
def checkR(reaction, user):
return user == ctx.message.author and str(reaction.emoji) == '?'
def checkP(reaction, user):
print("in paper")
return user == ctx.message.author and str(reaction.emoji) == '?'
def checkS(reaction, user):
return user == ctx.message.author and str(reaction.emoji) == '✂'
try:
reaction, user = await bot.wait_for('reaction_add', timeout=5, check=checkR)
reaction, user = await bot.wait_for('reaction_add', timeout=5, check=checkP)
reaction, user = await bot.wait_for('reaction_add', timeout=5, check=checkS)
except asyncio.TimeoutError:
await embed(ctx, "Game timed out.")
return
else:
# we got a reaction
await embed(ctx, "GOT A REACTION")
await discord.Message.delete(msg)
pass
答案 0 :(得分:0)
reaction, user = await bot.wait_for('reaction_add', timeout=5, check=checkR)
reaction, user = await bot.wait_for('reaction_add', timeout=5, check=checkP)
reaction, user = await bot.wait_for('reaction_add', timeout=5, check=checkS)
这等待有人按顺序对Rock,Paper,剪刀进行反应。它不只接受纸张,也不接受剪刀。它希望所有3个反应都按照该顺序进行。
您需要编写如下内容:
def check(reaction, user):
return user == ctx.message.author and str(reaction.emoji) in ['?', '?', '✂']
reaction, user = await bot.wait_for('reaction_add', timeout=5, check=check)
这将寻找一种反应,该反应是石头,纸张或剪刀。
答案 1 :(得分:0)
感谢我一直在寻找这种方法 这就是我想出的。
创建消息并添加反应:
@commands.command()
async def pageturn(self, ctx):
guildid = str(ctx.guild.id)
userid = str(ctx.author.id)
previuspage = '⬅️'
nextpage = '➡️'
page = 1
msg = await ctx.send(f'page{page}')
await msg.add_reaction(previuspage)
await msg.add_reaction(nextpage)
def checkforreaction(reaction, user):
return str(user.id) == userid and str(reaction.emoji) in [previuspage,nextpage]
#循环直到反应添加超时
loopclose = 0
while loopclose == 0:
try:
reaction, user = await self.client.wait_for('reaction_add', timeout=5,check = checkforreaction)
if reaction.emoji == nextpage:
page += 1
elif reaction.emoji == previuspage:
page-= 1
await msg.edit(content=f'page{page}')
except asyncio.TimeoutError:
await ctx.send('timeout')
loopclose = 1