dislash.py
v1.4.8
该图书馆不再维持支持分开。 Desnake是Discord.py的更新版本,并具有最新的API功能。 Slash命令的语法非常方便,因此,如果您打算在2022年4月之前切换到Slash命令,我们真的建议您使用分开。
如果您有任何疑问,请加入我们的Discord服务器
一个discord.py的扩展库,允许构建出色的消息组件和斜线命令。
由于Discord.py将不再保持最新状态,因此我们决定创建一个叉子:分开。它具有DPY 2.0 +应用程序命令的所有功能。
在终端中运行这些命令中的任何一个:
pip install dislash.py
python -m pip install dislash.py
该库需要Discord.py 。
from discord . ext import commands
from dislash import InteractionClient
bot = commands . Bot ( command_prefix = "!" )
inter_client = InteractionClient ( bot , test_guilds = [ 12345 , 98765 ])
# If 'test_guilds' param isn't specified, the commands are registered globally.
# Global registration takes up to 1 hour.
@ inter_client . slash_command (
name = "hello" , # Defaults to the function name
description = "Says hello" ,
guild_ids = test_guilds
)
async def hello ( inter ):
await inter . reply ( "Hello!" )
bot . run ( "BOT_TOKEN" )此示例显示了如何使用按钮发送消息。
from discord . ext import commands
from dislash import InteractionClient , ActionRow , Button , ButtonStyle
bot = commands . Bot ( command_prefix = "!" )
inter_client = InteractionClient ( bot )
@ bot . command ()
async def test ( ctx ):
# Make a row of buttons
row_of_buttons = ActionRow (
Button (
style = ButtonStyle . green ,
label = "Green button" ,
custom_id = "green"
),
Button (
style = ButtonStyle . red ,
label = "Red button" ,
custom_id = "red"
)
)
# Send a message with buttons
msg = await ctx . send (
"This message has buttons!" ,
components = [ row_of_buttons ]
)
# Wait for someone to click on them
def check ( inter ):
return inter . message . id == msg . id
inter = await ctx . wait_for_button_click ( check )
# Send what you received
button_text = inter . clicked_button . label
await inter . reply ( f"Button: { button_text } " )
bot . run ( "BOT_TOKEN" )此示例显示了如何使用菜单发送消息。
from discord . ext import commands
from dislash import InteractionClient , SelectMenu , SelectOption
bot = commands . Bot ( command_prefix = "!" )
inter_client = InteractionClient ( bot )
@ bot . command ()
async def test ( ctx ):
msg = await ctx . send (
"This message has a select menu!" ,
components = [
SelectMenu (
custom_id = "test" ,
placeholder = "Choose up to 2 options" ,
max_values = 2 ,
options = [
SelectOption ( "Option 1" , "value 1" ),
SelectOption ( "Option 2" , "value 2" ),
SelectOption ( "Option 3" , "value 3" )
]
)
]
)
# Wait for someone to click on it
inter = await msg . wait_for_dropdown ()
# Send what you received
labels = [ option . label for option in inter . select_menu . selected_options ]
await inter . reply ( f"Options: { ', ' . join ( labels ) } " )
bot . run ( "BOT_TOKEN" )此示例显示了如何创建上下文菜单并与它们互动。
from discord . ext import commands
from dislash import InteractionClient
bot = commands . Bot ( command_prefix = "!" )
inter_client = InteractionClient ( bot )
@ inter_client . user_command ( name = "Press me" )
async def press_me ( inter ):
# User commands are visible in user context menus
# They can be global or per guild, just like slash commands
await inter . respond ( "Hello there!" )
@ inter_client . message_command ( name = "Resend" )
async def resend ( inter ):
# Message commands are visible in message context menus
# inter is instance of ContextMenuInteraction
await inter . respond ( inter . message . content )
bot . run ( "BOT_TOKEN" )