WebSocketsに依存せず、したがって、Stateless WebHook環境で使用できるDiscord Interactions APIのラッパー。
さらに、コマンドの構造と、トリガー時に受信されるデータとの間の厳密な分離が可能になります。
Python 3.8+が必要です
pip install discord-interactions.pypip install git+https://github.com/LiBa001/discord-interactions.py注記
GitHubから直接インストールするには、コンピューターにGitをインストールする必要があります。
python -m pip install ...py -m pip install ... このライブラリは、Flask Microframeworkでシームレスに動作するように特別に設計されています。
フラスコ拡張機能を備えた最もAPIのような例は次のとおりです。
from discord_interactions . flask_ext import Interactions
from discord_interactions import (
ApplicationCommand ,
ApplicationCommandOption ,
ApplicationCommandOptionType ,
Interaction ,
InteractionResponse ,
InteractionResponseType ,
InteractionApplicationCommandCallbackData ,
)
from flask import Flask
import os
app = Flask ( __name__ )
interactions = Interactions ( app , os . getenv ( "CLIENT_PUBLIC_KEY" ))
echo_cmd = ApplicationCommand ( "echo" , "what goes around comes around" )
echo_cmd . add_option (
ApplicationCommandOption (
type = ApplicationCommandOptionType . STRING ,
name = "message" ,
description = "This will be echoed." ,
required = True ,
)
)
@ interactions . command ( echo_cmd )
def _echo ( interaction : Interaction ):
msg = interaction . data . options [ 0 ]. value # "message" option content
return InteractionResponse (
type = InteractionResponseType . CHANNEL_MESSAGE_WITH_SOURCE ,
data = InteractionApplicationCommandCallbackData ( content = msg ),
)ここでは、基本的に元のAPIモデルの正確なカウンターパートである、初歩的なApplicationCommand 、 Interaction 、およびInteractionResponseクラスを使用します。
もう少しシンプルにしましょう:
@ interactions . command ( echo_cmd )
def _echo ( interaction : Interaction ):
# different way of getting an option
msg = interaction . data . get_option ( "message" ). value
return msgこれで、 InteractionResponseを処理する必要はもうありませんが、代わりに応答コンテンツを文字列として返すだけです。応答タイプは、 InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCEにデフォルトで行われます。応答を送信したくない場合は、何も返さないこともあります。また、ブール値を2番目の値として返すだけで、コマンドコールをDiscordに表示するかどうかを示します(つまり、応答タイプの_WITH_SOURCE部分)。また、 get_optionヘルパーメソッドを介してオプションを取得します。
ただし、このライブラリは別の抽象化レイヤーを提供します。データベースORMの概念に触発されたこのコマンドのクラスを定義できるオブジェクトコマンドマッパー(OCM)があり、コマンドの一般的な構造記述( ApplicationCommandなど)と、コマンドが呼び出されたときに受信した実際のデータのコンテナ( Interactionなど)のコンテナがあります。
したがって、可能な限り単純な例は次のようになります:
from discord_interactions . flask_ext import Interactions
from discord_interactions . ocm import Command , Option
from flask import Flask
import os
app = Flask ( __name__ )
interactions = Interactions ( app , os . getenv ( "CLIENT_PUBLIC_KEY" ))
class _Echo ( Command ):
""" what goes around comes around """
message : str = Option ( "This will be echoed." , required = True )
@ interactions . command
def _echo ( cmd : _Echo ):
return cmd . message最初の応答の後にメッセージを送信する場合は、フォローアップメッセージを作成する必要があります。この目的のために、実際のコマンド関数が返された後に呼び出される関数を登録するafter_commandデコレーターを使用できます。この関数は、 Interactionや初期InteractionResponse要件など、いくつかのものが含まれるAfterCommandContext 1つのパラメーターを正確に取得する必要があります。
interactions = Interactions ( app , PUBLIC_KEY )
@ interactions . command ( "delay" )
def delay ( _ : Interaction ):
return "starting countdown" , True # this message is ephemeral
@ delay . after_command
def after_delay ( ctx : AfterCommandContext ):
delay_time = ctx . interaction . data . options [ 0 ]. value
time . sleep ( delay_time )
ctx . send ( f" { delay_time } seconds have passed" )ボタンなどのメッセージコンポーネントのコールバックを登録することもできます。コンポーネントは、 custom_idによって登録および識別されます。
@ interactions . component ( "my_button" )
def my_button_handler ( ctx : ComponentContext ):
return f" { ctx . interaction . user . username } clicked the button" さまざまな機能のその他の例については、例をご覧ください。
Slashコマンドを使用してDiscord Botを機能させる方法とすべてをセットアップする方法を知りたい場合は、この例プロジェクトをご覧ください。 Google Cloud Runを介してサーバーレス環境でプログラムをホストし、デモボットも提供するため、Discordサーバーでスラッシュコマンドを試すことができます。詳細を確認するためにチェックしてください!