[英]Slash command autocompletion in pycord
我想开始在 pycord 中使用 SlashCommand 自动完成功能。 由于我认为我在上一个问题中的请求是不可能的,所以我想也许我可以根据用户在其中一个斜杠命令条目中的选择来更改自动完成功能,并更早地向用户显示所需的条目。 我问这个是因为我没有找到任何学习这个的好资源而且我不懂文档
Pycord 文档中有一个例子,我想如果你试验这部分代码,你就会明白它是如何工作的
https://github.com/Pycord-Development/pycord/blob/master/examples/app_commands/slash_autocomplete.py
如果你想在 cogs 中使用它们,我的部分就是 cogs
import discord
from discord import option
from discord.ext import commands
mycolors = ["red", "white", "yellow"]
class ExampleAutocompletion(commands.Cog):
ctx_parse = discord.ApplicationContext
def __init__(self, bot: discord.Bot):
self.bot = bot.user
@staticmethod
def colorAutocomplete(self: discord.AutocompleteContext):
return mycolors
@staticmethod
def flowertypeAutocomplete(self: discord.AutocompleteContext):
chosen_color = self.options["color"]
match chosen_color:
case "red":
return ["Rose", "Georgina"]
case "white":
return ["Chrisantem", "Gortensia"]
case "yellow":
return ["Sunflower", "Narciss"]
case _:
return ["There is no flower with this color"]
@commands.slash_command(
guild_ids=["Your Guild ID for test"],
description="")
@option("color", description="What's your favourite color?",
autocomplete=colorAutocomplete)
@option("flowertype", description="and that's your flower shape!",
autocomplete=flowertypeAutocomplete)
async def beautyflowers(self, ctx: ctx_parse,
color: str, flowertype: str):
await ctx.respond(f"My flower is {flowertype} and its color is {color}!")
def setup(bot):
bot.add_cog(ExampleAutocompletion(bot))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.