Skip to content
Advertisement

Have a search command on discord.py

would like to know if it’s possible to add a search command to my discord bot, I will basically use the command to find files inside a directory. For example there is a file named name.txt, and I could do /search nam and it would search for this file and output the name of similar files in the directory.

Advertisement

Answer

It’s simple, just import listdir from os module and give folder path to listdir function then add one parameter in the command which will be used to match the files in the given path.

Try following code:

@bot.command()
async def search(ctx, file_name):
    search_result = 'Search Results:'
    from os import listdir
    for file in listdir('data'):
        if file_name in file.lower():
            search_result = search_result+'n'+file
    if search_result == 'Search Results:':
        await ctx.send('Error: Not matched with any file.')
    else:
        await ctx.send(search_result)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement