diff --git a/toitadesbot.py b/toitadesbot.py index a3482cd..28414e0 100755 --- a/toitadesbot.py +++ b/toitadesbot.py @@ -7,6 +7,7 @@ import io import glob import re import discord +from pathlib import Path from pydub import AudioSegment from discord.ext import commands @@ -50,9 +51,31 @@ async def remove(ctx, name): await ctx.send(filename + " a été supprimé. Salut mon pote !") @bot.command() -async def search(ctx, keyword) +async def search(ctx, keyword): searchTerm = sanitizeFileName(keyword) - await ctx.send(filename + " a été supprimé. Salut mon pote !") + result = find_all(keyword, sounds_folder) + + await ctx.send(searchInFolder(searchTerm)) + +def searchInFolder(searchTerm): + filenames = find_all(searchTerm, sounds_folder) + if len(filenames) == 0: + return "Aucun résultat !" + elif len(filenames) == 1: + return filenames[0] + else: + return str(len(filenames)) + " résultats :" + "\n * " + "\n * ".join(filenames) + +def find_all(name, path): + result = [] + for root, dirs, files in os.walk(path): + for filepath in files: + if filepath.endswith(".mp3"): + filename = Path(filepath).stem + if filename.lower().find(name.lower()) != -1: + result.append(filename) + return result + def listSounds(): soundLists = "```\n"