【问题标题】:How do I make an optional parameter in a slash command (Pycord)如何在斜杠命令中创建可选参数(Pycord)
【发布时间】:2021-12-07 11:14:59
【问题描述】:

我的 Pycord 机器人中有一个斜杠命令。 代码如下:

@bot.slash_command(name='greet', description='Greet someone!')
async def greet(ctx, name):
    await ctx.send('Hello ' + name + '!')

如何使“名称”成为可选参数?我尝试设置 name=None,但它不起作用。

【问题讨论】:

  • 您可以简单地将name的默认值设置为空字符串async def greet(ctx, name=''):
  • 哇,这行得通,谢谢!

标签: python discord pycord


【解决方案1】:

有几种方法可以做到这一点。第一种方法是最简单和最懒惰的方法,只需将参数设置为默认值:

@bot.slash_command(name='greet', description='Greet someone!')
async def greet(ctx, name=''):
    await ctx.respond(f'Hello {name}!')

我知道的第二种方式来自Pycord repository中的示例代码:

from discord.commands import Option

@bot.slash_command(name='greet', description='Greet someone!')
async def greet(ctx, name: Option(str, "Enter your friend's name", required = False, default = '')):
    await ctx.respond(f'Hello {name}!')

编辑:

await ctx.send(f'Hello {name}!') 已更改为 await ctx.respond(f'Hello {name}!'),因为 discord 需要斜杠命令的响应,否则 discord 会显示一条丑陋的错误消息,指出没有响应。

【讨论】:

    【解决方案2】:

    你可以从 pycord repo examples/app_commands找到例子

    # example.py
    
    import discord
    import json
    from discord.ext import commands
    from discord.commands.context import ApplicationContext
    from discord.commands import Option
    from discord.file import File
    import os
    
    # example for getting image
    def get_image_paths():
        file_paths = []
    
        for fn in os.listdir('./img'):
            file_paths.append(os.path.join(os.getcwd(), 'img', fn))
    
        return file_paths
    
    async def images(ctx : ApplicationContext):
        imgs = get_image_paths()
    
        files = [File(path) for path in imgs]
    
        await ctx.respond(files=files)
    
    async def say_hello(ctx  : ApplicationContext):
        await ctx.respond(f"hello")
    
    # guild_ids are optional, but for fast registration I recommand it
    # name (optional) : if not provided, your command name will shown as function name; this case "my_command"
    @commands.slash_command(
        name = "my_command",
        description= "This is sample command",
        guild_ids = [ <your guild(server) id> ],
    )
    async def sample_command(
        ctx : ApplicationContext,
        opt: Option(str,
                        "reaction you want", 
                        choices=["Hi", "image"], 
                        required=True)
    ):
    
        if opt == "Hi":
            await say_hello(ctx)
    
        if opt == "image":
            await images(ctx)
    

    它看起来像:

    将此脚本导入为from example import library_command

    别忘了添加命令

    # at main.py
    ...
    
    bot.add_application_command(library_command)
    ...
    
    bot.run(<your token>)
    

    您必须发送为“响应”而不是“发送”,否则不和谐将被视为“未能回复”

    【讨论】:

      猜你喜欢
      • 2022-10-05
      • 1970-01-01
      • 2023-02-26
      • 2022-10-17
      • 2022-10-07
      • 2022-06-20
      • 2023-02-22
      • 2021-08-24
      • 1970-01-01
      相关资源
      最近更新 更多