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

Discord.py添加Cog触发TypeError:module()最多接受2个参数(传入3个)

错误原因

你触发该TypeError的核心原因是Cog基类引用错误:

  • Discord.py中供用户继承的Cog基类是commands.Cog(首字母大写),而你代码中写的commands.cog是commands模块下的一个子模块对象,而非类。当你尝试继承模块对象并传参实例化时,就会触发模块对象的参数数量不匹配错误,也就是你看到的提示:

module() takes at most 2 arguments (3 given)

  • 除此之外你的代码还有两个隐藏问题:
    1. self.client.guilds()写法错误,guilds是客户端的属性,不是可调用方法,不需要加括号
    2. client.add_cog是异步方法,你调用时没有加await,会触发异步调用异常
修复方案

1. 修正Cog类实现代码

# 继承的基类改为大写开头的commands.Cog
class Commands(commands.Cog):
  def __init__(self, client):
    self.client = client
    self.song_queue = {}
    self.setup()

  def setup(self):
    # guilds是属性,去掉括号
    for guild in self.client.guilds:
      self.song_queue[guild.id] = []

2. 修正Cog注册加载代码

# setup函数需要接收client作为入参
async def setup(client):
  # add_cog是异步方法,需要await
  await client.add_cog(Commands(client))

# 主文件中注册写法:
client.loop.create_task(setup(client))
# 如果是将Cog写为独立扩展文件,不需要手动调用setup,直接使用await client.load_extension("你的Cog文件名")加载即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 10:15:07