def get_evts(bot, timezone, type=None): result = [] for evt in weekly_events: if type is not None and evt['type'] != type: continue evt_begin = evt['begin'].split(':') dt_evt_begin = DateUtil.get_dt_now(bot.tz_str).replace( hour=int(evt_begin[0]), minute=int(evt_begin[1]), second=0, microsecond=0 ) while dt_evt_begin.isoweekday() != int(evt['weekday']): dt_evt_begin += timedelta(days=1) dt_evt_begin = dt_evt_begin.replace(tzinfo=None) dt_evt_begin = timezone.localize(dt_evt_begin, is_dst=False) evt_end = evt['end'].split(':') dt_evt_end = DateUtil.get_dt_now(bot.tz_str).replace( hour=int(evt_end[0]), minute=int(evt_end[1]), second=0, microsecond=0 ) while dt_evt_end.isoweekday() != int(evt['weekday']): dt_evt_end += timedelta(days=1) dt_evt_end = dt_evt_end.replace(tzinfo=None) dt_evt_end = timezone.localize(dt_evt_end, is_dst=False) evt['dt_begin'] = dt_evt_begin evt['dt_end'] = dt_evt_end result.append(evt) result.sort(key=lambda x: x['dt_begin']) return result
def get_gmc_map(bot, timezone): dt_now = DateUtil.get_dt_now(bot.tz_str) weekday_today = dt_now.isoweekday() weekday_tomorrow = 1 if weekday_today == 7 else (weekday_today + 1) evts = get_evts(bot, timezone, type='gmc') gmc_map = {} gmc_map['ongoing'] = '' gmc_map['next'] = '' gmc_map['today'] = '' gmc_map['tomorrow'] = '' next_evt = evts[0] for evt in evts: str_woe = '' + format_evt(bot, evt, timezone) + '\n' if int(evt['weekday']) == weekday_today: gmc_map['today'] += str_woe elif int(evt['weekday']) == weekday_tomorrow: gmc_map['tomorrow'] += str_woe if evt['dt_begin'] <= dt_now <= evt['dt_end']: gmc_map['ongoing'] = format_evt(bot, evt, timezone) if evt['dt_begin'] <= dt_now: continue if next_evt['dt_begin'] < dt_now or evt['dt_begin'] < next_evt['dt_begin']: next_evt = evt if next_evt != None: gmc_map['next'] = format_evt(bot, next_evt, timezone) return gmc_map
def get_woe_map(bot, timezone): dt_now = DateUtil.get_dt_now(bot.tz_str) evts = get_evts(bot, timezone, type='woe') woe_map = {} woe_map['ongoing'] = '' woe_map['next'] = '' woe_map['saturday'] = '' woe_map['sunday'] = '' next_evt = evts[0] for evt in evts: str_woe = '' + format_evt(bot, evt, timezone) + '\n' if int(evt['weekday']) == 7: woe_map['sunday'] += str_woe else: woe_map['saturday'] += str_woe if evt['dt_begin'] <= dt_now <= evt['dt_end']: woe_map['ongoing'] = format_evt(bot, evt, timezone) if evt['dt_begin'] <= dt_now: continue if next_evt['dt_begin'] < dt_now or evt['dt_begin'] < next_evt[ 'dt_begin']: next_evt = evt if next_evt != None: woe_map['next'] = format_evt(bot, next_evt, timezone) return woe_map
async def timer(self): if self.bot.ws is not None: dt_now = DateUtil.get_dt_now(self.bot.tz_str) if self.bot.race_time is not None: race_begin = self.bot.race_time race_end = self.bot.race_time + timedelta(hours=1) if dt_now > race_end: await update_race_time(self.bot, None) else: str = 'Summer Race' if race_begin <= dt_now <= race_end: str += ' ends ' str += DateUtil.fmt_time_short((race_end - dt_now) / timedelta(milliseconds=1)) else: str += ' ' str += DateUtil.fmt_time_short((race_begin - dt_now) / timedelta(milliseconds=1)) str += ' (TalonRO)' game = discord.Game(name=str) await CoroutineUtil.run(self.bot.change_presence(activity=game)) return evt = get_next_evt(self.bot, timezone(self.bot.tz_str)) str = ('WoE ' if evt['type'] == 'woe' else '') + evt['name'] if evt['dt_begin'] <= dt_now <= evt['dt_end']: str += ' ends ' str += DateUtil.fmt_time_short((evt['dt_end'] - dt_now) / timedelta(milliseconds=1)) else: str += ' ' str += DateUtil.fmt_time_short((evt['dt_begin'] - dt_now) / timedelta(milliseconds=1)) str += ' (TalonRO)' game = discord.Game(name=str) await CoroutineUtil.run(self.bot.change_presence(activity=game))
async def timer(self): if self.bot.ws is not None: dt_now = DateUtil.get_dt_now(self.bot.tz_str) evt = get_next_evt(self.bot, timezone(self.bot.tz_str)) str = ('WoE ' if evt['type'] == 'woe' else '') + evt['name'] if evt['dt_begin'] <= dt_now <= evt['dt_end']: str += ' ends ' str += DateUtil.fmt_time_short((evt['dt_end'] - dt_now) / timedelta(milliseconds=1)) else: str += ' ' str += DateUtil.fmt_time_short((evt['dt_begin'] - dt_now) / timedelta(milliseconds=1)) str += ' (TalonRO)' game = discord.Game(name=str) await CoroutineUtil.run(self.bot.change_presence(activity=game))
async def woe(self, ctx): woe_map = get_woe_map(self.bot, timezone(self.bot.tz_str)) embed = discord.Embed() embed.title = ':european_castle: WoE' embed.colour = discord.Colour.gold() if woe_map['ongoing'] != '': embed.add_field(name='Ongoing :boom:', value=woe_map['ongoing'], inline=False) embed.add_field(name='Next :arrow_right:', value=woe_map['next'], inline=False) embed.add_field(name='Saturday', value=woe_map['saturday'], inline=False) embed.add_field(name='Sunday', value=woe_map['sunday'], inline=False) footer = 'Server Time (' + self.bot.tz_str + '): ' footer += DateUtil.fmt_dt(DateUtil.get_dt_now(self.bot.tz_str)) embed.set_footer(text=footer) await CoroutineUtil.run(ctx.send(embed=embed))
def format_evt(bot, evt, timezone): dt_now = DateUtil.get_dt_now(bot.tz_str) result = '**' result += evt['name'] result += '** ' result += evt['dt_begin'].strftime('%H:%M') if evt['dt_begin'] <= dt_now <= evt['dt_end']: result += ' (ends ' result += DateUtil.fmt_time((evt['dt_end'] - dt_now) / timedelta(milliseconds=1)) else: result += ' (' result += DateUtil.fmt_time((evt['dt_begin'] - dt_now) / timedelta(milliseconds=1)) result += ')' return result
def get_next_evt(bot, timezone): dt_now = DateUtil.get_dt_now(bot.tz_str) evts = get_evts(bot, timezone) next_evt = None for evt in evts: if evt['dt_end'] < dt_now: continue if next_evt is None: next_evt = evt if evt['dt_begin'] <= dt_now <= evt['dt_end']: #ongoing return next_evt break if evt['dt_begin'] < next_evt['dt_begin']: #next next_evt = evt return next_evt
async def gmc(self, ctx): gmc_map = get_gmc_map(self.bot, timezone(self.bot.tz_str)) embed = discord.Embed() embed.title = ':dragon_face: GMC' embed.colour = discord.Colour.gold() if gmc_map['ongoing'] != '': embed.add_field(name='Ongoing :boom:', value=gmc_map['ongoing'], inline=False) embed.add_field(name='Next :arrow_right:', value=gmc_map['next'], inline=False) if gmc_map['today'] != '': embed.add_field(name='Today', value=gmc_map['today'], inline=False) if gmc_map['tomorrow'] != '': embed.add_field(name='Tomorrow', value=gmc_map['tomorrow'], inline=False) footer = 'Server Time (' + self.bot.tz_str + '): ' footer += DateUtil.fmt_dt(DateUtil.get_dt_now(self.bot.tz_str)) embed.set_footer(text=footer) await CoroutineUtil.run(ctx.send(embed=embed))
async def track(self, ctx, *args): validate_msg = validate_channel(self.bot, ctx.message.channel) if validate_msg is not None: await CoroutineUtil.run(ctx.send(validate_msg)) return guild_state = self.bot.guild_state_map[ctx.guild.id] channel_state = guild_state['channel_state_map'].get( ctx.message.channel.id) if channel_state is None: await CoroutineUtil.run( ctx.send('This command can only be used in a track channel.')) return mins_ago = 0 if len(args) == 0: return query_last_idx = len(args) - 1 if args[query_last_idx].isnumeric(): mins_ago = int(args[query_last_idx]) query_last_idx -= 1 elif len(args[query_last_idx]) == 5 and args[query_last_idx][2] == ':': hrs_mins = args[query_last_idx].split(':') if len(hrs_mins) == 2 and hrs_mins[0].isnumeric( ) and hrs_mins[1].isnumeric(): hh = int(hrs_mins[0]) mm = int(hrs_mins[1]) track_time = DateUtil.get_dt_now(self.bot.tz_str) if hh > track_time.hour or (hh == track_time.hour and mm > track_time.minute): track_time -= timedelta(days=1) track_time = track_time.replace(hour=hh, minute=mm) td = DateUtil.get_dt_now(self.bot.tz_str) - track_time mins_ago = td / timedelta(minutes=1) query_last_idx -= 1 if mins_ago < 0: mins_ago = 0 query = args[0] for i in range(1, query_last_idx + 1): query += ' ' + args[i] type = channel_state['type'] results = find_entry(self.bot, query, type) if len(results) == 0: await CoroutineUtil.run( ctx.send(entry_desc(type) + ' "' + query + '" not found.')) elif len(results) == 1: bot_reply_msg = await update_track_time(self.bot, channel_state, results[0], mins_ago, ctx.message.author.id) await CoroutineUtil.run(ctx.send(bot_reply_msg)) else: bot_reply_msg = 'More than one ' + entry_desc(type) bot_reply_msg += ' starting with "' + query + '" has been found.\n' bot_reply_msg += '\n' bot_reply_msg += 'Type or react with the number of the ' + entry_desc( type) bot_reply_msg += ' that you want to track:\n' bot_reply_msg += '```' for i in range(0, min(len(results), 5)): bot_reply_msg += str(i + 1) + '. ' + results[i]['name'] if type == TrackType.MVP: bot_reply_msg += ' (' + results[i]['map'] + ')' bot_reply_msg += '\n' bot_reply_msg += '```' bot_msg = await CoroutineUtil.run(ctx.send(bot_reply_msg)) user_entry_state = guild_state['user_state_map'].get( ctx.message.author.id) if user_entry_state is not None and user_entry_state[ 'bot_msg'] is not None: await CoroutineUtil.run(user_entry_state['bot_msg'].delete()) guild_state['user_state_map'][ctx.message.author.id] = { 'command': 'TRACK', 'bot_msg': bot_msg, 'results': results, 'mins_ago': mins_ago } for i in range(0, min(len(results), 5)): await CoroutineUtil.run( bot_msg.add_reaction(number_emoji_list[i]))