import telebot import datetime from telegram_bot import * from formatter import * from persistence import * from fortnite_client import * from fortnite_status import * __stats_now__ = 'stats_now' __stats_day__ = 'stats_day' __stats_week__ = 'stats_week' __stats_month__ = 'stats_month' class StartCommand(CommandHandler): __telegram_bot: TelegramBot __user_repository: UserRepository def __init__(self, telegram_bot: TelegramBot, user_repository: UserRepository): self.__telegram_bot = telegram_bot self.__user_repository = user_repository async def handle(self, message: telebot.types.Message): if message.chat.type == 'private': alias = message.chat.username else: alias = message.chat.title self.__user_repository.put_user(message.chat.id, alias) await self.__telegram_bot.reply(message, 'This chat successfully registered to receive Fortnite updates') class GetStatusCommand(CommandHandler): __telegram_bot: TelegramBot __fortnite_status: FortniteStatus def __init__(self, telegram_bot: TelegramBot): self.__telegram_bot = telegram_bot self.__fortnite_status = FortniteStatus() async def handle(self, message: telebot.types.Message): await self.__telegram_bot.reply(message, format_fortnite_status(self.__fortnite_status.get_status())) class GetFriendsCommand(CommandHandler): __telegram_bot: TelegramBot __fortnite_client: FortniteClient def __init__(self, telegram_bot: TelegramBot, fortnite_client: FortniteClient): self.__telegram_bot = telegram_bot self.__fortnite_client = fortnite_client async def handle(self, message: telebot.types.Message): if self.__fortnite_client.is_initialized(): friends = await self.__fortnite_client.get_friends() await self.__telegram_bot.reply(message, format_users(friends)) class GetStatsCallbackQueryHandler(CallbackQueryHandler): __telegram_bot: TelegramBot __fortnite_client: FortniteClient __stats_repository: StatsRepository def __init__(self, telegram_bot: TelegramBot, fortnite_client: FortniteClient, stats_repository: StatsRepository): self.__telegram_bot = telegram_bot self.__fortnite_client = fortnite_client self.__stats_repository = stats_repository async def handle(self, call: telebot.types.CallbackQuery): if self.__fortnite_client.is_initialized(): if call.data == __stats_now__: await self.reply_with_today_stats(call.message) elif call.data == __stats_day__: await self.reply_with_stats_days_difference(call.message, 1, 'day') elif call.data == __stats_week__: await self.reply_with_stats_days_difference(call.message, 7, 'week') elif call.data == __stats_month__: await self.reply_with_stats_days_difference(call.message, 30, 'month') async def reply_with_today_stats(self, message): friends = await self.__fortnite_client.get_friends() stats = [await friend.fetch_stats() for friend in friends] await self.__telegram_bot.reply(message, format_user_stats_list(stats)) async def reply_with_stats_days_difference(self, message, days: int, timeframe_alias: str): stats_datetime = datetime.datetime.now() - datetime.timedelta(days = days) persisted_stats = self.__stats_repository.get_stats(stats_datetime) if len(persisted_stats) > 0: friends = await self.__fortnite_client.get_friends() current_stats = [await friend.fetch_stats() for friend in friends] await self.__telegram_bot.reply(message, format_user_stats_difference(persisted_stats, current_stats, timeframe_alias)) else: await self.__telegram_bot.reply(message, 'No stats available yet for selected time period') class GetStatsCommand(CommandHandler): __telegram_bot: TelegramBot __fortnite_client: FortniteClient __stats_repository: StatsRepository __reply_markup: any def __init__(self, telegram_bot: TelegramBot, fortnite_client: FortniteClient, stats_repository: StatsRepository): self.__telegram_bot = telegram_bot self.__fortnite_client = fortnite_client self.__stats_repository = stats_repository self.__reply_markup = telebot.types.InlineKeyboardMarkup() self.__reply_markup.add(telebot.types.InlineKeyboardButton('Now', callback_data=__stats_now__)) self.__reply_markup.add(telebot.types.InlineKeyboardButton('1 day', callback_data=__stats_day__)) self.__reply_markup.add(telebot.types.InlineKeyboardButton('1 week', callback_data=__stats_week__)) self.__reply_markup.add(telebot.types.InlineKeyboardButton('1 month', callback_data=__stats_month__)) self.__telegram_bot.register_callback_query( GetStatsCallbackQueryHandler(self.__telegram_bot, self.__fortnite_client, self.__stats_repository), lambda call: True) async def handle(self, message: telebot.types.Message): await self.__telegram_bot.reply(message, 'Which statistics would you like to see?', reply_markup=self.__reply_markup) class GetTodayStatsCommand(CommandHandler): __telegram_bot: TelegramBot def __init__(self, telegram_bot: TelegramBot): self.__telegram_bot = telegram_bot async def handle(self, message: telebot.types.Message): await self.__telegram_bot.reply(message, 'Use new command /stats, you must, young Padawan') class RecordStatsCommand(CommandHandler): __fortnite_client: FortniteClient __stats_repository: StatsRepository def __init__(self, fortnite_client: FortniteClient, stats_repository: StatsRepository): self.__fortnite_client = fortnite_client self.__stats_repository = stats_repository async def handle(self, message: telebot.types.Message): if self.__fortnite_client.is_initialized(): stats_datetime = datetime.datetime.now() friends = await self.__fortnite_client.get_friends() for friend in friends: await self.__stats_repository.put_stats(friend, stats_datetime)