Пример #1
0
def warn(bot: octobot.OctoBot, context: octobot.Context):
    if octobot.Database.redis is None:
        raise octobot.DatabaseNotAvailable
    if context.update.message.reply_to_message is None:
        context.reply(context.localize("Reply to user to give them a warn"))
        return
    if octobot.check_permissions(
            chat=context.chat,
            user=context.update.message.reply_to_message.from_user,
            permissions_to_check={"is_admin"})[0]:
        context.reply(context.localize("Can't warn administrator"))
        return
    target_id = context.update.message.reply_to_message.from_user.id
    target_name = context.update.message.reply_to_message.from_user.first_name
    user_warns_db_id = create_warn_db_id(context.chat.id, target_id)
    max_warns = int(context.chat_db.get("max_warns", 3))
    reason = context.query
    if reason == "":
        reason = context.localize("Warn reason not specified")
    octobot.Database.redis.lpush(user_warns_db_id, reason)
    user_warn_count = int(octobot.Database.redis.llen(user_warns_db_id))
    action_taken = ""
    reply_markup = telegram.InlineKeyboardMarkup.from_button(
        telegram.InlineKeyboardButton(
            text="Remove last warn",
            callback_data=f"warn_cancel:{target_id}:{context.chat.id}"))
    if user_warn_count >= max_warns:
        action_taken = context.localize(
            "\n\n<i>User had reached maximum warnings in chat and was banned</i>"
        )
        bot.kick_chat_member(chat_id=context.chat.id, user_id=target_id)
        octobot.Database.redis.delete(user_warns_db_id)
        reply_markup = telegram.InlineKeyboardMarkup.from_button(
            telegram.InlineKeyboardButton(
                callback_data=f"ban_cancel:{target_id}:{context.chat.id}",
                text=context.localize("Unban")))
    context.reply(context.localize('⚠�<a href="tg://user?id={admin_id}">{admin}</a> warned ' + \
                                   '<a href="tg://user?id={target_id}">{target_name}</a> ' + \
                                   '({warn_count}/{max_warns})' + \
                                   '\nReason: <i>{reason}</i>').format(
        admin_id=context.user.id,
        admin=html.escape(context.user.first_name),
        target_id=target_id,
        target_name=html.escape(target_name),
        reason=html.escape(reason),
        warn_count=user_warn_count,
        max_warns=max_warns
    ) + action_taken, parse_mode="HTML",
                  reply_markup=reply_markup)
Пример #2
0
def ban(bot: octobot.OctoBot, chat: telegram.Chat, user: telegram.User):
    bot.kick_chat_member(chat.id, user.id)