Pycord迁移至斜杠命令后,投票命令无法添加消息反应
问题原因
在Pycord的斜杠命令逻辑里,ctx.respond()返回的是InteractionResponse交互响应对象,而非普通的Message消息对象,因此无法直接调用add_reaction()方法。而你之前用前缀命令时,ctx.send()返回的是标准Message对象,所以添加反应的逻辑能正常执行。
解决方法
修改发送消息与添加反应的代码段:先通过ctx.respond()完成斜杠命令的交互响应,再调用ctx.original_response()获取实际发送到频道的消息对象,之后再为该对象添加反应。
修改后的完整代码:
import discord import datetime from discord.ext import commands class Poll(commands.Cog): def __init__(self, client): self.client = client @discord.slash_command(description = "Create a poll in the current channel") @commands.has_permissions(manage_channels = True) @commands.bot_has_permissions(manage_messages = True, add_reactions=True) async def poll(self, ctx, *, question): poll = discord.Embed( title = "Poll", description = f"{question}", timestamp = datetime.datetime.utcnow(), colour = 0xeeffee) poll.add_field(name = "Usage:", value = "`✅ Yes | ❎ No`") await ctx.respond(embed=poll) # 获取已发送的投票消息实例 message = await ctx.original_response() await message.add_reaction("✅") await message.add_reaction("❎") def setup(client): client.add_cog(Poll(client))
额外说明:我在bot_has_permissions里显式添加了add_reactions=True,虽然manage_messages权限已经包含添加反应的权限,但显式声明能让权限要求更清晰。
内容的提问来源于stack exchange,提问作者ImAnthox
相关产品推荐
相关产品推荐

