実行フロー 1. コマンド処理 2. インタラクション処理 class MyView(discord.ui.View): @discord.ui.button( label="Click me!", style=discord.ButtonStyle.primary ) async def button_callback( self, interaction: discord.Interaction, button: discord.Button ): await interaction.response.send_message( "Button clicked!" @bot.tree.command( name="hello", description="Hello world!" ) async def hello(interaction: discord.Interaction): await interaction.response.send_message( "Hello world!", view=MyView() ) 実行結果 BOT: Hello world! Click me! BOT: Button clicked! ポイント • bot.tree.sync()でコマンド反映 • discord.ButtonStyleでボタンデザイン変更可能 • Viewは複数ボタンやSelectメニューも追加可能 discord.ui.Viewを継承したカスタムクラスを定義 ボタンやその他UIコンポーネントを含むViewを作成 1 @discord.ui.buttonデコレータでボタンを追加 ラベル、スタイル、コールバック関数を指定 2 @bot.tree.commandでスラッシュコマンドを定義 名前と説明を指定し、コマンドを登録 3 コマンド実行時にViewをインスタンス化して表示 4 ユーザーが/helloコマンドを入力 ↓ interaction.responseでメッセージ+ボタン表示 ↓ ユーザーがボタンをクリック ↓ button_callback関数が呼び出される ↓ interaction.responseで応答メッセージ ↓