async def task_check_for_data_updates(discord_client): filenames = LANG_FILES + [ 'World.json', 'User.json', 'Campaign.json', 'Soulforge.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
async def task_update_dbl_stats(client): if client.topgg_client is None: return try: await client.topgg_client.post_guild_count() log.debug('Posted server count ({})'.format(client.topgg_client.guild_count)) except Exception as e: log.exception('Failed to post server count\n{}: {}'.format(type(e).__name__, e))
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)
async def show_latest_news(self): if not self.is_ready(): return with open(NewsDownloader.NEWS_FILENAME) as f: articles = json.load(f) articles.reverse() if articles: log.debug( f'Distributing {len(articles)} news articles to {len(self.subscriptions)} channels.' ) for article in articles: e = discord.Embed(title='Gems of War news', color=self.WHITE, url=article['url']) content = self.views.trim_news_to_length(article['content'], article['url']) e.add_field(name=article['title'], value=content) embeds = [e] for image_url in article['images']: e = discord.Embed(type='image', color=self.WHITE) e.set_image(url=image_url) embeds.append(e) for subscription in self.subscriptions: relevant_news = subscription.get(article['platform']) if not relevant_news: continue channel = self.get_channel(subscription['channel_id']) if not channel: log.debug( f'Subscription {subscription} is broken, skipping.') continue log.debug( f'Sending [{article["platform"]}] {article["title"]} to {channel.guild.name}/{channel.name}.' ) if not await self.is_writable(channel): message = 'is not writable' if channel else 'does not exist' log.debug(f'Channel {message}.') continue try: for e in embeds: await channel.send(embed=e) except Exception as e: log.error('Could not send out news, exception follows') log.exception(e) with open(NewsDownloader.NEWS_FILENAME, 'w') as f: f.write('[]')
async def show_latest_news(self): if not self.is_ready(): return with open(NewsDownloader.NEWS_FILENAME) as f: articles = json.load(f) articles.reverse() if articles: log.debug( f'Distributing {len(articles)} news articles to {len(self.subscriptions)} channels.' ) for article in articles: embeds = self.views.render_news(article) for subscription in self.subscriptions: relevant_news = subscription.get(article['platform']) if not relevant_news: continue channel = self.get_channel(subscription['channel_id']) if not channel: log.debug( f'Subscription {subscription} is broken, skipping.') continue log.debug( f'Sending [{article["platform"]}] {article["title"]} to {channel.guild.name}/{channel.name}.' ) if not await self.is_writable(channel): message = 'is not writable' if channel else 'does not exist' log.debug(f'Channel {message}.') continue try: for e in embeds: await channel.send(embed=e) except Exception as ex: log.error('Could not send out news, exception follows') log.error(repr(e.fields)) log.exception(ex) with open(NewsDownloader.NEWS_FILENAME, 'w') as f: f.write('[]')