Ejemplo n.º 1
0
def owes_helper(chat_id, ower, owee, amount):
    owed = helper.loadjson(loc_owedjson)

    try:
        owed[chat_id]
    except KeyError:
        owed[chat_id] = {}

    try:
        owed[chat_id][ower]
    except KeyError:
        owed[chat_id][ower] = {}
        print("Added new ower: " + ower + ".")

    try:
        owed[chat_id][ower][owee]
    except KeyError:
        owed[chat_id][ower][owee] = 0
        print("Added new owee for ower " + ower + ".")

    owed[chat_id][ower][owee] += float(amount)
    result = owed[chat_id][ower][owee]

    # check whether owed sum is now 0 and removes if necessary
    if owed[chat_id][ower][owee] == 0:
        owed[chat_id][ower].pop(owee)
        if owed[chat_id][ower] == {}:
            owed[chat_id].pop(ower)
            if owed[chat_id] == {}:
                owed.pop(chat_id)

    helper.dumpjson(loc_owedjson, owed)
    return result
Ejemplo n.º 2
0
def rm_command(bot, update, args):
    cmds = helper.loadjson(loc_cmdsjson)
    chat_id = str(update.message.chat_id)

    if len(args) == 1:
        # remove command from command repo
        cmd_name = "/" + args[0]
        try:
            del cmds[chat_id][cmd_name]
            helper.dumpjson(loc_cmdsjson, cmds)
            print("removed command \"" + cmd_name + "\".")
        except KeyError:
            return
Ejemplo n.º 3
0
def clear(bot, update, args):
    owed = helper.loadjson(loc_owedjson)
    chat_id = str(update.message.chat_id)
    sender = update.message.from_user

    try:
        owed[chat_id]
    except KeyError:
        owed[chat_id] = {}

    if sender.id != Bot.OWNER_ID:
        update.message.reply_text(strings.errNotAdmin)
        print(strings.errUnauthCommand.format(sender.username))
        return

    if len(args) == 1 and args[0] == "all":
        helper.dumpjson(loc_bckpjson, owed)
        owed.pop(chat_id)
        update.message.reply_text(msgAllDebtsCleared)
        print(msgAllDebtsClearedTerm)

    elif len(args) == 2:
        try:
            owed[chat_id][args[0]]
        except KeyError:
            update.message.reply_text(strings.errNoOwer + args[0])
            return

        if args[1] == "all":
            owed[chat_id].pop(args[0])
            print(msgDebtsOfCleared.format(args[0]))

        else:
            owed[chat_id][args[0]].pop(args[1])
            update.message.reply_text(
                msgDebtsOfToCleared.format(args[0], args[1]))

            # remove from database if no debts
            if owed[chat_id] == {}:
                owed.pop(chat_id)
            elif owed[chat_id][args[0]] == {}:
                owed[chat_id].pop(args[0])

    else:
        update.message.reply_text(strings.errBadFormat)

    helper.dumpjson(loc_owedjson, owed)
Ejemplo n.º 4
0
def save_note(bot, update, args):
    notes = helper.loadjson(loc_notesjson)
    chat_id = str(update.message.chat_id)

    try:
        notes[chat_id]
    except KeyError:
        notes[chat_id] = {}

    if len(args) >= 2:
        # add note to note repo
        notename = args[0]
        del args[0]
        note_data = " ".join(args)
        notes[chat_id][notename] = note_data
        print("Added new note \"" + notename + "\" with content \"" +
              note_data + "\".")
    else:
        update.message.reply_text(strings.errBadFormat)

    helper.dumpjson(loc_notesjson, notes)
Ejemplo n.º 5
0
def add_command(bot, update, args):
    cmds = helper.loadjson(loc_cmdsjson)
    chat_id = str(update.message.chat_id)

    try:
        cmds[chat_id]
    except KeyError:
        cmds[chat_id] = {}

    if len(args) == 1:
        update.message.reply_text(no_cmd_text_given)
    elif len(args) >= 2:
        # add command to command repo
        cmd_name = "/" + args[0]
        del args[0]
        cmd_data = " ".join(args)
        cmds[chat_id][cmd_name] = cmd_data
        print("Added new cmd \"" + cmd_name + "\" with content \"" + cmd_data +
              "\".")
    else:
        update.message.reply_text(errBadFormat)

    helper.dumpjson(loc_cmdsjson, cmds)