diff --git a/requirements.txt b/requirements.txt index 826c927..1575c7f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ Flask==2.2.2 pyjwt==2.6.0 Flask-SocketIO==5.3.2 SQLAlchemy==1.4.20 -pymysql==1.0.2 \ No newline at end of file +pymysql==1.0.2 +discord.py==2.1.0 diff --git a/truthseeker/__init__.py b/truthseeker/__init__.py index 14cbd3a..7f3c9de 100644 --- a/truthseeker/__init__.py +++ b/truthseeker/__init__.py @@ -2,6 +2,8 @@ import flask from flask_socketio import SocketIO import os +from truthseeker import discord_bot + class TruthSeekerApp(flask.Flask): def __init__(self): @@ -10,8 +12,17 @@ class TruthSeekerApp(flask.Flask): self.games_list = {} self.set_app_secret() + self.socketio_app = SocketIO(self) + self.discord_bot = discord_bot.DiscordBot() + token = self.get_discord_bot_token() + if token: + pass + self.discord_bot.start(token) + else: + print("No token set. Not starting discord bot") + def run_app(self): self.socketio_app.run(self) @@ -30,6 +41,14 @@ class TruthSeekerApp(flask.Flask): f.close() print("Generated secret and wrote to secret.txt !") + def get_discord_bot_token(self): + if os.path.isfile("instance/discord_bot_token.txt"): + f = open("instance/discord_bot_token.txt", "r") + token = f.read() + f.close() + return token + return None + APP = TruthSeekerApp() from truthseeker.routes import routes_api, routes_ui, routes_socketio diff --git a/truthseeker/discord_bot.py b/truthseeker/discord_bot.py new file mode 100644 index 0000000..9d42acb --- /dev/null +++ b/truthseeker/discord_bot.py @@ -0,0 +1,53 @@ +import discord +import threading +import truthseeker +import asyncio + +async def empty_coro(): + return + +class DiscordBot: + def __init__(self): + self.bot = discord.Client(intents=discord.Intents.default()) + self.__channel__ = None + + @self.bot.event + async def on_ready(): + print('Discord bot connected !') + self.event_loop = asyncio.get_event_loop() + + self.__setup__channel__() + self.update_games_presence() + + def __setup__channel__(self): + if len(self.bot.guilds) == 1: + self.__channel__ = discord.utils.get(self.bot.guilds[0].channels, name="bot") + else: + print("Could not find channel #bot") + + def start(self, token): + thr = threading.Thread(target=self.bot.run, args=(token,)) + thr.start() + return thr + + def API(func): + def decorator(self, *args, **kwargs): + if self.bot and self.bot.is_ready(): + self.event_loop.create_task(func(self, *args, **kwargs)) + else: + print(f"Discord bot not ready, not processing function {func.__name__}()") + return decorator + + @API + async def update_games_presence(self): + games_n = len(truthseeker.APP.games_list) + activity_name = f"Handling {games_n} game{'' if games_n==1 else 's'} !" + activity = discord.Activity(name=activity_name, type=discord.ActivityType.watching) + await self.bot.change_presence(activity=activity) + + @API + async def send_message(self, text): + if self.__channel__: + await self.__channel__.send(text) + else: + print("channel member not defined, not sending discord message") diff --git a/truthseeker/routes/routes_api.py b/truthseeker/routes/routes_api.py index df0e6db..da09201 100644 --- a/truthseeker/routes/routes_api.py +++ b/truthseeker/routes/routes_api.py @@ -24,6 +24,8 @@ def create_game(): flask.session["is_owner"] = True flask.session["username"] = username + APP.discord_bot.update_games_presence() + return response @routes_api.route("/joinGame", methods=["GET", "POST"])