1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2025-02-17 10:53:57 +00:00
royalnet/royalpack/commands/play.py

65 lines
2.1 KiB
Python
Raw Normal View History

2020-05-11 00:46:12 +02:00
from typing import *
2019-11-11 09:56:08 +01:00
import discord
2020-01-30 20:08:31 +01:00
import asyncio as aio
2020-05-11 00:46:12 +02:00
import royalnet.commands as rc
import royalnet.backpack.tables as rbt
2019-11-11 09:56:08 +01:00
2020-05-11 00:46:12 +02:00
class PlayCommand(rc.Command):
2019-11-11 09:56:08 +01:00
name: str = "play"
aliases = ["p"]
description: str = "Aggiunge un url alla coda della chat vocale."
2019-12-01 23:53:47 +01:00
syntax = "{url}"
2019-11-11 09:56:08 +01:00
2020-05-11 00:46:12 +02:00
async def get_urls(self, args: rc.CommandArgs):
2020-01-30 20:08:31 +01:00
url = args.joined(require_at_least=1)
if not (url.startswith("http://") or url.startswith("https://")):
2020-05-11 00:46:12 +02:00
raise rc.InvalidInputError(f"L'URL specificato non inizia con il nome di un protocollo supportato"
2020-06-09 02:09:08 +02:00
f" ([c]http://[/c] o [c]https://[/c]).")
2020-03-28 19:38:14 +01:00
return [url]
2020-01-30 20:08:31 +01:00
def get_embed_color(self) -> Optional[int]:
return None
2020-05-11 00:46:12 +02:00
async def run(self, args: rc.CommandArgs, data: rc.CommandData) -> None:
2019-12-01 23:53:47 +01: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
2019-12-01 23:53:47 +01:00
else:
guild_id = None
2020-01-13 14:51:06 +01:00
2020-05-11 00:46:12 +02:00
user: rbt.User = await data.get_author()
2020-01-13 14:51:06 +01:00
user_str = None
if user is not None:
try:
2020-05-11 00:46:12 +02:00
user_discord: rbt.Discord = user.discord[0]
2020-01-13 14:51:06 +01:00
except (AttributeError, IndexError):
user_str = str(user)
2019-11-11 09:56:08 +01:00
else:
2020-01-13 14:51:06 +01:00
user_str = str(f"<@{user_discord.discord_id}>")
2020-03-28 19:38:14 +01:00
urls = await self.get_urls(args)
2020-01-30 20:08:31 +01:00
play_task: aio.Task = self.loop.create_task(
self.interface.call_herald_event("discord", "discord_play",
2020-03-28 19:38:14 +01:00
urls=urls,
2020-01-30 20:08:31 +01:00
guild_id=guild_id,
user=user_str,
force_color=self.get_embed_color())
)
2020-03-28 19:38:14 +01:00
if len(urls) > 1:
await data.reply("⌛ Attendi qualche minuto...")
else:
await data.reply("⌛ Attendi un attimo...")
2020-01-30 20:08:31 +01:00
await play_task