简体   繁体   English

如何为选项提供描述斜杠命令 discord.py

[英]How to give an option a description slash commands discord.py

I installed discord.py using python3 -m pip install -U git+https://github.com/Rapptz/discord.py .我使用python3 -m pip install -U git+https://github.com/Rapptz/discord.py安装了 discord.py 。

pic of what I want我想要的图片

This is main.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')

The slash command works, but I want the text option to have a description that says "Text to repeat".斜杠命令有效,但我希望文本选项有一个描述为“重复文本”的描述。

I used this tutorial and got most of my code from here .我使用了本教程并从这里获得了我的大部分代码。

Please help.请帮忙。 Thank you谢谢

When using slash commands I'd recommend py-cord instead of discord.py .使用斜杠命令时,我建议使用py-cord而不是discord.py

An example would be一个例子是

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")

There are two ways of adding a description to app command arguments.有两种方法可以向应用命令 arguments 添加描述。

  1. by using the app_commands.describe(arg=desc) decorator.通过使用app_commands.describe(arg=desc)装饰器。
  2. by creating a docstring for the function which takes the argument descriptions as well as the command description from there.通过为 function 创建一个文档字符串,该文档字符串从那里获取参数描述和命令描述。 Both of them are shown below.它们都如下所示。
 ​    ​@​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