1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-30 15:04:18 +00:00

Possible bugfix for the forkbomb

This commit is contained in:
Steffo 2018-01-15 13:42:40 +01:00
parent f95f1f43f1
commit dbf9bd0e02
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

@ -7,6 +7,7 @@ import sys
import db import db
import errors import errors
import youtube_dl import youtube_dl
import concurrent.futures
# Init the event loop # Init the event loop
import asyncio import asyncio
@ -37,7 +38,8 @@ voice_player = None
voice_queue = [] voice_queue = []
voice_playing = None voice_playing = None
# Init the executor
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
class Video: class Video:
def __init__(self): def __init__(self):
@ -91,7 +93,7 @@ class Video:
}], }],
"outtmpl": "music.%(ext)s", "outtmpl": "music.%(ext)s",
"quiet": True}) as ytdl: "quiet": True}) as ytdl:
info = await loop.run_in_executor(None, functools.partial(ytdl.extract_info, self.info["webpage_url"])) info = await loop.run_in_executor(executor, functools.partial(ytdl.extract_info, self.info["webpage_url"]))
except Exception as e: except Exception as e:
client.send_message(self.channel, f"⚠ Errore durante il download del video:\n" client.send_message(self.channel, f"⚠ Errore durante il download del video:\n"
f"```" f"```"
@ -100,8 +102,8 @@ class Video:
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(executor, db.Session)
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(executor, session.query(db.Discord).filter_by(discord_id=user.id).join(db.Royal).first)
return user return user
@ -136,7 +138,7 @@ async def on_message(message: discord.Message):
global voice_player global voice_player
if message.content.startswith("!register"): if message.content.startswith("!register"):
await client.send_typing(message.channel) await client.send_typing(message.channel)
session = await loop.run_in_executor(None, db.Session()) session = await loop.run_in_executor(executor, db.Session())
try: try:
username = message.content.split(" ", 1)[1] username = message.content.split(" ", 1)[1]
except IndexError: except IndexError:
@ -186,7 +188,7 @@ async def on_message(message: discord.Message):
# Extract the info from the url # Extract the info from the url
try: try:
with youtube_dl.YoutubeDL({"quiet": True, "skip_download": True, "noplaylist": True, "format": "webm[abr>0]/bestaudio/best"}) as ytdl: with youtube_dl.YoutubeDL({"quiet": True, "skip_download": True, "noplaylist": True, "format": "webm[abr>0]/bestaudio/best"}) as ytdl:
info = await loop.run_in_executor(None, functools.partial(ytdl.extract_info, url)) info = await loop.run_in_executor(executor, functools.partial(ytdl.extract_info, url))
except youtube_dl.utils.DownloadError as e: except youtube_dl.utils.DownloadError as e:
if "is not a valid URL" in str(e) or "Unsupported URL" in str(e): if "is not a valid URL" in str(e) or "Unsupported URL" in str(e):
await client.send_message(message.channel, f"⚠️ Il link inserito non è valido.\n" await client.send_message(message.channel, f"⚠️ Il link inserito non è valido.\n"
@ -241,7 +243,7 @@ async def on_message(message: discord.Message):
# Extract the info from the url # Extract the info from the url
try: try:
with youtube_dl.YoutubeDL({"quiet": True, "skip_download": True, "noplaylist": True, "format": "webm[abr>0]/bestaudio/best"}) as ytdl: with youtube_dl.YoutubeDL({"quiet": True, "skip_download": True, "noplaylist": True, "format": "webm[abr>0]/bestaudio/best"}) as ytdl:
info = await loop.run_in_executor(None, functools.partial(ytdl.extract_info, f"ytsearch:{text}")) info = await loop.run_in_executor(executor, functools.partial(ytdl.extract_info, f"ytsearch:{text}"))
except youtube_dl.utils.DownloadError as e: except youtube_dl.utils.DownloadError as e:
if "is not a valid URL" in str(e) or "Unsupported URL" in str(e): if "is not a valid URL" in str(e) or "Unsupported URL" in str(e):
await client.send_message(message.channel, f"⚠️ Il video ottenuto dalla ricerca non è valido. Prova a cercare qualcos'altro...") await client.send_message(message.channel, f"⚠️ Il video ottenuto dalla ricerca non è valido. Prova a cercare qualcos'altro...")
@ -331,7 +333,7 @@ async def on_message(message: discord.Message):
async def update_users_pipe(users_connection): async def update_users_pipe(users_connection):
await client.wait_until_ready() await client.wait_until_ready()
while True: while True:
msg = await loop.run_in_executor(None, users_connection.recv) msg = await loop.run_in_executor(executor, users_connection.recv)
if msg == "/cv": if msg == "/cv":
discord_members = list(client.get_server(config["Discord"]["server_id"]).members) discord_members = list(client.get_server(config["Discord"]["server_id"]).members)
users_connection.send(discord_members) users_connection.send(discord_members)
@ -369,9 +371,14 @@ async def update_music_queue():
await client.change_presence(game=discord.Game(name=video.info.get("title"), type=2)) await client.change_presence(game=discord.Game(name=video.info.get("title"), type=2))
def process(users_connection): def process(users_connection=None):
print("Discordbot starting...") print("Discordbot starting...")
loop.create_task(update_users_pipe(users_connection)) if users_connection is not None:
loop.create_task(update_music_queue()) asyncio.ensure_future(update_users_pipe(users_connection))
asyncio.ensure_future(update_music_queue())
client.on_error = on_error client.on_error = on_error
client.run(config["Discord"]["bot_token"]) client.run(config["Discord"]["bot_token"])
if __name__ == "__main__":
process()