#Please how can i add discord.Option choices as file names and if idk how many files is in the folder

1 messages · Page 1 of 1 (latest)

dawn notch
#

something like that (i know this code is wrong) :

def addchoice(filename, ctx, choices) :
    folder = pathlib.Path(f"users_data/{ctx.guild.id}")
    for filename in folder.iterdir() :
        if filename.is_file() :
            choices.append("name")
            
@bot.slash_command(name = "makeinvoice", description = "CMD used to make a new invoice for a customer")
async def setfee(ctx,   customer : discord.Option(discord.Member, "Select here the customer", required = True),
                        cost : discord.Option(int or float, 'Write Here The transaction cost (without including extra fees !)', required = True),
                        paymentmethod : discord.Option(str, 'Select Here The Payment Method used by customer', choices = addchoice(), required = True)) :
    
    
    await ctx.respond("noice")
calm bay
dawn notch
calm bay
#

regular choices are hard set and can't change without manually being updated

#

autocomplete is a system that lets you dynamically load presets for users to choose, but they aren't forced to pick so you should validate inside the command

spiral apexBOT
#

Here's the slash autocomplete example.

dawn notch
#

oh

dawn notch
#

@calm bay like that ? 😄

calm bay
#

Just give it a go

dawn notch
calm bay
dawn notch
calm bay
#

mhm

dawn notch
#

??

#

@calm bay could you correct it if it's wrong pls ?

calm bay
#

just... give it a go

#

if you run into errors it's fine to post them here, but it's better for you to trial and error yourself and see what solutions to come up with

dawn notch
#

okok

#

@calm bay hm ?

async def Choices(ctx : discord.AutocompleteContext):
    folder = pathlib.Path(f"users_data/{ctx.interaction.guild.id}")
    filename =  folder.iterdir()
    return [file for file in filename if file.is_file()]
calm bay
#

like str(file) for file in filename...

dawn notch
#

okok

#

it's working tysm

calm bay
#

allgood

dawn notch
#

@calm bay sorry for disturbing you again

#

i just want to know how to get the files names without path or extension

calm bay
#

Like split along slash and select last item with -1

#

It's probably built into pathlib if you read the doc but I can't right now

dawn notch
#

ah

dawn notch
#

@calm bay something like that ?py async def Choices(ctx : discord.AutocompleteContext): folder = pathlib.Path(f"users_data/{ctx.interaction.guild.id}") filedir = folder.iterdir() [str(file) = for file in filedir if file.is_file()] file = file[0].split('/') return file

calm bay
calm bay
#

split will return a list of strings across your splits, so e.g.py "folder/file.txt".split("/") would return ["folder", "file.txt"]

dawn notch
#

ohhh okk

#

could you give me the right syntax pls ?

calm bay
#

what you want to do is apply this to every item in filedir and also select the last item there

#

you're already doing list comprehension, so you just need to add a bit more to it

#

something like str(file).split("/")[-1]

dawn notch
#

could you just give me syntax 🤣

calm bay
#

that's what it is though...

dawn notch