Add search method

This commit is contained in:
Artlef 2024-10-25 22:34:38 +02:00
parent 052d430286
commit d6301ee87a

View File

@ -7,6 +7,7 @@ import io
import glob import glob
import re import re
import discord import discord
from pathlib import Path
from pydub import AudioSegment from pydub import AudioSegment
from discord.ext import commands from discord.ext import commands
@ -50,9 +51,31 @@ async def remove(ctx, name):
await ctx.send(filename + " a été supprimé. Salut mon pote !") await ctx.send(filename + " a été supprimé. Salut mon pote !")
@bot.command() @bot.command()
async def search(ctx, keyword) async def search(ctx, keyword):
searchTerm = sanitizeFileName(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(): def listSounds():
soundLists = "```\n" soundLists = "```\n"