-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_expired.py
43 lines (31 loc) · 1.21 KB
/
update_expired.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
import asyncio
import datetime
import interactions
from db import Survey
from loguru import logger
class ExpirationUpdater(interactions.Extension):
def __init__(self, bot):
self.bot = bot
self.have_expired = {}
bot._loop.create_task(self.update_expired())
async def update_expired(self):
while True:
await asyncio.sleep(0.5)
now = datetime.datetime.utcnow()
expired_surveys = (Survey
.select()
.where(Survey.expires <= now, Survey.active == True))
for expired in expired_surveys:
logger.info(f"Expiring: {expired.id}")
message_url = expired.message_url
if self.have_expired.get(message_url, None) is not None:
continue
Survey.update(active=False).where(Survey.id == expired.id).execute()
if message_url == "":
continue
self.have_expired[message_url] = True
expired.active = False
await self.bot.send_survey(self.bot, expired, expired.message_url)
break
def setup(bot):
ExpirationUpdater(bot)