Skip to content

2. Add Command

Necnion8 edited this page Dec 12, 2024 · 4 revisions

Add Command

from logging import getLogger
from dncore.plugin import Plugin
from dncore.command import oncommand, CommandContext

log = getLogger(__name__)


class ExampleTestPlugin(Plugin):
    @oncommand(defaults=True)
    async def cmd_hello(self, ctx: CommandContext):
        """
        {command}
        > これはコマンドの説明文です。help コマンドに表示されます。
        """
        await ctx.send_info(f"こんにちは! `{ctx.prefix}{ctx.execute_name}` が実行されたよ!")

各行の説明

コマンドの実装に必要なメソッドとクラスをインポートします

from dncore.command import oncommand, CommandContext

asyncメソッドで@oncommandデコレーションを使うことで、そのメソッドをコマンドのハンドラとして宣言します

@oncommand(defaults=True)

Note

defaultsTrue に設定されているので、デフォルト状態で誰でもこのコマンドを実行できます。

Note

oncommandメソッドには、コマンド登録に関する設定ができます。

  • コマンド名 name=
  • 別名リスト aliases=
  • 初期状態で実行できる人 defaults= など

コマンドの説明と処理の部分です。

async def cmd_hello(self, ctx: CommandContext):
     """
     {command}
     > これはコマンドの説明文です。help コマンドに表示されます。
     """
     await ctx.send_info(f"こんにちは! `{ctx.prefix}{ctx.execute_name}` が実行されたよ!")

Note

CommandContextクラスには、コマンド実行に関する情報が含まれています。

  • コマンド接頭辞 .prefix
  • コマンドの引数 .args
  • 実行者 .author
  • コマンドメッセージの削除 .clean_message など

その他

  • コマンドの設定は config/commands.yml に保存されます。
    • 設定ファイルを直接編集することもできますが、通常はCommandConfiguratorプラグインを使うことを推奨します
  • スラッシュコマンドには対応していません。拡張プラグインにて対応予定です。

Clone this wiki locally