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

Discord猜拳Bot仅DM可用,服务器频道无法响应命令

解决Discord石头剪刀布Bot服务器频道无响应问题

问题背景

开发了一款带炸弹扩展元素的石头剪刀布Discord Bot,Bot在私信(DM)中能正常响应+test和+testb命令,但在服务器频道中无法读取消息并返回结果。已知条件:

  • DM内功能完全正常
  • Bot处于在线状态
  • Bot已获取服务器全部必要权限

问题原因

Discord.py v2.0及以上版本中,消息内容意图(Message Content Intent) 默认未启用,服务器频道的消息读取需要明确开启该意图。私信场景下存在权限豁免,但服务器频道必须通过配置该意图才能读取消息内容。

修复方案

1. 开启Discord开发者门户的消息内容意图

登录Discord开发者门户,进入你的Bot应用页面:

  • 切换到「Bot」标签页
  • 在「Privileged Gateway Intents」区域,开启「Message Content Intent」选项

2. 修改代码中的Intents配置

在代码中更新Intents初始化部分,添加message_content = True:

intents = discord.Intents.default()
intents.typing = False
intents.presences = False
intents.message_content = True  # 新增这一行

完整修复后的代码

import discord
import random

TOKEN = 'SEU_TOKEN_AQUI'
PREFIX = '+'

intents = discord.Intents.default()
intents.typing = False
intents.presences = False
intents.message_content = True  # 启用消息内容意图

client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f'Bot {client.user} está online!')

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith(PREFIX + 'test'):
        await jogar_pedra_papel_tesoura(message)

    if message.content.startswith(PREFIX + 'testb'):
        await jogar_pedra_papel_tesoura_com_bomba(message)

async def jogar_pedra_papel_tesoura(message):
    escolhas = ['Pedra', 'Papel', 'Tesoura']
    escolha_bot = random.choice(escolhas)
    escolha_usuario = message.content[len(PREFIX + 'test'):].strip().capitalize()

    if escolha_usuario not in escolhas:
        await message.channel.send("Escolha inválida. Use 'Pedra', 'Papel' ou 'Tesoura'.")
        return

    if escolha_bot == escolha_usuario:
        resultado = 'Empate!'
    elif (escolha_bot == 'Pedra' and escolha_usuario == 'Tesoura') or (escolha_bot == 'Tesoura' and escolha_usuario == 'Papel') or (escolha_bot == 'Papel' and escolha_usuario == 'Pedra'):
        resultado = f'O bot escolheu {escolha_bot}. Você perdeu!'
    else:
        resultado = f'O bot escolheu {escolha_bot}. Você venceu!'

    await message.channel.send(resultado)

async def jogar_pedra_papel_tesoura_com_bomba(message):
    escolhas = ['Pedra', 'Papel', 'Tesoura', 'Bomba']
    escolha_bot = random.choice(escolhas)
    escolha_usuario = message.content[len(PREFIX + 'testb'):].strip().capitalize()

    if escolha_usuario not in escolhas:
        await message.channel.send("Escolha inválida. Use 'Pedra', 'Papel', 'Tesoura' ou 'Bomba'.")
        return

    if escolha_bot == escolha_usuario:
        resultado = 'Empate!'
    elif (escolha_bot == 'Pedra' and escolha_usuario == 'Tesoura') or (escolha_bot == 'Tesoura' and escolha_usuario == 'Papel') or (escolha_bot == 'Papel' and escolha_usuario == 'Pedra'):
        resultado = f'O bot escolheu {escolha_bot}. Você perdeu!'
    elif escolha_bot == 'Bomba' and escolha_usuario != 'Papel':
        resultado = f'O bot escolheu {escolha_bot}. Você venceu!'
    else:
        resultado = f'O bot escolheu {escolha_bot}. Você perdeu!'

    await message.channel.send(resultado)

client.run(TOKEN)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 07:07:06