Discord.py中Cog发送Embed消息无响应问题求助
Discord.py中Cog发送Embed消息无响应问题求助
嘿,我一眼就瞅出问题所在啦!你代码里的关键疏漏是没导入discord模块本身——你只导入了discord.ext.commands,但discord.Embed这个类属于顶级的discord模块,没导入的话这行代码其实会抛出异常,但因为异步函数里的异常没被正确捕获(你说没看到报错,大概率是运行环境没显示异步栈的错误信息),导致代码卡在创建Embed这一步,后面的ctx.send根本没机会执行。
给你两个简单的修正方案:
方案1:导入完整的discord模块
在代码开头补上这行:
import discord from discord.ext import commands
方案2:直接导入Embed类(更简洁)
把开头的导入语句改成:
from discord import Embed from discord.ext import commands
同时把创建Embed的代码调整为:
self.embed = Embed(title="example title", description="example description", color=0x43b128)
另外给你提个小规范建议:按照Python的PEP8命名规范,类名应该用大驼峰写法,比如把helpCog改成HelpCog,这样代码的可读性会更好~
修正后的完整代码参考:
import discord from discord.ext import commands class HelpCog(commands.Cog): def __init__(self, bot): self.bot = bot @commands.command() async def help(self, ctx): self.embed = discord.Embed(title="example title", description="example description", color=0x43b128) await ctx.send(embed=self.embed) async def setup(bot): await bot.add_cog(HelpCog(bot))
你试试这个,应该就能正常发送Embed消息啦!
备注:内容来源于stack exchange,提问作者user18238076
相关产品推荐
相关产品推荐

