Skip to content

Commit e29d44d

Browse files
authored
Merge pull request #129 from kyb3r/selfhosted
Add ability to selfhost logs
2 parents 144b516 + 1b67e0a commit e29d44d

File tree

10 files changed

+447
-135
lines changed

10 files changed

+447
-135
lines changed

Procfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
worker: python bot.py
1+
worker: python bot.py
2+
web: python app.py

app.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
3+
from sanic import Sanic, response
4+
from motor.motor_asyncio import AsyncIOMotorClient
5+
import aiohttp
6+
7+
from core.models import LogEntry
8+
9+
app = Sanic(__name__)
10+
11+
@app.listener('before_server_start')
12+
async def init(app, loop):
13+
app.db = AsyncIOMotorClient(os.getenv('MONGO_URI')).modmail_bot
14+
15+
@app.get('/')
16+
async def index(request):
17+
return response.text('Welcome! This simple website is used to display your modmail logs.')
18+
19+
@app.get('/logs/<key>')
20+
async def getlogsfile(request, key):
21+
"""Returned the plain text rendered log entry"""
22+
23+
log = await app.db.logs.find_one({'key': key})
24+
25+
if log is None:
26+
return response.text('Not Found', status=404)
27+
else:
28+
return response.text(str(LogEntry(log)))
29+
30+
if __name__ == '__main__':
31+
app.run(host='0.0.0.0', port=os.getenv('PORT'))

bot.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
import aiohttp
3636
from discord.ext import commands
3737
from discord.ext.commands.view import StringView
38+
from motor.motor_asyncio import AsyncIOMotorClient
39+
3840
from colorama import init, Fore, Style
3941
import emoji
4042

41-
from core.api import Github, ModmailApiClient
43+
from core.clients import Github, ModmailApiClient, SelfhostedClient
4244
from core.thread import ThreadManager
4345
from core.config import ConfigManager
4446
from core.changelog import ChangeLog
@@ -58,7 +60,10 @@ def __init__(self):
5860
self.threads = ThreadManager(self)
5961
self.session = aiohttp.ClientSession(loop=self.loop)
6062
self.config = ConfigManager(self)
61-
self.modmail_api = ModmailApiClient(self)
63+
self.selfhosted = bool(self.config.get('mongo_uri'))
64+
if self.selfhosted:
65+
self.db = AsyncIOMotorClient(self.config.mongo_uri).modmail_bot
66+
self.modmail_api = SelfhostedClient(self) if self.selfhosted else ModmailApiClient(self)
6267
self.data_task = self.loop.create_task(self.data_loop())
6368
self.autoupdate_task = self.loop.create_task(self.autoupdate_loop())
6469
self._add_commands()
@@ -158,9 +163,16 @@ async def get_pre(bot, message):
158163
return [bot.prefix, f'<@{bot.user.id}> ', f'<@!{bot.user.id}> ']
159164

160165
async def on_connect(self):
161-
print(line + Fore.RED + Style.BRIGHT)
162-
await self.validate_api_token()
163166
print(line)
167+
print(Fore.CYAN, end='')
168+
if not self.selfhosted:
169+
print('MODE: Using the Modmail API')
170+
print(line)
171+
await self.validate_api_token()
172+
print(line)
173+
else:
174+
print('Mode: Selfhosting logs.')
175+
print(line)
164176
print(Fore.CYAN + 'Connected to gateway.')
165177
await self.config.refresh()
166178
status = self.config.get('status')
@@ -355,12 +367,16 @@ async def validate_api_token(self):
355367
try:
356368
self.config.modmail_api_token
357369
except KeyError:
370+
print(Fore.RED + Style.BRIGHT, end='')
358371
print('MODMAIL_API_TOKEN not found.')
359372
print('Set a config variable called MODMAIL_API_TOKEN with a token from https://dashboard.modmail.tk')
373+
print('If you want to selfhost logs, input a MONGO_URI config variable.')
374+
print('A modmail api token is not needed if you are selfhosting logs.')
360375
valid = False
361376
else:
362377
valid = await self.modmail_api.validate_token()
363378
if not valid:
379+
print(Fore.RED + Style.BRIGHT, end='')
364380
print('Invalid MODMAIL_API_TOKEN - get one from https://dashboard.modmail.tk')
365381
finally:
366382
if not valid:
@@ -393,11 +409,18 @@ async def data_loop(self):
393409
await asyncio.sleep(3600)
394410

395411
async def autoupdate_loop(self):
396-
while True:
397-
if self.config.get('disable_autoupdates'):
398-
await asyncio.sleep(3600)
399-
continue
412+
await self.wait_until_ready()
400413

414+
if self.config.get('disable_autoupdates'):
415+
print('Autoupdates disabled.')
416+
return
417+
418+
if self.selfhosted and not self.config.get('github_access_token'):
419+
print('Github access token not found.')
420+
print('Autoupdates disabled.')
421+
return
422+
423+
while True:
401424
metadata = await self.modmail_api.get_metadata()
402425

403426
if metadata['latest_version'] != self.version:

cogs/modmail.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,9 @@ async def logs(self, ctx, *, member: Union[discord.Member, discord.User, obj]=No
343343
time = date.strftime(r'%H:%M')
344344

345345
key = entry['key']
346-
user_id = entry['user_id']
346+
user_id = entry.get('user_id')
347347
closer = entry['closer']['name']
348-
log_url = f"https://logs.modmail.tk/{user_id}/{key}"
348+
log_url = f"https://logs.modmail.tk/{user_id}/{key}" if not self.bot.selfhosted else self.bot.config.log_url + f'/logs/{key}'
349349

350350
truncate = lambda c: c[:47].strip() + '...' if len(c) > 50 else c
351351

core/api.py

Lines changed: 0 additions & 119 deletions
This file was deleted.

0 commit comments

Comments
 (0)