1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-12-17 23:24:20 +00:00
royalnet/royalpack/commands/lazyplay.py

63 lines
2.1 KiB
Python
Raw Normal View History

2020-05-10 22:46:12 +00:00
from typing import *
2020-03-29 19:19:58 +00:00
import discord
import asyncio as aio
2020-05-10 22:46:12 +00:00
import royalnet.commands as rc
import royalnet.backpack.tables as rbt
2020-03-29 19:19:58 +00:00
2020-05-10 22:46:12 +00:00
class LazyplayCommand(rc.Command):
2020-03-29 19:19:58 +00:00
name: str = "lazyplay"
aliases = ["lp"]
description: str = "Aggiunge un url alla coda della chat vocale, ma lo scarica solo quando sta per essere" \
" riprodotto."
syntax = "{url}"
2020-05-10 22:46:12 +00:00
async def get_urls(self, args: rc.CommandArgs):
2020-03-29 19:19:58 +00:00
url = args.joined(require_at_least=1)
if not (url.startswith("http://") or url.startswith("https://")):
2020-05-10 22:46:12 +00:00
raise rc.InvalidInputError(f"L'URL specificato non inizia con il nome di un protocollo supportato"
2020-03-29 19:19:58 +00:00
f" ([c]http://[/c] o [c]https://[/c]).")
return [url]
def get_embed_color(self) -> Optional[int]:
return None
2020-05-10 22:46:12 +00:00
async def run(self, args: rc.CommandArgs, data: rc.CommandData) -> None:
2020-03-29 19:19:58 +00:00
if self.interface.name == "discord":
message: discord.Message = data.message
guild: discord.Guild = message.guild
if guild is None:
guild_id = None
else:
guild_id: Optional[int] = guild.id
else:
guild_id = None
2020-05-10 22:46:12 +00:00
user: rbt.User = await data.get_author()
2020-03-29 19:19:58 +00:00
user_str = None
if user is not None:
try:
2020-05-10 22:46:12 +00:00
user_discord: rbt.Discord = user.discord[0]
2020-03-29 19:19:58 +00:00
except (AttributeError, IndexError):
user_str = str(user)
else:
user_str = str(f"<@{user_discord.discord_id}>")
urls = await self.get_urls(args)
play_task: aio.Task = self.loop.create_task(
self.interface.call_herald_event("discord", "discord_lazy_play",
urls=urls,
guild_id=guild_id,
user=user_str,
force_color=self.get_embed_color())
)
await data.reply("⌛ Attendi un attimo...")
await play_task