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

Discord机器人无法响应命令,初级开发者求助

Discord机器人命令无法识别的解决方案

核心错误修复:Intent设置错误

你的代码中对message_content意图的设置有误——你修改的是Intents类的属性,而非创建的intents实例。这会导致机器人无法接收消息内容,自然识别不了命令。

修改代码中的这一行:

Intents.message_content = True #v2

改为:

intents.message_content = True #v2

必须的平台配置:开启Message Content权限

即使代码中修复了Intent设置,你还需要在Discord开发者门户中开启对应权限:

  • 登录Discord开发者门户,进入你的机器人应用页面
  • 切换到Bot标签页
  • 在Privileged Gateway Intents区域,找到Message Content Intent并开启
  • 保存设置后重启机器人

额外检查项

  • 确认发送命令时严格使用前缀!,例如:!generate cat wearing a hat,不要遗漏前缀或打错符号
  • 如果添加了自定义on_message事件,必须调用await bot.process_commands(message),否则命令系统会被事件拦截。示例测试代码:
@bot.event
async def on_message(message):
    print(f"Received message: {message.content}")
    await bot.process_commands(message)

修复后的完整代码

import requests
import io
import base64
from PIL import Image
from discord.ext import commands
from discord import Intents, File

intents = Intents.default()
intents.message_content = True # 修复:改为实例属性赋值
bot = commands.Bot(command_prefix='!', intents=intents)

@bot.command(name='generate')
async def generate(ctx, prompt: str):
    print(f"Received command to generate image with prompt: {prompt}")
    url = "http://127.0.0.1:7860"
    payload = {
        "prompt": prompt,
        "steps": 5
    }

    response = requests.post(url=f'{url}/sdapi/v1/txt2img', json=payload)
    r = response.json()

    for i in r['images']:
        image = Image.open(io.BytesIO(base64.b64decode(i.split(",", 1)[0])))

        png_payload = {
            "image": "data:image/png;base64," + i
        }
        response2 = requests.post(url=f'{url}/sdapi/v1/png-info', json=png_payload)

        image_path = 'output.png'
        image.save(image_path)
        print(f"Image saved as {image_path}")

        with open(image_path, 'rb') as img_file:
            await ctx.send(file=File(img_file, 'output.png'))

# Replace this with your actual bot token
bot_token = 'TOKEN'
bot.run(bot_token)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 01:38:35