import asyncio import configparser import logging import os from commands import Commands from extracommands import ExtraCommands from clients.discord import DiscordClient from clients.twitch import TwitchClient TIMEOUT = 0.2 def main(): config = configparser.ConfigParser() config.read('settings.cfg') level = logging.DEBUG if bool(int(os.getenv('DEBUG', 0))) else logging.INFO handler = logging.StreamHandler() handler.setFormatter(logging.Formatter('CMD: %(levelname)s: %(message)s')) commands_logger = logging.getLogger('commands') commands_logger.addHandler(handler) commands_logger.setLevel(level) handler = logging.StreamHandler() handler.setFormatter(logging.Formatter('DISCORD: %(levelname)s: %(message)s')) discord_logger = logging.getLogger('discord') discord_logger.addHandler(handler) discord_logger.setLevel(level) handler = logging.StreamHandler() handler.setFormatter(logging.Formatter('TWITCH: %(levelname)s: %(message)s')) twitch_logger = logging.getLogger('irc.client') twitch_logger.addHandler(handler) twitch_logger.setLevel(level) commands = Commands(config, commands_logger) extra_commands = ExtraCommands() async def monitor_extra_commands(): while True: try: extra_commands.reload_if_needed() except Exception as e: commands_logger.info('Exception', exc_info=e) await asyncio.sleep(TIMEOUT) async def run_twitch_client(): twitch_client = TwitchClient(config, twitch_logger, commands, extra_commands) twitch_client.connect_() while True: try: twitch_client.process_data() except Exception as e: twitch_logger.info('Exception', exc_info=e) await asyncio.sleep(TIMEOUT) async def run_discord_client(): while True: discord_client = DiscordClient(config, discord_logger, commands, extra_commands) try: await discord_client.start_() except Exception as e: discord_logger.info('Exception', exc_info=e) await discord_client.logout() loop = asyncio.get_event_loop() try: asyncio.ensure_future(monitor_extra_commands()) asyncio.ensure_future(run_twitch_client()) asyncio.ensure_future(run_discord_client()) loop.run_forever() finally: loop.close() if __name__ == "__main__": main()