Add discord bot commands

This commit is contained in:
Artlef 2023-05-08 15:03:50 +02:00
parent ffbeba2c6c
commit f9b53454d5
2 changed files with 51 additions and 6 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.mp3
token

55
main.py
View File

@ -1,17 +1,64 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys
import yt_dlp import yt_dlp
import os import os
import io
import glob
import re
import discord
from pydub import AudioSegment from pydub import AudioSegment
from discord.ext import commands
if len(sys.argv) < 2:
print("./main.py TOKEN")
exit(1)
sounds_folder = 'Sounds' 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): def downloadAndCutVideo(url, filename, start = -1, end = -1):
if start == -1 and end == -1: if start == -1 and end == -1:
downloadVideo(url, filename) downloadVideo(url, filename)
return return
start = int(start)
end = int(end)
tmpFilename = filename + "tmp" tmpFilename = filename + "tmp"
downloadVideo(url, tmpFilename) downloadVideo(url, tmpFilename)
song = AudioSegment.from_mp3(computePath(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": "bestaudio", "outtmpl": sounds_folder + "/" + filename + ".%(ext)s"}
ydl_opts = { ydl_opts = {
'format': 'm4a/bestaudio/best', 'format': 'm4a/bestaudio/best',
# See help(yt_dlp.postprocessor) for a list of available Postprocessors and their arguments
'postprocessors': [{ # Extract audio using ffmpeg 'postprocessors': [{ # Extract audio using ffmpeg
'key': 'FFmpegExtractAudio', 'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3', 'preferredcodec': 'mp3',
@ -40,8 +86,5 @@ def downloadVideo(url, filename):
def computePath(filename): def computePath(filename):
return sounds_folder + '/' + filename + ".mp3" return sounds_folder + '/' + filename + ".mp3"
def refreshAvailableSounds():
bot.run(sys.argv[1])
if __name__ == '__main__':
downloadAndCutVideo('https://www.youtube.com/watch?v=wyNOM_HIv0U', "jesuislelaitier", 6, 10)