discord.py中'Interaction'对象无'edit'属性的AttributeError问题
问题
使用discord.py开发Discord机器人时,调用Interaction对象的edit方法触发AttributeError: 'Interaction' object has no attribute 'edit'错误。已升级discord.py到最新版本,导入模块正确,且代码在其他机器可正常运行。
简化后的Botinfo.py代码
import discord from discord.ext import commands import time from main import start_time from main import emergenymessage class Botinfo(commands.Cog): def __init__(self, bot): self.bot = bot @commands.slash_command(name="botinfo", description="Show information and statistics about the bot.") async def stats(self, ctx): global message, shard_id from main import start_time message = await ctx.respond(embed=discord.Embed(colour=0x86DB98, title="Collecting Stats...", description="This should only take a second or two!")) end_time = time.time() # 此处省略部分代码 await message.edit(content=f"{emergenymessage}", embed=embed, view=view) @commands.Cog.listener() async def on_interaction(self, interaction): global message, shard_id if interaction.custom_id == "reping": await interaction.response.defer() message = await message.edit(embed=discord.Embed(colour=0x86DB98, title="Collecting Stats...", description="This should only take a second or two!")) # 此处省略大量代码 await message.edit(content=f"{emergenymessage}", embed=embed, view=view) def setup(bot): bot.add_cog(Botinfo(bot))
报错信息
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/discord/bot.py", line 1114, in invoke_application_command await ctx.command.invoke(ctx) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/discord/commands/core.py", line 375, in invoke await injected(ctx) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/discord/commands/core.py", line 132, in wrapped raise ApplicationCommandInvokeError(exc) from exc discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: AttributeError: 'Interaction' object has no attribute 'edit'
解决方案
问题根源是全局变量message被意外污染,导致其指向了Interaction对象而非具备edit方法的InteractionMessage对象。全局变量在跨函数调用时容易被错误赋值,加上on_interaction监听逻辑中未对变量有效性做判断,最终触发报错。
修正步骤:
- 替换全局变量为Cog的实例变量,避免跨函数的变量干扰
- 在
on_interaction中增加消息对象的有效性判断,防止空对象调用
修正后的代码
import discord from discord.ext import commands import time from main import start_time, emergenymessage class Botinfo(commands.Cog): def __init__(self, bot): self.bot = bot self.botinfo_msg = None # 用实例变量存储消息对象 @commands.slash_command(name="botinfo", description="Show information and statistics about the bot.") async def stats(self, ctx): from main import start_time # 将消息对象存入实例变量 self.botinfo_msg = await ctx.respond(embed=discord.Embed(colour=0x86DB98, title="Collecting Stats...", description="This should only take a second or two!")) end_time = time.time() # 此处省略部分代码(需确保embed和view已定义) await self.botinfo_msg.edit(content=f"{emergenymessage}", embed=embed, view=view) @commands.Cog.listener() async def on_interaction(self, interaction): # 先判断消息对象是否存在,再处理按钮交互 if interaction.custom_id == "reping" and self.botinfo_msg is not None: await interaction.response.defer() # 更新实例变量中的消息对象 self.botinfo_msg = await self.botinfo_msg.edit(embed=discord.Embed(colour=0x86DB98, title="Collecting Stats...", description="This should only take a second or two!")) # 此处省略大量代码(需确保embed和view已定义) await self.botinfo_msg.edit(content=f"{emergenymessage}", embed=embed, view=view) def setup(bot): bot.add_cog(Botinfo(bot))
额外检查项
- 执行
pip show discord.py确认版本为最新,若不是则重新运行pip install --upgrade discord.py - 确保
reping按钮仅在/botinfo命令触发后才会被点击,避免消息对象为空的情况
内容的提问来源于stack exchange,提问作者EK01
相关产品推荐
相关产品推荐

