Ejemplo n.º 1
0
def self_kick_callback(bot, message):
    """ if bot gets kicked it erases all data for chat """

    cid = get_cid(message)

    with db:
        chat = Chats.select().where(Chats.cid == cid)

        if chat.exists():
            chat = chat.get()
            invited_by = chat.invited_by
            chat.delete_instance()

            tacos = Tacos.select().where(Tacos.chat == cid)
            if tacos.exists():
                tacos.get().delete_instance()

            chat_title = message.chat.title

    try:
        bot.send_message(invited_by,
                         data_deleted_phrase.format(chat_title),
                         parse_mode='html')
    except Exception as e:
        """ user blocked the bot """
        print(e)
Ejemplo n.º 2
0
def tacoinflator():
    for chat in Tacos.select():
        chat = chat.get()
        tacos = json.loads(chat.taco_balance)
        for user in tacos:
            tacos.update({user: tacos.get(user) + 15})
        chat.taco_balance = json.dumps(tacos)
        chat.save()
Ejemplo n.º 3
0
def tacoinflator():
    for chat in Tacos.select():
        chat = chat.get()
        tacos = chat.taco_balance
        for user in tacos:
            tacos.update(
                {user: tacos.get(user) + default_taco_inflation_amount})
        chat.taco_balance = tacos
        chat.save()
Ejemplo n.º 4
0
def new_chat_callback(bot, message):
    """ triggers when bot gets added to new chat """

    cid = get_cid(message)
    store_name(message.from_user)

    invited_by = message.from_user

    Chats.create(id=len(Chats.select()) + 1000,
                 cid=cid,
                 invited_by=invited_by.id)

    Tacos.create(id=len(Tacos.select()) + 1000, chat=cid)

    bot.send_message(cid, chat_enabled_phrase, parse_mode='html')
Ejemplo n.º 5
0
def self_kick_callback(
        update, context):  # if bot gets kicked it erases all data for chat
    cid = get_cid(update)

    chat = Chats.select().where(Chats.cid == cid)

    if chat.exists():
        chat = chat.get()
        invited_by = chat.invited_by

        tacos = Tacos.select().where(Tacos.chat == chat.id)
        if tacos.exists():
            Tacos.get(Tacos.chat == chat.id).delete_instance()
        chat.delete_instance()

        try:
            chat_title = update.effective_message.chat.title
            context.bot.send_message(invited_by,
                                     data_deleted_phrase.format(chat_title),
                                     parse_mode=ParseMode.HTML)
        except Unauthorized:
            pass  # user deleted bot or didnt /start it
Ejemplo n.º 6
0
 def filter(self, message):
     chat = Chats.get(Chats.cid == message.chat.id)
     tacos = Tacos.select(Tacos.chat == chat.id)
     return tacos.exists()
Ejemplo n.º 7
0
def chat_reply_callback(update, context):  # callback for taco-transfer
    cid = get_cid(update)
    store_name(update)

    chat = Chats.get(Chats.cid == cid)
    tacos = Tacos.select().where(Tacos.chat == chat.id)
    if not tacos.exists():
        context.bot.send_message(cid,
                                 no_init_phrase,
                                 parse_mode=ParseMode.HTML)
        return

    tacos = Tacos.get(Tacos.chat == chat.id)

    sender = update.effective_message.from_user  # finding sender/receiver
    receiver = update.effective_message.reply_to_message.from_user

    if receiver.username is None:
        first_name = receiver.first_name
        last_name = receiver.last_name
        if last_name is None:
            receiver_name = first_name
        else:
            receiver_name = first_name + ' ' + last_name
    else:
        receiver_name = '@' + receiver.username

    if receiver.is_bot:  # no tacos for bots
        update.message.reply_text(no_bots_allowed_phrase,
                                  parse_mode=ParseMode.HTML)
        return

    sender_id = str(sender.id)
    receiver_id = str(receiver.id)

    if sender_id == receiver_id:  # self-tacoing is forbidden
        update.message.reply_text(self_tacoing_phrase,
                                  parse_mode=ParseMode.HTML)
        return

    tacos_sent = len(
        re.findall('{}'.format(taco_emoji),
                   update.effective_message.text))  # counting tacos

    if tacos.taco_balance is None:  # initialising/restoring user-balances
        amounts = dict()
        amounts.update({sender_id: default_amount})
        amounts.update({receiver_id: default_amount})
    else:
        amounts = json.loads(tacos.taco_balance)
        if sender_id not in amounts.keys():
            amounts.update({sender_id: default_amount})
        if receiver_id not in amounts.keys():
            amounts.update({receiver_id: default_amount})

    if tacos_sent > amounts.get(sender_id):  # can't send more than you have
        update.message.reply_text(balance_low_phrase,
                                  parse_mode=ParseMode.HTML)
        return

    amounts.update({sender_id: amounts.get(sender_id) - tacos_sent
                    })  # actual taco-transfer
    amounts.update({receiver_id: amounts.get(receiver_id) + tacos_sent})

    if tacos_sent < 3:
        comment = taco_transfer_comment_low
    elif tacos_sent > 9:
        comment = taco_transfer_comment_high
    else:
        comment = taco_transfer_comment_medium.format(receiver_name)

    update.message.reply_text(taco_transfer_phrase.format(
        tacos_sent, receiver_name, comment),
                              parse_mode=ParseMode.HTML)

    tacos.taco_balance = json.dumps(amounts)  # saving data
    tacos.save()