Merge pull request #24 from ThomasRubini/bot_discord

This commit is contained in:
Thomas Rubini 2023-01-11 11:06:21 +01:00 committed by GitHub
commit a217834999
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 1 deletions

View File

@ -3,3 +3,4 @@ pyjwt==2.6.0
Flask-SocketIO==5.3.2 Flask-SocketIO==5.3.2
SQLAlchemy==1.4.20 SQLAlchemy==1.4.20
pymysql==1.0.2 pymysql==1.0.2
discord.py==2.1.0

View File

@ -2,6 +2,8 @@ import flask
from flask_socketio import SocketIO from flask_socketio import SocketIO
import os import os
from truthseeker import discord_bot
class TruthSeekerApp(flask.Flask): class TruthSeekerApp(flask.Flask):
def __init__(self): def __init__(self):
@ -10,8 +12,17 @@ class TruthSeekerApp(flask.Flask):
self.games_list = {} self.games_list = {}
self.set_app_secret() self.set_app_secret()
self.socketio_app = SocketIO(self) 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): def run_app(self):
self.socketio_app.run(self) self.socketio_app.run(self)
@ -30,6 +41,14 @@ class TruthSeekerApp(flask.Flask):
f.close() f.close()
print("Generated secret and wrote to secret.txt !") 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() APP = TruthSeekerApp()
from truthseeker.routes import routes_api, routes_ui, routes_socketio from truthseeker.routes import routes_api, routes_ui, routes_socketio

View File

@ -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")

View File

@ -24,6 +24,8 @@ def create_game():
flask.session["is_owner"] = True flask.session["is_owner"] = True
flask.session["username"] = username flask.session["username"] = username
APP.discord_bot.update_games_presence()
return response return response
@routes_api.route("/joinGame", methods=["GET", "POST"]) @routes_api.route("/joinGame", methods=["GET", "POST"])