-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·155 lines (118 loc) · 5.33 KB
/
server.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import discord
import asyncio
import dateparser
import datetime
import math
import sqlite3
from aiohttp import web
class Bot():
def __init__(self, loop, client):
self.alarms = []
self.client = client
self.connection = self.connect_to_database()
client.event(self.on_message)
client.event(self.on_ready)
loop.create_task(self.alarm_checker(client))
def check(message):
try:
int(message.content)
return True
except ValueError:
return False
async def on_message(self, message):
if message.author == self.client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
if message.content.startswith('test'):
await message.channel.send('testing 123')
if message.content.startswith('$timer'):
timer = 0
await message.channel.send('Understood!')
await message.channel.send('Will be back in one minute!')
await asyncio.sleep(60)
await message.channel.send('One minute is up!')
if message.content.startswith('$settimer'):
# Example: $settimer in 5 minutes with message blabla
author = message.author.name
author_user_id = message.author.id
channel_id = message.channel.id
# This string delimits the beginning of a message associated with the timer
timer_message_prefix = "with message"
timer_message_prefix_index = message.content.find(timer_message_prefix)
if timer_message_prefix_index != -1:
when = message.content[len('$settimer'):timer_message_prefix_index].strip()
timer_message = message.content[timer_message_prefix_index + len(timer_message_prefix):].strip()
else:
when = message.content[len('$settimer'):].strip()
timer_message = ''
alarm_time = dateparser.parse(when)
self.create_alarm(alarm_time, author, author_user_id, timer_message, channel_id)
print('Timer set {} by {}'.format(when, author))
def create_alarm(self, alarm_time, author, author_user_id, message, channel_id):
c = self.connection.cursor()
#@TODO sql injection protection with author name and message
query = 'INSERT INTO timers VALUES ({}, {}, "{}", {}, "{}", {})'.format(
math.floor(alarm_time.timestamp()),
math.floor(datetime.datetime.now().timestamp()),
author,
int(author_user_id),
message,
int(channel_id)
)
c.execute(query)
self.connection.commit()
async def on_ready(self):
print('We have logged in as {0.user}'.format(self.client))
async def alarm_checker(self, client):
c = self.connection.cursor()
while True:
now = math.floor(datetime.datetime.now().timestamp())
c.execute('SELECT rowid, * FROM timers WHERE alarm_time <= {} ORDER BY alarm_time ASC'.format(now))
for record in c.fetchall():
author_user_id = int(record[4])
message = record[5]
channel = client.get_channel(int(record[6]))
await channel.send('{}, <@{}>'.format(message, author_user_id))
c.execute('DELETE FROM timers WHERE alarm_time <= {} '.format(now))
self.connection.commit()
await asyncio.sleep(60)
def connect_to_database(self):
connection = sqlite3.connect('timers.db')
print('Connected to timer registry')
c = connection.cursor()
c.execute('SELECT count(name) FROM sqlite_master WHERE type="table" AND name="timers"')
# Create a table if it doesn't exist
if c.fetchone()[0] == 0:
c.execute('''CREATE TABLE timers
(
alarm_time timestamp,
created_at timestamp,
author text,
author_user_id integer,
message text,
channel_id integer
)''')
return connection
async def hello(self, request):
c = self.connection.cursor()
c.execute('SELECT rowid, * FROM timers ORDER BY alarm_time ASC')
for record in c.fetchall():
data = [{'alarm_time': int(record[1]), 'created_at': int(record[2]), 'message': record[5], 'author': record[3]}]
return web.json_response(data, headers={'Access-Control-Allow-Origin': 'http://localhost:3000'})
def main():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
client = discord.Client(loop=loop)
bot = Bot(loop, client)
api_server = web.Application()
api_server.router.add_get('/timers', bot.hello)
runner = web.AppRunner(api_server)
loop.run_until_complete(runner.setup())
site = web.TCPSite(runner)
loop.run_until_complete(site.start())
client.run('NzUzMjQ5MjEyMDk1MDA0ODA0.X1jb_g.NMyBMC_PMqRfZxIfK3zCodF0WB4')
finally:
loop.close()
main()