mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-27 13:34:28 +00:00
stuffs
This commit is contained in:
parent
b13da5b176
commit
df5f0d89bb
5 changed files with 87 additions and 26 deletions
17
db.py
17
db.py
|
@ -611,13 +611,16 @@ class CVMusic(Base):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_and_add(url: str, user: Royal, enqueued: datetime.datetime, started: datetime.datetime):
|
def create_and_add(url: str, user: Royal, enqueued: datetime.datetime, started: datetime.datetime):
|
||||||
session = Session()
|
try:
|
||||||
session.add(CVMusic(url=url,
|
session = Session()
|
||||||
enqueued=enqueued,
|
session.add(CVMusic(url=url,
|
||||||
started=started,
|
enqueued=enqueued,
|
||||||
user_id=user.id))
|
started=started,
|
||||||
session.commit()
|
user_id=user.id))
|
||||||
session.close()
|
session.commit()
|
||||||
|
session.close()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"ERRORE CVMusic: {e}")
|
||||||
|
|
||||||
# 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__":
|
||||||
|
|
|
@ -2,6 +2,9 @@ import datetime
|
||||||
import discord
|
import discord
|
||||||
import discord.opus
|
import discord.opus
|
||||||
import functools
|
import functools
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
import db
|
import db
|
||||||
import errors
|
import errors
|
||||||
import youtube_dl
|
import youtube_dl
|
||||||
|
@ -95,6 +98,14 @@ async def find_user(user: discord.User):
|
||||||
user = await loop.run_in_executor(None, session.query(db.Discord).filter_by(discord_id=user.id).join(db.Royal).first)
|
user = await loop.run_in_executor(None, session.query(db.Discord).filter_by(discord_id=user.id).join(db.Royal).first)
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
def on_error(event, *args, **kwargs):
|
||||||
|
print(f"ERRORE CRITICO NELL'EVENTO `{event}`\n"
|
||||||
|
f"Il bot si è chiuso per prevenire altri errori.\n"
|
||||||
|
f"Dettagli dell'errore:\n"
|
||||||
|
f"{sys.exc_info()}")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
@client.event
|
@client.event
|
||||||
async def on_message(message: discord.Message):
|
async def on_message(message: discord.Message):
|
||||||
global voice_queue
|
global voice_queue
|
||||||
|
@ -130,12 +141,13 @@ async def on_message(message: discord.Message):
|
||||||
else:
|
else:
|
||||||
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("!madd"):
|
elif message.content.startswith("!play"):
|
||||||
await client.send_typing(message.channel)
|
await client.send_typing(message.channel)
|
||||||
# The bot should be in voice chat
|
# The bot should be in voice chat
|
||||||
if voice_client is None:
|
if voice_client is None:
|
||||||
await client.send_message(message.channel, "⚠️ Non sono connesso alla cv!\n"
|
await client.send_message(message.channel, "⚠️ Non sono connesso alla cv!\n"
|
||||||
"Fammi entrare scrivendo `!cv` mentre sei in chat vocale.")
|
"Fammi entrare scrivendo `!cv` mentre sei in chat vocale.")
|
||||||
|
return
|
||||||
# Find the sent url
|
# Find the sent url
|
||||||
try:
|
try:
|
||||||
url = message.content.split(" ", 1)[1]
|
url = message.content.split(" ", 1)[1]
|
||||||
|
@ -158,14 +170,14 @@ async def on_message(message: discord.Message):
|
||||||
return
|
return
|
||||||
if "_type" not in info:
|
if "_type" not in info:
|
||||||
# If target is a single video
|
# If target is a single video
|
||||||
video = await Video.init(author=message.author, info=info, timestamp=datetime.datetime.now(), channel=message.channel)
|
video = await Video.init(author=message.author, info=info, enqueued=datetime.datetime.now(), channel=message.channel)
|
||||||
await client.send_message(message.channel, f"✅ Aggiunto alla coda:", embed=video.create_embed())
|
await client.send_message(message.channel, f"✅ Aggiunto alla coda:", embed=video.create_embed())
|
||||||
voice_queue.append(video)
|
voice_queue.append(video)
|
||||||
elif info["_type"] == "playlist":
|
elif info["_type"] == "playlist":
|
||||||
# If target is a playlist
|
# If target is a playlist
|
||||||
if len(info["entries"]) < 20:
|
if len(info["entries"]) < 20:
|
||||||
for single_info in info["entries"]:
|
for single_info in info["entries"]:
|
||||||
video = await Video.init(author=message.author, info=single_info, timestamp=datetime.datetime.now(), channel=message.channel)
|
video = await Video.init(author=message.author, info=single_info, enqueued=datetime.datetime.now(), channel=message.channel)
|
||||||
await client.send_message(message.channel, f"✅ Aggiunto alla coda:", embed=video.create_embed())
|
await client.send_message(message.channel, f"✅ Aggiunto alla coda:", embed=video.create_embed())
|
||||||
voice_queue.append(video)
|
voice_queue.append(video)
|
||||||
else:
|
else:
|
||||||
|
@ -177,18 +189,19 @@ async def on_message(message: discord.Message):
|
||||||
if "sì" in answer.content.lower() or "si" in answer.content.lower():
|
if "sì" in answer.content.lower() or "si" in answer.content.lower():
|
||||||
for single_info in info["entries"]:
|
for single_info in info["entries"]:
|
||||||
video = await Video.init(author=message.author, info=single_info,
|
video = await Video.init(author=message.author, info=single_info,
|
||||||
timestamp=datetime.datetime.now(), channel=message.channel)
|
enqueued=datetime.datetime.now(), channel=message.channel)
|
||||||
await client.send_message(message.channel, f"✅ Aggiunto alla coda:", embed=video.create_embed())
|
await client.send_message(message.channel, f"✅ Aggiunto alla coda:", embed=video.create_embed())
|
||||||
voice_queue.append(video)
|
voice_queue.append(video)
|
||||||
elif "no" in answer.content.lower():
|
elif "no" in answer.content.lower():
|
||||||
await client.send_message(message.channel, f"ℹ Operazione annullata.")
|
await client.send_message(message.channel, f"ℹ Operazione annullata.")
|
||||||
return
|
return
|
||||||
elif message.content.startswith("!msearch"):
|
elif message.content.startswith("!search"):
|
||||||
await client.send_typing(message.channel)
|
await client.send_typing(message.channel)
|
||||||
# The bot should be in voice chat
|
# The bot should be in voice chat
|
||||||
if voice_client is None:
|
if voice_client is None:
|
||||||
await client.send_message(message.channel, "⚠️ Non sono connesso alla cv!\n"
|
await client.send_message(message.channel, "⚠️ Non sono connesso alla cv!\n"
|
||||||
"Fammi entrare scrivendo `!cv` mentre sei in chat vocale.")
|
"Fammi entrare scrivendo `!cv` mentre sei in chat vocale.")
|
||||||
|
return
|
||||||
# Find the sent text
|
# Find the sent text
|
||||||
try:
|
try:
|
||||||
text = message.content.split(" ", 1)[1]
|
text = message.content.split(" ", 1)[1]
|
||||||
|
@ -208,32 +221,32 @@ async def on_message(message: discord.Message):
|
||||||
video = await Video.init(author=message.author, info=info["entries"][0], enqueued=datetime.datetime.now(), channel=message.channel)
|
video = await Video.init(author=message.author, info=info["entries"][0], enqueued=datetime.datetime.now(), channel=message.channel)
|
||||||
await client.send_message(message.channel, f"✅ Aggiunto alla coda:", embed=video.create_embed())
|
await client.send_message(message.channel, f"✅ Aggiunto alla coda:", embed=video.create_embed())
|
||||||
voice_queue.append(video)
|
voice_queue.append(video)
|
||||||
elif message.content.startswith("!mskip"):
|
elif message.content.startswith("!skip"):
|
||||||
global voice_player
|
global voice_player
|
||||||
voice_player.stop()
|
voice_player.stop()
|
||||||
voice_player = None
|
voice_player = None
|
||||||
await client.send_message(message.channel, f"⏩ Video saltato.")
|
await client.send_message(message.channel, f"⏩ Video saltato.")
|
||||||
elif message.content.startswith("!mpause"):
|
elif message.content.startswith("!pause"):
|
||||||
if voice_player is None or not voice_player.is_playing():
|
if voice_player is None or not voice_player.is_playing():
|
||||||
await client.send_message(message.channel, f"⚠️ Non è in corso la riproduzione di un video, pertanto non c'è niente da pausare.")
|
await client.send_message(message.channel, f"⚠️ Non è in corso la riproduzione di un video, pertanto non c'è niente da pausare.")
|
||||||
return
|
return
|
||||||
voice_player.pause()
|
voice_player.pause()
|
||||||
await client.send_message(message.channel, f"⏸ Riproduzione messa in pausa.\n"
|
await client.send_message(message.channel, f"⏸ Riproduzione messa in pausa.\n"
|
||||||
f"Riprendi con `!mresume`.")
|
f"Riprendi con `!mresume`.")
|
||||||
elif message.content.startswith("!mresume"):
|
elif message.content.startswith("!resume"):
|
||||||
if voice_player is None or voice_player.is_playing():
|
if voice_player is None or voice_player.is_playing():
|
||||||
await client.send_message(message.channel, f"⚠️ Non c'è nulla in pausa da riprendere!")
|
await client.send_message(message.channel, f"⚠️ Non c'è nulla in pausa da riprendere!")
|
||||||
return
|
return
|
||||||
voice_player.resume()
|
voice_player.resume()
|
||||||
await client.send_message(message.channel, f"▶️ Riproduzione ripresa.")
|
await client.send_message(message.channel, f"▶️ Riproduzione ripresa.")
|
||||||
elif message.content.startswith("!mcancel"):
|
elif message.content.startswith("!cancel"):
|
||||||
try:
|
try:
|
||||||
video = voice_queue.pop()
|
video = voice_queue.pop()
|
||||||
except IndexError:
|
except IndexError:
|
||||||
await client.send_message(message.channel, f"⚠ La playlist è vuota.")
|
await client.send_message(message.channel, f"⚠ La playlist è vuota.")
|
||||||
return
|
return
|
||||||
await client.send_message(message.channel, f"❌ Rimosso dalla playlist:", embed=video.create_embed())
|
await client.send_message(message.channel, f"❌ Rimosso dalla playlist:", embed=video.create_embed())
|
||||||
elif message.content.startswith("!mstop"):
|
elif message.content.startswith("!stop"):
|
||||||
if voice_player is None:
|
if voice_player is None:
|
||||||
await client.send_message(message.channel, f"⚠ Non c'è nulla da interrompere!")
|
await client.send_message(message.channel, f"⚠ Non c'è nulla da interrompere!")
|
||||||
return
|
return
|
||||||
|
@ -241,6 +254,28 @@ async def on_message(message: discord.Message):
|
||||||
voice_player.stop()
|
voice_player.stop()
|
||||||
voice_player = None
|
voice_player = None
|
||||||
await client.send_message(message.channel, f"⏹ Riproduzione interrotta e playlist svuotata.")
|
await client.send_message(message.channel, f"⏹ Riproduzione interrotta e playlist svuotata.")
|
||||||
|
elif message.content.startswith("!np"):
|
||||||
|
if voice_player is None:
|
||||||
|
await client.send_message(message.channel, f"ℹ Non c'è nulla in riproduzione al momento.")
|
||||||
|
return
|
||||||
|
voice_queue = []
|
||||||
|
voice_player.stop()
|
||||||
|
voice_player = None
|
||||||
|
await client.send_message(message.channel, f"ℹ Ora in riproduzione in <#{voice_client.channel.id}>:", embed=voice_playing.create_embed())
|
||||||
|
elif message.content.startswith("!queue"):
|
||||||
|
if voice_player is None:
|
||||||
|
await client.send_message(message.channel, f"ℹ Non c'è nulla in riproduzione al momento.")
|
||||||
|
return
|
||||||
|
to_send = ""
|
||||||
|
to_send += f"0. {voice_playing.info['title'] if voice_playing.info['title'] is not None else '_Senza titolo_'} - <{voice_playing.info['webpage_url'] if voice_playing.info['webpage_url'] is not None else ''}>\n"
|
||||||
|
for n, video in enumerate(voice_queue):
|
||||||
|
to_send += f"{n+1}. {video.info['title'] if video.info['title'] is not None else '_Senza titolo_'} - <{video.info['webpage_url'] if video.info['webpage_url'] is not None else ''}>\n"
|
||||||
|
if len(to_send) >= 2000:
|
||||||
|
to_send = to_send[0:1997] + "..."
|
||||||
|
break
|
||||||
|
await client.send_message(message.channel, to_send)
|
||||||
|
elif __debug__ and message.content.startswith("!exception"):
|
||||||
|
raise Exception("sei un mostro")
|
||||||
|
|
||||||
|
|
||||||
async def update_users_pipe(users_connection):
|
async def update_users_pipe(users_connection):
|
||||||
|
@ -256,17 +291,22 @@ async def update_music_queue():
|
||||||
await client.wait_until_ready()
|
await client.wait_until_ready()
|
||||||
while True:
|
while True:
|
||||||
global voice_player
|
global voice_player
|
||||||
|
global voice_playing
|
||||||
# Wait until there is nothing playing
|
# Wait until there is nothing playing
|
||||||
if voice_client is not None and voice_player is not None and (voice_player.is_playing() and not voice_player.is_done()):
|
if voice_client is not None and voice_player is not None and (voice_player.is_playing() and not voice_player.is_done()):
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
continue
|
continue
|
||||||
if len(voice_queue) == 0:
|
if len(voice_queue) == 0:
|
||||||
|
if voice_playing is not None:
|
||||||
|
# Set the playing status
|
||||||
|
voice_playing = None
|
||||||
|
await client.change_presence()
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
continue
|
continue
|
||||||
# Get the last video in the queue
|
# Get the last video in the queue
|
||||||
video = voice_queue.pop(0)
|
video = voice_queue.pop(0)
|
||||||
# Notify the chat of the download
|
# Notify the chat of the download
|
||||||
await client.send_message(video.channel, f"ℹ E' iniziato il download di:", embed=video.create_embed())
|
await client.send_message(video.channel, f"ℹ E' iniziato il download della prossima canzone.")
|
||||||
# Download the video
|
# Download the video
|
||||||
await video.download()
|
await video.download()
|
||||||
# Play the video
|
# Play the video
|
||||||
|
@ -274,15 +314,16 @@ async def update_music_queue():
|
||||||
voice_player.start()
|
voice_player.start()
|
||||||
# Notify the chat of the start
|
# Notify the chat of the start
|
||||||
await client.send_message(video.channel, f"▶ Ora in riproduzione in <#{voice_client.channel.id}>:", embed=video.create_embed())
|
await client.send_message(video.channel, f"▶ Ora in riproduzione in <#{voice_client.channel.id}>:", embed=video.create_embed())
|
||||||
|
# Set the playing status
|
||||||
|
voice_playing = video
|
||||||
|
await client.change_presence(game=discord.Game(name=video.info.get("title"), type=2))
|
||||||
# Add the video to the db
|
# Add the video to the db
|
||||||
try:
|
await loop.run_in_executor(None, functools.partial(video.add_to_db, started=datetime.datetime.now()))
|
||||||
await loop.run_in_executor(None, functools.partial(video.add_to_db, started=datetime.datetime.now()))
|
|
||||||
except sqlalchemy.exc.OperationalError:
|
|
||||||
await client.send_message(video.channel, f"⚠ Ehi, <@77703771181817856>, il database si è rotto. Vallo a sistemare!")
|
|
||||||
|
|
||||||
|
|
||||||
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))
|
||||||
loop.create_task(update_music_queue())
|
loop.create_task(update_music_queue())
|
||||||
|
client.on_error = on_error
|
||||||
client.run(config["Discord"]["bot_token"])
|
client.run(config["Discord"]["bot_token"])
|
6
requirements.txt
Normal file
6
requirements.txt
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
discord.py
|
||||||
|
python-telegram-bot
|
||||||
|
flask
|
||||||
|
sqlalchemy
|
||||||
|
youtube-dl
|
||||||
|
requests
|
|
@ -20,6 +20,17 @@ table.rl {
|
||||||
color: white !important;
|
color: white !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
table.dota {
|
||||||
|
background-color: #2e2d45 !important;
|
||||||
|
border: none !important;
|
||||||
|
padding: 12px !important;
|
||||||
|
color: white !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
th.dota {
|
||||||
|
border-bottom: 4px solid #6BF !important;
|
||||||
|
}
|
||||||
|
|
||||||
.rl-rank-hidden {
|
.rl-rank-hidden {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
<h1>
|
<h1>
|
||||||
Dota 2
|
Dota 2
|
||||||
</h1>
|
</h1>
|
||||||
<table class="table table-hover sortable">
|
<table id="dota" class="table table-hover sortable dota">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Royal</th>
|
<th>Royal</th>
|
||||||
|
@ -38,7 +38,7 @@
|
||||||
<h1>
|
<h1>
|
||||||
Rocket League
|
Rocket League
|
||||||
</h1>
|
</h1>
|
||||||
<table class="table table-hover sortable rl">
|
<table id="rl" class="table table-hover sortable rl">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Royal</th>
|
<th>Royal</th>
|
||||||
|
@ -121,7 +121,7 @@
|
||||||
<h1>
|
<h1>
|
||||||
Overwatch
|
Overwatch
|
||||||
</h1>
|
</h1>
|
||||||
<table class="table table-hover sortable">
|
<table id="overwatch" class="table table-hover sortable">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Royal</th>
|
<th>Royal</th>
|
||||||
|
@ -156,7 +156,7 @@
|
||||||
<h1>
|
<h1>
|
||||||
Osu!
|
Osu!
|
||||||
</h1>
|
</h1>
|
||||||
<table class="table table-hover sortable">
|
<table id="osu" class="table table-hover sortable">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Royal</th>
|
<th>Royal</th>
|
||||||
|
|
Loading…
Reference in a new issue