1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

All main commands done

This commit is contained in:
Steffo 2019-12-03 01:57:21 +01:00
parent 5603f40e9f
commit 332dea5206
8 changed files with 161 additions and 125 deletions

View file

@ -10,10 +10,10 @@ from .smecds import SmecdsCommand
from .videochannel import VideochannelCommand from .videochannel import VideochannelCommand
# from .trivia import TriviaCommand # from .trivia import TriviaCommand
# from .matchmaking import MatchmakingCommand # from .matchmaking import MatchmakingCommand
# from .pause import PauseCommand from .pause import PauseCommand
from .play import PlayCommand from .play import PlayCommand
# from .playmode import PlaymodeCommand # from .playmode import PlaymodeCommand
# from .queue import QueueCommand from .queue import QueueCommand
from .skip import SkipCommand from .skip import SkipCommand
from .summon import SummonCommand from .summon import SummonCommand
from .youtube import YoutubeCommand from .youtube import YoutubeCommand
@ -40,10 +40,10 @@ available_commands = [
VideochannelCommand, VideochannelCommand,
# TriviaCommand, # TriviaCommand,
# MatchmakingCommand, # MatchmakingCommand,
# PauseCommand, PauseCommand,
PlayCommand, PlayCommand,
# PlaymodeCommand, # PlaymodeCommand,
# QueueCommand, QueueCommand,
SkipCommand, SkipCommand,
SummonCommand, SummonCommand,
YoutubeCommand, YoutubeCommand,

View file

@ -11,3 +11,5 @@ class GooglevideoCommand(PlayCommand):
syntax = "{ricerca}" syntax = "{ricerca}"
_URL_FORMAT = "gvsearch:{url}" _URL_FORMAT = "gvsearch:{url}"
# Too bad gvsearch: always finds nothing.

View file

@ -1,53 +1,27 @@
import typing
import discord import discord
from typing import *
from royalnet.commands import * from royalnet.commands import *
from royalnet.bots import DiscordBot
class PauseCommand(Command): class PauseCommand(Command):
name: str = "pause" name: str = "pause"
description: str = "Mette in pausa o riprende la riproduzione della canzone attuale." aliases = ["resume"]
syntax = "[ [guild] ]" description: str = "Metti in pausa o riprendi la riproduzione di un file."
@staticmethod
async def _legacy_pause_handler(bot: DiscordBot, guild_name: typing.Optional[str]):
# Find the matching guild
if guild_name:
guilds: typing.List[discord.Guild] = bot.client.find_guild_by_name(guild_name)
else:
guilds = bot.client.guilds
if len(guilds) == 0:
raise CommandError("No guilds with the specified name found.")
if len(guilds) > 1:
raise CommandError("Multiple guilds with the specified name found.")
guild = list(bot.client.guilds)[0]
# Set the currently playing source as ended
voice_client: discord.VoiceClient = bot.client.find_voice_client_by_guild(guild)
if not (voice_client.is_playing() or voice_client.is_paused()):
raise CommandError("There is nothing to pause.")
# Toggle pause
resume = voice_client._player.is_paused()
if resume:
voice_client._player.resume()
else:
voice_client._player.pause()
return {"resumed": resume}
_event_name = "_legacy_pause"
def __init__(self, interface: CommandInterface):
super().__init__(interface)
if interface.name == "discord":
interface.register_herald_action(self._event_name, self._legacy_pause_handler)
async def run(self, args: CommandArgs, data: CommandData) -> None: async def run(self, args: CommandArgs, data: CommandData) -> None:
guild_name, = args.match(r"(?:\[(.+)])?") if self.interface.name == "discord":
response = await self.interface.call_herald_action("discord", self._event_name, { message: discord.Message = data.message
"guild_name": guild_name guild: discord.Guild = message.guild
}) guild_id: Optional[int] = guild.id
if response["resumed"]:
await data.reply(f"▶️ Riproduzione ripresa.")
else: else:
await data.reply(f"⏸ Riproduzione messa in pausa.") guild_id = None
response: Dict[str, Any] = await self.interface.call_herald_event("discord", "discord_pause",
guild_id=guild_id)
if response["action"] == "paused":
await data.reply("⏸ Riproduzione messa in pausa.")
elif response["action"] == "resumed":
await data.reply("▶️ Riproduzione ripresa!")

View file

@ -1,9 +1,9 @@
import typing
import pickle import pickle
import base64
import discord import discord
from typing import *
from royalnet.commands import * from royalnet.commands import *
from royalnet.utils import numberemojiformat from royalnet.utils import *
from royalnet.bots import DiscordBot
class QueueCommand(Command): class QueueCommand(Command):
@ -11,83 +11,52 @@ class QueueCommand(Command):
aliases = ["q"] aliases = ["q"]
description: str = "Visualizza la coda di riproduzione attuale." description: str = "Visualizza la coda di riproduzione attuale.."
syntax = "[ [guild] ]"
@staticmethod
async def _legacy_queue_handler(bot: "DiscordBot", guild_name: typing.Optional[str]):
# Find the matching guild
if guild_name:
guilds: typing.List[discord.Guild] = bot.client.find_guild_by_name(guild_name)
else:
guilds = bot.client.guilds
if len(guilds) == 0:
raise CommandError("No guilds with the specified name found.")
if len(guilds) > 1:
raise CommandError("Multiple guilds with the specified name found.")
guild = list(bot.client.guilds)[0]
# Check if the guild has a PlayMode
playmode = bot.music_data.get(guild)
if not playmode:
return {
"type": None
}
try:
queue = playmode.queue_preview()
except NotImplementedError:
return {
"type": playmode.__class__.__name__
}
return {
"type": playmode.__class__.__name__,
"queue":
{
"strings": [str(dfile.info) for dfile in queue],
"pickled_embeds": str(pickle.dumps([dfile.info.to_discord_embed() for dfile in queue]))
}
}
_event_name = "_legacy_queue"
def __init__(self, interface: CommandInterface):
super().__init__(interface)
if interface.name == "discord":
interface.register_herald_action(self._event_name, self._legacy_queue_handler)
async def run(self, args: CommandArgs, data: CommandData) -> None: async def run(self, args: CommandArgs, data: CommandData) -> None:
guild_name, = args.match(r"(?:\[(.+)])?")
response = await self.interface.call_herald_action("discord", self._event_name, {"guild_name": guild_name})
if response["type"] is None:
await data.reply(" Non c'è nessuna coda di riproduzione attiva al momento.")
return
elif "queue" not in response:
await data.reply(f" La coda di riproduzione attuale ([c]{response['type']}[/c]) non permette l'anteprima.")
return
if response["type"] == "Playlist":
if len(response["queue"]["strings"]) == 0:
message = f" Questa [c]Playlist[/c] è vuota."
else:
message = f" Questa [c]Playlist[/c] contiene {len(response['queue']['strings'])} elementi, e i prossimi saranno:\n"
elif response["type"] == "Pool":
if len(response["queue"]["strings"]) == 0:
message = f" Questo [c]Pool[/c] è vuoto."
else:
message = f" Questo [c]Pool[/c] contiene {len(response['queue']['strings'])} elementi, tra cui:\n"
elif response["type"] == "Layers":
if len(response["queue"]["strings"]) == 0:
message = f" Nessun elemento è attualmente in riproduzione, pertanto non ci sono [c]Layers[/c]:"
else:
message = f" I [c]Layers[/c] dell'elemento attualmente in riproduzione sono {len(response['queue']['strings'])}, tra cui:\n"
else:
if len(response["queue"]["strings"]) == 0:
message = f" Il PlayMode attuale, [c]{response['type']}[/c], è vuoto.\n"
else:
message = f" Il PlayMode attuale, [c]{response['type']}[/c], contiene {len(response['queue']['strings'])} elementi:\n"
if self.interface.name == "discord": if self.interface.name == "discord":
await data.reply(message) message: discord.Message = data.message
for embed in pickle.loads(eval(response["queue"]["pickled_embeds"]))[:5]: guild: discord.Guild = message.guild
await data.message.channel.send(embed=embed) guild_id: Optional[int] = guild.id
else: else:
message += numberemojiformat(response["queue"]["strings"][:10]) guild_id = None
await data.reply(message) response: Dict[str, Any] = await self.interface.call_herald_event("discord", "discord_queue",
guild_id=guild_id)
queue_type = response["type"]
if queue_type == "PlayableYTDQueue":
next_up = response["next_up"]
now_playing = response["now_playing"]
await data.reply(f" La coda contiene {len(next_up)} file.\n\n")
if now_playing is not None:
reply = f"Attualmente, sta venendo riprodotto:\n"
if self.interface.name == "discord":
await data.reply(reply)
embed = pickle.loads(base64.b64decode(bytes(now_playing["stringified_base64_pickled_discord_embed"],
encoding="ascii")))
# noinspection PyUnboundLocalVariable
await message.channel.send(embed=embed)
else:
reply += f"▶️ {now_playing['title']}\n\n"
await data.reply(reply)
else:
await data.reply("⏹ Attualmente, non sta venendo riprodotto nulla.")
reply = ""
if len(next_up) >= 1:
reply += "I prossimi file in coda sono:\n"
if self.interface.name == "discord":
await data.reply(reply)
for item in next_up[:5]:
embed = pickle.loads(base64.b64decode(bytes(item["stringified_base64_pickled_discord_embed"],
encoding="ascii")))
# noinspection PyUnboundLocalVariable
await message.channel.send(embed=embed)
else:
reply += numberemojiformat([a["title"] for a in next_up[:5]])
await data.reply(reply)
else:
await data.reply(" Non ci sono altri file in coda.")
else:
raise CommandError(f"Non so come visualizzare il contenuto di un [c]{queue_type}[/c].")

View file

@ -11,3 +11,5 @@ class YahoovideoCommand(PlayCommand):
syntax = "{ricerca}" syntax = "{ricerca}"
_URL_FORMAT = "yvsearch:{url}" _URL_FORMAT = "yvsearch:{url}"
# Too bad yvsearch: always finds nothing.

View file

@ -3,6 +3,8 @@ from .discord_cv import DiscordCvEvent
from .discord_summon import DiscordSummonEvent from .discord_summon import DiscordSummonEvent
from .discord_play import DiscordPlayEvent from .discord_play import DiscordPlayEvent
from .discord_skip import DiscordSkipEvent from .discord_skip import DiscordSkipEvent
from .discord_queue import DiscordQueueEvent
from .discord_pause import DiscordPauseEvent
# Enter the commands of your Pack here! # Enter the commands of your Pack here!
available_events = [ available_events = [
@ -10,6 +12,8 @@ available_events = [
DiscordSummonEvent, DiscordSummonEvent,
DiscordPlayEvent, DiscordPlayEvent,
DiscordSkipEvent, DiscordSkipEvent,
DiscordQueueEvent,
DiscordPauseEvent,
] ]
# Don't change this, it should automatically generate __all__ # Don't change this, it should automatically generate __all__

View file

@ -0,0 +1,36 @@
import discord
from typing import *
from royalnet.commands import *
from royalnet.serf.discord import *
class DiscordPauseEvent(Event):
name = "discord_pause"
async def run(self,
guild_id: Optional[int] = None,
**kwargs) -> dict:
if not isinstance(self.serf, DiscordSerf):
raise UnsupportedError()
client: discord.Client = self.serf.client
if len(self.serf.voice_players) == 1:
voice_player: VoicePlayer = self.serf.voice_players[0]
else:
if guild_id is None:
# TODO: trovare un modo per riprodurre canzoni su più server da Telegram
raise InvalidInputError("Non so in che Server riprodurre questo file...\n"
"Invia il comando su Discord, per favore!")
guild: discord.Guild = client.get_guild(guild_id)
if guild is None:
raise InvalidInputError("Impossibile trovare il Server specificato.")
voice_player: VoicePlayer = self.serf.find_voice_player(guild)
if voice_player is None:
raise UserError("Il bot non è in nessun canale vocale.\n"
"Evocalo prima con [c]summon[/c]!")
if voice_player.voice_client.is_paused():
voice_player.voice_client.resume()
return {"action": "resumed"}
else:
voice_player.voice_client.pause()
return {"action": "paused"}

View file

@ -0,0 +1,49 @@
import discord
import pickle
import base64
from typing import *
from royalnet.commands import *
from royalnet.serf.discord import *
class DiscordQueueEvent(Event):
name = "discord_queue"
async def run(self,
guild_id: Optional[int] = None,
**kwargs) -> dict:
if not isinstance(self.serf, DiscordSerf):
raise UnsupportedError()
client: discord.Client = self.serf.client
if len(self.serf.voice_players) == 1:
voice_player: VoicePlayer = self.serf.voice_players[0]
else:
if guild_id is None:
# TODO: trovare un modo per riprodurre canzoni su più server da Telegram
raise InvalidInputError("Non so in che Server riprodurre questo file...\n"
"Invia il comando su Discord, per favore!")
guild: discord.Guild = client.get_guild(guild_id)
if guild is None:
raise InvalidInputError("Impossibile trovare il Server specificato.")
voice_player: VoicePlayer = self.serf.find_voice_player(guild)
if voice_player is None:
raise UserError("Il bot non è in nessun canale vocale.\n"
"Evocalo prima con [c]summon[/c]!")
if isinstance(voice_player.playing, PlayableYTDQueue):
now_playing = voice_player.playing.now_playing
return {
"type": f"{voice_player.playing.__class__.__qualname__}",
"now_playing": {
"title": now_playing.info.title,
"stringified_base64_pickled_discord_embed": str(base64.b64encode(pickle.dumps(now_playing.embed())),
encoding="ascii")
} if now_playing is not None else None,
"next_up": [{
"title": ytd.info.title,
"stringified_base64_pickled_discord_embed": str(base64.b64encode(pickle.dumps(ytd.embed())),
encoding="ascii")
} for ytd in voice_player.playing.contents]
}
else:
raise CommandError(f"Non so come visualizzare il contenuto di un "
f"[c]{voice_player.playing.__class__.__qualname__}[/c].")