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

Discord.py中@bot.command装饰器失效问题求助

解决Discord机器人部署到Heroku后命令失效问题

问题描述

Discord机器人连接正常,on_ready事件可正常触发,但使用@bot.command装饰器定义的命令(如!test)无法被机器人识别。本地运行时功能正常,部署到Heroku后机器人在线但命令失效。

机器人代码

import discord
import pickle

from discord.ext import commands
from discord.ext.commands import bot

token = ""
bot = commands.Bot(command_prefix='!', case_insensitive=True, intents=discord.Intents.default())


@bot.event
async def on_ready():
    print('We have logged in as {0.user}'.format(bot))


@bot.command()
async def test(ctx):
    print('received')
    await ctx.send('msg received')


bot.run(token)

Requirements文件内容

git+https://github.com/Rapptz/discord.py
discord~=1.7.3
setuptools~=60.2.0 

解决方案

  • 修复依赖冲突:requirements中同时指定了discord.py开发版和稳定版,两者会产生依赖冲突,导致Heroku安装的版本异常。保留稳定版即可,修改后的requirements:

    discord~=1.7.3
    setuptools~=60.2.0
    
  • 调整Intents权限:discord.py 1.7.x版本中,默认Intents可能未开启消息读取相关权限,导致机器人无法捕捉命令消息。显式开启必要权限:

    intents = discord.Intents.default()
    intents.messages = True
    intents.message_content = True
    bot = commands.Bot(command_prefix='!', case_insensitive=True, intents=intents)
    

    同时前往Discord开发者后台的Bot页面,开启对应的Privileged Gateway Intents(若有需要)。

  • 查看Heroku日志:通过heroku logs --tail命令查看部署日志,确认是否存在依赖安装失败、权限报错或命令加载异常的信息,定位具体问题。

  • 移除冗余导入:代码中from discord.ext.commands import bot属于冗余导入,可直接删除,避免变量混淆。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 14:54:19