python-3.x 不和谐机器人没有对它的消息做出React,而是在控制台中抛出错误,这是怎么回事?discord.py

wrrgggsh  于 2023-07-01  发布在  Python
关注(0)|答案(1)|浏览(80)

我正在为我的学校服务器开发一个机器人,人们可以得到非常恼人的,这就是为什么我试图使一个超时命令,用户可以投票给某人超时。
我已经使用了await message.add_reaction(emoji = vote),但由于某种原因,机器人没有React,也没有向控制台发送错误。
这是我的代码,定义了命令和错误处理程序:

  1. @bot.tree.command(name = 'timeout_vote', description = 'vote for someone to be timed out')
  2. @app_commands.checks.has_any_role('verified')
  3. @app_commands.describe(timeout = 'Person you want to vote on to timeout')
  4. async def timeout_vote(interaction: discord.Interaction, timeout: discord.Member, reason: str = 'No reason given'):
  5. embed = discord.Embed(title = 'TIMEOUT VOTE', description = '')
  6. embed.set_author(name = timeout.name, icon_url = timeout.display_avatar)
  7. embed.add_field(name = 'Member being voted on:', value = timeout.mention, inline = False)
  8. embed.add_field(name = 'Responsible vote executer:', value = interaction.user.mention, inline = False)
  9. if timeout.display_avatar == None:
  10. embed.set_thumbnail(url = interaction.avatar.url)
  11. else:
  12. embed.set_thumbnail(url = timeout.display_avatar)
  13. vote = 'U+2705'
  14. message = await interaction.response.send_message(embed = embed)
  15. await message.add_reaction(emoji = vote)
  16. @timeout_vote.error
  17. async def timeout_vote_error(ctx, error):
  18. if isinstance(error, app_commands.MissingAnyRole):
  19. await bot.get_channel(1106325554942197992).send(content = 'You must be verified to complete this command')
smdnsysy

smdnsysy1#

首先,您的错误处理程序将抑制除app_commands.MissingAnyRole以外的任何错误,这就是为什么终端中不显示任何内容的原因。我建议添加一个else来引发未处理的错误:

  1. @timeout_vote.error
  2. async def timeout_vote_error(ctx, error):
  3. if isinstance(error, app_commands.MissingAnyRole):
  4. await bot.get_channel(1106325554942197992).send(content='You must be verified to complete this command')
  5. else:
  6. raise error

我认为产生的错误与方法InteractionResponse.send_message()没有返回任何内容有关。如果你想让消息作为响应从交互中发送,可以考虑使用Interaction.original_response():

  1. await interaction.response.send_message(embed=embed)
  2. message = await interaction.original_response()

相关问题