Sanitize filename
This commit is contained in:
parent
61a4998ac7
commit
44f29ccd1b
@ -19,7 +19,7 @@ sounds_folder = 'Sounds'
|
||||
|
||||
intents = discord.Intents.default()
|
||||
intents.message_content = True
|
||||
bot = commands.Bot(command_prefix='>', intents=intents)
|
||||
bot = commands.Bot(command_prefix='$', intents=intents)
|
||||
|
||||
@bot.command()
|
||||
async def list(ctx):
|
||||
@ -27,7 +27,7 @@ async def list(ctx):
|
||||
|
||||
@bot.command()
|
||||
async def play(ctx, arg):
|
||||
filepath = computePath(arg)
|
||||
filepath = computePath(sanitizeFileName(arg))
|
||||
if os.path.isfile(filepath):
|
||||
with open(filepath, mode='rb') as file:
|
||||
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)
|
||||
|
||||
@bot.command()
|
||||
async def add(ctx, name, url, start, end):
|
||||
if os.path.isfile(computePath(name)):
|
||||
async def add(ctx, name, url, start=-1, end=-1):
|
||||
filename = sanitizeFileName(name)
|
||||
if os.path.isfile(computePath(filename)):
|
||||
await ctx.send("Ce nom est déjà utilisé, merci d'en choisir un autre.")
|
||||
return
|
||||
downloadAndCutVideo(url, name, start, end)
|
||||
await play(name)
|
||||
downloadAndCutVideo(url, filename, start, end)
|
||||
await play(ctx, filename)
|
||||
|
||||
@bot.command()
|
||||
async def remove(ctx, name):
|
||||
os.remove(computePath(name))
|
||||
await ctx.send("salut mon pote !")
|
||||
filename = sanitizeFileName(name)
|
||||
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():
|
||||
soundLists = "```\n"
|
||||
@ -88,5 +95,9 @@ def downloadVideo(url, filename):
|
||||
def computePath(filename):
|
||||
return sounds_folder + '/' + filename + ".mp3"
|
||||
|
||||
def sanitizeFileName(filename):
|
||||
return re.sub(r"[/\\?%*:|\"<>\x7F\x00-\x1F]", "-", filename)
|
||||
|
||||
|
||||
|
||||
bot.run(sys.argv[1])
|
||||
|
Loading…
Reference in New Issue
Block a user