-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
64 lines (51 loc) · 1.75 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/env python3
from aiohttp import web
import asyncio
import discord
import json
import os
import threading
import time
class DiscordClient(discord.Client):
ready = False
async def on_ready(self):
self.ready = True
print('[INFO] Discord: Logged on as {0}!'.format(self.user))
def is_ready(self):
return self.ready
async def fetch_by_id(request):
uid = request.match_info['uid']
if not uid.isnumeric():
return web.Response(text=json.dumps({error:"UID must be numeric"}))
print("[INFO] fetch_by_id: Searching for uid:", uid)
discord_user = await client.fetch_user(uid)
user = {
"uid": discord_user.id,
"tag": discord_user.name + "#" + discord_user.discriminator,
"avatar": discord_user.avatar,
"bot": discord_user.bot,
"created_at": int(time.mktime(discord_user.created_at.timetuple()))
}
return web.Response(text=json.dumps(user))
async def handle_health(request):
if not client.is_ready() or client.is_closed():
return web.Response(text="no")
return web.Response(text="yes")
async def alive(request):
return web.Response(text="yes")
async def run_bot(token):
await client.start(token)
def run_it_forever(loop):
loop.run_forever()
token = os.environ['DISCORD_TOKEN']
client = DiscordClient()
loop = asyncio.get_event_loop()
loop.create_task(run_bot(token))
thread = threading.Thread(target=run_it_forever, args=(loop, ))
app = web.Application()
app.add_routes([web.get('/health', handle_health),
web.get('/alive', alive),
web.get('/by-uid/{uid}', fetch_by_id)])
web.run_app(app)
# This should be useless as web.run_app is a blocking call and the loop run ... forever
thread.join()