Skip to content

Commit a4e3539

Browse files
committed
Do not start the bot if MODMAIL_API_TOKEN is not valid
1 parent 2680e22 commit a4e3539

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Made the logs command require manage messages permissions to execute.
1111
- Before this patch, anyone could use the logs commands.
1212

13-
1413
# v2.0.1
1514

1615
Bug fixes and minor improvements.

bot.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __init__(self):
6161
self.data_task = self.loop.create_task(self.data_loop())
6262
self.autoupdate_task = self.loop.create_task(self.autoupdate_loop())
6363
self._add_commands()
64-
64+
6565
def _add_commands(self):
6666
"""Adds commands automatically"""
6767
self.remove_command('help')
@@ -91,7 +91,7 @@ def run(self):
9191
try:
9292
super().run(self.token)
9393
finally:
94-
print(Fore.CYAN + ' Shutting down bot' + Style.RESET_ALL)
94+
print(Fore.RED + ' - Shutting down bot' + Style.RESET_ALL)
9595

9696
@property
9797
def snippets(self):
@@ -142,12 +142,15 @@ async def get_pre(bot, message):
142142
return [bot.prefix, f'<@{bot.user.id}> ', f'<@!{bot.user.id}> ']
143143

144144
async def on_connect(self):
145+
print(line + Fore.RED + Style.BRIGHT)
146+
await self.validate_api_token()
145147
print(line)
146148
print(Fore.CYAN + 'Connected to gateway.')
147149
await self.config.refresh()
148150
status = self.config.get('status')
149151
if status:
150152
await self.change_presence(activity=discord.Game(status))
153+
151154

152155
async def on_ready(self):
153156
"""Bot startup, sets uptime."""
@@ -290,6 +293,24 @@ def overwrites(self, ctx):
290293

291294
return overwrites
292295

296+
async def validate_api_token(self):
297+
valid = True
298+
try:
299+
token = self.config.modmail_api_token
300+
except KeyError:
301+
print('MODMAIL_API_TOKEN not found.')
302+
print('Set a config variable called MODMAIL_API_TOKEN with a token from https://dashboard.modmail.tk')
303+
valid = False
304+
else:
305+
valid = await self.modmail_api.validate_token()
306+
if not valid:
307+
print('Invalid MODMAIL_API_TOKEN - get one from https://dashboard.modmail.tk')
308+
finally:
309+
if not valid:
310+
await self.logout()
311+
else:
312+
print(Style.RESET_ALL + Fore.CYAN + 'Validated API token.' + Style.RESET_ALL)
313+
293314
async def data_loop(self):
294315
await self.wait_until_ready()
295316

core/api.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ def __init__(self, app):
77
self.session = app.session
88
self.headers = None
99

10-
async def request(self, url, method='GET', payload=None):
10+
async def request(self, url, method='GET', payload=None, return_response=False):
1111
async with self.session.request(method, url, headers=self.headers, json=payload) as resp:
12+
if return_response:
13+
return resp
1214
try:
1315
return await resp.json()
1416
except:
@@ -38,6 +40,10 @@ def __init__(self, bot):
3840
self.headers = {
3941
'Authorization': 'Bearer ' + self.token
4042
}
43+
44+
async def validate_token(self):
45+
resp = await self.request(self.base + '/token/verify', return_response=True)
46+
return resp.status == 200
4147

4248
def get_user_info(self):
4349
return self.request(self.github + '/userinfo')

0 commit comments

Comments
 (0)