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

Discord Python机器人开发遇AttributeError:module 'discord.ext.commands'无'event'属性问题求助

问题排查与解决方案

嘿,我一眼就找到问题所在啦!你的报错是因为错误覆盖了client变量,导致它指向了模块而非Bot实例。

错误原因分析

你代码里的这一行:

bot = client = commands

直接把之前创建好的client = commands.Bot(...) Bot实例给覆盖了,让client变成了discord.ext.commands模块本身。而@client.event装饰器是Bot类实例专属的属性,模块本身并没有event这个属性,所以才会抛出AttributeError: module 'discord.ext.commands' has no attribute 'event'的错误。

修复步骤

只需要删掉那行错误的赋值代码即可;如果你确实需要bot变量,就让它指向已经创建好的client实例就行:

修正后的完整代码

import os
import discord
import random
from discord.ext import commands
from itertools import cycle
from webserver import keep_alive

# 创建Bot实例,这行逻辑是正确的
client = commands.Bot(command_prefix = ["!$", "m!", "mi!", "m:", "mi:", "miracle:", "Miracle:", "@Miracle#4170"], case_insensitive=True, help_command = None)
keep_alive()

# 若需要bot变量,直接指向已创建的client实例,不要覆盖原变量
bot = client

@client.event
async def on_ready():
    print('Ready')
    await client.change_presence(activity=discord.Game(name="Your Server"))

修改后,client依然是你初始化的Bot实例,@client.event就能正常工作,机器人启动后应该会顺利输出Ready并设置好状态啦。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 20:17:33