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

67 lines
2.9 KiB
Python

import multiprocessing
import telegrambot
import discordbot
import redditbot
import statsupdater
import time
import logging
import coloredlogs
logging.getLogger().disabled = True
logger = logging.getLogger(__name__)
coloredlogs.install(level="DEBUG", logger=logger)
discord_telegram_pipe = multiprocessing.Pipe()
discord = multiprocessing.Process(target=discordbot.process, args=(discord_telegram_pipe[0],), daemon=True)
telegram = multiprocessing.Process(target=telegrambot.process, args=(discord_telegram_pipe[1],), daemon=True)
reddit = multiprocessing.Process(target=redditbot.process, daemon=True)
stats = multiprocessing.Process(target=statsupdater.process, daemon=True)
if __name__ == "__main__":
logger.info("Starting Discord Bot process...")
discord.start()
logger.info("Starting Telegram Bot process...")
telegram.start()
logger.info("Starting Reddit Bot process...")
reddit.start()
logger.info("Starting StatsUpdater process...")
stats.start()
try:
while True:
if discord.exitcode is not None:
logger.warning(f"Discord Bot exited with {discord.exitcode}")
del discord
logger.info("Restarting Discord Bot process...")
discord = multiprocessing.Process(target=discordbot.process, args=(discord_telegram_pipe[0],), daemon=True)
discord.start()
if telegram.exitcode is not None:
logger.warning(f"Telegram Bot exited with {telegram.exitcode}")
del telegram
telegram = multiprocessing.Process(target=telegrambot.process, args=(discord_telegram_pipe[1],), daemon=True)
logger.info("Restarting Telegram Bot process...")
telegram.start()
if reddit.exitcode is not None:
logger.warning(f"Reddit Bot exited with {reddit.exitcode}")
del reddit
reddit = multiprocessing.Process(target=redditbot.process, daemon=True)
logger.info("Restarting Reddit Bot process...")
reddit.start()
if stats.exitcode is not None:
logger.warning(f"StatsUpdater exited with {stats.exitcode}")
del stats
stats = multiprocessing.Process(target=statsupdater.process, daemon=True)
logger.info("Restarting StatsUpdater process...")
stats.start()
time.sleep(10)
except KeyboardInterrupt:
logger.info("Now stopping...")
logger.info("Asking Discord process to stop...")
discord_telegram_pipe[0].send("stop")
logger.info("Waiting for Discord Bot process to stop...")
discord.join()
logger.info("Waiting for Telegram Bot process to stop...")
telegram.join()
logger.info("Waiting for Reddit Bot process to stop...")
reddit.join()
logger.info("Waiting for StatsUpdater process to stop...")
stats.join()