mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 19:44:20 +00:00
wow nothing crashed yet
This commit is contained in:
parent
49ddc7eebb
commit
777abfae5e
3 changed files with 95 additions and 61 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -3,3 +3,4 @@ config.ini
|
||||||
__pycache__
|
__pycache__
|
||||||
diario.json
|
diario.json
|
||||||
libopus-0.dll
|
libopus-0.dll
|
||||||
|
music.opus
|
10
db.py
10
db.py
|
@ -605,15 +605,15 @@ class CVMusic(Base):
|
||||||
title = Column(String, nullable=False)
|
title = Column(String, nullable=False)
|
||||||
timestamp = Column(DateTime, nullable=False)
|
timestamp = Column(DateTime, nullable=False)
|
||||||
|
|
||||||
player_id = Column(Integer, ForeignKey("royals.id"))
|
user_id = Column(Integer, ForeignKey("royals.id"))
|
||||||
player = relationship("Royal")
|
user = relationship("Royal")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_and_add(title: str, player_id: int):
|
def create_and_add(title: str, user: Royal, timestamp: datetime):
|
||||||
session = Session()
|
session = Session()
|
||||||
session.add(CVMusic(title=title,
|
session.add(CVMusic(title=title,
|
||||||
timestamp=datetime.datetime.now(),
|
timestamp=timestamp,
|
||||||
player_id=player_id))
|
user_id=user.id))
|
||||||
session.commit()
|
session.commit()
|
||||||
session.close()
|
session.close()
|
||||||
|
|
||||||
|
|
143
discordbot.py
143
discordbot.py
|
@ -23,37 +23,67 @@ voice_player = None
|
||||||
voice_queue = asyncio.Queue()
|
voice_queue = asyncio.Queue()
|
||||||
|
|
||||||
|
|
||||||
|
class Video:
|
||||||
|
def __init__(self):
|
||||||
|
self.user = None
|
||||||
|
self.info = None
|
||||||
|
self.timestamp = None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def init(author, info, timestamp):
|
||||||
|
self = Video()
|
||||||
|
discord_user = await find_user(author)
|
||||||
|
self.user = discord_user.royal
|
||||||
|
self.info = info
|
||||||
|
self.timestamp = timestamp
|
||||||
|
return self
|
||||||
|
|
||||||
|
def add_to_db(self):
|
||||||
|
db.CVMusic.create_and_add(title=self.info["title"],
|
||||||
|
user=self.user,
|
||||||
|
timestamp=self.timestamp)
|
||||||
|
|
||||||
|
def create_embed(self):
|
||||||
|
embed = discord.Embed(type="rich",
|
||||||
|
title=self.info['title'] if 'title' in self.info else None,
|
||||||
|
url=self.info['webpage_url'] if 'webpage_url' in self.info else None,
|
||||||
|
colour=discord.Colour(13375518))
|
||||||
|
# Uploader
|
||||||
|
if "uploader" in self.info and self.info["uploader"] is not None:
|
||||||
|
embed.set_author(name=self.info["uploader"],
|
||||||
|
url=self.info["uploader_url"] if "uploader_url" in self.info else None)
|
||||||
|
# Thumbnail
|
||||||
|
if "thumbnail" in self.info:
|
||||||
|
embed.set_thumbnail(url=self.info["thumbnail"])
|
||||||
|
# Duration
|
||||||
|
embed.add_field(name="Durata", value=str(datetime.timedelta(seconds=self.info["duration"])))
|
||||||
|
# Views
|
||||||
|
if "view_count" in self.info and self.info["view_count"] is not None:
|
||||||
|
embed.add_field(name="Visualizzazioni", value="{:_}".format(self.info["view_count"]).replace("_", " "))
|
||||||
|
# Likes
|
||||||
|
if "like_count" in self.info and self.info["like_count"] is not None:
|
||||||
|
embed.add_field(name="Mi piace", value="{:_}".format(self.info["like_count"]).replace("_", " "))
|
||||||
|
# Dislikes
|
||||||
|
if "dislike_count" in self.info and self.info["dislike_count"] is not None:
|
||||||
|
embed.add_field(name="Non mi piace", value="{:_}".format(self.info["dislike_count"]).replace("_", " "))
|
||||||
|
return embed
|
||||||
|
|
||||||
|
async def download(self):
|
||||||
|
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, self.info["webpage_url"]))
|
||||||
|
|
||||||
|
|
||||||
async def find_user(user: discord.User):
|
async def find_user(user: discord.User):
|
||||||
session = await loop.run_in_executor(None, db.Session)
|
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)
|
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 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"):
|
||||||
|
@ -112,15 +142,16 @@ 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
|
||||||
await client.send_message(message.channel, f"✅ Aggiunto alla coda:", embed=create_music_embed(info))
|
video = await Video.init(author=message.author, info=info, timestamp=datetime.datetime.now())
|
||||||
voice_queue.put(info)
|
await client.send_message(message.channel, f"✅ Aggiunto alla coda:", embed=video.create_embed())
|
||||||
|
await voice_queue.put(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 video in info["entries"]:
|
for single_info in info["entries"]:
|
||||||
await client.send_message(message.channel, f"✅ Aggiunto alla coda:",
|
video = await Video.init(author=message.author, info=single_info, timestamp=datetime.datetime.now())
|
||||||
embed=create_music_embed(video))
|
await client.send_message(message.channel, f"✅ Aggiunto alla coda:", embed=video.create_embed())
|
||||||
voice_queue.put(video)
|
await voice_queue.put(video)
|
||||||
else:
|
else:
|
||||||
await client.send_message(message.channel, f"ℹ La playlist contiene {len(info['entries'])} video.\n"
|
await client.send_message(message.channel, f"ℹ La playlist contiene {len(info['entries'])} video.\n"
|
||||||
f"Sei sicuro di volerli aggiungere alla coda?\n"
|
f"Sei sicuro di volerli aggiungere alla coda?\n"
|
||||||
|
@ -128,15 +159,14 @@ async def on_message(message: discord.Message):
|
||||||
f"_(Il bot potrebbe crashare.)_")
|
f"_(Il bot potrebbe crashare.)_")
|
||||||
answer = await client.wait_for_message(timeout=60, author=message.author, channel=message.channel)
|
answer = await client.wait_for_message(timeout=60, author=message.author, channel=message.channel)
|
||||||
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 video in info["entries"]:
|
for single_info in info["entries"]:
|
||||||
await client.send_message(message.channel, f"✅ Aggiunto alla coda:",
|
video = await Video.init(author=message.author, info=single_info,
|
||||||
embed=create_music_embed(video))
|
timestamp=datetime.datetime.now())
|
||||||
voice_queue.put(video)
|
await client.send_message(message.channel, f"✅ Aggiunto alla coda:", embed=video.create_embed())
|
||||||
|
await voice_queue.put(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("!mstart"):
|
|
||||||
await update_music_queue()
|
|
||||||
elif message.content.startswith("!msearch"):
|
elif message.content.startswith("!msearch"):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
elif message.content.startswith("!mskip"):
|
elif message.content.startswith("!mskip"):
|
||||||
|
@ -161,24 +191,27 @@ async def update_users_pipe(users_connection):
|
||||||
|
|
||||||
|
|
||||||
async def update_music_queue():
|
async def update_music_queue():
|
||||||
# Get the last video in the queue
|
await client.wait_until_ready()
|
||||||
info = voice_queue.get()
|
while True:
|
||||||
# Download the video
|
global voice_player
|
||||||
with youtube_dl.YoutubeDL({"noplaylist": True,
|
# Wait until there is nothing playing
|
||||||
"format": "bestaudio",
|
if voice_player is None or voice_player.is_playing():
|
||||||
"postprocessors": [{
|
if voice_queue.qsize() == 0:
|
||||||
"key": 'FFmpegExtractAudio',
|
await asyncio.sleep(1)
|
||||||
"preferredcodec": 'opus'
|
continue
|
||||||
}],
|
# Get the last video in the queue
|
||||||
"outtmpl": "music.%(ext)s"}) as ytdl:
|
video = await voice_queue.get()
|
||||||
info = await loop.run_in_executor(None, functools.partial(ytdl.extract_info, info["webpage_url"]))
|
# Download the video
|
||||||
# Play the video
|
await video.download()
|
||||||
global voice_player
|
# Play the video
|
||||||
voice_player = voice_client.create_ffmpeg_player(f"music.opus")
|
voice_player = voice_client.create_ffmpeg_player(f"music.opus")
|
||||||
voice_player.start()
|
voice_player.start()
|
||||||
pass
|
# Add the video to the db
|
||||||
|
await loop.run_in_executor(None, video.add_to_db)
|
||||||
|
|
||||||
|
|
||||||
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())
|
||||||
client.run(config["Discord"]["bot_token"])
|
client.run(config["Discord"]["bot_token"])
|
Loading…
Reference in a new issue