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

Discord Bot程序返回message.content为空的问题求助

Discord Bot 无法获取消息内容问题解决

问题情况

你的Discord Bot接收消息时,message.content和message.clean_content都返回空值,控制台输出的消息对象里channel和guild字段也为空。你发送了/ask tell me about the striking offense of brandon moreno,但程序拿不到这条消息的内容。

问题原因

核心是两个配置缺失:

  • 代码里的Intents未开启message_content权限,这是读取消息内容的必备配置
  • Discord开发者后台未启用对应的Message Content Intent权限

解决步骤

  • 修改代码的Intents配置:在现有Intents设置中新增一行,开启消息内容读取权限
    intents.message_content = True  # 新增此行,开启消息内容读取权限
    
  • 在Discord开发者后台开启权限:
    1. 打开Discord开发者门户,找到你的Bot应用
    2. 切换到「Bot」标签页
    3. 在「Privileged Gateway Intents」区域,开启「Message Content Intent」
  • 确认Bot服务器权限:确保Bot在目标服务器拥有「读取消息历史」和「发送消息」权限,且所在频道未限制Bot读取消息的权限

修正后的完整代码

import os
import discord
from discord import Intents
from dotenv import load_dotenv
from chatbot import ChatBot

load_dotenv()
discord_token = os.getenv('DISCORD_TOKEN')

chatbot = ChatBot()

intents = Intents.default()
intents.typing = False
intents.presences = False
intents.messages = True
intents.message_content = True  # 开启消息内容读取权限

client = discord.Client(intents=intents)

chat_history = []

@client.event
async def on_ready():
    print(f'已登录为 {client.user}')

@client.event
async def on_message(message):
    print("收到一条消息")
    if message.author == client.user:
        return
    print('收到的消息对象:', message)
    print(f'消息内容: {message.clean_content}')
    if message.content.startswith('/ask'):
        user_input = message.content[5:].strip()
        print('用户输入: ', repr(user_input))
        response = chatbot.get_response(user_input, chat_history)
        await message.channel.send(response)

client.run(discord_token)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 18:32:55