wrapper สำหรับการโต้ตอบ Discord API ที่ไม่ได้พึ่งพา WebSockets และสามารถใช้ในสภาพแวดล้อม 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 ... ห้องสมุดนี้ได้รับการออกแบบมาโดยเฉพาะเพื่อทำงานอย่างราบรื่นกับ 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 ),
) ที่นี่เราใช้คลาส ApplicationCommand -Mommand, 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 คุณยังสามารถกลับมาไม่ได้ถ้าคุณไม่ต้องการส่งคำตอบ นอกจากนี้คุณยังสามารถส่งคืนบูลีนเป็นค่าที่สองซึ่งระบุว่าควรแสดงการโทรคำสั่งใน Discord หรือไม่ (เช่นส่วน _WITH_SOURCE ของประเภทการตอบกลับ) นอกจากนี้เรายังได้รับตัวเลือกผ่านวิธี get_option HELPER
ไลบรารีนี้มีเลเยอร์ที่เป็นนามธรรมอีกชั้นหนึ่ง ได้รับแรงบันดาลใจจากแนวคิดของฐานข้อมูล ORMS มันมี Mapper-Command Mapper (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 ซึ่งลงทะเบียนฟังก์ชั่นที่จะเรียกหลังจากฟังก์ชั่นคำสั่งจริงกลับมาแล้ว ฟังก์ชั่นจำเป็นต้องใช้พารามิเตอร์หนึ่งพารามิเตอร์ 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 ของคุณทำงานด้วยคำสั่ง Slash และวิธีการตั้งค่าทุกอย่างให้ดูที่โครงการตัวอย่างนี้ มันโฮสต์โปรแกรมในสภาพแวดล้อมที่ไม่มีเซิร์ฟเวอร์ผ่าน Google Cloud Run และยังมีบอทสาธิตดังนั้นคุณสามารถลองใช้คำสั่ง Slash ในเซิร์ฟเวอร์ Discord ของคุณ ตรวจสอบเพื่อเรียนรู้เพิ่มเติม!