Discord Python报错:bot对象无event属性问题求助
问题排查:AttributeError: module 'discord.client' has no attribute 'event'
错误根源
- 你错误地将
commands.bot(模块)赋值给了client,正确的类是commands.Bot(注意首字母大写)。 - 未实例化
Bot类:commands.Bot是一个类,必须创建实例并传入必要参数(如命令前缀、Intents)才能使用其event装饰器。
修正后的代码
import discord from discord.ext import commands # 实例化Bot类,指定命令前缀和默认Intents client = commands.Bot(command_prefix='!', intents=discord.Intents.default()) @client.event async def on_ready(): print("Bot is ready to execute action") # 替换为你的Bot令牌,从Discord开发者平台获取 client.run('YOUR_BOT_TOKEN')
注意事项
- Intents是Discord Bot的必填项,
discord.Intents.default()提供基础权限,若需要读取消息等更多功能,需在开发者门户开启对应Intents并在代码中声明。 - 务必保护好你的Bot令牌,不要公开或提交到版本控制系统。
内容的提问来源于stack exchange,提问作者That one noob coder
相关产品推荐
相关产品推荐

