Merge pull request #24 from ThomasRubini/bot_discord
This commit is contained in:
commit
a217834999
@ -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
|
||||
pymysql==1.0.2
|
||||
discord.py==2.1.0
|
||||
|
@ -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
|
||||
|
53
truthseeker/discord_bot.py
Normal file
53
truthseeker/discord_bot.py
Normal 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")
|
@ -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"])
|
||||
|
Loading…
Reference in New Issue
Block a user