Sanitize filename

This commit is contained in:
Artlef 2024-10-25 20:54:36 +02:00
parent 61a4998ac7
commit 44f29ccd1b

View File

@ -19,7 +19,7 @@ sounds_folder = 'Sounds'
intents = discord.Intents.default() intents = discord.Intents.default()
intents.message_content = True intents.message_content = True
bot = commands.Bot(command_prefix='>', intents=intents) bot = commands.Bot(command_prefix='$', intents=intents)
@bot.command() @bot.command()
async def list(ctx): async def list(ctx):
@ -27,7 +27,7 @@ async def list(ctx):
@bot.command() @bot.command()
async def play(ctx, arg): async def play(ctx, arg):
filepath = computePath(arg) filepath = computePath(sanitizeFileName(arg))
if os.path.isfile(filepath): if os.path.isfile(filepath):
with open(filepath, mode='rb') as file: with open(filepath, mode='rb') as file:
await ctx.send(file=discord.File(file, filename=arg + ".mp3")) await ctx.send(file=discord.File(file, filename=arg + ".mp3"))
@ -35,17 +35,24 @@ async def play(ctx, arg):
await ctx.send("connais pas " + arg) await ctx.send("connais pas " + arg)
@bot.command() @bot.command()
async def add(ctx, name, url, start, end): async def add(ctx, name, url, start=-1, end=-1):
if os.path.isfile(computePath(name)): filename = sanitizeFileName(name)
if os.path.isfile(computePath(filename)):
await ctx.send("Ce nom est déjà utilisé, merci d'en choisir un autre.") await ctx.send("Ce nom est déjà utilisé, merci d'en choisir un autre.")
return return
downloadAndCutVideo(url, name, start, end) downloadAndCutVideo(url, filename, start, end)
await play(name) await play(ctx, filename)
@bot.command() @bot.command()
async def remove(ctx, name): async def remove(ctx, name):
os.remove(computePath(name)) filename = sanitizeFileName(name)
await ctx.send("salut mon pote !") os.remove(computePath(filename))
await ctx.send(filename + " a été supprimé. Salut mon pote !")
@bot.command()
async def search(ctx, keyword)
searchTerm = sanitizeFileName(keyword)
await ctx.send(filename + " a été supprimé. Salut mon pote !")
def listSounds(): def listSounds():
soundLists = "```\n" soundLists = "```\n"
@ -88,5 +95,9 @@ def downloadVideo(url, filename):
def computePath(filename): def computePath(filename):
return sounds_folder + '/' + filename + ".mp3" return sounds_folder + '/' + filename + ".mp3"
def sanitizeFileName(filename):
return re.sub(r"[/\\?%*:|\"<>\x7F\x00-\x1F]", "-", filename)
bot.run(sys.argv[1]) bot.run(sys.argv[1])