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

Discord Python Bot猜颜色命令报TypeError:'str' object is not callable求助

问题修复方案

报错根因

你代码中的is_correct校验函数里错误地将m.content属性当成方法调用了:

  • m.content是discord.py中消息对象自带的字符串属性,存储了用户发送的文本内容,本身不需要加括号调用
  • 你之前写数字猜测的时候用的m.content.isdigit()是合法的,因为isdigit()是字符串对象自带的方法,需要加括号执行
    你加了()尝试调用字符串对象,就会抛出TypeError: 'str' object is not callable错误。

还有一个额外的隐性问题:你定义的colors列表里indigo后面多了一个多余的英文逗号,会导致即使用户输入正确的靛蓝单词也会匹配失败。

修复后的代码

elif user_message.lower().startswith('!guesscolor'):
    await message.channel.send('Guess a color of a rainbow')

    def is_correct(m):
        # 去掉m.content后的括号,同时校验输入是否为合法彩虹色、发送者匹配
        return m.author == message.author and m.content.lower() in ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
    colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
    answer = random.choice(colors)
    print(f'The correct color is: {answer}.')

    try:
        guess = await client.wait_for('message', check=is_correct, timeout=5.0)
    except asyncio.TimeoutError:
        return await message.channel.send(f'Sorry, you took too long it was {answer}.')
    # 统一转小写避免大小写匹配问题
    if guess.content.lower() == answer:
        await message.channel.send('You are right!')
    else:
        await message.channel.send(f'Oops, it was actually {answer}.')
        return
    return

可选优化说明

  • 校验时统一将用户输入转小写,避免用户输入大写/大小写混合的内容导致匹配失败
  • 在校验逻辑里直接判断输入是否在合法颜色列表里,避免收到无关内容也被当成有效猜测

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 16:54:01