简体   繁体   English

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

[英]Discord py - Subcommand is not working properly

I'm trying to add a few subcommands to my code to make everything clearer.我正在尝试在我的代码中添加一些子命令,以使一切更清晰。

Unfortunately, I have the problem that certain sections of code are simply not executed.不幸的是,我遇到的问题是某些代码部分根本没有执行。

My Code:我的代码:

@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)

The problem is that the command ?server avatar does not provide any output.问题是命令?server avatar没有提供任何 output。 The commands ?server and ?server banner work perfectly.命令?server?server 横幅可以完美运行。

Am I using the subcommands incorrectly or why do I get no output?我是否错误地使用了子命令,或者为什么我没有得到 output?

The reason why it didn't work is that I already had another command called avatar .它不起作用的原因是我已经有另一个名为avatar的命令。 The Bot would always take the command (avatar) instead the subcommand (server avatar). Bot 将始终采用命令(化身)而不是子命令(服务器化身)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM