def _save_new_description(self, message): '''Getting a new card description. Parameters ---------- message : Message User message. ''' handler = tools.Handler(self.bot) if handler.cancel(message): return # Getting a unique card key from the database. fetch = db.Fetch(message.chat.id) card_key = fetch.user_attribute('session') # Card description update. update = db.Update(message.chat.id, 'collections', 'card') update.card_attribute(card_key, 'description', message.text) # User status update. update = db.Update(message.chat.id) update.user_attribute('action', 0) update.user_attribute('session', None) self.bot.send_message(message.chat.id, messages.CARDS['CARD_EDITED']) self.send_menu(message, card_key)
def delete_yes(self, call): '''Consent to remove the card. Parameters ---------- call : CallbackQuery Response to button press. ''' card_key = re.findall(r'\w-\d+-\d+-\w+', call.data)[0] # Getting card information from the database. fetch = db.Fetch(call.message.chat.id, 'collections', 'card') name = fetch.card_attribute(card_key, 'name') key = fetch.card_attribute(card_key, 'key') # Updating the number of user cards. update = db.Update(call.message.chat.id) update.change_user_attribute('cards', -1) # Updating the number of cards in the user's collection. update = db.Update(call.message.chat.id, 'collections', 'collection') update.change_collection_attribute(key, 'cards', -1) # Removing a card from the database. delete = db.Delete(call.message.chat.id, 'collections', 'card') delete.delete_card(card_key) text = messages.CARD['DELETE']['SUCCESSFUL'].format(name) self.bot.answer_callback_query(call.id, text, True) cards = Cards(self.bot) cards.call_menu(call, key, 0)
def copy_collection(self, message): '''Creating a copy of a user collection. Parameters ---------- message : Message User message. ''' # Getting a copy of the name and number of cards in the collection. fetch = db.Fetch(message.chat.id, 'collections', 'collection') name = fetch.general_collection(message.text, 'name') cards = fetch.general_collection(message.text, 'cards') karma = fetch.general_collection(message.text, 'user_id') # Checking for a duplicate collection in the database. fetch = db.Fetch(message.chat.id, 'collections', 'collection') result = fetch.copy_check('name', f'{name} (Копия)') if result: self.bot.send_message(message.chat.id, messages.ERRORS[9]) self.bot.register_next_step_handler(message, self.copy_collection) return # Getting a unique collection key from the database. fetch = db.Fetch(message.chat.id) key = fetch.user_attribute('session') # Making copies of cards from the original collection. date = datetime.datetime.now() insert = db.Insert(message.chat.id, 'collections', 'card') insert.copy_collection(message.text, key, date) # Inserting the name of the collection # and the number of cards in the database. update = db.Update(message.chat.id, 'collections', 'collection') update.collection_attribute(key, 'name', f'{name} (Копия)') update.collection_attribute(key, 'cards', cards) # User status update. update = db.Update(message.chat.id) update.user_attribute('action', 0) update.user_attribute('session', None) update.change_user_attribute('collections', 1) update.change_user_attribute('cards', cards) # Adding karma to the user whose collection was copied. if karma != message.chat.id: update = db.Update(karma) update.change_user_attribute('karma', 1) text = messages.COLLECTIONS['COLLECTION_COPIED'] self.bot.send_message(message.chat.id, text.format(name)) self.send_menu(message)
def create_collection(self, call): '''Collection creation. Parameters ---------- call : CallbackQuery Response to button press. ''' handler = tools.Handler(self.bot) if handler.error(call.message) or handler.cancel(call.message): return date = datetime.datetime.now() key = f'k-{random.randint(1, 1000000000)}' \ f'-{random.randint(1, 1000000000)}-n' # User status update. update = db.Update(call.message.chat.id) update.user_attribute('action', 1) update.user_attribute('session', key) # Allocating space for a collection in the database. insert = db.Insert(call.message.chat.id, 'collections', 'collection') insert.create_collection(key, date) text = messages.COLLECTIONS['CREATE_COLLECTION'] self.bot.answer_callback_query(call.id, text, True) self.bot.send_message(call.message.chat.id, text) self.bot.register_next_step_handler(call.message, self._save_collection)
def delete_yes(self, call): '''Consent to delete collection. Parameters ---------- call : CallbackQuery Response to button press. ''' key = re.findall(r'\w-\d+-\d+-\w+', call.data)[0] # Getting collection name from database. fetch = db.Fetch(call.message.chat.id, 'collections', 'collection') name = fetch.collection_attribute(key, 'name') cards = fetch.collection_attribute(key, 'cards') # Change the number of collections and cards in the user profile. update_user_status = db.Update(call.message.chat.id) update_user_status.change_user_attribute('collections', -1) update_user_status.change_user_attribute('cards', -int(cards)) # Removing a collection from a database. delete = db.Delete(call.message.chat.id, 'collections', 'collection') delete.delete_collection(key) # Removing all cards in a collection from the database. delete = db.Delete(call.message.chat.id, 'collections', 'card') delete.delete_collection_cards(key) text = messages.COLLECTION['DELETE']['SUCCESSFUL'].format(name) self.bot.answer_callback_query(call.id, text, True) collections = Collections(self.bot) collections.call_menu(call)
def edit_description(self, call): '''Change card description. Parameters ---------- call : CallbackQuery Response to button press. ''' handler = tools.Handler(self.bot) if handler.error(call.message) or handler.cancel(call.message): return card_key = re.findall(r'\w-\d+-\d+-\w+', call.data)[0] # User status update. update = db.Update(call.message.chat.id) update.user_attribute('action', 5) update.user_attribute('session', card_key) next_step = self._save_new_description text = messages.CARDS['EDIT_DESCRIPTION_CARD'] self.bot.answer_callback_query(call.id, text, True) self.bot.send_message(call.message.chat.id, text) self.bot.register_next_step_handler(call.message, next_step)
def cancel(message): '''Canceling the current operation. Parameters ---------- message : Message User message. ''' # Getting information about user status from the database. fetch = db.Fetch(message.chat.id) action = fetch.user_attribute('action') session = fetch.user_attribute('session') if action == 1: # Removing a collection-reserved space from the database. delete = db.Delete(message.chat.id, 'collections', 'collection') delete.delete_collection(session) elif action == 3: # Removing a card-reserved space from the database. delete = db.Delete(message.chat.id, 'collections', 'card') delete.delete_card(session) # User status update. update = db.Update(message.chat.id) update.user_attribute('action', 0) update.user_attribute('session', None) bot.send_message(message.chat.id, messages.ASSISTANCE['CANCEL']) private_office(message)
def create_card(self, call): '''Create a card. Parameters ---------- call : CallbackQuery Response to button press. ''' handler = tools.Handler(self.bot) if handler.error(call.message) or handler.cancel(call.message): return key = re.findall(r'\w-\d+-\d+-\w+', call.data)[0] card_key = f'c-{random.randint(1, 1000000000)}-' \ f'{random.randint(1, 1000000000)}-d' # User status update. update = db.Update(call.message.chat.id) update.user_attribute('action', 3) update.user_attribute('session', card_key) # Allocating space for the card in the database. insert = db.Insert(call.message.chat.id, 'collections', 'card') insert.create_card(key, card_key, datetime.datetime.now()) text = messages.CARDS['CARD_NAME'] self.bot.answer_callback_query(call.id, text, True) self.bot.send_message(call.message.chat.id, text) self.bot.register_next_step_handler(call.message, self._card_name)
def _save_collection(self, message): '''Get the name of the collection and save it to the database. Parameters ---------- message : Message User message. ''' handler = tools.Handler(self.bot) if handler.cancel(message): return # Checking for the existence of a collection with a similar key. fetch = db.Fetch(message.chat.id, 'collections', 'collection') copy_key = fetch.general_collection(message.text, 'key') if copy_key: self.copy_collection(message) return # Checking for a duplicate collection in the database. fetch = db.Fetch(message.chat.id, 'collections', 'collection') result = fetch.copy_check('name', message.text) if result: self.bot.send_message(message.chat.id, messages.ERRORS[3]) self.bot.register_next_step_handler(message, self._save_collection) return # Getting a unique collection key from the database. fetch = db.Fetch(message.chat.id) key = fetch.user_attribute('session') # Inserting the collection name to the database. update = db.Update(message.chat.id, 'collections', 'collection') update.collection_attribute(key, 'name', message.text) # User status update. update = db.Update(message.chat.id) update.user_attribute('action', 0) update.user_attribute('session', None) update.change_user_attribute('collections', 1) text = messages.COLLECTIONS['COLLECTION_CREATED'] self.bot.send_message(message.chat.id, text.format(message.text)) self.send_menu(message)
def _save_new_name(self, message): '''Getting a new card name. Parameters ---------- message : Message User message. ''' handler = tools.Handler(self.bot) if handler.cancel(message): return # Getting a unique card key. fetch = db.Fetch(message.chat.id) card_key = fetch.user_attribute('session') # Getting the unique key of the collection that the card belongs to. fetch = db.Fetch(message.chat.id, 'collections', 'card') key = fetch.card_attribute(card_key, 'key') # Getting the old card name and # checking for a duplicate card in the database. fetch = db.Fetch(message.chat.id, 'collections', 'card') old_name = fetch.card_attribute(card_key, 'name') result = fetch.card_copy_check(key, 'name', message.text) if result: self.bot.send_message(message.chat.id, messages.ERRORS[6]) self.bot.register_next_step_handler(message, self._save_new_name) return # Updating the card name in the database. update = db.Update(message.chat.id, 'collections', 'card') update.card_attribute(card_key, 'name', message.text) # User status update. update = db.Update(message.chat.id) update.user_attribute('action', 0) update.user_attribute('session', None) text = messages.CARDS['CARD_RENAMED'].format(old_name, message.text) self.bot.send_message(message.chat.id, text) self.send_menu(message, card_key)
def _card_description(self, message): '''Getting a card description. Parameters ---------- message : Message User message. ''' handler = tools.Handler(self.bot) if handler.cancel(message): return # Getting a unique card key. fetch = db.Fetch(message.chat.id) card_key = fetch.user_attribute('session') # Getting the unique key of the collection that the card belongs to. fetch = db.Fetch(message.chat.id, 'collections', 'card') key = fetch.card_attribute(card_key, 'key') # Getting the card name. fetch = db.Fetch(message.chat.id, 'collections', 'card') card_name = fetch.card_attribute(card_key, 'name') # Inserting a card description to the database. update = db.Update(message.chat.id, 'collections', 'card') update.card_attribute(card_key, 'description', message.text) # Updating user status and number of cards. update = db.Update(message.chat.id) update.user_attribute('action', 0) update.user_attribute('session', None) update.change_user_attribute('cards', 1) # Updating the number of cards in the user's collection. update = db.Update(message.chat.id, 'collections', 'collection') update.change_collection_attribute(key, 'cards', 1) text = messages.CARDS['CARD_CREATED'] self.bot.send_message(message.chat.id, text.format(card_name)) self.send_menu(message, key)
def _save_new_name(self, message): '''Getting a new collection name. Parameters ---------- message : Message User message. ''' handler = tools.Handler(self.bot) if handler.cancel(message): return # Getting the name of a collection to check for a duplicate # collection in the database. fetch = db.Fetch(message.chat.id, 'collections', 'collection') result = fetch.copy_check('name', message.text) if result: self.bot.send_message(message.chat.id, messages.ERRORS[3]) self.bot.register_next_step_handler(message, self._save_new_name) return # Getting a unique key and old collection name. fetch_status = db.Fetch(message.chat.id) key = fetch_status.user_attribute('session') old_name = fetch.collection_attribute(key, 'name') # Updating the collection name in the database. update = db.Update(message.chat.id, 'collections', 'collection') update.collection_attribute(key, 'name', message.text) # User status update. update = db.Update(message.chat.id) update.user_attribute('action', 0) update.user_attribute('session', None) text = messages.COLLECTIONS['COLLECTION_RENAMED'] formatted_text = text.format(old_name, message.text) self.bot.send_message(message.chat.id, formatted_text) self.send_menu(message, key)
def private_office(message): '''User's personal account. Parameters ---------- message : Message User message. ''' menu = tools.Maker.keyboard(3, **messages.OFFICE['BUTTONS']) menu_message = bot.send_message(chat_id=message.chat.id, text=messages.OFFICE['INTERFACE'], reply_markup=menu) # Updating the message_id of the Personal Account. update = db.Update(message.chat.id) update.user_attribute('menu_id', menu_message.message_id)
def send_menu(self, message): '''Sending a collections menu. Parameters ---------- message : Message User message. ''' menu = self._create_menu(message) menu_message = self.bot.send_message( chat_id=message.chat.id, text=messages.COLLECTIONS['INTERFACE'], reply_markup=menu) # Updating the message_id of the Private Office. update = db.Update(message.chat.id) update.user_attribute('menu_id', menu_message.message_id)
def result(self, call): '''Getting learning outcomes and moving to the next card. Parameters ---------- call : CallbackQuery Response to button press. ''' minutes = int(call.data[-4:]) status = datetime.datetime.now() + datetime.timedelta(minutes=minutes) card_key = re.findall(r'_\w-\d+-\d+-\w_(\w-\d+-\d+-\w)', call.data)[0] # Inserting a new result to the database. update = db.Update(call.message.chat.id, 'collections', 'card') update.card_attribute(card_key, 'status', status) training = collection.Collection(self.bot) training.continue_learning(call)
def send_menu(self, message, card_key): '''Sending the main menu of the card. Parameters ---------- message : Message User message. card_key : str Unique card key. ''' menu, text = self._create_menu(message, card_key) menu_message = self.bot.send_message(chat_id=message.chat.id, text=text, reply_markup=menu, parse_mode='Markdown') # Updating the message_id of the Private Office. update = db.Update(message.chat.id) update.user_attribute('menu_id', menu_message.message_id)
def send_menu(self, message, key): '''Sending menu cards collection. Parameters ---------- call : CallbackQuery Response to button press. key : str Unique collection key (default is None). level : int Menu page (default is None). ''' menu, text = self._create_menu(message, key) menu_message = self.bot.send_message(chat_id=message.chat.id, text=text, reply_markup=menu) # Updating the message_id of the Private Office. update = db.Update(message.chat.id) update.user_attribute('menu_id', menu_message.message_id)
def change_voice_mask(call): '''Change the voice mask of a specific user. Parameters ---------- call : CallbackQuery Response to button press. ''' mask_id = call.data.split('_')[2] if (mask_id == '0') or (mask_id == '3'): update = db.Update(call.message.chat.id) update.user_attribute('voice', int(mask_id)) bot.answer_callback_query(call.id, 'Голосовая маска изменена!', True) else: bot.answer_callback_query(call.id) menu = tools.Maker.keyboard(2, **messages.PROFILE['VOICE']) bot.edit_message_text(text=messages.PROFILE['VOICE_INTERFACE'], chat_id=call.message.chat.id, message_id=call.message.message_id, reply_markup=menu)
def _card_name(self, message): '''Getting the name of the card. Parameters ---------- message : Message User message. ''' handler = tools.Handler(self.bot) if handler.cancel(message): return # Getting a unique card key. fetch = db.Fetch(message.chat.id) card_key = fetch.user_attribute('session') # Getting the unique key of the collection that the card belongs to. fetch = db.Fetch(message.chat.id, 'collections', 'card') key = fetch.card_attribute(card_key, 'key') # Checking for the existence of a duplicate card in the database. fetch = db.Fetch(message.chat.id, 'collections', 'card') result = fetch.card_copy_check(key, 'name', message.text) if result: self.bot.send_message(message.chat.id, messages.ERRORS[6]) self.bot.register_next_step_handler(message, self._card_name) return # Inserting the card name to the database. update = db.Update(message.chat.id, 'collections', 'card') update.card_attribute(card_key, 'name', message.text) text = messages.CARDS['CARD_DESCRIPTION'] self.bot.send_message(message.chat.id, text) self.bot.register_next_step_handler(message, self._card_description)
def rename(self, call): '''Renaming a card. Parameters ---------- call : CallbackQuery Response to button press. ''' handler = tools.Handler(self.bot) if handler.error(call.message) or handler.cancel(call.message): return card_key = re.findall(r'\w-\d+-\d+-\w+', call.data)[0] # User status update. update = db.Update(call.message.chat.id) update.user_attribute('action', 4) update.user_attribute('session', card_key) text = messages.CARDS['RENAME_CARD'] self.bot.answer_callback_query(call.id, text, True) self.bot.send_message(call.message.chat.id, text) self.bot.register_next_step_handler(call.message, self._save_new_name)