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

AttributeError: 'coroutine'对象无'content'属性问题修复求助

Discord石头剪刀布机器人错误修复

问题描述

我正在为Discord服务器制作石头剪刀布机器人,在Discord输入答案前运行正常,但输入答案后命令提示符出现以下错误:

AttributeError: 'coroutine' object has no attribute 'content'
C:\Users\tuhin\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py:348: RuntimeWarning: coroutine 'wait_for' was never awaited
  await self.on_error(event_name, *args, **kwargs)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

原代码如下:

# bot.py
import os
import discord
from dotenv import load_dotenv
import random
from discord.ext import commands
load_dotenv()
PYTHONTRACEMALLOC = 1
TOKEN = "MTAwOTI1OTYxOTMwNDA5OTkwMQ.Gr-Cgn.L-m3iIIgWXkVvdGbdX3lWun9yetTOAuzIkbGI0"
client = discord.Client()
list = ['rock', 'paper', 'scissors']
@client.event
async def on_ready():
    print(f'{client.user.name} has connected to Discord!')

@client.event
async def on_member_join(member):
    member.create_dm()
    member.dm_channel.send(
        f'Hi {member.name}, welcome to my Discord server!'
    )
@client.event
async def on_message(message):
    if message.author.bot: return
    if message.content.lower() == "rps":
        await message.channel.send(message.author.mention + " started a game of rps")
        def is_correct(m):
            return m.author == message.author
            
        user_choice = (client.wait_for('message', check=is_correct)).content
        choices = random.choice(list)
        print(choices)
        if user_choice.lower() == choices:
            message.channel.send("Tie!")
        elif user_choice.lower() == "rock" and choices == "paper":
            message.channel.send("i win, i chose paper")
        elif user_choice.lower() == "rock" and choices == "scissors":
            message.channel.send("you win, i chose scissors")
        elif user_choice.lower() == "scissors" and choices == "paper":
            message.channel.send("you win, i chose paper")
        elif user_choice.lower() == "scissors" and choices == "rock":
            message.channel.send("i win, i chose rock")
        elif user_choice.lower() == "paper" and choices == "scissors":
            message.channel.send("i win, i chose scissors")
        elif user_choice.lower() == "paper" and choices == "rock":
            message.channel.send("you win, i chose rock")

client.run(TOKEN)

错误原因与修复方案

1. 未异步等待wait_for返回结果

client.wait_for是异步协程函数,必须用await等待它执行完成才能获取到消息对象。直接调用会得到coroutine对象,无法访问.content属性。

修复:

# 替换原获取用户选择的代码
user_message = await client.wait_for('message', check=is_correct)
user_choice = user_message.content

2. 发送消息未使用await

Discord.py中channel.send是异步函数,必须用await调用,否则会触发协程未被等待的警告。

修复:
所有message.channel.send(...)前添加await,例如:

await message.channel.send("Tie!")

3. on_member_join中的异步调用遗漏await

member.create_dm()和member.dm_channel.send()同样是异步函数,需要添加await:

@client.event
async def on_member_join(member):
    await member.create_dm()
    await member.dm_channel.send(
        f'Hi {member.name}, welcome to my Discord server!'
    )

4. 变量名冲突

list是Python内置类型,用它做变量名会覆盖内置功能,建议修改为choices_list:

choices_list = ['rock', 'paper', 'scissors']
bot_choice = random.choice(choices_list)

修复后的完整代码

# bot.py
import os
import discord
from dotenv import load_dotenv
import random
from discord.ext import commands
load_dotenv()
PYTHONTRACEMALLOC = 1
TOKEN = "MTAwOTI1OTYxOTMwNDA5OTkwMQ.Gr-Cgn.L-m3iIIgWXkVvdGbdX3lWun9yetTOAuzIkbGI0"
client = discord.Client()
choices_list = ['rock', 'paper', 'scissors']

@client.event
async def on_ready():
    print(f'{client.user.name} has connected to Discord!')

@client.event
async def on_member_join(member):
    await member.create_dm()
    await member.dm_channel.send(
        f'Hi {member.name}, welcome to my Discord server!'
    )

@client.event
async def on_message(message):
    if message.author.bot: return
    if message.content.lower() == "rps":
        await message.channel.send(f"{message.author.mention} started a game of rps")
        
        def is_correct(m):
            return m.author == message.author
            
        user_message = await client.wait_for('message', check=is_correct)
        user_choice = user_message.content
        bot_choice = random.choice(choices_list)
        print(bot_choice)
        
        if user_choice.lower() == bot_choice:
            await message.channel.send("Tie!")
        elif user_choice.lower() == "rock" and bot_choice == "paper":
            await message.channel.send("I win, I chose paper")
        elif user_choice.lower() == "rock" and bot_choice == "scissors":
            await message.channel.send("You win, I chose scissors")
        elif user_choice.lower() == "scissors" and bot_choice == "paper":
            await message.channel.send("You win, I chose paper")
        elif user_choice.lower() == "scissors" and bot_choice == "rock":
            await message.channel.send("I win, I chose rock")
        elif user_choice.lower() == "paper" and bot_choice == "scissors":
            await message.channel.send("I win, I chose scissors")
        elif user_choice.lower() == "paper" and bot_choice == "rock":
            await message.channel.send("You win, I chose rock")

client.run(TOKEN)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 00:21:41