1
0

More than 1 year has passed since last update.

pycord async on_command_error() で起きる主なエラー

Last updated at Posted at 2022-03-15

async on_command_error() とは

コマンド形式 (プレフィクス式コマンド) で起きたエラーをキャッチする関数です。
引数は context, error の2つです。

@bot.event
async def on_command_error(ctx: commands.Context, error):
    :
    :

基本的な対処法

あくまで自分の考えですのでこの通りにしろ、というわけではありません。

  1. この関数内で isinstance(error, エラー名)で指定して拾う
  2. @コマンド名.error で拾う。

ただし、いちいち 2 を書くのがめんどくさい、2 では拾いきれないエラーが生じることもある。という点で この関数を使用することをお勧めします。

主なエラーと説明

用語:
ユーザー ... 実行者 (人間側)
クライアント ... Bot
エラー ... クライアントがプログラムを実行した際、実行に失敗した時に起こる

commands.CommandNotFound

その名の通りコマンドが存在しません。ユーザーの打ち間違い等で起こります。
NotFound は他に以下のものがあります。

拾い方
if isinstance(error, commands.CommandNotFound):

commands.MissingPermissions

@bot.has_permission(manage_messages=True)
@bot.command()
async def edit_msg(ctx: commands.Context, member: int, *, args: str):
    pass

この場合、メッセージ編集権限がないとこのエラーを吐きます。
また、不足している権限が missing_permissions でリストで返されます。

commands.BotMissingPermissions もありますが、これはBot側の権限不足で起こります。再招待してもらったりすると直る可能性があります。

discord.InvalidArgument

コマンドに対する引数が足りない時に起こります。

拾い方
if isinstance(error, discord.InvalidArgument):

discord.HTTPException

あまり起きませんが、クライアントのネット状況でメッセージが送れなかったりした場合に起こります。

拾い方
if isinstance(error, discord.HTTPException):

discord.ExtensionError

commands.Cog を使って作成しているときに発生します。

最後に

エラーはよく起きますが、焦らず読みましょう。英語で書かれていますがエラー名である程度分かることが多いです。

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0