Discord.py Cog中Embed无法发送问题求助
问题排查:Discord.py Cog中Embed消息无法发送
我编写了一个Discord.py的Cog组件,但其中的Embed消息无法发送,仅能成功发送第一条文本消息,尝试调整后仍未解决。代码如下:
from discord.ext import commands class HelpCommand(commands.Cog): def __init__(self, client): self.client = client @commands.Cog.listener() async def on_ready(self): print("Test Ready") @commands.command(aliases=["h"]) async def command(self, ctx): await ctx.send(f'Please wait') embed = discord.Embed(title="Help for the bot", description="", color=discord.Colour.purple()) embed.set_footer(icon_url=ctx.author.avatar_url, text=f"Requested by {ctx.author.name}") await ctx.send(embed=embed) async def setup(client): await client.add_cog(HelpCommand(client))
问题原因及修复方案
- 缺少discord核心模块导入:代码中使用了
discord.Embed、discord.Colour等类,但仅导入了discord.ext.commands,未导入discord模块,会触发NameError直接中断后续代码执行。 - 缩进错误:
HelpCommand类内部的__init__、on_ready、command方法均未正确缩进,不符合Python语法规范,会导致Cog无法被正常加载,命令逻辑无法完整执行。 - 废弃属性使用(针对Discord.py v2.x版本):
ctx.author.avatar_url已被废弃,需替换为ctx.author.avatar.url,否则会引发属性错误。
修正后的完整代码
import discord from discord.ext import commands class HelpCommand(commands.Cog): def __init__(self, client): self.client = client @commands.Cog.listener() async def on_ready(self): print("Test Ready") @commands.command(aliases=["h"]) async def command(self, ctx): await ctx.send(f'Please wait') embed = discord.Embed(title="Help for the bot", description="", color=discord.Colour.purple()) embed.set_footer(icon_url=ctx.author.avatar.url, text=f"Requested by {ctx.author.name}") await ctx.send(embed=embed) async def setup(client): await client.add_cog(HelpCommand(client))
内容的提问来源于stack exchange,提问作者ger
相关产品推荐
相关产品推荐

