mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-24 03:54:20 +00:00
Add more basic music support
This commit is contained in:
parent
7c3113af34
commit
8a25a6a49f
2 changed files with 119 additions and 43 deletions
19
db.py
19
db.py
|
@ -598,6 +598,25 @@ class Diario(Base):
|
||||||
session.close()
|
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 run as script, create all the tables in the db
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
Base.metadata.create_all(bind=engine)
|
Base.metadata.create_all(bind=engine)
|
|
@ -1,5 +1,8 @@
|
||||||
|
import datetime
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
import discord.opus
|
import discord.opus
|
||||||
|
import functools
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
import db
|
import db
|
||||||
import re
|
import re
|
||||||
|
@ -21,12 +24,18 @@ discord.opus.load_opus("libopus-0.dll")
|
||||||
voice_client = None
|
voice_client = None
|
||||||
voice_player = 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
|
@client.event
|
||||||
async def on_message(message: discord.Message):
|
async def on_message(message: discord.Message):
|
||||||
# Open a new postgres session
|
|
||||||
session = db.Session()
|
|
||||||
try:
|
|
||||||
if message.content.startswith("!register"):
|
if message.content.startswith("!register"):
|
||||||
|
await client.send_typing(message.channel)
|
||||||
|
session = await loop.run_in_executor(None, db.Session())
|
||||||
try:
|
try:
|
||||||
username = message.content.split(" ", 1)[1]
|
username = message.content.split(" ", 1)[1]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
|
@ -41,32 +50,80 @@ async def on_message(message: discord.Message):
|
||||||
return
|
return
|
||||||
session.add(d)
|
session.add(d)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
session.close()
|
||||||
await client.send_message(message.channel, "✅ Sincronizzazione completata!")
|
await client.send_message(message.channel, "✅ Sincronizzazione completata!")
|
||||||
elif message.content.startswith("!cv") and discord.opus.is_loaded():
|
elif message.content.startswith("!cv") and discord.opus.is_loaded():
|
||||||
|
await client.send_typing(message.channel)
|
||||||
if message.author.voice.voice_channel is None:
|
if message.author.voice.voice_channel is None:
|
||||||
await client.send_message(message.channel, "⚠ Non sei in nessun canale!")
|
await client.send_message(message.channel, "⚠ Non sei in nessun canale!")
|
||||||
return
|
return
|
||||||
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("!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():
|
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.")
|
await client.send_message(message.channel, f"⚠ Il bot non è connesso in nessun canale.")
|
||||||
return
|
return
|
||||||
|
# Get the URL from the message
|
||||||
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.")
|
||||||
return
|
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
|
global voice_player
|
||||||
if voice_player is not None:
|
if voice_player is not None:
|
||||||
voice_player.stop()
|
voice_player.stop()
|
||||||
voice_player = new_voice_player
|
voice_player = new_voice_player
|
||||||
|
# Start playing the music
|
||||||
voice_player.start()
|
voice_player.start()
|
||||||
finally:
|
# Create the video rich embed
|
||||||
session.close()
|
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):
|
async def update_users_pipe(users_connection):
|
||||||
await client.wait_until_ready()
|
await client.wait_until_ready()
|
||||||
|
|
Loading…
Reference in a new issue