示例#1
0
def banish_user(update, context):
    """
    Function that executes from the /banish command. Adds specified user to the block list if not already blocked.
    Args:
        update: default telegram arg
        context: default telegram arg
    """
    #if invalid user, the validate_user function will prompt to register with /war command
    if um.validate_user(update, context, update.message.chat_id, update.message.from_user.username) is False:
        return None
    else:
        #if valid user, load in user data
        user1 = um.load_user_data([update.message.from_user.username])
    #prompt usage if no context.args provided
    if context.args == []:
        context.bot.send_message(chat_id=user1["userid"], text='Usage: <b>/banish &lt;username&gt;</b>', parse_mode=ParseMode.HTML)
        return None
    else:
        username2 = context.args[0]
    #check if specified user exist(registered user)
    if um.check_exist_user("./userinfo/" + username2 + ".json") is False:
        context.bot.send_message(chat_id=user1["userid"], text="User cannot be found!")
        return None
    else:
        user2 = um.load_user_data([username2])
    #check if user is already blocked
    if user2["userid"] in user1["block_list"]:
        context.bot.send_message(chat_id=user1["userid"], text="<b>" + username2 + "</b> is already banished!", parse_mode=ParseMode.HTML)
    else:
        #if not blocked, add to block list
        user1["block_list"].append(user2["userid"])
        um.save_user_data([user1])
        context.bot.send_message(chat_id=user1["userid"], text="You have banished <b>" + username2 + "</b>! (To unbanish, use /unbanish)", parse_mode=ParseMode.HTML)
    return None
示例#2
0
def create_user(update, context):
    """
    Function to create a user.
    Args:
        userid: userid of the new user
    """
    #set default values and save to userinfo folder
    #The userid folder stores a mapping of userid to registered username in case a player changes username in future
    if um.check_exist_user(update.message.chat_id):
        update.message.reply_text("You are already registered!")
    else:
        new_info = {
            "username": update.message.from_user.username,
            "userid": str(update.message.chat_id),
            "mode": "1",
            "user_group": "normal",
            "code_snippet": ""
        }
        lg.logbook(new_info, "register")
        with open("./userinfo/" + str(update.message.chat_id) + ".json",
                  'w+') as info_file:
            json.dump(new_info, info_file)
        update.message.reply_text(
            "Registration successfully completed. <b>/code</b> to start coding!",
            parse_mode=ParseMode.HTML)
    return None
示例#3
0
def run_code(update, context):
    """
    Run the code snippet of the user.
    Args:
        update: default telegram arg
        context: default telegram arg
    """
    executing_code = False

    def load_animation(user, update, message):
        """
        Function that provides loading animation during code execution.
        Args:
            user: user running the code
            update: default telegram arg
            context: default telegram arg
        """
        lg.logbook(user, "run_code")
        while executing_code:
            message.edit_text(text="<b>Executing Code /</b>",
                              parse_mode=ParseMode.HTML)
            message.edit_text(text="<b>Executing Code -</b>",
                              parse_mode=ParseMode.HTML)
            message.edit_text(text="<b>Executing Code \\</b>",
                              parse_mode=ParseMode.HTML)
            message.edit_text(text="<b>Executing Code |</b>",
                              parse_mode=ParseMode.HTML)
        message.edit_text(text="<b>Execution Complete:</b>",
                          parse_mode=ParseMode.HTML)
        return None

    if not um.check_exist_user(update.message.chat_id):
        update.message.reply_text(
            "You are not registered. Try <b>/register</b>",
            parse_mode=ParseMode.HTML)
    else:
        executing_code = True
        executing = update.message.reply_text("<b>Executing Code |</b>",
                                              parse_mode=ParseMode.HTML)
        user = um.load_user_data(update.message.chat_id)
        threading.Thread(target=load_animation,
                         args=(user, update, executing)).start()
        with open("./config/endpoint.json", "r") as file:
            endpoint = json.load(file)["endpoint"]
        res = requests.post(endpoint, data={"userid": user["userid"]})
        output = res.content.decode('utf-8')[1:-1]
        executing_code = False
        update.message.reply_text(output)
    return None
示例#4
0
def initiate_fight_user(update, context):
    """
    Function that executes from the /fight command. Initiates a battle request against another user.
    Args:
        update: default telegram arg
        context: default telegram arg
    """
    #if invalid user, the validate_user function will prompt to register with /war command
    if um.validate_user(update, context, update.message.chat_id, update.message.from_user.username) is False:
        return None
    else:
        #if valid user, load in user data
        user1 = um.load_user_data([update.message.from_user.username])
    #prompt usage if no context.args provided, else check user current status to make sure eligibility for making request
    if context.args == []:
        context.bot.send_message(chat_id=user1["userid"], text="Usage: <b>/fight &lt;username&gt;</b>", parse_mode=ParseMode.HTML)
        return None
    elif user1["user_status"] == "1":
        context.bot.send_message(chat_id=user1["userid"], text="You already have a pending request!")
        return None
    elif user1["user_status"] == "2" or user1["user_status"] == "3":
        context.bot.send_message(chat_id=user1["userid"], text="You are in the midst of a war!")
        return None
    else:
        username2 = context.args[0]
    #check if specified user exist(registered user)
    if um.check_exist_user("./userinfo/" + username2 + ".json") is False:
        context.bot.send_message(chat_id=user1["userid"], text="User cannot be found!")
        return None
    else:
        user2 = um.load_user_data([username2])
    #refuse the request if specified user has blocked requesting user
    if user1["userid"] in user2["block_list"]:
        context.bot.send_message(chat_id=user1["userid"], text="<b>" + user2["username"] + "</b> is unavailable for war!", parse_mode=ParseMode.HTML)
    elif user2["user_status"] == "1":
        context.bot.send_message(chat_id=user1["userid"], text="<b>" + user2["username"] + "</b> has a pending request!", parse_mode=ParseMode.HTML)
    elif user2["user_status"] == "2" or user1["user_status"] == "3":  
        context.bot.send_message(chat_id=user1["userid"], text="<b>" + user2["username"] + "</b> is in the midst of a war!", parse_mode=ParseMode.HTML)
    else:
        #switch user statuses to pending and send out battle declaration
        user1["user_status"], user2["user_status"] = "1", "1"
        um.save_user_data([user1, user2])
        reply_markup = mc.show_user_options(user1["username"], user2["username"], 2, ["Yes", "No"], ["accept", "decline"])
        context.bot.send_message(chat_id=user1["userid"], text="Enemy Detected...")
        context.bot.send_message(chat_id=user1["userid"], text="Declaring Battle...")
        declare_war = context.bot.send_message(chat_id=user2["userid"], text="<b>" + user1["username"] + "</b> has declared battle! Are you ready?", reply_markup=reply_markup, parse_mode=ParseMode.HTML)
        #start a thread to enforce a 30 second timeout rule
        threading.Thread(target=um.connect_users, args=(context.bot, user1, user2, declare_war.message_id, 0.5)).start()
    return None
示例#5
0
def clear_code(update, context):
    """
    Clear the code snippet of the user.
    Args:
        update: default telegram arg
        context: default telegram arg
    """
    if not um.check_exist_user(update.message.chat_id):
        update.message.reply_text(
            "You are not registered. Try <b>/register</b>",
            parse_mode=ParseMode.HTML)
    else:
        user = um.load_user_data(update.message.chat_id)
        user["code_snippet"] = ""
        um.save_user_data(user)
        update.message.reply_text("<b>Code Cleared</b>",
                                  parse_mode=ParseMode.HTML)
    return None
示例#6
0
def check_mode(update, context):
    """
    Function to check mode of user.
    Args:
        update: default telegram arg
        context: default telegram arg
    """
    if not um.check_exist_user(update.message.chat_id):
        update.message.reply_text(
            "You are not registered. Try <b>/register</b>",
            parse_mode=ParseMode.HTML)
    else:
        user = um.load_user_data(update.message.chat_id)
        mode = user["mode"]
        if mode == "0":
            track_code(update.message.text, user)
        else:
            update.message.reply_text(
                "Invalid input. Use /code to toggle code mode.")
    return None
示例#7
0
def view_code(update, context):
    """
    View the current code of the user.
    Args:
        update: default telegram arg
        context: default telegram arg
    """
    if not um.check_exist_user(update.message.chat_id):
        update.message.reply_text(
            "You are not registered. Try <b>/register</b>",
            parse_mode=ParseMode.HTML)
    else:
        user = um.load_user_data(update.message.chat_id)
        code = jsbeautifier.beautify(user["code_snippet"])
        if code == "":
            code = "<b>No Existing Code Found.</b>"
            update.message.reply_text(code, parse_mode=ParseMode.HTML)
        else:
            update.message.reply_text(code)
    return None
示例#8
0
def toggle_code(update, context):
    """
    Function to toggle coding mode for user.
    Args:
        update: default telegram arg
        context: default telegram arg
    """
    if not um.check_exist_user(update.message.chat_id):
        update.message.reply_text(
            "You are not registered. Try <b>/register</b>",
            parse_mode=ParseMode.HTML)
    else:
        user = um.load_user_data(update.message.chat_id)
        if user["mode"] == "0":
            user["mode"] = "1"
            update.message.reply_text("<b>Code Mode Disabled</b>",
                                      parse_mode=ParseMode.HTML)
        else:
            user["mode"] = "0"
            update.message.reply_text("<b>Code Mode Enabled</b>",
                                      parse_mode=ParseMode.HTML)
        um.save_user_data(user)
    return None
示例#9
0
def view_logs(update, context):
    """
    View the logs of the bot (admin only)
    Args:
        update: default telegram arg
        context: default telegram arg
    """
    if not um.check_exist_user(update.message.chat_id):
        update.message.reply_text(
            "You are not registered. Try <b>/register</b>",
            parse_mode=ParseMode.HTML)
    else:
        user = um.load_user_data(update.message.chat_id)
    if not um.check_user_permission(user, "0"):
        update.message.reply_text("<b>Insufficient Permission.</b>",
                                  parse_mode=ParseMode.HTML)
    else:
        list_of_logs = os.listdir("./logs")
        retrieved_logs = show_logs(len(list_of_logs), list_of_logs, user)
        update.message.reply_text("<b>Please select a log:</b>",
                                  reply_markup=retrieved_logs,
                                  parse_mode=ParseMode.HTML)
    return None