You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyCord升级至2.6.0后Slash命令报错:Interaction无respond属性

升级PyCord至2.6.0后机器人报错AttributeError的解决方法

问题描述

将PyCord从2.4.1版本升级到2.6.0版本后,机器人完全无法运行,报错信息如下:

Extension 'cogs.stats' raised an error: AttributeError: type object 'Interaction' has no attribute 'respond'

原代码使用await ctx.response.send_message("example")发送响应,测试ctx.respond.send_message和ctx.response.send_message均出现相同错误。

相关代码

main.py

import asyncio
import log_helper
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv

bot = commands.Bot(intents=discord.Intents.all(), command_prefix="$$")
log = log_helper.create("main")

bot.help_command = None

@bot.event
async def on_ready():
    await bot.change_presence(activity=discord.Game(name="Drachen Kult"), status=discord.Status.online)
    log("Bot ist Online!", "SUCCESS")

async def load_extensions():
    log("Lade Cogs...", "SYSTEM")
    for filename in os.listdir("cogs"):
        if filename.endswith(".py"):
            log(f"{filename} wird geladen!")
            try:
                await bot.load_extension(f"cogs.{filename[:-3]}")
            except Exception as e:
                log(f"Beim Laden von {filename} ist ein Fehler aufgetreten: {e}", "CRITICAL")
    log("Alle Cogs wurden geladen!", "SUCCESS")

if __name__ == "__main__":
    load_dotenv()
    asyncio.run(load_extensions())
    log("Starte Bot...", "SYSTEM")
    bot.run(os.getenv('TOKEN'))

test.py

import discord
from discord.ext import commands, tasks
from discord.commands import slash_command


import log_helper

log = log_helper.create("Test")
load_dotenv()

class Test(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_ready(self):
        Test.bot = self.bot
      

    @slash_command(name="test")
    async def restart(self, ctx):
        await ctx.respond.send_message("Test!")


def setup(bot):
    bot.add_cog(Test(bot))

解决方案

1. 修正Slash命令的响应方式

PyCord 2.5+版本对Slash命令的交互响应API做了简化,不再需要嵌套调用send_message,直接使用ctx.respond()方法即可发送初始响应:

修改test.py中的restart方法:

@slash_command(name="test")
async def restart(self, ctx):
    await ctx.respond("Test!")

如果需要发送后续消息,可以直接使用await ctx.send("后续消息内容")。

2. 优化Bot启动流程(可选)

原main.py中asyncio.run(load_extensions())和bot.run()会创建两个独立的事件循环,可能引发潜在冲突。建议将加载扩展和启动Bot合并到同一个异步函数中:

修改main.py的启动部分:

async def main():
    load_dotenv()
    await load_extensions()
    log("Starte Bot...", "SYSTEM")
    async with bot:
        await bot.start(os.getenv('TOKEN'))

if __name__ == "__main__":
    asyncio.run(main())

确保所有异步操作在同一个事件循环中执行,避免循环冲突问题。

内容的提问来源于stack exchange,提问作者ItsKoga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 18:22:20