From b6ffbef521427a0a85c34231d16682b7b924aab9 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 24 Aug 2019 00:00:42 +0200 Subject: [PATCH] Fix leftover bugs --- royalnet/bots/discord.py | 3 +- royalnet/bots/generic.py | 4 +-- royalnet/bots/telegram.py | 6 ++-- royalnet/commands/__init__.py | 3 +- royalnet/commands/royalgames/__init__.py | 34 +----------------- royalnet/royalgames.py | 20 +++-------- royalnet/shareradio.py | 46 ------------------------ royalnet/utils/__init__.py | 3 +- 8 files changed, 15 insertions(+), 104 deletions(-) delete mode 100644 royalnet/shareradio.py diff --git a/royalnet/bots/discord.py b/royalnet/bots/discord.py index cf13fe68..e5e4f27c 100644 --- a/royalnet/bots/discord.py +++ b/royalnet/bots/discord.py @@ -24,6 +24,7 @@ class DiscordConfig: class DiscordBot(GenericBot): """A bot that connects to `Discord `_.""" + interface_name = "discord" def _init_voice(self): """Initialize the variables needed for the connection to voice chat.""" @@ -36,7 +37,7 @@ class DiscordBot(GenericBot): # noinspection PyMethodParameters,PyAbstractClass class DiscordInterface(GenericInterface): - name = "discord" + name = self.interface_name prefix = "!" return DiscordInterface diff --git a/royalnet/bots/generic.py b/royalnet/bots/generic.py index a40923e8..1ed2ed7a 100644 --- a/royalnet/bots/generic.py +++ b/royalnet/bots/generic.py @@ -24,7 +24,7 @@ class GenericBot: self._Interface = self._interface_factory() self._Data = self._data_factory() self.commands = {} - for SelectedCommand in self.commands: + for SelectedCommand in commands: interface = self._Interface() self.commands[f"{interface.prefix}{SelectedCommand.name}"] = SelectedCommand(interface) self.network_handlers: typing.Dict[str, typing.Type[NetworkHandler]] = {} @@ -107,7 +107,7 @@ class GenericBot: """Create an :py:class:`royalnet.database.Alchemy` with the tables required by the commands. Then, find the chain that links the ``master_table`` to the ``identity_table``. """ log.debug(f"Initializing database") - required_tables = set() + required_tables = {database_config.master_table, database_config.identity_table} for command in commands: required_tables = required_tables.union(command.require_alchemy_tables) log.debug(f"Found {len(required_tables)} required tables") diff --git a/royalnet/bots/telegram.py b/royalnet/bots/telegram.py index ebe222fd..2247bea4 100644 --- a/royalnet/bots/telegram.py +++ b/royalnet/bots/telegram.py @@ -21,6 +21,7 @@ class TelegramConfig: class TelegramBot(GenericBot): """A bot that connects to `Telegram `_.""" + interface_name = "telegram" def _init_client(self): """Create the :py:class:`telegram.Bot`, and set the starting offset.""" @@ -35,7 +36,7 @@ class TelegramBot(GenericBot): # noinspection PyMethodParameters,PyAbstractClass class TelegramInterface(GenericInterface): - name = "telegram" + name = self.interface_name prefix = "/" return TelegramInterface @@ -99,7 +100,7 @@ class TelegramBot(GenericBot): command_text, *parameters = text.split(" ") command_name = command_text.replace(f"@{self.client.username}", "").lower() # Send a typing notification - self.client.send_chat_action(update.message.chat, telegram.ChatAction.TYPING) + update.message.chat.send_action(telegram.ChatAction.TYPING) # Find the command try: command = self.commands[command_name] @@ -125,4 +126,3 @@ class TelegramBot(GenericBot): self._offset = last_updates[-1].update_id + 1 except IndexError: pass - diff --git a/royalnet/commands/__init__.py b/royalnet/commands/__init__.py index 17b25642..726c0206 100644 --- a/royalnet/commands/__init__.py +++ b/royalnet/commands/__init__.py @@ -1,5 +1,6 @@ from .commandinterface import CommandInterface from .command import Command from .commanddata import CommandData +from .commandargs import CommandArgs -__all__ = ["CommandInterface", "Command", "CommandData"] +__all__ = ["CommandInterface", "Command", "CommandData", "CommandArgs"] diff --git a/royalnet/commands/royalgames/__init__.py b/royalnet/commands/royalgames/__init__.py index 33f07a4b..0b41e08e 100644 --- a/royalnet/commands/royalgames/__init__.py +++ b/royalnet/commands/royalgames/__init__.py @@ -3,38 +3,6 @@ These probably won't suit your needs, as they are tailored for the bots of the Royal Games gaming community, but they may be useful to develop new ones.""" -from .null import NullCommand from .ping import PingCommand -from .ship import ShipCommand -from .smecds import SmecdsCommand -from .ciaoruozi import CiaoruoziCommand -from .color import ColorCommand -from .sync import SyncCommand -from .diario import DiarioCommand -from .rage import RageCommand -from .dateparser import DateparserCommand -from .author import AuthorCommand -from .reminder import ReminderCommand -from .kvactive import KvactiveCommand -from .kv import KvCommand -from .kvroll import KvrollCommand -from .videoinfo import VideoinfoCommand -from .summon import SummonCommand -from .play import PlayCommand -from .skip import SkipCommand -from .playmode import PlaymodeCommand -from .videochannel import VideochannelCommand -from .missing import MissingCommand -from .cv import CvCommand -from .pause import PauseCommand -from .queue import QueueCommand -from .royalnetprofile import RoyalnetprofileCommand -from .id import IdCommand -from .dlmusic import DlmusicCommand - -__all__ = ["NullCommand", "PingCommand", "ShipCommand", "SmecdsCommand", "CiaoruoziCommand", "ColorCommand", - "SyncCommand", "DiarioCommand", "RageCommand", "DateparserCommand", "AuthorCommand", "ReminderCommand", - "KvactiveCommand", "KvCommand", "KvrollCommand", "VideoinfoCommand", "SummonCommand", "PlayCommand", - "SkipCommand", "PlaymodeCommand", "VideochannelCommand", "MissingCommand", "CvCommand", "PauseCommand", - "QueueCommand", "RoyalnetprofileCommand", "IdCommand", "DlmusicCommand"] +__all__ = ["PingCommand"] diff --git a/royalnet/royalgames.py b/royalnet/royalgames.py index 9ce17d8e..d0a2bb47 100644 --- a/royalnet/royalgames.py +++ b/royalnet/royalgames.py @@ -4,10 +4,7 @@ import os import asyncio import logging from royalnet.bots import DiscordBot, DiscordConfig, TelegramBot, TelegramConfig -from royalnet.commands import * -# noinspection PyUnresolvedReferences -from royalnet.commands import DebugCreateCommand -from royalnet.commands import ErrorHandlerCommand +from royalnet.commands.royalgames import * from royalnet.network import RoyalnetServer, RoyalnetConfig from royalnet.database import DatabaseConfig from royalnet.database.tables import Royal, Telegram, Discord @@ -19,15 +16,10 @@ stream_handler = logging.StreamHandler() stream_handler.formatter = logging.Formatter("{asctime}\t{name}\t{levelname}\t{message}", style="{") log.addHandler(stream_handler) -commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, SyncCommand, - DiarioCommand, RageCommand, ReminderCommand, KvactiveCommand, KvCommand, - KvrollCommand, SummonCommand, PlayCommand, SkipCommand, PlaymodeCommand, - VideochannelCommand, CvCommand, PauseCommand, QueueCommand, RoyalnetprofileCommand, VideoinfoCommand, - IdCommand, DlmusicCommand] +commands = [PingCommand] # noinspection PyUnreachableCode if __debug__: - commands = [DebugCreateCommand, DateparserCommand, AuthorCommand, *commands] log.setLevel(logging.DEBUG) else: log.setLevel(logging.INFO) @@ -42,15 +34,11 @@ print("Starting bots...") ds_bot = DiscordBot(discord_config=DiscordConfig(os.environ["DS_AK"]), royalnet_config=RoyalnetConfig(f"ws://{address}:{port}", os.environ["MASTER_KEY"]), database_config=DatabaseConfig(os.environ["DB_PATH"], Royal, Discord, "discord_id"), - commands=commands, - error_command=ErrorHandlerCommand, - missing_command=MissingCommand) + commands=commands) tg_bot = TelegramBot(telegram_config=TelegramConfig(os.environ["TG_AK"]), royalnet_config=RoyalnetConfig(f"ws://{address}:{port}", os.environ["MASTER_KEY"]), database_config=DatabaseConfig(os.environ["DB_PATH"], Royal, Telegram, "tg_id"), - commands=commands, - error_command=ErrorHandlerCommand, - missing_command=MissingCommand) + commands=commands) loop.create_task(tg_bot.run()) loop.create_task(ds_bot.run()) diff --git a/royalnet/shareradio.py b/royalnet/shareradio.py deleted file mode 100644 index fd1d0c16..00000000 --- a/royalnet/shareradio.py +++ /dev/null @@ -1,46 +0,0 @@ -"""The production Royalnet, active at @royalgamesbot on Telegram and Royalbot on Discord.""" - -import os -import asyncio -import logging -from royalnet.bots import DiscordBot, DiscordConfig, TelegramBot, TelegramConfig -from royalnet.commands import * -# noinspection PyUnresolvedReferences -from royalnet.commands import DebugCreateCommand -from royalnet.commands import ErrorHandlerCommand -from royalnet.network import RoyalnetServer, RoyalnetConfig - -loop = asyncio.get_event_loop() - -log = logging.root -stream_handler = logging.StreamHandler() -stream_handler.formatter = logging.Formatter("{asctime}\t{name}\t{levelname}\t{message}", style="{") -log.addHandler(stream_handler) -log.setLevel(logging.INFO) - -commands = [PingCommand, SummonCommand, PlayCommand, SkipCommand, PlaymodeCommand, PauseCommand, QueueCommand] - -address, port = "127.0.0.1", 1234 - -print("Starting master...") -master = RoyalnetServer(address, port, os.environ["MASTER_KEY"]) -loop.run_until_complete(master.start()) - -print("Starting bots...") -ds_bot = DiscordBot(discord_config=DiscordConfig(os.environ["DS_AK"]), - royalnet_config=RoyalnetConfig(f"ws://{address}:{port}", os.environ["MASTER_KEY"]), - database_config=None, - commands=commands, - error_command=ErrorHandlerCommand, - missing_command=MissingCommand) -tg_bot = TelegramBot(telegram_config=TelegramConfig(os.environ["TG_AK"]), - royalnet_config=RoyalnetConfig(f"ws://{address}:{port}", os.environ["MASTER_KEY"]), - database_config=None, - commands=commands, - error_command=ErrorHandlerCommand, - missing_command=MissingCommand) -loop.create_task(tg_bot.run()) -loop.create_task(ds_bot.run()) - -print("Running loop...") -loop.run_forever() diff --git a/royalnet/utils/__init__.py b/royalnet/utils/__init__.py index a7b6dc40..7455eacc 100644 --- a/royalnet/utils/__init__.py +++ b/royalnet/utils/__init__.py @@ -2,13 +2,12 @@ from .asyncify import asyncify from .escaping import telegram_escape, discord_escape -from .commandargs import CommandArgs from .safeformat import safeformat from .classdictjanitor import cdj from .sleepuntil import sleep_until from .networkhandler import NetworkHandler from .formatters import andformat, plusformat, fileformat, ytdldateformat, numberemojiformat -__all__ = ["asyncify", "safeformat", "cdj", "sleep_until", "plusformat", "CommandArgs", +__all__ = ["asyncify", "safeformat", "cdj", "sleep_until", "plusformat", "NetworkHandler", "andformat", "plusformat", "fileformat", "ytdldateformat", "numberemojiformat", "telegram_escape", "discord_escape"]