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

121 lines
3.5 KiB
Python
Raw Normal View History

2021-04-26 02:39:14 +00:00
"""
The PDA ("main" class) for the :mod:`royalnet_telethon` frontend.
"""
from __future__ import annotations
2021-04-26 02:39:14 +00:00
import enum
import logging
import discord
2021-04-26 02:39:14 +00:00
import discord as d
import royalnet.engineer as engi
import royalnet.royaltyping as t
2021-04-26 02:39:14 +00:00
from .bullet.projectiles import DiscordMessageReceived, DiscordMessageEdited, DiscordMessageDeleted
log = logging.getLogger(__name__)
class DiscordpyPDAMode(enum.Enum):
"""
.. todo:: Document this.
"""
GLOBAL = enum.auto()
CHANNEL = enum.auto()
USER = enum.auto()
CHANNEL_USER = enum.auto()
class DiscordpyPDAImplementation(engi.ConversationListImplementation):
"""
.. todo:: Document this.
"""
@property
def namespace(self):
return "discordpy"
def __init__(self, name: str, bot_token: str,
2021-04-29 23:19:32 +00:00
mode: DiscordpyPDAMode = DiscordpyPDAMode.CHANNEL_USER,
intents: discord.Intents = discord.Intents.default()):
2021-04-26 02:39:14 +00:00
super().__init__(name=name)
self.mode: DiscordpyPDAMode = mode
"""
The mode to use for mapping dispensers.
"""
self.bot_token: str = bot_token
"""
.. todo:: Document this.
"""
# noinspection PyMethodParameters
class CustomClient(d.Client):
async def on_ready(cli):
log.debug("CustomClient is ready!")
async def on_error(self, event_method, *args, **kwargs):
log.error(f"An error occoured in CustomClient: {event_method!r} {args!r} {kwargs!r}")
async def on_message(cli, message: d.Message):
log.debug("Triggered on_message, putting in dispenser...")
await self.put(
key=self._determine_key(message=message),
projectile=DiscordMessageReceived(event=message)
)
2021-04-26 02:39:14 +00:00
async def on_message_edit(cli, message: d.Message):
log.debug("Triggered on_message_edit, putting in dispenser...")
2021-04-27 15:45:11 +00:00
await self.put(
key=self._determine_key(message=message),
projectile=DiscordMessageEdited(event=message)
)
async def on_message_delete(cli, message: d.Message):
log.debug("Triggered on_message_delete, putting in dispenser...")
await self.put(
key=self._determine_key(message=message),
projectile=DiscordMessageDeleted(event=message)
)
2021-04-29 23:19:32 +00:00
self.client: d.Client = CustomClient(intents=intents)
2021-04-26 02:39:14 +00:00
"""
.. todo:: Document this.
2021-04-26 02:39:14 +00:00
"""
def _determine_key(self, message: d.Message):
"""
.. todo:: Document this.
"""
if self.mode == DiscordpyPDAMode.GLOBAL:
return None
elif self.mode == DiscordpyPDAMode.USER:
author: d.User = message.author
return author.id
elif self.mode == DiscordpyPDAMode.CHANNEL:
channel: t.Union[d.DMChannel, d.TextChannel] = message.channel
return channel.id
elif self.mode == DiscordpyPDAMode.CHANNEL_USER:
author: d.User = message.author
channel: t.Union[d.DMChannel, d.TextChannel] = message.channel
return author.id, channel.id
else:
raise TypeError("Invalid mode")
async def run(self) -> t.NoReturn:
await self.client.start(self.bot_token, bot=True, reconnect=True)
2021-04-26 02:39:14 +00:00
__all__ = (
"DiscordpyPDAMode",
2021-04-26 02:39:14 +00:00
"DiscordpyPDAImplementation",
)