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

discord.py中wait_for()函数报错:缺少必填参数'event'求助

Discord Wordle机器人报错:TypeError: wait_for() missing 1 required positional argument: 'event'

我正在制作一款Wordle Discord机器人,但运行时持续报错。代码如下:

async def playwordle(ctx):
  word_list = []
  word_file = open("wordle_words.txt")
  for word in word_file:
    word_list.append(word.strip())

  wordle_answer = random.choice(word_list)
  guess_num = 0
  guess_correct = False

  while guess_num < 6 and not guess_correct:
    msg = await client.wait_for('message', check=lambda m: m.author==ctx.author and m.channel==ctx.channel)
    ctx.send(msg)
    guess_num += 1
    guess_correct = processGuess(ctx, wordle_answer, msg)

报错信息:

TypeError: wait_for() missing 1 required positional argument: 'event'


问题分析与修复方案

1. 核心错误:client.wait_for调用问题

报错提示缺少event参数,主要有两种原因:

  • client不是正确的Discord客户端实例:确保你的client是discord.Client()或commands.Bot()的合法实例,且在playwordle函数中能正常访问。比如用commands.Bot初始化时,变量名通常是bot,此时要对应使用bot.wait_for:
    import discord
    from discord.ext import commands
    
    bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
    
  • 实例作用域问题:如果client是在其他作用域定义的(比如主函数里),要确保playwordle能正确引用到这个变量,可通过参数传递或全局声明解决。

2. 其他必修复的问题

  • ctx.send缺少await关键字:ctx.send是异步方法,必须加await才能执行;同时直接发送msg(Message对象)无效,要发送用户输入的内容msg.content:
    await ctx.send(msg.content)
    
  • 文件操作优化:用with语句读取文件,自动管理资源避免泄漏:
    word_list = []
    with open("wordle_words.txt", "r") as word_file:
        for word in word_file:
            word_list.append(word.strip())
    
  • 确认processGuess函数有效性:确保该函数能正确接收ctx、答案、用户消息三个参数,并返回布尔值表示是否猜对。

修复后的完整代码示例

async def playwordle(ctx):
    word_list = []
    with open("wordle_words.txt", "r") as word_file:
        for word in word_file:
            word_list.append(word.strip())

    wordle_answer = random.choice(word_list)
    guess_num = 0
    guess_correct = False

    while guess_num < 6 and not guess_correct:
        msg = await client.wait_for('message', check=lambda m: m.author == ctx.author and m.channel == ctx.channel)
        await ctx.send(msg.content)
        guess_num += 1
        guess_correct = processGuess(ctx, wordle_answer, msg)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 04:30:54