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

Python开发的Discord Bot无消息响应、编辑时发送空消息问题

Discord Bot消息无响应&编辑消息发空消息的解决方法

问题根源

  1. 缺少消息内容读取权限:你使用discord.Intents.default()初始化客户端,但默认意图不包含message_content权限,导致Bot无法读取消息文本内容——这直接让on_message里的所有条件判断都无法触发,同时编辑消息时before.content和after.content为空,发送的通知看起来像空消息。
  2. 未过滤Bot自身消息:如果不排除Bot自己发送的消息,可能会触发不必要的循环或干扰逻辑执行。

修复方案

1. 更新代码中的Intents配置

修改客户端初始化代码,明确开启所需的意图:

import discord

# 配置需要的意图
intents = discord.Intents.default()
intents.message_content = True  # 开启消息内容读取权限
intents.messages = True  # 确保消息事件正常触发

client = discord.Client(intents=intents)

2. 在Discord开发者门户开启特权网关意图

登录Discord开发者门户,找到你的Bot应用:

  • 进入Bot选项卡
  • 下滑到Privileged Gateway Intents区域,开启Message Content Intent

3. 优化on_message事件逻辑

添加判断,忽略Bot自身发送的消息,避免无效处理:

@client.event                                                      #system reakcji na wiadomości
async def on_message(wiadomosc):
    # 跳过Bot自己的消息
    if wiadomosc.author.bot:
        return
    
    if wiadomosc.content == "Siema Pyton":
        await wiadomosc.channel.send('Elo, co tam?')
    elif wiadomosc.content == 'co tam u ciebie pyton?':
        await wiadomosc.channel.send('a wszystko git :D')
    if wiadomosc.content == 'fajnie':
        await wiadomosc.add_reaction('\U0001F60E')
    if wiadomosc.content == 'dzień dobry':
        await wiadomosc.channel.send('A witam witam :)')
    if wiadomosc.content == '60':
        await wiadomosc.channel.send('Spadaj')
    if wiadomosc.content == 'konfident':
        await wiadomosc.channel.send('Spadaj na drzewo!')

完成以上步骤后,Bot就能正常响应消息,编辑消息的通知也会显示完整的前后内容,不会再出现空消息的情况。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 03:45:55