不依賴Websocket的Discord Interactions API的包裝器,因此可以在無狀態的Webhook環境中使用。
此外,它允許在命令的結構和触發它時收到的數據之間進行嚴格的分離。
需要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 ... 該庫是專門設計的,可與燒瓶小框架無縫合作。
燒瓶擴展名最類似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 ),
)在這裡,我們使用基本ApplicationCommand , Interaction和InteractionResponse類別,它們在其結構中基本上是原始API模型的準確對應物。
讓我們更簡單:
@ 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 。如果您不想發送答复,也可以返回。您還可以簡單地將布爾值作為第二個值返回,以指示是否應在不和諧中顯示命令調用(即響應類型的_WITH_SOURCE部分)。另外,我們通過get_option助手方法獲得了選項。
但是,該庫提供了另一個抽象層。受數據庫ORMS概念的啟發,它具有一個對象命令映射器(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 Decorator,該命令返回後要調用的函數。該函數需要精確地採用一個參數,即AfterCommandContext ,其中包含幾件事,例如Interaction和初始InteractionResponse 。
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" 有關不同功能的更多示例,請查看示例。
如果您想知道如何使您的Discord Bot與Slash命令一起使用以及如何設置所有內容,請查看此示例項目。它通過Google Cloud運行在無服務器環境中託管該程序,還提供了一個演示機器人,因此您可以在Discord Server中嘗試使用Slash命令。檢查一下以了解更多!