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

使用discord.py开发Discord机器人遇TypeError: event()缺少必填参数'coro'

问题解决:TypeError: event() missing 1 required positional argument: 'coro'

错误根源在于你没有正确实例化Discord的Client类:

  • 原代码中client = discord.Client只是将变量指向了类本身,而非创建类的实例
  • @client.event装饰器需要作用在Client实例上才能正确绑定协程函数,直接使用类会触发参数缺失错误

修正后的代码

#import
import discord
import os

#client - 新增括号实例化Client类
client = discord.Client()

@client.event
async def on_ready():
    print('Best bot is online. ({0.user})'.format(client))

#commands
@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('Ωhello'):
        await message.channel.send('responded Hello World!')

#run
client.run(os.getenv('token'))

关键说明

  1. discord.Client()会创建Client类的实例,这是Discord机器人运行的核心对象
  2. 只有基于实例使用@client.event装饰器,才能正确注册on_ready、on_message这类事件处理协程

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 23:10:52