Discord Python Rewrite-ヘルプコマンドエラー(カスタム)

ラフィエル

だから、私はうまくいくヘルプを作りましたが、ユーザーが入力したカテゴリが無効である場合は何かを言いたいです。カテゴリが無効な場合、エラーなしで動作するコードを取得しました。コード:

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

動作しますが、無効なカテゴリを入力しようとすると、モデレーションが送信されます。

Mr_Spaar

あなたのエラーはあなたの2番目の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を返すため、無効なカテゴリを入力すると、2番目の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)
            

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

discord.py rewriteのインストール方法は?

分類Dev

on_guild_role_createイベントがdiscord.py-rewrite v1.3.4で機能しない

分類Dev

DiscordボットPythonのコマンドのクールダウン

分類Dev

Discord.py Rewrite Cogs:他のアーカイブから拡張機能をロードする

分類Dev

Discord-Rewriteのパージコマンドに問題がある

分類Dev

Python Discord Bot、サーバーIDが必要

分類Dev

カスタム絵文字とアニメーション絵文字が機能しない[discord.py-rewrite]

分類Dev

コマンドを使用して埋め込みを作成する簡単な方法はありますか?| discord.py REWRITE

分類Dev

discord.py-rewrite-close()メソッドとlogout()メソッドの違いは?

分類Dev

ランタイムエラーを修正する方法:実行中のイベントループを閉じることができません-Python Discord Bot

分類Dev

Python Discord.py `time.sleep()`コルーチン

分類Dev

Get server ID in discord.py rewrite

分類Dev

Is there a command that I can use to send messages to other people in discord rewrite?

分類Dev

How do I make a Discord bot using discord.py rewrite that can make bot users that are not real

分類Dev

特定のメッセージに自動的に応答するコマンドを作成しました。コマンドは機能しますが、他のすべてのコマンドが機能しなくなります(discord.py rewrite)

分類Dev

Python Discord2クライアント

分類Dev

Discord Python Rewrite-Cogs Error(client.load_extention(cog)AttributeError: 'Bot'オブジェクトには属性 'load_extention'がありません)

分類Dev

discord.py rewriteのクラスのメソッドを使用してコマンドを作成するにはどうすればよいですか?

分類Dev

discord.py rewriteを使用してpythondiscordボットのアバター(プロフィール写真)を変更する必要があります

分類Dev

Discord.py Rewrite Giphy Cogエラー:コネクタが閉じていません

分類Dev

Can we fetch the Latency (ping) of a Discord user using Discord.py rewrite?

分類Dev

Discord.js-コマンドにエイリアスがない場合、ヘルプコマンドは「未定義」を返します

分類Dev

discord.pyプレフィックス変更コマンドのインデントエラー

分類Dev

discord.pyを使用した複数ページのヘルプコマンド

分類Dev

PythonでのDiscordボットのプログラミング-ミュートコマンドを作成するにはどうすればよいですか?

分類Dev

mod rewriteヘルプ-WAMP

分類Dev

コマンドラインコマンドのPythonエスケープファイルパス

分類Dev

カスタムソートを使用したPythonでのマルチインデックスヘッダーデータフレームのスライス

分類Dev

コマンド ライン ショートカットがあるように Python プログラムを pip インストールする方法

Related 関連記事

  1. 1

    discord.py rewriteのインストール方法は?

  2. 2

    on_guild_role_createイベントがdiscord.py-rewrite v1.3.4で機能しない

  3. 3

    DiscordボットPythonのコマンドのクールダウン

  4. 4

    Discord.py Rewrite Cogs:他のアーカイブから拡張機能をロードする

  5. 5

    Discord-Rewriteのパージコマンドに問題がある

  6. 6

    Python Discord Bot、サーバーIDが必要

  7. 7

    カスタム絵文字とアニメーション絵文字が機能しない[discord.py-rewrite]

  8. 8

    コマンドを使用して埋め込みを作成する簡単な方法はありますか?| discord.py REWRITE

  9. 9

    discord.py-rewrite-close()メソッドとlogout()メソッドの違いは?

  10. 10

    ランタイムエラーを修正する方法:実行中のイベントループを閉じることができません-Python Discord Bot

  11. 11

    Python Discord.py `time.sleep()`コルーチン

  12. 12

    Get server ID in discord.py rewrite

  13. 13

    Is there a command that I can use to send messages to other people in discord rewrite?

  14. 14

    How do I make a Discord bot using discord.py rewrite that can make bot users that are not real

  15. 15

    特定のメッセージに自動的に応答するコマンドを作成しました。コマンドは機能しますが、他のすべてのコマンドが機能しなくなります(discord.py rewrite)

  16. 16

    Python Discord2クライアント

  17. 17

    Discord Python Rewrite-Cogs Error(client.load_extention(cog)AttributeError: 'Bot'オブジェクトには属性 'load_extention'がありません)

  18. 18

    discord.py rewriteのクラスのメソッドを使用してコマンドを作成するにはどうすればよいですか?

  19. 19

    discord.py rewriteを使用してpythondiscordボットのアバター(プロフィール写真)を変更する必要があります

  20. 20

    Discord.py Rewrite Giphy Cogエラー:コネクタが閉じていません

  21. 21

    Can we fetch the Latency (ping) of a Discord user using Discord.py rewrite?

  22. 22

    Discord.js-コマンドにエイリアスがない場合、ヘルプコマンドは「未定義」を返します

  23. 23

    discord.pyプレフィックス変更コマンドのインデントエラー

  24. 24

    discord.pyを使用した複数ページのヘルプコマンド

  25. 25

    PythonでのDiscordボットのプログラミング-ミュートコマンドを作成するにはどうすればよいですか?

  26. 26

    mod rewriteヘルプ-WAMP

  27. 27

    コマンドラインコマンドのPythonエスケープファイルパス

  28. 28

    カスタムソートを使用したPythonでのマルチインデックスヘッダーデータフレームのスライス

  29. 29

    コマンド ライン ショートカットがあるように Python プログラムを pip インストールする方法

ホットタグ

アーカイブ