如何在斜杠命令中创建可选参数 (Pycord)
How do I make an optional parameter in a slash command (Pycord)
我的 Pycord 机器人中有一个斜杠命令。
这是代码:
@bot.slash_command(name='greet', description='Greet someone!')
async def greet(ctx, name):
await ctx.send('Hello ' + name + '!')
如何使“名称”成为可选参数?我尝试设置 name=None,但它不起作用。
有几种方法可以做到这一点。第一种方法是最简单和最懒惰的方法,它只是将参数设置为默认值:
@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 将显示一条丑陋的错误消息,指出没有响应。
您可以从 pycord 存储库中找到示例 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>)
您必须以 'respond' 而不是 'send' 的形式发送,否则 discord 将被视为“回复失败”
我的 Pycord 机器人中有一个斜杠命令。 这是代码:
@bot.slash_command(name='greet', description='Greet someone!')
async def greet(ctx, name):
await ctx.send('Hello ' + name + '!')
如何使“名称”成为可选参数?我尝试设置 name=None,但它不起作用。
有几种方法可以做到这一点。第一种方法是最简单和最懒惰的方法,它只是将参数设置为默认值:
@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 将显示一条丑陋的错误消息,指出没有响应。
您可以从 pycord 存储库中找到示例 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>)
您必须以 'respond' 而不是 'send' 的形式发送,否则 discord 将被视为“回复失败”