def ban_bot(bot, update, chat_data, to_ban: Bot, ban_state: bool): if to_ban.disabled and ban_state is True: update.message.reply_text( mdformat.none_action("{} is already banned.".format(to_ban)), parse_mode="markdown", ) return if not to_ban.disabled and ban_state is False: update.message.reply_text( mdformat.none_action("{} is not banned.".format(to_ban)), parse_mode="markdown", ) return if ban_state: to_ban.disable(Bot.DisabledReason.banned) update.message.reply_text("Bot was banned.") else: to_ban.enable() update.message.reply_text("Bot was unbanned.") to_ban.save() from components.explore import send_bot_details return send_bot_details(bot, update, chat_data, to_ban)
def ban_user(bot, update, user: User, ban_state: bool): if user.banned and ban_state is True: update.message.reply_text(mdformat.none_action( "User {} is already banned.".format(user)), parse_mode='markdown') return if not user.banned and ban_state is False: update.message.reply_text(mdformat.none_action( "User {} is not banned.".format(user)), parse_mode='markdown') return user.banned = ban_state if ban_state is True: with db.atomic(): users_bots = Bot.select().where((Bot.approved == False) & (Bot.submitted_by == user)) for b in users_bots: b.delete_instance() users_suggestions = Suggestion.select().where( (Suggestion.executed == False) & (Suggestion.user == user)) for s in users_suggestions: s.delete_instance() update.message.reply_text(mdformat.success( "User {} banned, all bot submissions and suggestions removed.". format(user)), parse_mode='markdown') Statistic.of(update, 'ban', user.markdown_short) else: update.message.reply_text(mdformat.success( "User {} unbanned.".format(user)), parse_mode='markdown') Statistic.of(update, 'unban', user.markdown_short) user.save()
def add_favorite(bot, update, item: Bot, callback_alert=None): user = User.from_update(update) uid = util.uid_from_update(update) mid = util.mid_from_update(update) from components.basic import main_menu_buttons main_menu_markup = ReplyKeyboardMarkup(main_menu_buttons(uid in settings.MODERATORS)) fav, created = Favorite.add(user=user, item=item) if created: Statistic.of(user, 'add-favorite', item.username) text = mdformat.love("{} added to your {}favorites.".format(fav.bot, '' if callback_alert else '/')) if callback_alert: update.callback_query.answer(text=text, show_alert=False) else: msg = util.send_md_message(bot, uid, text, to_edit=mid, reply_markup=main_menu_markup) mid = msg.message_id util.wait(bot, update) send_favorites_list(bot, update, to_edit=mid) else: text = mdformat.none_action( "{} is already a favorite of yours.{}".format(fav.bot, '' if callback_alert else ' /favorites')) if callback_alert: update.callback_query.answer(text=text, show_alert=False) else: util.send_md_message(bot, uid, text, reply_markup=main_menu_markup) return ConversationHandler.END
def finish(self): # set last update self.channel.last_update = datetime.date.today() self._save_channel() new_bots = Bot.select_new_bots() if not self.silent and len(new_bots) > 0: self.notify_admin("Sending notifications to subscribers...") subscribers = Notifications.select().where(Notifications.enabled == True) notification_count = 0 for sub in subscribers: try: util.send_md_message(self.bot, sub.chat_id, messages.BOTLIST_UPDATE_NOTIFICATION.format( n_bots=len(new_bots), new_bots=Bot.get_new_bots_markdown())) notification_count += 1 sub.last_notification = datetime.date.today() sub.save() except TelegramError: pass self.sent['notifications'] = "Notifications sent to {} users.".format( notification_count) changes_made = len(self.sent) > 1 or len(self.sent['category']) > 0 if changes_made: text = util.success('{}{}'.format('BotList updated successfully:\n\n', mdformat.results_list(self.sent))) else: text = mdformat.none_action("No changes were necessary.") log.info(self.sent) self.bot.formatter.send_or_edit(self.chat_id, text, to_edit=self.message_id)
def explore(bot, update, chat_data): cid = update.effective_chat.id uid = update.effective_user.id mid = util.mid_from_update(update) explorable_bots = Bot.explorable_bots() chat_data["explored"] = chat_data.get("explored", list()) # don't explore twice for explored in chat_data["explored"]: explorable_bots.remove(explored) if len(explorable_bots) == 0: util.send_md_message( bot, cid, mdformat.none_action( "You have explored all the bots. Congratulations, you might be the first 😜" ), ) return random_bot = random.choice(explorable_bots) buttons = [ [ InlineKeyboardButton( captions.ADD_TO_FAVORITES, callback_data=util.callback_for_action( CallbackActions.ADD_TO_FAVORITES, {"id": random_bot.id}), ), InlineKeyboardButton(captions.SHARE, switch_inline_query=random_bot.username), ], [ InlineKeyboardButton( random_explore_text(), callback_data=util.callback_for_action( CallbackActions.EXPLORE_NEXT), ) ], ] markup = InlineKeyboardMarkup(buttons) text = random_bot.detail_text if uid in settings.MODERATORS and util.is_private_message(update): text += "\n\n🛃 /edit{}".format(random_bot.id) msg = bot.formatter.send_or_edit(cid, text, to_edit=mid, reply_markup=markup) chat_data["explored"].append(random_bot)
def ban_user(_bot, update, user: User, ban_state: bool): if user.banned and ban_state is True: update.message.reply_text( mdformat.none_action("User {} is already banned.".format(user)), parse_mode="markdown", ) raise DispatcherHandlerStop if not user.banned and ban_state is False: update.message.reply_text( mdformat.none_action("User {} is not banned.".format(user)), parse_mode="markdown", ) raise DispatcherHandlerStop user.banned = ban_state if ban_state is True: with db.atomic(): user_submissions = Bot.select().where( (Bot.approved == False) & (Bot.submitted_by == user) # TODO: does this need to include `Bot.deleted == True`? ) for b in user_submissions: b.delete_instance() users_suggestions = Suggestion.select().where( (Suggestion.executed == False) & (Suggestion.user == user)) for s in users_suggestions: s.delete_instance() update.message.reply_text( mdformat.success( "User {} banned, all bot submissions and suggestions removed.". format(user)), parse_mode="markdown", ) Statistic.of(update, "ban", user.markdown_short) else: update.message.reply_text(mdformat.success( "User {} unbanned.".format(user)), parse_mode="markdown") Statistic.of(update, "unban", user.markdown_short) user.save()
def add_custom(bot, update, username): uid = util.uid_from_update(update) user = User.from_update(update) mid = util.mid_from_update(update) from components.basic import main_menu_buttons main_menu_markup = ReplyKeyboardMarkup(main_menu_buttons(uid in settings.MODERATORS)) try: fav = Favorite.get(custom_bot=username) util.send_or_edit_md_message( bot, uid, mdformat.none_action( "{} is already a favorite of yours. /favorites".format(fav.custom_bot)), to_edit=mid, reply_markup=main_menu_markup) except Favorite.DoesNotExist: fav = Favorite(user=user, custom_bot=username, date_added=datetime.date.today()) fav.save() msg = bot.formatter.send_or_edit(uid, mdformat.love("{} added to your /favorites.".format(fav.custom_bot)), to_edit=mid) mid = msg.message_id util.wait(bot, update) send_favorites_list(bot, update, to_edit=mid) return ConversationHandler.END
def notify_bot_offline(bot, update, args=None): tg_user = update.message.from_user user = User.from_telegram_object(tg_user) if util.stop_banned(update, user): return reply_to = util.original_reply_id(update) if args: text = ' '.join(args) else: text = update.message.text command_no_args = len(re.findall( r'^/offline\s*$', text)) > 0 or text.lower().strip() == '/offline@botlistbot' if command_no_args: update.message.reply_text(util.action_hint( "Please use this command with an argument. For example:\n/offline @mybot" ), reply_to_message_id=reply_to) return # `#offline` is already checked by handler try: username = re.match(settings.REGEX_BOT_IN_TEXT, text).groups()[0] if username == '@' + settings.SELF_BOT_NAME: log.info("Ignoring {}".format(text)) return except AttributeError: if args: update.message.reply_text(util.failure( "Sorry, but you didn't send me a bot `@username`."), quote=True, parse_mode=ParseMode.MARKDOWN, reply_to_message_id=reply_to) else: log.info("Ignoring {}".format(text)) # no bot username, ignore update pass return def already_reported(): update.message.reply_text(mdformat.none_action( "Someone already reported this, thanks anyway 😊"), reply_to_message_id=reply_to) try: offline_bot = Bot.get( fn.lower(Bot.username)**username.lower(), Bot.approved == True) if offline_bot.offline: return already_reported() if offline_bot.official: update.message.reply_text(mdformat.none_action( "Official bots usually don't go offline for a long time. " "Just wait a couple hours and it will be back up ;)"), reply_to_message_id=reply_to) return try: Suggestion.get(action="offline", subject=offline_bot, executed=False) return already_reported() except Suggestion.DoesNotExist: suggestion = Suggestion(user=user, action="offline", value=True, date=datetime.date.today(), subject=offline_bot) suggestion.save() update.message.reply_text(util.success( "Thank you! We will review your suggestion and set the bot offline." ), reply_to_message_id=reply_to) except Bot.DoesNotExist: update.message.reply_text( util.action_hint("The bot you sent me is not in the @BotList."), reply_to_message_id=reply_to) return ConversationHandler.END
def already_reported(): update.message.reply_text(mdformat.none_action( "Someone already reported this, thanks anyway 😊"), reply_to_message_id=reply_to)