def handle_next(session, bot, chat, tg_chat, user): """Handle the /next call or the 'next' button click.""" # We are tagging a whole sticker set. Skip the current sticker if chat.tag_mode == TagMode.STICKER_SET: # Check there is a next sticker stickers = chat.current_sticker.sticker_set.stickers for index, sticker in enumerate(stickers): if sticker == chat.current_sticker and index + 1 < len(stickers): # We found the next sticker. Send the messages and return chat.current_sticker = stickers[index + 1] send_tag_messages(chat, tg_chat, user) return # There are no stickers left, reset the chat and send success message. chat.current_sticker.sticker_set.completely_tagged = True send_tagged_count_message(session, bot, user, chat) tg_chat.send_message('The full sticker set is now tagged.', reply_markup=get_main_keyboard(user)) chat.cancel(bot) # Find a random sticker with no changes elif chat.tag_mode == TagMode.RANDOM: base_query = session.query(Sticker) \ .outerjoin(Sticker.changes) \ .join(Sticker.sticker_set) \ .filter(Change.id.is_(None)) \ .filter(StickerSet.international.is_(False)) \ .filter(StickerSet.banned.is_(False)) \ .filter(StickerSet.nsfw.is_(False)) \ .filter(StickerSet.furry.is_(False)) \ # Let the users tag the deluxe sticker set first. # If there are no more deluxe sets, just tag another random sticker. # Remove the favoring of deluxe stickers until the deluxe pool is bigger again. # sticker = base_query.filter(StickerSet.deluxe.is_(True)) \ # .order_by(func.random()) \ # .limit(1) \ # .one_or_none() # if sticker is None: sticker = base_query \ .order_by(func.random()) \ .limit(1) \ .one_or_none() # No stickers for tagging left :) if not sticker: call_tg_func(tg_chat, 'send_message', ['It looks like all stickers are already tagged :).'], {'reply_markup': get_main_keyboard(user)}) chat.cancel(bot) return # Found a sticker. Send the messages chat.current_sticker = sticker send_tag_messages(chat, tg_chat, user, send_set_info=True)
def start(bot, update, session, chat, user): """Send the start text.""" if chat.is_maintenance or chat.is_newsfeed: update.message.chat.send_message("Hello there", reply_markup=get_main_keyboard(user)) else: update.message.chat.send_message( i18n.t("text.misc.start"), reply_markup=get_main_keyboard(user), parse_mode="Markdown", disable_web_page_preview=True, )
def start(bot, update, session, chat, user): """Send the start text.""" if chat.is_maintenance or chat.is_newsfeed: call_tg_func(update.message.chat, 'send_message', ['Hello there'], {'reply_markup': get_main_keyboard(user)}) else: update.message.chat.send_message( start_text, reply_markup=get_main_keyboard(user), parse_mode="Markdown", disable_web_page_preview=True, )
def cleanup(session, context): """Triggering a one time conversion from text changes to tags.""" threshold = datetime.strptime('Jan 1 2000', '%b %d %Y') full_cleanup(session, threshold, chat=context.tg_chat) context.tg_chat.send_message('Cleanup finished.', reply_markup=get_main_keyboard(context.user)) context.message.delete()
def main_menu(session, context): """Show the main menu.""" context.query.message.edit_text( start_text, reply_markup=get_main_keyboard(context.user), parse_mode="Markdown", disable_web_page_preview=True, )
def cancel(bot, update, session, chat, user): """Send a help text.""" if not send_tagged_count_message(session, bot, user, chat): keyboard = get_main_keyboard(user) update.message.chat.send_message('All running commands are canceled', reply_markup=keyboard) chat.cancel(bot)
def handle_cancel_tagging(session, context): """Cancel tagging for now.""" bot = context.bot # Send a message to the user, which shows how many stickers he already tagged, # if the user was just tagging some stickers. # Otherwise just send the normal cancel success message. if not send_tagged_count_message(session, bot, context.user, context.chat): context.query.answer('All active commands have been canceled') context.tg_chat.send_message('All running commands are canceled', reply_markup=get_main_keyboard(context.user)) context.chat.cancel(bot)
def refresh_ocr(session, context): """Refresh all stickers and rescan for text.""" tg_chat = context.tg_chat sticker_sets = session.query(StickerSet).all() tg_chat.send_message(f'Found {len(sticker_sets)} sticker sets.') count = 0 for sticker_set in sticker_sets: refresh_stickers(session, sticker_set, context.bot, refresh_ocr=True) count += 1 if count % 200 == 0: progress = f'Updated {count} sets ({len(sticker_sets) - count} remaining).' tg_chat.send_message(progress) tg_chat.send_message('All sticker sets are refreshed.', reply_markup=get_main_keyboard(context.user)) context.message.delete()
def handle_check_user(session, context): """Handle all actions from the check_user task.""" task = session.query(Task).get(context.payload) # Ban the user if CallbackResult(context.action).name == 'ban': task.user.banned = True call_tg_func(context.query, 'answer', ['User banned']) elif CallbackResult(context.action).name == 'unban': task.user.banned = False call_tg_func(context.query, 'answer', ['User ban reverted']) message = f'Your ban has been lifted.' context.bot.send_message(task.user.id, message, reply_markup=get_main_keyboard(task.user)) # Revert user changes elif CallbackResult(context.action).name == 'revert': task.reverted = True revert_user_changes(session, task.user) call_tg_func(context.query, 'answer', ['All user changes reverted']) elif CallbackResult(context.action).name == 'undo_revert': task.reverted = False undo_user_changes_revert(session, task.user) call_tg_func(context.query, 'answer', ['User changes revert undone']) # Change the language of all changes of this task. elif CallbackResult(context.action).name == 'change_language': change_language_of_task_changes(session, task) call_tg_func(context.query, 'answer', ['Language changed']) elif CallbackResult(context.action).name == 'ok': if not task.reviewed: task.reviewed = True check_maintenance_chat(session, context.tg_chat, context.chat) keyboard = check_user_tags_keyboard(task) call_tg_func(context.query.message, 'edit_reply_markup', [], {'reply_markup': keyboard})
def handle_check_user(session, context): """Handle all actions from the check_user task.""" task = session.query(Task).get(context.payload) # Ban the user if CallbackResult(context.action).name == "ban": task.user.banned = True call_tg_func(context.query, "answer", ["User banned"]) elif CallbackResult(context.action).name == "unban": task.user.banned = False call_tg_func(context.query, "answer", ["User ban reverted"]) message = f"Your ban has been lifted." context.bot.send_message(task.user.id, message, reply_markup=get_main_keyboard(task.user)) # Revert user changes elif CallbackResult(context.action).name == "revert": task.reverted = True revert_user_changes(session, task.user) call_tg_func(context.query, "answer", ["All user changes reverted"]) elif CallbackResult(context.action).name == "undo_revert": task.reverted = False undo_user_changes_revert(session, task.user) call_tg_func(context.query, "answer", ["User changes revert undone"]) # Change the language of all changes of this task. elif CallbackResult(context.action).name == "change_language": change_language_of_task_changes(session, task) call_tg_func(context.query, "answer", ["Language changed"]) elif CallbackResult(context.action).name == "ok": if not task.reviewed: task.reviewed = True check_maintenance_chat(session, context.tg_chat, context.chat) keyboard = check_user_tags_keyboard(task) call_tg_func(context.query.message, "edit_reply_markup", [], {"reply_markup": keyboard})
def stats(session, context): """Send a help text.""" # Users one_month_old = datetime.now() - timedelta(days=30) month_user_count = session.query(User) \ .join(User.inline_queries) \ .filter(InlineQuery.created_at > one_month_old) \ .group_by(User) \ .count() one_week_old = datetime.now() - timedelta(days=7) week_user_count = session.query(User) \ .join(User.inline_queries) \ .filter(InlineQuery.created_at > one_week_old) \ .group_by(User) \ .count() total_user_count = session.query(User).join( User.inline_queries).group_by(User).count() # Tags and emojis total_tag_count = session.query(sticker_tag.c.sticker_file_id) \ .join(Tag, sticker_tag.c.tag_name == Tag.name) \ .filter(Tag.emoji.is_(False)) \ .count() english_tag_count = session.query(Tag) \ .filter(Tag.international.is_(False)) \ .filter(Tag.emoji.is_(False)) \ .count() international_tag_count = session.query(Tag) \ .filter(Tag.international.is_(True)) \ .filter(Tag.emoji.is_(False)) \ .count() emoji_count = session.query(Tag).filter(Tag.emoji.is_(True)).count() # Stickers and sticker/text sticker/tag ratio sticker_count = session.query(Sticker).count() tagged_sticker_count = session.query(distinct(sticker_tag.c.sticker_file_id)) \ .join(Tag, sticker_tag.c.tag_name == Tag.name) \ .filter(Tag.emoji.is_(False)) \ .count() text_sticker_count = session.query(Sticker) \ .filter(Sticker.text.isnot(None)) \ .count() # Sticker set stuff sticker_set_count = session.query(StickerSet).count() normal_set_count = session.query(StickerSet) \ .filter(StickerSet.nsfw.is_(False)) \ .filter(StickerSet.furry.is_(False)) \ .filter(StickerSet.banned.is_(False)) \ .filter(StickerSet.international.is_(False)) \ .count() deluxe_set_count = session.query(StickerSet).filter( StickerSet.deluxe.is_(True)).count() nsfw_set_count = session.query(StickerSet).filter( StickerSet.nsfw.is_(True)).count() furry_set_count = session.query(StickerSet).filter( StickerSet.furry.is_(True)).count() banned_set_count = session.query(StickerSet).filter( StickerSet.banned.is_(True)).count() not_english_set_count = session.query(StickerSet).filter( StickerSet.international.is_(True)).count() # Inline queries total_queries_count = session.query(InlineQuery).count() last_day_queries_count = session.query(InlineQuery)\ .filter(InlineQuery.created_at > datetime.now() - timedelta(days=1)) \ .count() stats = f"""Users: => last week: {week_user_count} => last month: {month_user_count} => total: {total_user_count} Tags: => total: {total_tag_count} => english: {english_tag_count} => international: {international_tag_count} => emojis: {emoji_count} Stickers: => total: {sticker_count} => with tags: {tagged_sticker_count} => with text: {text_sticker_count} Sticker sets: => total: {sticker_set_count} => normal: {normal_set_count} => deluxe: {deluxe_set_count} => nsfw: {nsfw_set_count} => furry: {furry_set_count} => banned: {banned_set_count} => international: {not_english_set_count} Total queries : {total_queries_count} => last day: {last_day_queries_count} """ context.message.edit_text(stats, reply_markup=get_main_keyboard(context.user))