-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
61 lines (48 loc) · 1.37 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
from threading import Thread
import tornado.ioloop
import tornado.web
import json
import discord
import asyncio
with open("config.json", "r") as f:
config = json.load(f)
def sawait(coro, loop):
return asyncio.run_coroutine_threadsafe(coro, loop).result()
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
async def send_message(channel, msg):
#Grrrrrrrrrrrrrrrrrr
return "AAAAAAAA"
class MainHandler(tornado.web.RequestHandler):
def get(self, channel = None):
ip = self.request.remote_ip
if len(config["allow"]) > 0 and ip not in config["allow"]:
self.write("Unauthorized")
self.set_status(401)
return
statuscode = 200
try:
message = self.get_argument("message", None, True)
if channel is None or len(channel) is 0:
raise Exception("Missing channel name")
if message is None or len(message) is 0:
raise Exception("Missing message")
sawait(send_message(channel, message), client.loop)
self.write("OK")
except Exception as e:
self.write("Exception: {}".format(e))
statuscode = 500
finally:
self.set_status(statuscode)
if __name__ == "__main__":
app = tornado.web.Application([
(r"/(.*)", MainHandler),
])
app.listen(config["port"])
Thread(target=client.run, args=(config["token"],)).start()
tornado.ioloop.IOLoop.current().start()