mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-27 13:34:28 +00:00
Do some more stuff
This commit is contained in:
parent
b2d5039c76
commit
c2a04301ef
7 changed files with 58 additions and 67 deletions
|
@ -21,11 +21,20 @@ class SummonCommand(Command):
|
||||||
syntax = "[channelname]"
|
syntax = "[channelname]"
|
||||||
|
|
||||||
async def run(self, args: CommandArgs, data: CommandData) -> None:
|
async def run(self, args: CommandArgs, data: CommandData) -> None:
|
||||||
|
if self.interface.name == "discord":
|
||||||
|
msg: Optional["discord.Message"] = data.message
|
||||||
|
member: Optional["discord.Member"] = msg.author
|
||||||
|
guild: Optional["discord.Guild"] = msg.guild
|
||||||
|
else:
|
||||||
|
member = None
|
||||||
|
guild = None
|
||||||
try:
|
try:
|
||||||
await self.interface.call_herald_action("discord", "discordvoice", {
|
await self.interface.call_herald_event("discord", "discordvoice", {
|
||||||
"operation": "summon",
|
"operation": "summon",
|
||||||
"data": {
|
"data": {
|
||||||
"channel_name": args.joined()
|
"channel_name": args.joined(),
|
||||||
|
"member_id": member.id if member is not None else None,
|
||||||
|
"guild_id": guild.id if member is not None else None,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
@ -5,6 +5,7 @@ from royalnet.serf import Serf
|
||||||
from royalnet.serf.discord import DiscordSerf
|
from royalnet.serf.discord import DiscordSerf
|
||||||
from royalnet.bard import DiscordBard
|
from royalnet.bard import DiscordBard
|
||||||
from royalnet.bard.implementations import *
|
from royalnet.bard.implementations import *
|
||||||
|
import weakref
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import discord
|
import discord
|
||||||
|
@ -17,18 +18,18 @@ class DiscordvoiceEvent(Event):
|
||||||
|
|
||||||
def __init__(self, serf: Serf):
|
def __init__(self, serf: Serf):
|
||||||
super().__init__(serf)
|
super().__init__(serf)
|
||||||
self.bards: Dict["discord.Guild", DiscordBard] = {}
|
self.bards: weakref.WeakValueDictionary = weakref.WeakValueDictionary()
|
||||||
|
|
||||||
async def run(self, data: dict):
|
async def run(self,
|
||||||
|
operation: str,
|
||||||
|
data: dict):
|
||||||
if not isinstance(self.serf, DiscordSerf):
|
if not isinstance(self.serf, DiscordSerf):
|
||||||
raise ValueError("`discordvoice` event cannot run on other serfs.")
|
raise ValueError("`discordvoice` event cannot run on other serfs.")
|
||||||
|
|
||||||
operation = data["operation"]
|
|
||||||
|
|
||||||
if operation == "summon":
|
if operation == "summon":
|
||||||
channel_name: str = data["data"]["channel_name"]
|
channel_name: str = data["channel_name"]
|
||||||
member_id: int = data["data"].get("member_id")
|
member_id: int = data.get("member_id")
|
||||||
guild_id: int = data["data"].get("guild_id")
|
guild_id: int = data.get("guild_id")
|
||||||
client: discord.Client = self.serf.client
|
client: discord.Client = self.serf.client
|
||||||
|
|
||||||
# Get the guild, if it exists
|
# Get the guild, if it exists
|
||||||
|
@ -105,9 +106,13 @@ class DiscordvoiceEvent(Event):
|
||||||
raise CommandError("The bot is already connected in another channel.\n"
|
raise CommandError("The bot is already connected in another channel.\n"
|
||||||
" Please disconnect it before resummoning!")
|
" Please disconnect it before resummoning!")
|
||||||
|
|
||||||
|
# Create a new bard, if it doesn't already exist
|
||||||
|
# TODO: does this work? are the voice clients correctly disposed of?
|
||||||
|
self.bards[channel.guild] = DBQueue()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"connected": True
|
"connected": True
|
||||||
}
|
}
|
||||||
|
# TODO: play, skip, playmode, remove, something else?
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Invalid operation received: {operation}")
|
raise ValueError(f"Invalid operation received: {operation}")
|
||||||
|
|
|
@ -27,28 +27,18 @@ class CommandInterface:
|
||||||
A reference to a :class:`~royalnet.serf.telegram.TelegramSerf`."""
|
A reference to a :class:`~royalnet.serf.telegram.TelegramSerf`."""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def alchemy(self):
|
def alchemy(self) -> "Alchemy":
|
||||||
"""A shortcut for :attr:`serf.alchemy`."""
|
"""A shortcut for :attr:`serf.alchemy`."""
|
||||||
return self.serf.alchemy
|
return self.serf.alchemy
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def loop(self):
|
def loop(self) -> AbstractEventLoop:
|
||||||
"""A shortcut for :attr:`serf.loop`."""
|
"""A shortcut for :attr:`serf.loop`."""
|
||||||
return self.serf.loop
|
return self.serf.loop
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.command: Optional[Command] = None # Will be bound after the command has been created
|
self.command: Optional[Command] = None # Will be bound after the command has been created
|
||||||
|
|
||||||
def register_herald_action(self,
|
async def call_herald_event(self, destination: str, event_name: str, args: dict) -> dict:
|
||||||
event_name: str,
|
|
||||||
coroutine: Callable[[Any], Awaitable[dict]]):
|
|
||||||
# TODO: document this
|
# TODO: document this
|
||||||
raise UnsupportedError(f"{self.register_herald_action.__name__} is not supported on this platform")
|
raise UnsupportedError(f"{self.call_herald_event.__name__} is not supported on this platform")
|
||||||
|
|
||||||
def unregister_herald_action(self, event_name: str):
|
|
||||||
# TODO: document this
|
|
||||||
raise UnsupportedError(f"{self.unregister_herald_action.__name__} is not supported on this platform")
|
|
||||||
|
|
||||||
async def call_herald_action(self, destination: str, event_name: str, args: dict) -> dict:
|
|
||||||
# TODO: document this
|
|
||||||
raise UnsupportedError(f"{self.call_herald_action.__name__} is not supported on this platform")
|
|
||||||
|
|
|
@ -12,9 +12,9 @@ class Event:
|
||||||
tables: set = set()
|
tables: set = set()
|
||||||
"""A set of :mod:`royalnet.alchemy` tables that must exist for this event to work."""
|
"""A set of :mod:`royalnet.alchemy` tables that must exist for this event to work."""
|
||||||
|
|
||||||
def __init__(self, serf: Serf):
|
def __init__(self, serf: "Serf"):
|
||||||
"""Bind the event to a :class:`~royalnet.serf.Serf`."""
|
"""Bind the event to a :class:`~royalnet.serf.Serf`."""
|
||||||
self.serf: Serf = serf
|
self.serf: "Serf" = serf
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def alchemy(self):
|
def alchemy(self):
|
||||||
|
@ -26,5 +26,5 @@ class Event:
|
||||||
"""A shortcut for :attr:`.serf.loop`"""
|
"""A shortcut for :attr:`.serf.loop`"""
|
||||||
return self.serf.loop
|
return self.serf.loop
|
||||||
|
|
||||||
async def run(self, data: dict):
|
async def run(self, **kwargs):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
|
@ -33,14 +33,14 @@ class DiscordSerf(Serf):
|
||||||
def __init__(self, *,
|
def __init__(self, *,
|
||||||
alchemy_config: Optional[AlchemyConfig] = None,
|
alchemy_config: Optional[AlchemyConfig] = None,
|
||||||
commands: List[Type[Command]] = None,
|
commands: List[Type[Command]] = None,
|
||||||
network_config: Optional[HeraldConfig] = None,
|
herald_config: Optional[HeraldConfig] = None,
|
||||||
secrets_name: str = "__default__"):
|
secrets_name: str = "__default__"):
|
||||||
if discord is None:
|
if discord is None:
|
||||||
raise ImportError("'discord' extra is not installed")
|
raise ImportError("'discord' extra is not installed")
|
||||||
|
|
||||||
super().__init__(alchemy_config=alchemy_config,
|
super().__init__(alchemy_config=alchemy_config,
|
||||||
commands=commands,
|
commands=commands,
|
||||||
network_config=network_config,
|
herald_config=herald_config,
|
||||||
secrets_name=secrets_name)
|
secrets_name=secrets_name)
|
||||||
|
|
||||||
self.Client = self.client_factory()
|
self.Client = self.client_factory()
|
||||||
|
|
|
@ -47,7 +47,8 @@ class Serf:
|
||||||
def __init__(self, *,
|
def __init__(self, *,
|
||||||
alchemy_config: Optional[AlchemyConfig] = None,
|
alchemy_config: Optional[AlchemyConfig] = None,
|
||||||
commands: List[Type[Command]] = None,
|
commands: List[Type[Command]] = None,
|
||||||
network_config: Optional[HeraldConfig] = None,
|
events: List[Type[Event]] = None,
|
||||||
|
herald_config: Optional[HeraldConfig] = None,
|
||||||
secrets_name: str = "__default__"):
|
secrets_name: str = "__default__"):
|
||||||
self.secrets_name = secrets_name
|
self.secrets_name = secrets_name
|
||||||
|
|
||||||
|
@ -87,22 +88,21 @@ class Serf:
|
||||||
self.register_commands(commands)
|
self.register_commands(commands)
|
||||||
log.info(f"Commands: total {len(self.commands)}")
|
log.info(f"Commands: total {len(self.commands)}")
|
||||||
|
|
||||||
self.herald_handlers: Dict[str, Callable[["Serf", Any], Awaitable[Optional[dict]]]] = {}
|
|
||||||
"""A :class:`dict` linking :class:`Request` event names to coroutines returning a :class:`dict` that will be
|
|
||||||
sent as :class:`Response` to the event."""
|
|
||||||
|
|
||||||
self.herald: Optional[Link] = None
|
self.herald: Optional[Link] = None
|
||||||
"""The :class:`Link` object connecting the Serf to the rest of the herald network."""
|
"""The :class:`Link` object connecting the Serf to the rest of the herald network."""
|
||||||
|
|
||||||
self.herald_task: Optional[Task] = None
|
self.herald_task: Optional[Task] = None
|
||||||
"""A reference to the :class:`asyncio.Task` that runs the :class:`Link`."""
|
"""A reference to the :class:`asyncio.Task` that runs the :class:`Link`."""
|
||||||
|
|
||||||
|
self.events: Dict[str, Event] = {}
|
||||||
|
"""A dictionary containing all :class:`Event` that can be handled by this :class:`Serf`."""
|
||||||
|
|
||||||
if Link is None:
|
if Link is None:
|
||||||
log.info("Herald: not installed")
|
log.info("Herald: not installed")
|
||||||
elif network_config is None:
|
elif herald_config is None:
|
||||||
log.info("Herald: disabled")
|
log.info("Herald: disabled")
|
||||||
else:
|
else:
|
||||||
self.init_network(network_config)
|
self.init_herald(herald_config, events)
|
||||||
log.info(f"Herald: {self.herald}")
|
log.info(f"Herald: {self.herald}")
|
||||||
|
|
||||||
self.loop: Optional[AbstractEventLoop] = None
|
self.loop: Optional[AbstractEventLoop] = None
|
||||||
|
@ -148,22 +148,7 @@ class Serf:
|
||||||
alchemy: Alchemy = self.alchemy
|
alchemy: Alchemy = self.alchemy
|
||||||
serf: "Serf" = self
|
serf: "Serf" = self
|
||||||
|
|
||||||
def register_herald_action(ci,
|
async def call_herald_event(ci, destination: str, event_name: str, args: Dict) -> Dict:
|
||||||
event_name: str,
|
|
||||||
coroutine: Callable[[Any], Awaitable[Dict]]) -> None:
|
|
||||||
"""Allow a coroutine to be called when a :class:`royalherald.Request` is received."""
|
|
||||||
if self.herald is None:
|
|
||||||
raise UnsupportedError("`royalherald` is not enabled on this bot.")
|
|
||||||
self.herald_handlers[event_name] = coroutine
|
|
||||||
|
|
||||||
def unregister_herald_action(ci, event_name: str):
|
|
||||||
"""Disable a previously registered coroutine from being called on reception of a
|
|
||||||
:class:`royalherald.Request`."""
|
|
||||||
if self.herald is None:
|
|
||||||
raise UnsupportedError("`royalherald` is not enabled on this bot.")
|
|
||||||
del self.herald_handlers[event_name]
|
|
||||||
|
|
||||||
async def call_herald_action(ci, destination: str, event_name: str, args: Dict) -> Dict:
|
|
||||||
"""Send a :class:`royalherald.Request` to a specific destination, and wait for a
|
"""Send a :class:`royalherald.Request` to a specific destination, and wait for a
|
||||||
:class:`royalherald.Response`."""
|
:class:`royalherald.Response`."""
|
||||||
if self.herald is None:
|
if self.herald is None:
|
||||||
|
@ -228,34 +213,36 @@ class Serf:
|
||||||
else:
|
else:
|
||||||
log.warning(f"Ignoring (already defined): {SelectedCommand.__qualname__} -> {interface.prefix}{alias}")
|
log.warning(f"Ignoring (already defined): {SelectedCommand.__qualname__} -> {interface.prefix}{alias}")
|
||||||
|
|
||||||
def init_network(self, config: HeraldConfig):
|
def init_herald(self, config: HeraldConfig, events: List[Type[Event]]):
|
||||||
"""Create a :py:class:`Link`, and run it as a :py:class:`asyncio.Task`."""
|
"""Create a :py:class:`Link`, and run it as a :py:class:`asyncio.Task`."""
|
||||||
log.debug(f"Initializing herald...")
|
|
||||||
self.herald: Link = Link(config, self.network_handler)
|
self.herald: Link = Link(config, self.network_handler)
|
||||||
|
log.debug(f"Binding events...")
|
||||||
|
for SelectedEvent in events:
|
||||||
|
log.debug(f"Binding event: {SelectedEvent.name}.")
|
||||||
|
self.events[SelectedEvent.name] = SelectedEvent(self)
|
||||||
|
|
||||||
async def network_handler(self, message: Union[Request, Broadcast]) -> Response:
|
async def network_handler(self, message: Union[Request, Broadcast]) -> Response:
|
||||||
try:
|
try:
|
||||||
herald_handler = self.herald_handlers[message.handler]
|
event: Event = self.events[message.handler]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
log.warning(f"Missing network_handler for {message.handler}")
|
log.warning(f"No event for '{message.handler}'")
|
||||||
return ResponseFailure("no_handler", f"This bot is missing a network handler for {message.handler}.")
|
return ResponseFailure("no_event", f"This serf does not have any event for {message.handler}.")
|
||||||
else:
|
log.debug(f"Event called: {event.name}")
|
||||||
log.debug(f"Using {herald_handler} as handler for {message.handler}")
|
|
||||||
if isinstance(message, Request):
|
if isinstance(message, Request):
|
||||||
try:
|
try:
|
||||||
response_data = await herald_handler(self, **message.data)
|
response_data = await event.run(**message.data)
|
||||||
return ResponseSuccess(data=response_data)
|
return ResponseSuccess(data=response_data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
sentry_sdk.capture_exception(e)
|
sentry_sdk.capture_exception(e)
|
||||||
log.error(f"Exception {e} in {herald_handler}")
|
log.error(f"Event error: {e.__class__.__qualname__} in {event.name}")
|
||||||
return ResponseFailure("exception_in_handler",
|
return ResponseFailure("exception_in_event",
|
||||||
f"An exception was raised in {herald_handler} for {message.handler}.",
|
f"An exception was raised in the event for '{message.handler}'.",
|
||||||
extra_info={
|
extra_info={
|
||||||
"type": e.__class__.__name__,
|
"type": e.__class__.__qualname__,
|
||||||
"message": str(e)
|
"message": str(e)
|
||||||
})
|
})
|
||||||
elif isinstance(message, Broadcast):
|
elif isinstance(message, Broadcast):
|
||||||
await herald_handler(self, **message.data)
|
await event.run(**message.data)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def init_sentry(dsn):
|
def init_sentry(dsn):
|
||||||
|
|
|
@ -38,14 +38,14 @@ class TelegramSerf(Serf):
|
||||||
def __init__(self, *,
|
def __init__(self, *,
|
||||||
alchemy_config: Optional[AlchemyConfig] = None,
|
alchemy_config: Optional[AlchemyConfig] = None,
|
||||||
commands: List[Type[Command]] = None,
|
commands: List[Type[Command]] = None,
|
||||||
network_config: Optional[HeraldConfig] = None,
|
herald_config: Optional[HeraldConfig] = None,
|
||||||
secrets_name: str = "__default__"):
|
secrets_name: str = "__default__"):
|
||||||
if telegram is None:
|
if telegram is None:
|
||||||
raise ImportError("'telegram' extra is not installed")
|
raise ImportError("'telegram' extra is not installed")
|
||||||
|
|
||||||
super().__init__(alchemy_config=alchemy_config,
|
super().__init__(alchemy_config=alchemy_config,
|
||||||
commands=commands,
|
commands=commands,
|
||||||
network_config=network_config,
|
herald_config=herald_config,
|
||||||
secrets_name=secrets_name)
|
secrets_name=secrets_name)
|
||||||
|
|
||||||
self.client = telegram.Bot(self.get_secret("telegram"), request=TRequest(5, read_timeout=30))
|
self.client = telegram.Bot(self.get_secret("telegram"), request=TRequest(5, read_timeout=30))
|
||||||
|
|
Loading…
Reference in a new issue