#Load slashcommand choices data from a yaml file

1 messages · Page 1 of 1 (latest)

peak root
#

Hello, I have a Yaml file from which I am loading a list. I would like to use this list as choices for my SlashCommand.

SlashCommand: py @slash_command(name="test-yaml") async def testyaml(self, ctx, choice: Option(choices=YAMLDATAHERE): print(choice)

GetDataFromYaml: py with open('data.yaml', 'r') as file: data = yaml.safe_load(file) airports = data.get('Flughäfen', [])

keen rampart
#

Choices are static, you need a bot restart for them to apply
But yes, you can do that

light imp
# peak root Hello, I have a Yaml file from which I am loading a list. I would like to use th...

you can define a function that loads the airports somewhere in your code:

def get_airports():
    with open('data.yaml', 'r') as file:
        data = yaml.safe_load(file)
        airports = data.get('Flughäfen', [])

Then your command

class MyCog(commands.Cog):
    def __init__(self, bot: discord.Bot):
        self.bot = bot
    @discord.slash_command(name="test-yaml")
    async def testyaml(self, ctx, choice: discord.Option(str, choices=get_airports()):
        print(choice)

...

bot.add_cog(MyCog(bot))
left mirage
#

or if you're looking to define say, whole commands like that in a yaml file, you could create a custom decorator to do that for you

#

little more advanced though

fleet salmon
peak root
split finch
#

Or you can also create a function that will before the bot is run add every name, description, choices

#

maybe more complicated but with that you will not have to care about anything else like adding the check each time etc since it will do everything automatically

peak root
keen rampart
#

not sure, i dont think so necessarily

#

try it out

split finch
light imp
#

Wait with autocomplete

#

you could do that

#

why didn't we think of it

finite cosmosBOT
keen rampart
#

Because autocomplete generally isnt great if you wanna ensure the user picks a valid thing

light imp
#

In the end you can still say what input choices you want

#

Maybe I'm wrong but I don't get it

keen rampart
#

yea but choices make sure the input is valid already

#

autocomplete doesnt

light imp
#

I believe

keen rampart
#

yes, you can

#

thats the entire problem with autocomplete

#

it sucks because its floppy and doesnt validate

#

You have to use it if your choices are dynamic, but it still sucks

light imp
#

@peak root If you are ok with allowing the user to choose invalid values you can use autocomplete, and check in the command the chosen airport.

#

It would look basically the same, except not checking the validity of the answer