async def parse_hltv_matches(match_elements: list) -> None: now = to_helsinki(as_utc(datetime.datetime.now())).replace(tzinfo=None) if match_elements: for element in match_elements: date = to_helsinki(as_utc(pandas.to_datetime((int(element[0][0][0].values()[2])),unit='ms'))).replace(tzinfo=None) # table -> tr -> td -> div date = datetime.datetime.strptime(str(date), "%Y-%m-%d %H:%M:%S") if date < now: log.info('HLTV Match already started or played, not adding it to matches dict') continue home_team = element[0][1].text_content().replace('\n','').strip() # table -> tr -> div -> div> away_team = element[0][3].text_content().replace('\n', '').strip() competition = element[0][4].text_content().replace('\n', '').strip() map = element[0][5].text_content().replace('\n', '').strip() if map in ["bo1", "bo2", "bo3", "bo4", "bo5"]: map = "TBD (%s)" % map tod = ('%s:%s' % (date.hour, (str(date.minute)) if date.minute != 0 else str(date.minute) + "0")) item = {'competition': competition[:15], 'home_team': home_team, 'away_team': away_team, 'map': map, 'status': 'Upcoming', 'date': date, 'tod': tod} matches_for_date = MATCHES_DICT.get(date.date(), None) if matches_for_date: if item not in matches_for_date: matches_for_date.append(item) else: log.info('HLTV Match is already added') else: MATCHES_DICT.update({date.date():[item]}) log.info("HLTV matches parsed.")
async def cmd_ence(client, message, arg) -> None: now = to_helsinki(as_utc(datetime.datetime.now())).replace(tzinfo=None) if not LAST_CHECKED: await message.channel.send("https://i.ytimg.com/vi/CRvlTjeHWzA/maxresdefault.jpg\n(Matches haven't been fetched yet as the bot was just started, please try again soon)") else: list_of_matches = deepcopy([x for y in sorted(MATCHES_DICT.values(), key=lambda x: x[0].get('date')) for x in y]) list_of_matches = [await convert_to_list(match_dict) for match_dict in list_of_matches if match_dict.get('date') > now] await message.channel.send((("\nAs of %s: ```" % to_helsinki(as_utc(LAST_CHECKED)).strftime( "%Y-%m-%d %H:%M")) + tablemaker( ['COMPETITION', 'HOME TEAM', 'AWAY TEAM', 'MAP', 'STATUS', 'DATE', 'TOD'] , list_of_matches) + "\n#EZ4ENCE```"))
async def start_match_start_spam_task(client, channels_query: list, earliest_match: dict) -> None: # todo: maybe make a 'spam function' which can be used by both functions match_time = earliest_match.get('date') match = as_utc(match_time).replace(tzinfo=None) now = to_helsinki(as_utc(datetime.datetime.now())).replace(tzinfo=None) delta = max((match - now - datetime.timedelta(seconds=900)), datetime.timedelta(seconds=0)) log.info('Match spammer task: going to sleep for %s seconds' % delta.seconds) await sleep(delta.seconds) log.info('Match spammer task: waking up and attempting to spam') if await not_rescheduled(earliest_match): for row in channels_query: channel = util.threadsafe(client, client.fetch_channel(int(row['channel_id']))) util.threadsafe(client, channel.send(("The %s match %s versus %s is about to start! (announced starting time: %s) \n#EZ4ENCE" % (earliest_match[0], earliest_match[1], earliest_match[2], earliest_match[6])))) await update_last_spammed_time()
async def cmd_muutto(client, message, query): muutto = to_utc(as_helsinki(datetime(2020, 9, 4, 16, 0))) muuttoover = to_utc(as_helsinki(datetime(2020, 9, 4, 23, 0))) now = as_utc(datetime.now()) now_to_last_muuttoover = now - muuttoover if (muutto < now) and (muuttoover > now): await message.channel.send("Muutto is currently happening!! (probably)" ) return if ((muuttoover + timedelta(days=4)) < now) and muutto < now: await message.channel.send(( "**Muutto ended:** {0} days, {1} hours, {2} minutes, {3} seconds ago, **next muutto:** TBA." ).format(*delta_to_tuple(now_to_last_muuttoover))) return if muuttoover < now: await message.channel.send( "Muutto is over, but infernal tuparit are still coming!!") return delta = muutto - now template = "Time left until muutto: {0} days, {1} hours, {2} minutes, {3} seconds!!" msg = template.format(*delta_to_tuple(delta)) await message.channel.send(msg)
async def get_most_used_emojis(emojilist, guild_id): used_emojis = [] for emoji in emojilist: result = await db.fetch(""" SELECT count(*) as times_used, extract(epoch from current_timestamp - $1) / 60 / 60 / 24 as daystocreated FROM message WHERE content ~ $2 AND NOT bot AND guild_id = $3 HAVING count(*) > 0""" , as_utc(emoji.created_at), str(emoji), guild_id) if result: for item in result: times_used = item['times_used'] used_per_day = times_used / item['daystocreated'] used_emojis.append((str(emoji), times_used, round(used_per_day, 3))) if no_emojis_used(used_emojis): return None most_used_top_twentyfive = sorted(used_emojis, key=lambda x: x[2], reverse=True)[:25] return most_used_top_twentyfive
async def cmd_laiva(client, message, _): if not is_allowed_channel(message.channel.id): await message.channel.send("You cannot use this command here.") return theme = templates["theme"] laiva = to_utc(as_helsinki(datetime(2022, 6, 17, 16, 0))) laivaover = to_utc(as_helsinki(datetime(2022, 6, 19, 12, 0))) now = as_utc(datetime.now()) if (laiva < now) and (laivaover > now): await message.channel.send(templates["happening"]) return if ((laivaover + timedelta(days=1)) < now) and laiva < now: time_ago = delta_to_str(now - laivaover) await message.channel.send(templates["ended"](time_ago)) return if laivaover < now: await message.channel.send(templates["over"]) return time_left = delta_to_str(laiva - now) msg = f"Time left until '{theme}': {time_left}!!" if (laiva - timedelta(days=len(memes) - 1)) < now: days_to_go = (laiva - now).days msg += "\n" + templates["meme_text"](days_to_go) await message.channel.send(msg)
async def get_time_until_reset_message() -> str: now = as_utc(datetime.now()) reset_time: datetime = await get_reset_date() delta = reset_time - now template = "Time until this week's stats will be reset: {0} days, {1} hours, {2} minutes, {3} seconds" msg = template.format(*delta_to_tuple(delta)) return msg
async def get_reset_date() -> datetime: date = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) if date.weekday() == 0: date += timedelta(1) while date.weekday() != 0: date += timedelta(1) return to_helsinki(as_utc(date))
async def check_if_ence_day(client): log.info("Checking if match day") now = to_helsinki(as_utc(datetime.datetime.now())).replace(tzinfo=None) matches_today = MATCHES_DICT.get(now.date(), None) if matches_today: if LAST_SPAMMED: if (LAST_SPAMMED.date() != now.date()): #If already spammed today await do_matchday_spam(client, matches_today) else: await do_matchday_spam(client, matches_today) else: log.info('No matches today')
async def cmd_lan(client, message, query): lan = to_utc(as_helsinki(datetime(2021, 11, 26, 16, 0))) lan_over = to_utc(as_helsinki(datetime(2021, 11, 28, 14, 30))) now = as_utc(datetime.now()) if (lan < now) and (lan_over > now): await message.channel.send("HelmiLAN is currently happening!!") return if lan < now: await message.channel.send( "HelmiLAN is already over but väsymys remains.") return delta = lan - now template = "Time until HelmiLAN: {0} days, {1} hours, {2} minutes, {3} seconds" msg = template.format(*delta_to_tuple(delta)) await message.channel.send(msg)
async def not_rescheduled(match_item: dict) -> bool: now = to_helsinki(as_utc(datetime.datetime.now())).replace(tzinfo=None) matches = MATCHES_DICT.get(now.date(), None) log.info('match_item = %s, matches[0] = %s' % (match_item, matches[0])) return matches[0] != match_item
def helsinki_date_now(): return to_helsinki(as_utc(datetime.datetime.now())).date()