[英]How to give an option a description slash commands discord.py
我使用python3 -m pip install -U git+https://github.com/Rapptz/discord.py
安裝了 discord.py 。
這是 main.py
guild_id = 964345157480747029
import discord
from discord import app_commands
class aclient(discord.Client):
def __init__(self):
super().__init__(intents = discord.Intents.default())
self.synced = False #we use this so the bot doesn't sync commands more than once
async def on_ready(self):
await self.wait_until_ready()
if not self.synced: #check if slash commands have been synced
await tree.sync(guild = discord.Object(id=guild_id)) #guild specific: leave blank if global (global registration can take 1-24 hours)
self.synced = True
print(f"We have logged in as {self.user}.")
client = aclient()
tree = app_commands.CommandTree(client)
@tree.command(guild = discord.Object(id=guild_id), name = 'test', description='A test slash command') #guild specific slash command
async def slash2(interaction: discord.Interaction, text: str)
await interaction.response.send_message(f'You said "{text}"!')
client.run('token')
斜杠命令有效,但我希望文本選項有一個描述為“重復文本”的描述。
請幫忙。 謝謝
使用斜杠命令時,我建議使用py-cord
而不是discord.py
。
一個例子是
import discord
from discord import option
bot = discord.Bot()
@bot.slash_command(name="test", guild_ids=[000000000000000000]) # replace with your guild id
@option(
"text",
str,
description="Enter some text"
)
async def test_command(ctx, text):
await ctx.respond(f"You wrote: {text}")
bot.run("TOKEN")
有兩種方法可以向應用命令 arguments 添加描述。
app_commands.describe(arg=desc)
裝飾器。 @app_commands.command(name="item")
@app_commands.describe(
choices="Do you want to buy or sell, in fact?!",
item_name="Which item, in fact?!",
)
async def get_item(
self,
interaction: discord.Interaction,
choices: app_commands.Choice[str],
item_name: str,
):
"""Get item information and buy/sell orders.
Args:
interaction (discord.Interaction): the interaction that invokes this coroutine
choices (app_commands.Choice[str]): buying or selling?
item_name (str): item name to search for
"""
pass
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.