-
Notifications
You must be signed in to change notification settings - Fork 0
2. Add Command
Necnion8 edited this page Dec 12, 2024
·
4 revisions
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, CommandContextasyncメソッドで@oncommandデコレーションを使うことで、そのメソッドをコマンドのハンドラとして宣言します
@oncommand(defaults=True)Note
defaults が True に設定されているので、デフォルト状態で誰でもこのコマンドを実行できます。
コマンドの説明と処理の部分です。
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プラグインを使うことを推奨します
- スラッシュコマンドには対応していません。拡張プラグインにて対応予定です。