From eaedfa25a9d141733c99c21ed9a3fa2e8d643f01 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 10 Apr 2019 18:16:47 +0200 Subject: [PATCH] Start work on the discord bot --- requirements.txt | 1 + royalnet/bots/discord.py | 68 +++++++++++++++++++++++++++++++++++++++ royalnet/bots/telegram.py | 3 +- 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 royalnet/bots/discord.py diff --git a/requirements.txt b/requirements.txt index 9bdb3618..58d949bb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ aiohttp>=3.5.4 sqlalchemy>=1.3.2 Markdown>=3.1 dateparser>=0.7.1 +discord.py>=1.0.1 diff --git a/royalnet/bots/discord.py b/royalnet/bots/discord.py new file mode 100644 index 00000000..f2905915 --- /dev/null +++ b/royalnet/bots/discord.py @@ -0,0 +1,68 @@ +import discord +import asyncio +import typing +import logging as _logging +from ..commands import NullCommand +from ..utils import asyncify, Call, Command, UnregisteredError +from ..network import RoyalnetLink, Message +from ..database import Alchemy, relationshiplinkchain + +loop = asyncio.get_event_loop() +log = _logging.getLogger(__name__) + + +async def todo(message: Message): + log.warning(f"Skipped {message} because handling isn't supported yet.") + + +class DiscordBot: + # noinspection PyMethodParameters + class DiscordClient(discord.Client): + pass + + def __init__(self, + api_key: str, + master_server_uri: str, + master_server_secret: str, + commands: typing.List[typing.Type[Command]], + database_uri: str, + master_table, + identity_table, + identity_column_name: str, + missing_command: typing.Type[Command] = NullCommand, + error_command: typing.Type[Command] = NullCommand): + self.bot = self.DiscordClient() + self.network: RoyalnetLink = RoyalnetLink(master_server_uri, master_server_secret, "discord", todo) + # Generate commands + self.commands = {} + required_tables = set() + for command in commands: + self.commands[f"/{command.command_name}"] = command + required_tables = required_tables.union(command.require_alchemy_tables) + # Generate the Alchemy database + self.alchemy = Alchemy(database_uri, required_tables) + self.master_table = self.alchemy.__getattribute__(master_table.__name__) + self.identity_table = self.alchemy.__getattribute__(identity_table.__name__) + self.identity_column = self.identity_table.__getattribute__(self.identity_table, identity_column_name) + self.identity_chain = relationshiplinkchain(self.master_table, self.identity_table) + + # noinspection PyMethodParameters + class DiscordCall(Call): + interface_name = "discord" + interface_obj = self + alchemy = self.alchemy + + async def reply(call, text: str): + raise NotImplementedError() + + async def net_request(call, message: Message, destination: str): + response = await self.network.request(message, destination) + return response + + async def get_author(self, error_if_none=False): + raise NotImplementedError() + + self.DiscordCall = DiscordCall + + async def run(self): + raise NotImplementedError() diff --git a/royalnet/bots/telegram.py b/royalnet/bots/telegram.py index c6e683b0..2cacaafa 100644 --- a/royalnet/bots/telegram.py +++ b/royalnet/bots/telegram.py @@ -12,9 +12,8 @@ loop = asyncio.get_event_loop() log = _logging.getLogger(__name__) -# noinspection PyUnusedLocal async def todo(message: Message): - pass + log.warning(f"Skipped {message} because handling isn't supported yet.") class TelegramBot: