簡體   English   中英

如何為選項提供描述斜杠命令 discord.py

[英]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 添加描述。

  1. 通過使用app_commands.describe(arg=desc)裝飾器。
  2. 通過為 function 創建一個文檔字符串,該文檔字符串從那里獲取參數描述和命令描述。 它們都如下所示。
 ​    ​@​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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM