1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

It finally works

This commit is contained in:
Steffo 2017-11-05 22:50:37 +01:00
parent 8a25a6a49f
commit 49ddc7eebb
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: C27544372FBB445D

View file

@ -1,16 +1,13 @@
import datetime import datetime
import discord import discord
import discord.opus import discord.opus
import functools import functools
from discord.ext import commands
import db import db
import re
import errors import errors
import youtube_dl
# Init the event loop # Init the event loop
import asyncio import asyncio
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
# Init the config reader # Init the config reader
@ -23,6 +20,7 @@ client = discord.Client()
discord.opus.load_opus("libopus-0.dll") discord.opus.load_opus("libopus-0.dll")
voice_client = None voice_client = None
voice_player = None voice_player = None
voice_queue = asyncio.Queue()
async def find_user(user: discord.User): async def find_user(user: discord.User):
@ -31,6 +29,31 @@ async def find_user(user: discord.User):
return user return user
def create_music_embed(ytdl_data: dict):
embed = discord.Embed(type="rich",
title=ytdl_data['title'] if 'title' in ytdl_data else None,
url=ytdl_data['webpage_url'] if 'webpage_url' in ytdl_data else None,
colour=discord.Colour(13375518))
# Uploader
if "uploader" in ytdl_data and ytdl_data["uploader"] is not None:
embed.set_author(name=ytdl_data["uploader"], url=ytdl_data["uploader_url"] if "uploader_url" in ytdl_data else None)
# Thumbnail
if "thumbnail" in ytdl_data:
embed.set_thumbnail(url=ytdl_data["thumbnail"])
# Duration
embed.add_field(name="Durata", value=str(datetime.timedelta(seconds=ytdl_data["duration"])))
# Views
if "view_count" in ytdl_data and ytdl_data["view_count"] is not None:
embed.add_field(name="Visualizzazioni", value="{:_}".format(ytdl_data["view_count"]).replace("_", " "))
# Likes
if "like_count" in ytdl_data and ytdl_data["like_count"] is not None:
embed.add_field(name="Mi piace", value="{:_}".format(ytdl_data["like_count"]).replace("_", " "))
# Dislikes
if "dislike_count" in ytdl_data and ytdl_data["dislike_count"] is not None:
embed.add_field(name="Non mi piace", value="{:_}".format(ytdl_data["dislike_count"]).replace("_", " "))
return embed
@client.event @client.event
async def on_message(message: discord.Message): async def on_message(message: discord.Message):
if message.content.startswith("!register"): if message.content.startswith("!register"):
@ -39,7 +62,8 @@ async def on_message(message: discord.Message):
try: try:
username = message.content.split(" ", 1)[1] username = message.content.split(" ", 1)[1]
except IndexError: except IndexError:
await client.send_message(message.channel, "⚠️ Non hai specificato un username!") await client.send_message(message.channel, "⚠️ Non hai specificato un username!\n"
"Sintassi corretta: `!register <username_ryg>`")
return return
try: try:
d = db.Discord.create(session, d = db.Discord.create(session,
@ -60,70 +84,72 @@ async def on_message(message: discord.Message):
global voice_client global voice_client
voice_client = await client.join_voice_channel(message.author.voice.voice_channel) voice_client = await client.join_voice_channel(message.author.voice.voice_channel)
await client.send_message(message.channel, f"✅ Mi sono connesso in <#{message.author.voice.voice_channel.id}>.") await client.send_message(message.channel, f"✅ Mi sono connesso in <#{message.author.voice.voice_channel.id}>.")
elif message.content.startswith("!play"): elif message.content.startswith("!madd"):
# Display typing status
await client.send_typing(message.channel) await client.send_typing(message.channel)
# Check if the bot is connected to voice chat # The bot should be in voice chat
if voice_client is None or not voice_client.is_connected(): if voice_client is None:
await client.send_message(message.channel, f"⚠ Il bot non è connesso in nessun canale.") await client.send_message(message.channel, "⚠️ Non sono connesso alla cv!\n"
return "Fammi entrare scrivendo `!cv` mentre sei in chat vocale.")
# Get the URL from the message # Find the sent url
try: try:
url = message.content.split(" ", 1)[1] url = message.content.split(" ", 1)[1]
except IndexError: except IndexError:
await client.send_message(message.channel, "⚠️ Non hai specificato un URL.") await client.send_message(message.channel, "⚠️ Non hai specificato un url!\n"
"Sintassi corretta: `!madd <video>`")
return return
# Download the file and create a new voice player # Se è una playlist, informa che potrebbe essere richiesto un po' di tempo
new_voice_player = await voice_client.create_ytdl_player(url, ytdl_options={ if "playlist" in url:
"noplaylist": True await client.send_message(message.channel, f" Hai inviato una playlist al bot.\n"
}) f"L'elaborazione potrebbe richiedere un po' di tempo.")
# Replace the old voice player with the new one # Extract the info from the url
global voice_player try:
if voice_player is not None: with youtube_dl.YoutubeDL({"quiet": True, "skip_download": True, "noplaylist": True, "format": "webm[abr>0]/bestaudio/best"}) as ytdl:
voice_player.stop() info = await loop.run_in_executor(None, functools.partial(ytdl.extract_info, url))
voice_player = new_voice_player except youtube_dl.utils.DownloadError as e:
# Start playing the music if "is not a valid URL" in str(e):
voice_player.start() await client.send_message(message.channel, f"⚠️ Il link inserito non è valido.\n"
# Create the video rich embed f"Se vuoi cercare un video su YouTube, usa `!msearch <query>`")
embed = discord.Embed(type="rich",
title=voice_player.title,
url=voice_player.url,
colour=discord.Colour(13375518))
embed.set_author(name=voice_player.uploader)
embed.add_field(name="Durata", value=str(datetime.timedelta(seconds=voice_player.duration)), inline=False)
if voice_player.views is not None:
embed.add_field(name="Visualizzazioni", value="{:_}".format(voice_player.views).replace("_", " "))
if voice_player.likes is not None:
embed.add_field(name="Mi piace", value="{:_}".format(voice_player.likes).replace("_", " "))
if voice_player.dislikes is not None:
embed.add_field(name="Non mi piace", value="{:_}".format(voice_player.dislikes).replace("_", " "))
# Send the embed in the chat channel where the command was sent
await client.send_message(message.channel, f"▶️ In riproduzione:", embed=embed)
# Find the message sender in the db
user = await find_user(message.author)
# Add the audio to the database
await loop.run_in_executor(None, functools.partial(db.CVMusic.create_and_add, voice_player.title, user.royal_id))
elif message.content.startswith("!pause"):
await client.send_typing(message.channel)
if voice_player is None:
await client.send_message(message.channel, f"⚠ Nessun file audio sta venendo attualmente riprodotto.")
return return
voice_player.pause() if "_type" not in info:
await client.send_message(message.channel, f"⏸ Riproduzione messa in pausa.") # If target is a single video
elif message.content.startswith("!resume"): await client.send_message(message.channel, f"✅ Aggiunto alla coda:", embed=create_music_embed(info))
await client.send_typing(message.channel) voice_queue.put(info)
if voice_player is None: elif info["_type"] == "playlist":
await client.send_message(message.channel, f"⚠ Nessun file audio sta venendo attualmente riprodotto.") # If target is a playlist
return if len(info["entries"]) < 20:
voice_player.resume() for video in info["entries"]:
await client.send_message(message.channel, f"▶️ Riproduzione ripresa.") await client.send_message(message.channel, f"✅ Aggiunto alla coda:",
elif message.content.startswith("!stop"): embed=create_music_embed(video))
await client.send_typing(message.channel) voice_queue.put(video)
if voice_player is None: else:
await client.send_message(message.channel, f"⚠ Nessun file audio sta venendo attualmente riprodotto.") await client.send_message(message.channel, f" La playlist contiene {len(info['entries'])} video.\n"
return f"Sei sicuro di volerli aggiungere alla coda?\n"
voice_player.stop() f"Rispondi **sì** o **no**.\n"
await client.send_message(message.channel, f"⏹ Riproduzione interrotta.") f"_(Il bot potrebbe crashare.)_")
answer = await client.wait_for_message(timeout=60, author=message.author, channel=message.channel)
if "" in answer.content.lower() or "si" in answer.content.lower():
for video in info["entries"]:
await client.send_message(message.channel, f"✅ Aggiunto alla coda:",
embed=create_music_embed(video))
voice_queue.put(video)
elif "no" in answer.content.lower():
await client.send_message(message.channel, f" Operazione annullata.")
return
elif message.content.startswith("!mstart"):
await update_music_queue()
elif message.content.startswith("!msearch"):
raise NotImplementedError()
elif message.content.startswith("!mskip"):
raise NotImplementedError()
elif message.content.startswith("!mpause"):
raise NotImplementedError()
elif message.content.startswith("!mresume"):
raise NotImplementedError()
elif message.content.startswith("!mcancel"):
raise NotImplementedError()
elif message.content.startswith("!mstop"):
raise NotImplementedError()
async def update_users_pipe(users_connection): async def update_users_pipe(users_connection):
await client.wait_until_ready() await client.wait_until_ready()
@ -134,6 +160,24 @@ async def update_users_pipe(users_connection):
users_connection.send(discord_members) users_connection.send(discord_members)
async def update_music_queue():
# Get the last video in the queue
info = voice_queue.get()
# Download the video
with youtube_dl.YoutubeDL({"noplaylist": True,
"format": "bestaudio",
"postprocessors": [{
"key": 'FFmpegExtractAudio',
"preferredcodec": 'opus'
}],
"outtmpl": "music.%(ext)s"}) as ytdl:
info = await loop.run_in_executor(None, functools.partial(ytdl.extract_info, info["webpage_url"]))
# Play the video
global voice_player
voice_player = voice_client.create_ffmpeg_player(f"music.opus")
voice_player.start()
pass
def process(users_connection): def process(users_connection):
print("Discordbot starting...") print("Discordbot starting...")
loop.create_task(update_users_pipe(users_connection)) loop.create_task(update_users_pipe(users_connection))