示例#1
0
    def _set_antispam_command_callback(self, update: Update, context: CallbackContext, new_state: str):
        bot = context.bot
        chat_id = update.effective_message.chat_id
        message_id = update.effective_message.message_id

        chat = self._persistence.get_chat(chat_id)
        chat.set_setting(SETTINGS_ANTISPAM_ENABLED_KEY, new_state)
        self._persistence.add_or_update_chat(chat)

        send_message(bot, chat_id, message="Antispam: {}".format(new_state), reply_to=message_id)
示例#2
0
    def _list_users_command_callback(self, update: Update, context: CallbackContext):
        bot = context.bot
        chat_id = update.effective_message.chat_id
        message_id = update.effective_message.message_id

        chat = self._persistence.get_chat(chat_id)
        message = "\n".join(
            list(map(lambda x: f"{x.id}: {x.username}" + (" (BANNED)" if x.is_banned else ""), chat.users)))

        send_message(bot, chat_id, message=message, reply_to=message_id)
示例#3
0
    def _config_command_callback(self, update: Update, context: CallbackContext):
        from container_app_conf.formatter.toml import TomlFormatter

        bot = context.bot
        chat_id = update.effective_message.chat_id
        message_id = update.effective_message.message_id

        text = self._config.print(formatter=TomlFormatter())
        text = "```\n{}\n```".format(text)
        send_message(bot, chat_id, text, parse_mode=ParseMode.MARKDOWN, reply_to=message_id)
示例#4
0
    def _set_chance_command_callback(self, update: Update, context: CallbackContext, probability):
        bot = context.bot
        chat_id = update.effective_message.chat_id
        message_id = update.effective_message.message_id

        chat = self._persistence.get_chat(chat_id)
        chat.set_setting(SETTINGS_TRIGGER_PROBABILITY_KEY, str(probability))
        self._persistence.add_or_update_chat(chat)

        send_message(bot, chat_id, message="TriggerChance: {}%".format(probability * 100), reply_to=message_id)
示例#5
0
    def _stats_command_callback(self, update: Update, context: CallbackContext) -> None:
        """
        /stats command handler
        :param update: the chat update object
        :param context: telegram context
        """
        bot = context.bot
        message_id = update.effective_message.message_id
        chat_id = update.effective_message.chat_id

        text = format_metrics()

        send_message(bot, chat_id, text, reply_to=message_id)
示例#6
0
    def _get_settings_command_callback(self, update: Update, context: CallbackContext):
        bot = context.bot
        chat_id = update.effective_message.chat_id
        message_id = update.effective_message.message_id

        chat = self._persistence.get_chat(chat_id)

        lines = []
        for setting in chat.settings:
            lines.append("{}: {}".format(setting.key, setting.value))

        message = "\n".join(lines)
        if message:
            send_message(bot, chat_id, message, reply_to=message_id)
        else:
            # TODO: would be better if this could display default values to give at least some information
            send_message(bot, chat_id, "No chat specific settings set", reply_to=message_id)
示例#7
0
    def _ban_command_callback(self, update: Update, context: CallbackContext, user: str):
        bot = context.bot
        chat_id = update.effective_message.chat_id
        message_id = update.effective_message.message_id

        try:
            user_id = int(user)
            user_entity = self._persistence.get_user(user_id)
        except:
            user_entity = self._persistence.get_user_by_username(user)

        if user_entity is None:
            send_message(bot, chat_id, message=f"User {user} is unknown", reply_to=message_id)
            return

        user_id = user_entity.id
        username = user_entity.username
        user_entity.is_banned = True
        self._persistence.add_or_update_user(user_entity)

        send_message(bot, chat_id, message=f"Banned user: {username} ({user_id})",
                     reply_to=message_id)
示例#8
0
    def _shout(self, bot: Bot, message, text: str, reply: bool or int = True):
        """
        Shouts a message into the given chat
        :param bot: bot object
        :param message: message object
        :param text: text to shout
        :param reply: True to reply to the message's user, int to reply to a specific message, False is no reply
        """
        shouting_text = "<b>{}!!!</b>".format(text.upper())

        reply_to_message_id = None
        if reply:
            if isinstance(reply, bool):
                reply_to_message_id = message.message_id
            elif isinstance(reply, int):
                reply_to_message_id = reply
            else:
                raise AttributeError(f"Unsupported reply parameter: {reply}")

        send_message(bot, message.chat_id,
                     message=shouting_text,
                     parse_mode=ParseMode.HTML,
                     reply_to=reply_to_message_id)
示例#9
0
 def _version_command_callback(self, update: Update, context: CallbackContext):
     bot = context.bot
     chat_id = update.effective_message.chat_id
     message_id = update.effective_message.message_id
     text = "Version: `{}`".format(DEINE_MUDDA_VERSION)
     send_message(bot, chat_id, text, parse_mode=ParseMode.MARKDOWN, reply_to=message_id)
示例#10
0
 def _help_command_callback(self, update: Update, context: CallbackContext):
     bot = context.bot
     chat_id = update.effective_message.chat_id
     text = generate_command_list(update, context)
     send_message(bot, chat_id, text, parse_mode=ParseMode.MARKDOWN)