1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-24 03:54:20 +00:00

Probably get rid of interfaces in the discord serf

This commit is contained in:
Steffo 2020-08-05 01:34:03 +02:00
parent f33e781e04
commit 105f489a02

View file

@ -1,6 +1,5 @@
import asyncio as aio import asyncio as aio
import logging import logging
import warnings
import io import io
import sys import sys
from typing import * from typing import *
@ -19,6 +18,7 @@ log = logging.getLogger(__name__)
class DiscordSerf(Serf): class DiscordSerf(Serf):
"""A :class:`Serf` that connects to `Discord <https://discordapp.com/>`_ as a bot.""" """A :class:`Serf` that connects to `Discord <https://discordapp.com/>`_ as a bot."""
interface_name = "discord" interface_name = "discord"
prefix = "!"
_identity_table = rbt.Discord _identity_table = rbt.Discord
_identity_column = "discord_id" _identity_column = "discord_id"
@ -55,26 +55,14 @@ class DiscordSerf(Serf):
self.Data: Type[rc.CommandData] = self.data_factory() self.Data: Type[rc.CommandData] = self.data_factory()
def interface_factory(self) -> Type[rc.CommandInterface]:
# noinspection PyPep8Naming
GenericInterface = super().interface_factory()
# noinspection PyMethodParameters,PyAbstractClass
class DiscordInterface(GenericInterface):
name = self.interface_name
prefix = "!"
return DiscordInterface
def data_factory(self) -> Type[rc.CommandData]: def data_factory(self) -> Type[rc.CommandData]:
# noinspection PyMethodParameters,PyAbstractClass # noinspection PyMethodParameters,PyAbstractClass
class DiscordData(rc.CommandData): class DiscordData(rc.CommandData):
def __init__(data, def __init__(data,
interface: rc.CommandInterface, command: rc.Command,
loop: aio.AbstractEventLoop,
message: "discord.Message"): message: "discord.Message"):
super().__init__(interface=interface, loop=loop) super().__init__(command=command)
data.message = message data.message: "discord.Message" = message
async def reply(data, text: str): async def reply(data, text: str):
await data.message.channel.send(escape(text)) await data.message.channel.send(escape(text))
@ -130,7 +118,8 @@ class DiscordSerf(Serf):
else: else:
session = None session = None
# Prepare data # Prepare data
data = self.Data(interface=command.interface, loop=self.loop, message=message) # noinspection PyArgumentList
data = self.Data(command=command, message=message)
# Call the command # Call the command
await self.call(command, data, parameters) await self.call(command, data, parameters)
# Close the alchemy session # Close the alchemy session
@ -141,6 +130,7 @@ class DiscordSerf(Serf):
"""Create a custom class inheriting from :py:class:`discord.Client`.""" """Create a custom class inheriting from :py:class:`discord.Client`."""
# noinspection PyMethodParameters # noinspection PyMethodParameters
class DiscordClient(discord.Client): class DiscordClient(discord.Client):
# noinspection PyMethodMayBeStatic
async def on_message(cli, message: "discord.Message") -> None: async def on_message(cli, message: "discord.Message") -> None:
"""Handle messages received by passing them to the handle_message method of the bot.""" """Handle messages received by passing them to the handle_message method of the bot."""
# TODO: keep reference to these tasks somewhere # TODO: keep reference to these tasks somewhere
@ -151,6 +141,7 @@ class DiscordSerf(Serf):
log.debug("Discord client is ready!") log.debug("Discord client is ready!")
await cli.change_presence(status=discord.Status.online, activity=None) await cli.change_presence(status=discord.Status.online, activity=None)
# noinspection PyMethodMayBeStatic
async def on_resume(cli) -> None: async def on_resume(cli) -> None:
log.debug("Discord client resumed connection.") log.debug("Discord client resumed connection.")