1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Add more basic music support

This commit is contained in:
Steffo 2017-11-01 19:23:11 +01:00
parent 7c3113af34
commit 8a25a6a49f
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
2 changed files with 119 additions and 43 deletions

19
db.py
View file

@ -598,6 +598,25 @@ class Diario(Base):
session.close()
class CVMusic(Base):
__tablename__ = "cvmusic"
id = Column(BigInteger, primary_key=True)
title = Column(String, nullable=False)
timestamp = Column(DateTime, nullable=False)
player_id = Column(Integer, ForeignKey("royals.id"))
player = relationship("Royal")
@staticmethod
def create_and_add(title: str, player_id: int):
session = Session()
session.add(CVMusic(title=title,
timestamp=datetime.datetime.now(),
player_id=player_id))
session.commit()
session.close()
# If run as script, create all the tables in the db
if __name__ == "__main__":
Base.metadata.create_all(bind=engine)

View file

@ -1,5 +1,8 @@
import datetime
import discord
import discord.opus
import functools
from discord.ext import commands
import db
import re
@ -21,12 +24,18 @@ discord.opus.load_opus("libopus-0.dll")
voice_client = None
voice_player = None
async def find_user(user: discord.User):
session = await loop.run_in_executor(None, db.Session)
user = await loop.run_in_executor(None, session.query(db.Discord).filter_by(discord_id=user.id).first)
return user
@client.event
async def on_message(message: discord.Message):
# Open a new postgres session
session = db.Session()
try:
if message.content.startswith("!register"):
await client.send_typing(message.channel)
session = await loop.run_in_executor(None, db.Session())
try:
username = message.content.split(" ", 1)[1]
except IndexError:
@ -41,32 +50,80 @@ async def on_message(message: discord.Message):
return
session.add(d)
session.commit()
session.close()
await client.send_message(message.channel, "✅ Sincronizzazione completata!")
elif message.content.startswith("!cv") and discord.opus.is_loaded():
await client.send_typing(message.channel)
if message.author.voice.voice_channel is None:
await client.send_message(message.channel, "⚠ Non sei in nessun canale!")
return
global voice_client
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}>.")
elif message.content.startswith("!music"):
elif message.content.startswith("!play"):
# Display typing status
await client.send_typing(message.channel)
# Check if the bot is connected to voice chat
if voice_client is None or not voice_client.is_connected():
await client.send_message(message.channel, f"⚠ Il bot non è connesso in nessun canale.")
return
# Get the URL from the message
try:
url = message.content.split(" ", 1)[1]
except IndexError:
await client.send_message(message.channel, "⚠️ Non hai specificato un URL.")
return
new_voice_player = await voice_client.create_ytdl_player(url)
# Download the file and create a new voice player
new_voice_player = await voice_client.create_ytdl_player(url, ytdl_options={
"noplaylist": True
})
# Replace the old voice player with the new one
global voice_player
if voice_player is not None:
voice_player.stop()
voice_player = new_voice_player
# Start playing the music
voice_player.start()
finally:
session.close()
# Create the video rich embed
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
voice_player.pause()
await client.send_message(message.channel, f"⏸ Riproduzione messa in pausa.")
elif message.content.startswith("!resume"):
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
voice_player.resume()
await client.send_message(message.channel, f"▶️ Riproduzione ripresa.")
elif message.content.startswith("!stop"):
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
voice_player.stop()
await client.send_message(message.channel, f"⏹ Riproduzione interrotta.")
async def update_users_pipe(users_connection):
await client.wait_until_ready()