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

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

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

@bot.tree.command(name = 'timeout_vote', description = 'vote for someone to be timed out')
@app_commands.checks.has_any_role('verified')
@app_commands.describe(timeout = 'Person you want to vote on to timeout')
async def timeout_vote(interaction: discord.Interaction, timeout: discord.Member, reason: str = 'No reason given'):
  embed = discord.Embed(title = 'TIMEOUT VOTE', description = '')
  embed.set_author(name = timeout.name, icon_url = timeout.display_avatar)
  embed.add_field(name = 'Member being voted on:', value = timeout.mention, inline = False)
  embed.add_field(name = 'Responsible vote executer:', value = interaction.user.mention, inline = False)
  if timeout.display_avatar == None:
    embed.set_thumbnail(url = interaction.avatar.url)
  else:
    embed.set_thumbnail(url = timeout.display_avatar)
  vote = 'U+2705'
  message = await interaction.response.send_message(embed = embed)
  await message.add_reaction(emoji = vote)

@timeout_vote.error
async def timeout_vote_error(ctx, error):
  if isinstance(error, app_commands.MissingAnyRole):
    await bot.get_channel(1106325554942197992).send(content = 'You must be verified to complete this command')
smdnsysy

smdnsysy1#

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

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

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

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

相关问题