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

Discord.py:用户输入变量输出为Message对象而非内容的问题

问题解决:Discord.py中获取用户输入文本而非Message对象

问题原因

你通过bot.wait_for("message")获取到的是完整的Message对象,直接发送这个对象时,Discord.py会输出该对象的字符串化详细信息,而非用户实际输入的文本内容。你看到的输出翻译为中文如下:

<消息 id=1029677639088214026 频道=<文本频道 id=807970608410525747 名称='bot-commands' 位置=1 非成人内容=False 新闻频道=False 分类id=805140558573600819> 类型=<消息类型.默认: 0> 作者=<成员 id=712669578525802517 名称='Plasma' 鉴别码='1163' 机器人=False 昵称=None 服务器=<服务器 id=805140558125203467 名称='Bot test' 分片id=None 已分块=True 成员数=12>> 标记=<消息标记 值=0>>

解决方案

要获取用户输入的文本内容,只需访问Message对象的content属性。修改代码中发送消息的部分,将message替换为message.content即可。

修改后的完整代码:

import asyncio
from discord.ext import commands

@bot.command()
async def test(ctx):
  await ctx.channel.send("say something")

  try:
    message = await bot.wait_for("message", check=lambda m: m.author == ctx.author and m.channel == ctx.channel, timeout=30.0)
  except asyncio.TimeoutError:
    await ctx.channel.send("You took too long to respond")
  else:
    await ctx.channel.send(message.content)  # 用content属性提取用户输入文本

多次获取输入的扩展

如果需要在单个命令里多次获取用户输入,重复调用bot.wait_for("message")逻辑即可,每次都通过message.content提取文本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 13:15:21