Exemple #1
0
def remind(event, text, storage):
    """<period message> - ask the bot to remind you about something in given period (e.g. '.remind 1h bleh bleh' sends you 'bleh bleh' in one hour"""

    # Get period and message
    data = text.split(" ", maxsplit=1)

    if len(data) != 2:
        return "Must specify period and message"

    # Extract all the data
    remind_seconds = time_utils.timeout_to_sec(data[0])
    message = data[1]

    if "remind" not in storage:
        storage["remind"] = []

    # Create new entry
    elem = {}
    elem["author"] = event.author.id
    elem["deadline"] = time_utils.tnow() + remind_seconds
    elem["message"] = message

    # Append it to the reminder list
    storage["remind"].append(elem)

    # Save it
    storage.sync()

    return "Okay!"
Exemple #2
0
    def fw_up(text):
        # Extract the duration
        if len(text) > 0:
            storage["fw"]["end_time"] = time_utils.tnow(
            ) + time_utils.timeout_to_sec(text[0])
        else:
            storage["fw"]["end_time"] = None

        storage["fw"]["status"] = "up"
        storage.sync()

        if storage["fw"]["end_time"]:
            reply("Firewall enabled. It will stop in %s" %
                  time_utils.sec_to_human(time_utils.timeout_to_sec(text[0])))
        else:
            reply("Firewall enabled. It will run indefinitely.")
Exemple #3
0
def set_timeout_for(storage, text, str_to_id):
    """
    <join event id, timeout> - Set timeout for a join event. Use '5s' for timeout to set it to 5s or 1m to set it to one minute.
    """
    text = text.rsplit(maxsplit=1)
    try:
        e_id = int(text[0])
    except ValueError:
        return "Please enter an event ID"
    evt = find_event_by_id(storage, text[0])
    if not evt:
        return "Entry not found"

    if evt["type"] == "role":
        return "Found a matching entry but it's a role assignation."

    try:
        evt["timeout"] = time_utils.timeout_to_sec(text[1])
        storage.sync()
        return "Done. Set timeout to %d seconds" % evt["timeout"]
    except:
        import traceback

        traceback.print_exc()

    storage.sync()
Exemple #4
0
def set_auto_bulau(storage, text):
    """
    Set auto bulau to a timeout value (e.g. 10h - 10 hours, 100d - 100 days, etc.).
    """
    if "join_params" not in storage:
        storage["join_params"] = {}

    storage["join_params"]["abulau"] = time_utils.timeout_to_sec(text)
    storage.sync()

    return "Set to %s" % time_utils.sec_to_human(
        storage["join_params"]["abulau"])
Exemple #5
0
async def timeout(text, server, storage, event, send_embed):
    """
    Timeout an user:
    timeout @plp 1m - timeouts plp for one minute
    timeout @plp - displays timeout for plp
    If the user is timeouted, the timeout can be modified by issuing the timeout command again.
    """
    text = text.split(" ")
    if len(text) == 0:
        return "Please specify a parameter:\n" + timeout.__doc__

    user = dutils.get_user_by_id(server, dutils.str_to_id(text[0]))
    if not user:
        return "Could not find user"

    crt_timeout = user.timeout
    # User info needed
    if len(text) == 1:
        if crt_timeout == None:
            return "User does not have a timeout set."
        else:
            return f"Timeout will expire in: {time_utils.sec_to_human(crt_timeout.timestamp() - time_utils.tnow())}"

    # Set a timeout
    elif len(text) >= 2:
        tosec = time_utils.timeout_to_sec(text[1])
        texp = time_utils.tnow() + tosec

        reason = "Not given"
        if len(text) >= 3:
            reason = " ".join(text[2:])

        await user.set_timeout(time_utils.time_to_date(texp))

        if crt_timeout != None:
            return f"Adjusted timeout to {time_utils.sec_to_human(tosec)}"
        else:
            # Create reason
            reason = add_reason(storage, event, user, reason, server, texp,
                                "timeout")

            # Log the action
            log_action(
                storage,
                reason,
                send_embed,
                "User given timeout",
            )

            return f"Set timeout to {time_utils.sec_to_human(tosec)}"
Exemple #6
0
def set_timeout_for(storage, text, str_to_id):
    """
    <join event, timeout> - Set timeout for a join event. Use '5s' for timeout to set it to 5s or 1m to set it to one minute.
    """
    text = text.rsplit(maxsplit=1)
    evt = find_event_by_text(storage, text[0], str_to_id)

    if len(evt) > 1:
        return "Found too many matching entries. Try restricting the search criteria"
    elif len(evt) == 0:
        return "Entry not found"

    if evt[0]["type"] == "role":
        return "Found a matching entry but it's a role assignation."

    try:
        evt[0]["timeout"] = time_utils.timeout_to_sec(text[1])
        storage.sync()
        return "Done. Set timeout to %d seconds" % evt[0]["timeout"]
    except:
        import traceback
        traceback.print_exc()

    storage.sync()
Exemple #7
0
def ban(user_id_to_object, str_to_id, text, storage, event, send_embed, server,
        send_pm):
    """
    <user [,time], reason> - ban someone permanently or for a given amount of time (e.g. `.ban @plp 5m` bans plp for 5 minutes).
    """
    text = text.split()
    if len(text) == 0:
        return ban.__doc__

    user = user_id_to_object(str_to_id(text[0]))

    # Check for valid user
    if user is None:
        return "User not found."

    texp = None
    timeout_sec = None
    permanent = True
    reason = "Not given"
    # Check if there's a timeout
    if len(text) > 1:
        try:
            timeout_sec = time_utils.timeout_to_sec(text[1])
        except:
            timeout_sec = 0

        # If there's no timeout, then the reason follows the username
        if timeout_sec == 0:
            reason = " ".join(text[1:])
        else:
            permanent = False
            if len(text) > 2:
                reason = " ".join(text[2:])

        texp = timeout_sec + datetime.datetime.now().timestamp()

    user_entry = create_user_reason(
        storage, user, event.author, reason,
        "https://discordapp.com/channels/%s/%s/%s" %
        (server.id, event.channel.id, event.msg.id), texp,
        "Ban" if permanent else "Temporary ban")

    # Log the action
    log_action(storage, user_entry, send_embed, "User banned")

    # Create command entry
    new_entry = {}
    new_entry["user_id"] = str(user.id)
    new_entry["user_name"] = user.name
    new_entry["expire"] = texp
    new_entry["reason_id"] = user_entry["Case ID"]

    details = "\nAuthor: %s" % event.author.name
    if reason:
        details += "\nReason: %s" % reason

    if not permanent:
        if "temp_bans" not in storage:
            storage["temp_bans"] = []

        storage["temp_bans"].append(new_entry)
        send_pm(
            text=
            "You have temporarily banned from %s. The ban will last for %s.\n%s"
            % (server.name, text[1], details),
            user=user)
    else:
        send_pm(text="You have been permanently banned from %s.\n%s" %
                (server.name, details),
                user=user)
    storage.sync()

    user.ban(server)
    return "User banned permanently." if permanent else "User banned temporarily."
Exemple #8
0
def give_temp_role(text, server, command_name, storage, event):
    # Remove extra whitespace and split
    text = " ".join(text.split()).split()

    if len(text) < 2:
        return "Needs at least user and time (e.g. .{CMD} @plp, 5m - to give @plp {CMD} for 5 minutes OR .{CMD} @plp 5m bad user - to give @plp {CMD} for 5m and save the reason \"bad user\")".format(CMD=command_name) + \
        "The abbrebiations are: s - seconds, m - minutes, h - hours, d - days."

    # Get user
    user = dutils.get_user_by_id(server, dutils.str_to_id(text[0]))
    if not user:
        return "No such user"

    # Get reason
    reason = "Not given"
    if len(text) >= 3:
        reason = " ".join(text[2:])

    # Get timeout
    timeout_sec = time_utils.timeout_to_sec(text[1])
    # If timeout is 0, double check the input
    if timeout_sec == 0 and text[1] != "0s":
        return "There may have been a problem parsing `%s`. Please check it and run the command again." % text[
            1]

    # When the timeout will expire
    texp = datetime.datetime.now().timestamp() + timeout_sec

    # Get the role
    role = dutils.get_role_by_id(server,
                                 storage["cmds"][command_name]["role_id"])

    if role is None:
        return "Could not find given role"

    if "temp_roles" not in storage:
        storage["temp_roles"] = {}

    if command_name not in storage["temp_roles"]:
        storage["temp_roles"][command_name] = []

    # Check if user is already in temp role
    extra = False
    for entry in storage["temp_roles"][command_name]:
        if entry["user_id"] == user.id:
            extra = True
            break

    crt_roles = []
    # Get current roles
    for urole in user.roles:
        # If the role has already been given, assume that it will be timed from now
        if urole.id == role.id:
            continue
        crt_roles.append(urole.id)

    # Check if it's extra time
    if not extra:
        # Create a new user entry
        reason_entry = create_user_reason(
            storage, user, event.author, reason,
            "https://discordapp.com/channels/%s/%s/%s" %
            (server.id, event.channel.id, event.msg.id), texp, command_name)

        # Create command entry
        new_entry = {}
        new_entry["user_id"] = str(user.id)
        new_entry["user_name"] = user.name
        new_entry["expire"] = texp
        new_entry["crt_roles"] = crt_roles
        new_entry["reason_id"] = reason_entry["Case ID"]

        storage["temp_roles"][command_name].append(new_entry)

        # Replace user roles
        user.replace_roles([role])

        storage.sync()

        user.send_pm(
            "You have been given the `%s` role. It will last for %s.\nReason: %s\nAuthor: %s"
            % (storage["cmds"][command_name]["role_name"], text[1], reason,
               event.author.name))

        return reason_entry

    else:
        adjust_user_reason(
            storage, event.author, user.id, command_name, texp,
            "https://discordapp.com/channels/%s/%s/%s" %
            (server.id, event.channel.id, event.msg.id))

        return "Adjusted time for user to %d" % timeout_sec

    return "Nothing happened"