diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aeabf15 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.mp3 +token diff --git a/main.py b/main.py index 09bc23c..3c790ec 100755 --- a/main.py +++ b/main.py @@ -1,17 +1,64 @@ #!/usr/bin/env python +import sys import yt_dlp import os +import io +import glob +import re +import discord from pydub import AudioSegment +from discord.ext import commands +if len(sys.argv) < 2: + print("./main.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(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, end): + downloadAndCutVideo(url, name, start, end) + await ctx.send("Téléchargé !") + +@bot.command() +async def remove(ctx, name): + os.remove(computePath(name)) + await ctx.send("salut mon pote !") + +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)) @@ -27,7 +74,6 @@ def downloadVideo(url, filename): ydl_opts = {"format": "bestaudio", "outtmpl": sounds_folder + "/" + filename + ".%(ext)s"} ydl_opts = { 'format': 'm4a/bestaudio/best', - # ℹ️ See help(yt_dlp.postprocessor) for a list of available Postprocessors and their arguments 'postprocessors': [{ # Extract audio using ffmpeg 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', @@ -40,8 +86,5 @@ def downloadVideo(url, filename): def computePath(filename): return sounds_folder + '/' + filename + ".mp3" -def refreshAvailableSounds(): - -if __name__ == '__main__': - downloadAndCutVideo('https://www.youtube.com/watch?v=wyNOM_HIv0U', "jesuislelaitier", 6, 10) +bot.run(sys.argv[1])