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

自定义Discord Bot命令失效,请求问题排查与解决

问题诊断与修复

你的Bot定时任务能正常运行,但指令?qod无响应,核心原因是消息内容意图(Message Content Intent)配置错误,具体修复方案如下:

1. 代码层面的意图配置修正

Discord.py v2.0+版本后,messages意图已被弃用,要读取用户发送的命令内容,必须显式开启message_content意图。原代码中的旧配置无法让Bot获取到消息文本,自然无法触发命令。

修改后的意图配置代码:

intents = discord.Intents.default()
intents.message_content = True

default()会包含基础的必要意图,再单独开启消息内容权限即可。

2. Discord开发者后台权限开启

仅修改代码还不够,需在Discord开发者门户中同步开启对应权限:

  • 进入你的Bot应用后台
  • 切换到「Bot」页面
  • 在「Privileged Gateway Intents」区域,开启「Message Content Intent」开关

修复后的完整代码

import discord
from discord.ext import commands, tasks
import requests

# 修正意图配置
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="?", intents=intents)

@bot.event
async def on_ready():
  print("Bot is ready.")
  quote.start()

@tasks.loop(hours=24)
async def quote():
  response = requests.get("https://zenquotes.io/api/random")
  json_data = response.json()
  quote_text = json_data[0]["q"] + " -" + json_data[0]["a"]  # 变量名与任务函数区分开,避免混淆
  channel = bot.get_channel(1234567890)  # 替换为你的实际频道ID
  await channel.send(quote_text)

@quote.before_loop
async def before_quote():
  await bot.wait_until_ready()
  print("Finished waiting")

@bot.command()
async def qod(ctx):
  response = requests.get("https://zenquotes.io/api/random")
  json_data = response.json()
  quote_text = json_data[0]["q"] + " -" + json_data[0]["a"]
  await ctx.send(quote_text)

bot.run("your-bot-token-here")  # 替换为你的Bot令牌

额外检查点

  • 确认发送命令时使用了正确前缀?,即发送?qod而非qod
  • 检查Bot在服务器中是否拥有发送消息的权限
  • repl.it会话中断会导致Bot离线,可借助后台运行工具保持Bot在线

内容的提问来源于stack exchange,提问作者code space sohan co

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 04:27:35