Discord py - 子命令无法正常工作

时间:2021-03-29 07:27:22

标签: python python-3.x discord discord.py

我正在尝试在我的代码中添加一些子命令以使一切更清晰。

不幸的是,我遇到了某些代码段根本没有执行的问题。

我的代码:

@commands.group(invoke_without_command=True)
async def server(self, ctx):

    created = ctx.guild.created_at
    x = re.search("^.*:", str(created))
    x = x.group()
    x = x[:-6]

    me = ctx.guild

    embed = discord.Embed(title = f"Information about ``{ctx.guild}``", description=me.description, color = 0xf7fcfd, timestamp=ctx.message.created_at)

    embed.add_field(name="__Information__", value=f"**Owner:** {me.owner}\n**Name:** {me.name}\n**ID:** {me.id}\n**Region:** {me.region}\n**Created at:** {x}", inline=False)
    embed.add_field(name="__Server Information__", value=f"**Member**: {len(me.members)}\n**Roles:** {len(me.roles)}\n**Max Emojis:** {me.emoji_limit}\n**Emojis:** {len(me.emojis)}", inline=False)
    embed.add_field(name="__Channel Information__", value=f"**Text-Channel:** {str(len(me.text_channels))}\n**Voice-Channel:** {str(len(me.voice_channels))}\n**AFK-Channel:** ``{me.afk_channel}``\n**AFK-Timeout:** ``{me.afk_timeout}sec``")

    embed.set_footer(text=f"{ctx.message.author.name}", icon_url=ctx.message.author.avatar_url)
    embed.set_thumbnail(url=me.icon_url)
    embed.set_image(url=me.banner_url)

    await ctx.send(embed=embed)

# SERVER AVATAR

@server.command()
async def avatar(self, ctx):

    if not ctx.guild.icon:
        embed = discord.Embed(title="Server has no avatar!", color=0xf7fcfd)
        return await ctx.send(embed=embed)

    else:
        embed = discord.Embed(title=f"Avatar of {ctx.guild.name}", color=0xf7fcfd)
        embed.set_image(url=ctx.guild.icon_url_as(size=1024))
        await ctx.send(embed=embed)

# SERVER BANNER

@server.command()
async def banner(self, ctx):

    if not ctx.guild.banner:
        embed = discord.Embed(title="Server has no banner!", color=0xf7fcfd)
        return await ctx.send(embed=embed)

    else:
        embed = discord.Embed(title=f"Banner of {ctx.guild.name}", color=0xf7fcfd)
        embed.set_image(url=ctx.guild.banner_url_as(format='png'))
        await ctx.send(embed=embed)

问题是命令 ?server avatar 不提供任何输出。命令 ?server?server banner 完美运行。

我是否错误地使用了子命令或为什么没有输出?

1 个答案:

答案 0 :(得分:0)

它不起作用的原因是我已经有了另一个名为 avatar 的命令。 Bot 将始终采用命令 (avatar) 而不是子命令 (server avatar)。