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

运行Gravital Discord Bot遇TypeError错误,求解决方法

解决Discord Bot的Intents错误问题

错误原因分析

  • 第一个错误TypeError: Client.__init__() missing 1 required keyword-only argument: 'intents':discord.py 2.0及以上版本要求必须显式指定机器人的intents(权限意图),不再支持旧版本的默认初始化方式。
  • 第二个错误TypeError: Client.__init__() takes 1 positional argument but 4 were given:你错误地在类继承时直接实例化discord.Client,继承语法要求写类名而非实例对象,这属于写法误用。

正确修改方法

方法1:实例化机器人时传入intents

保留原类定义,仅在创建实例阶段添加intents参数:

import discord

class ChatBot(discord.Client):
    # 原有类方法(如on_ready、on_message等)保持不变
    async def on_ready(self):
        print(f'Logged in as {self.user}')

# 创建intents对象,开发阶段可启用所有权限
intents = discord.Intents.all()
# 实例化机器人并传入intents参数
bot = ChatBot(intents=intents)
# 替换为你的机器人Token后运行
bot.run('你的Discord机器人Token')

方法2:在类的__init__方法中指定intents

若需要在类内部处理intents,可重写__init__方法:

import discord

class ChatBot(discord.Client):
    def __init__(self):
        # 创建intents对象
        intents = discord.Intents.all()
        # 调用父类初始化方法并传入intents
        super().__init__(intents=intents)
    
    async def on_ready(self):
        print(f'Logged in as {self.user}')

# 实例化并运行机器人
bot = ChatBot()
bot.run('你的Discord机器人Token')

注意事项

  • 上线部署时不建议使用Intents.all(),应根据机器人实际需求开启对应intents,同时要在Discord开发者平台的机器人页面开启对应的特权网关意图。
  • 确保discord.py为最新版本,可执行pip install -U discord.py更新。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 07:36:20