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 share_with_moderator(bot, update, bot_in_question, moderator): user = User.from_update(update) answer_text = mdformat.success( "I will ask {} to have a look at this submission.".format( moderator.plaintext)) if update.callback_query: update.callback_query.answer(text=answer_text) buttons = [[ InlineKeyboardButton('Yea, let me take this one!', callback_data=util.callback_for_action( CallbackActions.APPROVE_REJECT_BOTS, {'id': bot_in_question.id})) ]] reply_markup = InlineKeyboardMarkup(buttons) text = "{} thinks that you have the means to inspect this bot submission:\n▶️ {}".format( user.markdown_short, bot_in_question) util.send_md_message(bot, moderator.chat_id, text, reply_markup=reply_markup, disable_web_page_preview=True) Statistic.of( update, 'share', 'submission {} with {}'.format(bot_in_question.username, moderator.plaintext))
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 send_success(self, chat_id, text: str, add_punctuation=True, reply_markup=None, **kwargs): if add_punctuation: if text[-1] != '.': text += '.' if not reply_markup: reply_markup = ReplyKeyboardRemove() return self.bot.sendMessage(chat_id, success(text), reply_markup=reply_markup, **self._set_defaults(kwargs))
def send_broadcast(bot, update, user_data): uid = update.effective_user.id try: bc = user_data['broadcast'] text = bc['text'] recipient = bc['target_chat_id'] mode = bc.get('mode', 'just_send') except AttributeError: bot.formatter.send_failure( uid, "Missing attributes for broadcast. Aborting...") return ConversationHandler.END mid = bc.get('reply_to_message_id') if mode == 'replying': msg = util.send_md_message(bot, recipient, text, reply_to_message_id=mid) elif mode == 'editing': msg = bot.formatter.send_or_edit(recipient, text, to_edit=mid) else: msg = util.send_md_message(bot, recipient, text) # Post actions buttons = [ InlineKeyboardButton(captions.PIN, callback_data=util.callback_for_action( 'pin_message', {'mid': msg.message_id})), InlineKeyboardButton('Add "Thank You" counter', callback_data=util.callback_for_action( 'add_thank_you', { 'cid': recipient, 'mid': msg.message_id })), ] reply_markup = InlineKeyboardMarkup(util.build_menu(buttons, 1)) mid = util.mid_from_update(update) action_taken = "edited" if mode == 'editing' else "broadcasted" bot.formatter.send_or_edit(uid, mdformat.success( "Message {}.".format(action_taken)), mid, reply_markup=reply_markup)
def share_with_moderator(bot, update, bot_in_question, moderator): user = User.from_update(update) buttons = [[ InlineKeyboardButton( "Yea, let me take this one!", callback_data=util.callback_for_action( CallbackActions.APPROVE_REJECT_BOTS, {"id": bot_in_question.id}), ) ]] reply_markup = InlineKeyboardMarkup(buttons) text = "{} thinks that you have the means to inspect this bot submission:\n▶️ {}".format( user.markdown_short, bot_in_question) try: util.send_md_message( bot, moderator.chat_id, text, reply_markup=reply_markup, disable_web_page_preview=True, ) answer_text = mdformat.success( "I will ask {} to have a look at this submission.".format( moderator.plaintext)) except Exception as e: answer_text = mdformat.failure( f"Could not contact {moderator.plaintext}: {e}") if update.callback_query: update.callback_query.answer(text=answer_text) Statistic.of( update, "share", "submission {} with {}".format(bot_in_question.username, moderator.plaintext), )