forked from maduck/GoWDiscordTeamBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot_tasks.py
65 lines (57 loc) · 2.42 KB
/
bot_tasks.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
import asyncio
import datetime
import os
from discord.ext import tasks
from game_assets import GameAssets
from base_bot import log
from configurations import CONFIG
from jobs.news_downloader import NewsDownloader
from search import TeamExpander, update_translations
from translations import LANG_FILES
@tasks.loop(minutes=1, reconnect=True)
async def task_update_pet_rescues(discord_client):
lock = asyncio.Lock()
async with lock:
discord_client.pet_rescues = [rescue for rescue in discord_client.pet_rescues if rescue.active]
for rescue in discord_client.pet_rescues:
e = discord_client.views.render_pet_rescue(rescue)
await rescue.create_or_edit_posts(e)
@tasks.loop(minutes=CONFIG.get('news_check_interval_minutes'), reconnect=False)
async def task_check_for_news(discord_client):
lock = asyncio.Lock()
async with lock:
try:
downloader = NewsDownloader()
downloader.process_news_feed()
await discord_client.show_latest_news()
except Exception as e:
log.error('Could not update news. Stacktrace follows.')
log.exception(e)
@tasks.loop(seconds=CONFIG.get('file_update_check_seconds'))
async def task_check_for_data_updates(discord_client):
filenames = LANG_FILES + ['World.json', 'User.json', 'Campaign.json', 'Soulforge.json', 'Event.json', 'Store.json']
now = datetime.datetime.now()
modified_files = []
for filename in filenames:
file_path = GameAssets.path(filename)
try:
modification_time = datetime.datetime.fromtimestamp(os.path.getmtime(file_path))
except FileNotFoundError:
continue
modified = now - modification_time <= datetime.timedelta(seconds=CONFIG.get('file_update_check_seconds'))
if modified:
modified_files.append(filename)
if modified_files:
log.debug(f'Game file modification detected, reloading {", ".join(modified_files)}.')
await asyncio.sleep(5)
lock = asyncio.Lock()
async with lock:
try:
old_expander = discord_client.expander
del discord_client.expander
discord_client.expander = TeamExpander()
update_translations()
except Exception as e:
log.error('Could not update game file. Stacktrace follows.')
log.exception(e)
discord_client.expander = old_expander