mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-27 13:34:28 +00:00
asdfasdfasfdaf
This commit is contained in:
parent
e1cccf9d28
commit
2f56100b1b
4 changed files with 90 additions and 27 deletions
|
@ -41,8 +41,11 @@ config.read("config.ini")
|
||||||
class DurationError(Exception):
|
class DurationError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class LocalFileError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
class Video:
|
|
||||||
|
class OldVideo:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.enqueuer = None # type: typing.Optional[discord.User]
|
self.enqueuer = None # type: typing.Optional[discord.User]
|
||||||
self.filename = None # type: typing.Optional[str]
|
self.filename = None # type: typing.Optional[str]
|
||||||
|
@ -53,7 +56,7 @@ class Video:
|
||||||
async def init(user_id: str, *, filename=None, ytdl_url=None, data=None):
|
async def init(user_id: str, *, filename=None, ytdl_url=None, data=None):
|
||||||
if filename is None and ytdl_url is None:
|
if filename is None and ytdl_url is None:
|
||||||
raise Exception("Filename or url must be specified")
|
raise Exception("Filename or url must be specified")
|
||||||
self = Video()
|
self = OldVideo()
|
||||||
self.enqueuer = int(user_id)
|
self.enqueuer = int(user_id)
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
self.ytdl_url = ytdl_url
|
self.ytdl_url = ytdl_url
|
||||||
|
@ -107,6 +110,24 @@ class Video:
|
||||||
return f"<{self.ytdl_url}>"
|
return f"<{self.ytdl_url}>"
|
||||||
|
|
||||||
|
|
||||||
|
class Video:
|
||||||
|
def __init__(self, url=None, file=None):
|
||||||
|
self.url = url
|
||||||
|
self.file = file
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def retrieve_info(self):
|
||||||
|
if url is None:
|
||||||
|
raise LocalFileError()
|
||||||
|
with youtube_dl.YoutubeDL({"quiet": True,
|
||||||
|
"ignoreerrors": True,
|
||||||
|
"simulate": True}) as ytdl:
|
||||||
|
await loop.run_in_executor(executor, functools.partial(ytdl.extract_info, url=self.url, download=False)
|
||||||
|
# TODO
|
||||||
|
|
||||||
|
|
||||||
if __debug__:
|
if __debug__:
|
||||||
version = "Dev"
|
version = "Dev"
|
||||||
commit_msg = "_in sviluppo_"
|
commit_msg = "_in sviluppo_"
|
||||||
|
@ -131,7 +152,7 @@ elif platform.system() == "Windows":
|
||||||
|
|
||||||
voice_client: typing.Optional[discord.VoiceClient] = None
|
voice_client: typing.Optional[discord.VoiceClient] = None
|
||||||
voice_player: typing.Optional[discord.voice_client.StreamPlayer] = None
|
voice_player: typing.Optional[discord.voice_client.StreamPlayer] = None
|
||||||
voice_queue: typing.List[Video] = []
|
voice_queue: typing.List[OldVideo] = []
|
||||||
|
|
||||||
# Init the executor
|
# Init the executor
|
||||||
executor = concurrent.futures.ThreadPoolExecutor(max_workers=3)
|
executor = concurrent.futures.ThreadPoolExecutor(max_workers=3)
|
||||||
|
@ -210,7 +231,7 @@ async def on_message(message: discord.Message):
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if message.content.startswith("!ping"):
|
if message.content.startswith("!ping"):
|
||||||
await ping(channel=message.channel,
|
await cmd_ping(channel=message.channel,
|
||||||
author=message.author,
|
author=message.author,
|
||||||
params=["/ping"])
|
params=["/ping"])
|
||||||
elif message.content.startswith("!link"):
|
elif message.content.startswith("!link"):
|
||||||
|
@ -239,12 +260,6 @@ async def on_message(message: discord.Message):
|
||||||
author=message.author,
|
author=message.author,
|
||||||
params=message.content.split(" "))
|
params=message.content.split(" "))
|
||||||
elif message.content.startswith("!play"):
|
elif message.content.startswith("!play"):
|
||||||
await client.send_typing(message.channel)
|
|
||||||
# The bot should be in voice chat
|
|
||||||
if voice_client is None:
|
|
||||||
await client.send_message(message.channel, "⚠️ Non sono connesso alla cv!\n"
|
|
||||||
"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]
|
||||||
|
@ -267,13 +282,13 @@ async def on_message(message: discord.Message):
|
||||||
if entry is None:
|
if entry is None:
|
||||||
continue
|
continue
|
||||||
# Add the video to the queue
|
# Add the video to the queue
|
||||||
video = await Video.init(user_id=message.author.id, ytdl_url=entry["webpage_url"], data=entry)
|
video = await OldVideo.init(user_id=message.author.id, ytdl_url=entry["webpage_url"], data=entry)
|
||||||
voice_queue.append(video)
|
voice_queue.append(video)
|
||||||
await client.send_message(message.channel,
|
await client.send_message(message.channel,
|
||||||
f"✅ Aggiunti alla coda **{len(playlist_data['entries']) } video**.")
|
f"✅ Aggiunti alla coda **{len(playlist_data['entries']) } video**.")
|
||||||
else:
|
else:
|
||||||
# If target is a single video
|
# If target is a single video
|
||||||
video = await Video.init(user_id=message.author.id, ytdl_url=url)
|
video = await OldVideo.init(user_id=message.author.id, ytdl_url=url)
|
||||||
await client.send_message(message.channel, f"✅ Aggiunto alla coda: {str(video)}")
|
await client.send_message(message.channel, f"✅ Aggiunto alla coda: {str(video)}")
|
||||||
voice_queue.append(video)
|
voice_queue.append(video)
|
||||||
elif message.content.startswith("!search"):
|
elif message.content.startswith("!search"):
|
||||||
|
@ -291,7 +306,7 @@ async def on_message(message: discord.Message):
|
||||||
"Sintassi corretta: `!search <titolo>`")
|
"Sintassi corretta: `!search <titolo>`")
|
||||||
return
|
return
|
||||||
# If target is a single video
|
# If target is a single video
|
||||||
video = await Video.init(user_id=message.author.id, ytdl_url=f"ytsearch:{text}")
|
video = await OldVideo.init(user_id=message.author.id, ytdl_url=f"ytsearch:{text}")
|
||||||
await client.send_message(message.channel, f"✅ Aggiunto alla coda: {str(video)}")
|
await client.send_message(message.channel, f"✅ Aggiunto alla coda: {str(video)}")
|
||||||
voice_queue.append(video)
|
voice_queue.append(video)
|
||||||
elif message.content.startswith("!file"):
|
elif message.content.startswith("!file"):
|
||||||
|
@ -309,7 +324,7 @@ async def on_message(message: discord.Message):
|
||||||
"Sintassi corretta: `!file <nomefile>`")
|
"Sintassi corretta: `!file <nomefile>`")
|
||||||
return
|
return
|
||||||
# If target is a single video
|
# If target is a single video
|
||||||
video = await Video.init(user_id=message.author.id, filename=text)
|
video = await OldVideo.init(user_id=message.author.id, filename=text)
|
||||||
await client.send_message(message.channel, f"✅ Aggiunto alla coda: {str(video)}")
|
await client.send_message(message.channel, f"✅ Aggiunto alla coda: {str(video)}")
|
||||||
voice_queue.append(video)
|
voice_queue.append(video)
|
||||||
elif message.content.startswith("!skip"):
|
elif message.content.startswith("!skip"):
|
||||||
|
@ -429,7 +444,7 @@ def requires_cv(func):
|
||||||
return new_func
|
return new_func
|
||||||
|
|
||||||
|
|
||||||
def requires_rygdb(func):
|
def requires_rygdb(func, optional=False):
|
||||||
async def new_func(channel: discord.Channel, author: discord.Member, params: typing.List[str], *args, **kwargs):
|
async def new_func(channel: discord.Channel, author: discord.Member, params: typing.List[str], *args, **kwargs):
|
||||||
session = await loop.run_in_executor(executor, db.Session)
|
session = await loop.run_in_executor(executor, db.Session)
|
||||||
dbuser = await loop.run_in_executor(executor,
|
dbuser = await loop.run_in_executor(executor,
|
||||||
|
@ -437,13 +452,17 @@ def requires_rygdb(func):
|
||||||
.filter_by(discord_id=author.id)
|
.filter_by(discord_id=author.id)
|
||||||
.join(db.Royal)
|
.join(db.Royal)
|
||||||
.first)
|
.first)
|
||||||
|
if not optional and dbuser is None:
|
||||||
|
await client.send_message(channel,
|
||||||
|
"⚠️ Devi essere registrato su Royalnet per poter utilizzare questo comando.")
|
||||||
|
return
|
||||||
await loop.run_in_executor(executor, session.close)
|
await loop.run_in_executor(executor, session.close)
|
||||||
return await func(channel=channel, author=author, params=params, dbuser=dbuser, *args, **kwargs)
|
return await func(channel=channel, author=author, params=params, dbuser=dbuser, *args, **kwargs)
|
||||||
return new_func
|
return new_func
|
||||||
|
|
||||||
|
|
||||||
@command
|
@command
|
||||||
async def ping(channel: discord.Channel, author: discord.Member, params: typing.List[str]):
|
async def cmd_ping(channel: discord.Channel, author: discord.Member, params: typing.List[str]):
|
||||||
await client.send_message(channel, f"Pong!")
|
await client.send_message(channel, f"Pong!")
|
||||||
|
|
||||||
|
|
||||||
|
@ -461,6 +480,29 @@ async def cmd_cv(channel: discord.Channel, author: discord.Member, params: typin
|
||||||
await client.send_message(channel, f"✅ Mi sono connesso in <#{author.voice.voice_channel.id}>.")
|
await client.send_message(channel, f"✅ Mi sono connesso in <#{author.voice.voice_channel.id}>.")
|
||||||
|
|
||||||
|
|
||||||
|
@command
|
||||||
|
@requires_cv
|
||||||
|
async def cmd_play(channel: discord.Channel, author: discord.Member, params: typing.List[str]):
|
||||||
|
if len(params) < 2:
|
||||||
|
await client.send_message(channel, "⚠ Non hai specificato una canzone da riprodurre!\n"
|
||||||
|
"Sintassi: `!play <url|ricercayoutube|nomefile>`")
|
||||||
|
return
|
||||||
|
# Parse the parameter as URL
|
||||||
|
url = re.match(r"(?:https?://|ytsearch[0-9]*:).*", params[1].strip("<>"))
|
||||||
|
if url.group(0) is not None:
|
||||||
|
# This is a url
|
||||||
|
return
|
||||||
|
# Parse the parameter as file
|
||||||
|
file_path = os.path.join(os.path.join(os.path.curdir, "opusfiles"), params[1])
|
||||||
|
if os.path.exists(file_path):
|
||||||
|
# This is a file
|
||||||
|
return
|
||||||
|
# Search the parameter on youtube
|
||||||
|
search = params[1]
|
||||||
|
# This is a search
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
async def update_music_queue():
|
async def update_music_queue():
|
||||||
await client.wait_until_ready()
|
await client.wait_until_ready()
|
||||||
global voice_client
|
global voice_client
|
||||||
|
|
|
@ -12,12 +12,27 @@ h1, h2, h3, h4, h5, h6 {
|
||||||
color: @accent-color;
|
color: @accent-color;
|
||||||
}
|
}
|
||||||
|
|
||||||
input {
|
input[type="text"], input[type="password"] {
|
||||||
background-color: rgba(red(@text-color), green(@text-color), blue(@text-color), 0.1);
|
background-color: rgba(red(@text-color), green(@text-color), blue(@text-color), 0.1);
|
||||||
color: @text-color;
|
color: @text-color;
|
||||||
border: none;
|
border: none;
|
||||||
border-bottom: 1px dashed @text-color;
|
border-bottom: 1px dashed @text-color;
|
||||||
padding: 2px;
|
padding: 2px;
|
||||||
|
margin: 1px;
|
||||||
font-size: medium;
|
font-size: medium;
|
||||||
font-family: sans-serif;
|
font-family: sans-serif;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.input-grid {
|
||||||
|
display: grid;
|
||||||
|
|
||||||
|
label {
|
||||||
|
margin: 3px;
|
||||||
|
grid-column: 1;
|
||||||
|
max-width: 120px;
|
||||||
|
}
|
||||||
|
|
||||||
|
input {
|
||||||
|
grid-column: 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -8,15 +8,15 @@
|
||||||
<h1>
|
<h1>
|
||||||
Login
|
Login
|
||||||
</h1>
|
</h1>
|
||||||
<form action="{{ url_for('page_loggedin') }}" method="POST">
|
<form class="input-grid" action="{{ url_for('page_loggedin') }}" method="POST">
|
||||||
<label id="label-username">
|
<label id="label-username" for="input-username">
|
||||||
Username
|
Username
|
||||||
|
</label>
|
||||||
<input id="input-username" name="username" type="text">
|
<input id="input-username" name="username" type="text">
|
||||||
</label>
|
<label id="label-password" for="input-password">
|
||||||
<label id="label-password">
|
|
||||||
Password
|
Password
|
||||||
<input id="input-password" name="password" type="password">
|
|
||||||
</label>
|
</label>
|
||||||
|
<input id="input-password" name="password" type="password">
|
||||||
<input type="submit" value="Login">
|
<input type="submit" value="Login">
|
||||||
</form>
|
</form>
|
||||||
{% endblock %}
|
{% endblock %}
|
12
webserver.py
12
webserver.py
|
@ -69,6 +69,12 @@ def page_loggedin():
|
||||||
db_session = db.Session()
|
db_session = db.Session()
|
||||||
user = db_session.query(db.Royal).filter_by(username=username).one_or_none()
|
user = db_session.query(db.Royal).filter_by(username=username).one_or_none()
|
||||||
db_session.close()
|
db_session.close()
|
||||||
|
if user is None:
|
||||||
|
abort(403)
|
||||||
|
return
|
||||||
|
if user.password is None:
|
||||||
|
fl_session["username"] = username
|
||||||
|
return redirect(url_for(page_password))
|
||||||
if bcrypt.checkpw(bytes(password, encoding="utf8"), user.password):
|
if bcrypt.checkpw(bytes(password, encoding="utf8"), user.password):
|
||||||
fl_session["username"] = username
|
fl_session["username"] = username
|
||||||
return username
|
return username
|
||||||
|
@ -86,11 +92,11 @@ def page_password():
|
||||||
return
|
return
|
||||||
return render_template("password.html")
|
return render_template("password.html")
|
||||||
elif request.method == "POST":
|
elif request.method == "POST":
|
||||||
old_password = request.form["old"]
|
old_password = request.form.get("old")
|
||||||
new_password = request.form["new"]
|
new_password = request.form["new"]
|
||||||
db_session = db.Session()
|
db_session = db.Session()
|
||||||
user = db_session.query(db.Royal).filter_by(username=username).one_or_none()
|
user = db_session.query(db.Royal).filter_by(username=username).one_or_none()
|
||||||
if bcrypt.checkpw(bytes(old_password, encoding="utf8"), user.password):
|
if user.password is None or bcrypt.checkpw(bytes(old_password, encoding="utf8"), user.password):
|
||||||
user.password = bcrypt.hashpw(bytes(new_password, encoding="utf8"), bcrypt.gensalt())
|
user.password = bcrypt.hashpw(bytes(new_password, encoding="utf8"), bcrypt.gensalt())
|
||||||
db_session.commit()
|
db_session.commit()
|
||||||
db_session.close()
|
db_session.close()
|
||||||
|
@ -101,6 +107,6 @@ def page_password():
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
app.run(host="0.0.0.0", port=1234)
|
app.run(host="0.0.0.0", port=1234, debug=__debug__)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Reference in a new issue