#!/usr/bin/env python import sys import yt_dlp import os import io import glob import re import discord from pathlib import Path from pydub import AudioSegment from discord.ext import commands if len(sys.argv) < 2: print("./toitadesbot.py TOKEN") exit(1) sounds_folder = 'Sounds' intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix='$', intents=intents) @bot.command() async def list(ctx): await ctx.send(listSounds()) @bot.command() async def play(ctx, 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")) else: await ctx.send("connais pas " + arg) @bot.command() 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, filename, start, end) await play(ctx, filename) @bot.command() async def remove(ctx, name): 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) 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" for file in glob.glob(sounds_folder + "/" + "*.mp3"): filename = re.findall('Sounds\/(.*)\.mp3', file)[0] soundLists += filename + "\n" soundLists += "```\n" return soundLists def downloadAndCutVideo(url, filename, start = -1, end = -1): if start == -1 and end == -1: downloadVideo(url, filename) return start = int(start) end = int(end) tmpFilename = filename + "tmp" downloadVideo(url, tmpFilename) song = AudioSegment.from_mp3(computePath(tmpFilename)) if start != -1: start = start * 1000 if end != -1: end = end * 1000 song[start:end].export(computePath(filename), format="mp3") os.remove(computePath(tmpFilename)) def downloadVideo(url, filename): ydl_opts = {"format": "bestaudio", "outtmpl": sounds_folder + "/" + filename + ".%(ext)s"} ydl_opts = { 'format': 'm4a/bestaudio/best', 'postprocessors': [{ # Extract audio using ffmpeg 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', }], "outtmpl": sounds_folder + "/" + filename + ".%(ext)s" } with yt_dlp.YoutubeDL(ydl_opts) as ydl: ydl.download([url]) def computePath(filename): return sounds_folder + '/' + filename + ".mp3" def sanitizeFileName(filename): return re.sub(r"[/\\?%*:|\"<>\x7F\x00-\x1F]", "-", filename) bot.run(sys.argv[1])