コード例 #1
0
import model
from model.exceptions import Unauthorized
from utils import inject_quoted_voice_id

meme_storage = model.get_storage()


@inject_quoted_voice_id
def delete(_, update, quoted_voice_id):
    """Deletes a meme by voice file"""

    message = update.message

    try:
        meme_name = meme_storage.get_by_file_id(quoted_voice_id).name
    except KeyError:
        message.reply_text("I don't know that meme, sorry.")
        return

    try:
        meme_storage.delete_by_file_id(quoted_voice_id, message.from_user.id)
    except Unauthorized:
        message.reply_text(
            "Sorry, you can only delete the memes you added yourself.")
        return

    message.reply_text(
        'The meme "{name}" has been deleted.'.format(name=meme_name))
コード例 #2
0
import model


sticker_storage = model.get_storage()


def sticker_handler(_, update):
    """Handles known stickers, returns their description"""

    sticker = sticker_storage.get_by_file_id(update.message.sticker.file_id)
    tags = sticker_storage.get_tags(sticker.id)

    update.message.reply_text(
        'Tags: {}\n'.format(', '.join(tags))
      + 'Times used: {}'.format(sticker.times_used)
    )
コード例 #3
0
from functools import wraps

from telegram.ext import BaseFilter

import model


class IsInDatabase(BaseFilter):
    def __init__(self, storage):
        self.storage = storage

    def filter(self, message):
        return self.storage.has_sticker_with_file_id(message.sticker.file_id)


is_in_database = IsInDatabase(storage=model.get_storage())


def user_ids(user_ids, func):
    @wraps(func)
    def wrapped(bot, update, *args, **kwargs):
        if update.message.from_user.id in user_ids:
            return func(bot, update, *args, **kwargs)

    return wrapped