【问题标题】:Discord Python Rewrite - help command error (custom)Discord Python Rewrite - 帮助命令错误(自定义)
【发布时间】:2020-09-20 04:31:00
【问题描述】:

所以,我做了一个有效的帮助,但如果用户输入的类别无效,我希望它说些什么。如果类别无效,我得到一个没有错误的工作代码。代码:

@client.command()
async def help(ctx, *, category = None):

    if category is not None:
        if category == 'mod' or 'moderation' or 'Mod' or 'Moderation':
            modhelpembed = discord.Embed(
                title="Moderation Help",
                timestamp=datetime.datetime.now(),
                colour=discord.Color.green()
                )

            modhelpembed.add_field(name='kick', value="Kicks a member from the server", inline=False)
            modhelpembed.add_field(name='ban', value='bans a member from the server', inline=False)
            modhelpembed.add_field(name='unban', value='unbans a member from the server', inline=False)
            modhelpembed.add_field(name="nuke", value="Nukes a channel :>", inline=False)
            modhelpembed.add_field(name='mute', value="Mute a member", inline=False)
            modhelpembed.add_field(name="purge", value='purges (deletes) a certain number of messages', inline=False)

            await ctx.send(f'{ctx.author.mention}')
            await ctx.send(embed=modhelpembed)

        elif category == 'fun' or 'Fun':
            funembed = discord.Embed(
                title="Fun Help",
                timestamp=datetime.datetime.now(),
                colour=discord.Color.green()
                )
            
            funembed.add_field(name='meme', value='shows a meme from r/memes', inline=False)
            funembed.add_field(name='waifu', value='shows a waifu (pic or link) from r/waifu', inline=False)
            funembed.add_field(name='anime', value='shows a anime (image or link) from r/anime', inline=False)
            funembed.add_field(name='spotify', value='Tells you the targeted user listening on', inline=False)
            funembed.add_field(name="song", value="Tells you the whats the targeted user listening in Spotify", inline=False)
            funembed.add_field(name="album", value="Tells you whats the targeted user album", inline=False)
            funembed.add_field(name="timer", value="Sets a Timer for you.", inline=False)

            await ctx.send(f'{ctx.author.mention}')
            await ctx.send(embed=funembed)

    else:
        nonembed = discord.Embed(
            title="Help list",
            timestamp=datetime.datetime.now(),
            colour=discord.Color.green(),
            description='Category:\nmod\nfun'
            )
        
        await ctx.send(f'{ctx.author.mention}')
        await ctx.send(embed=nonembed)

它可以工作,但是当我尝试输入无效类别时,它会发送审核。

【问题讨论】:

    标签: discord discord.py discord.py-rewrite


    【解决方案1】:

    您的错误来自您的第二个if 声明。您只需将其替换为:

    • if category == ('mod' or 'moderation' or 'Mod' or 'Moderation'):
      
    • if category in ['mod', 'moderation', 'Mod', 'Moderation']:
      

    这就是当您输入无效类别时触发语句的原因:

    • 一个空字符串返回False(例如""),一个字符串返回True(例如"TEST")。

    • 如果你不放括号,它会将每个or 分隔为一个条件(if category == 'mod' / if 'mod' / if 'moderation' / if 'Mod' / if 'Moderation')。

    • 由于非空字符串返回 True,当您输入无效类别时,您的第二个 if 语句将被触发,并为您提供审核帮助消息。

    你也可以使用commands.Command属性进行一些重构:

    @client.command(description='Say hi to the bot')
    async def hello(ctx):
        await ctx.send(f'Hi {ctx.author.mention}')
    
    @client.command(description='Test command')
    async def test(ctx):
        await ctx.send('TEST')
    
    @client.command()
    async def help(ctx, *, category = None):
        categories = {
            'fun': ['hello',],
            'testing': ['test',],
        }
        if category is None:
           desc = '\n'.join(categories.keys())
           embed = discord.Embed(
                title="Help list",
                timestamp=datetime.datetime.now(),
                colour=discord.Color.green(),
                description=f'Categories:\n{desc}'
            )
        else:
            category = category.lower()
            if not category in categories.keys():
                await ctx.send('Category name is invalid!')
                return
            
            embed = discord.Embed(
                title=f"{category.capitalize()} Help",
                timestamp=datetime.datetime.now(),
                colour=discord.Color.green()
            )
    
            for cmd in categories[category]:
                cmd = client.get_command(cmd)
                embed.add_field(name=cmd.name, value=cmd.description)
    
            await ctx.send(ctx.author.mention, embed=embed)
                
    

    【讨论】:

    • description=f"Categories:\n{'\n'.join(categories.keys())}" ^ SyntaxError: f-string expression part cannot include a backslash
    • 抱歉回复晚了。 (我在睡觉)但是代码有效!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-08-10
    • 2018-05-06
    • 2021-01-01
    • 2020-03-13
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多