Search in sources :

Example 1 with UserLinkObject

use of com.github.vaerys.objects.UserLinkObject in project DiscordSailv2 by Vaerys-Dawn.

the class EditLinks method execute.

@Override
public String execute(String args, CommandObject command) {
    UserObject user = command.user;
    SplitFirstObject userCall = new SplitFirstObject(args);
    boolean adminEdit = false;
    if (GuildHandler.testForPerms(command, ADMIN_EDIT.getPermissions()) || GuildHandler.canBypass(command.user.get(), command.guild.get())) {
        user = Utility.getUser(command, userCall.getFirstWord(), false);
        if (user != null && userCall.getRest() != null && user.getProfile(command.guild) != null) {
            adminEdit = true;
        } else {
            user = command.user;
        }
    }
    SplitFirstObject linkName;
    if (adminEdit) {
        linkName = new SplitFirstObject(userCall.getRest());
    } else {
        linkName = new SplitFirstObject(args);
    }
    ProfileObject userObject = user.getProfile(command.guild);
    if (userObject == null) {
        return "> " + user.displayName + " Has not Spoken yet thus they have nothing to edit.";
    }
    userObject.setLinks();
    for (UserLinkObject link : userObject.getLinks()) {
        if (link.getName().equalsIgnoreCase(linkName.getFirstWord())) {
            userObject.getLinks().remove(link);
            if (adminEdit) {
                return "> " + user.displayName + "'s Link was removed.";
            } else {
                return "> Link removed.";
            }
        }
    }
    int maxLinks = 5;
    if (user.isPatron) {
        maxLinks += 5;
    }
    if (linkName.getRest() == null) {
        return "> Cannot add link, Must specify a URL.";
    } else {
        if (userObject.getLinks().size() >= maxLinks) {
            if (adminEdit) {
                return "> " + user.displayName + " already has " + maxLinks + " links, a link must be removed to add a new one.";
            } else {
                return "> You already have " + maxLinks + " links, you must remove one to add a new one.";
            }
        }
        if (linkName.getFirstWord().length() > 15) {
            return "> Link Name too long. (Max 15 chars)";
        }
        if (Utility.checkURL(linkName.getRest())) {
            try {
                new URL(linkName.getRest());
                userObject.getLinks().add(new UserLinkObject(linkName.getFirstWord(), linkName.getRest()));
                if (adminEdit) {
                    return "> New link added for " + user.displayName + ".";
                } else {
                    return "> New link added.";
                }
            } catch (MalformedURLException e) {
                return "> Cannot add link, Invalid URL.";
            }
        } else {
            return "> Cannot add link, Invalid URL.";
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) UserObject(com.github.vaerys.masterobjects.UserObject) UserLinkObject(com.github.vaerys.objects.UserLinkObject) SplitFirstObject(com.github.vaerys.objects.SplitFirstObject) ProfileObject(com.github.vaerys.objects.ProfileObject) URL(java.net.URL)

Aggregations

UserObject (com.github.vaerys.masterobjects.UserObject)1 ProfileObject (com.github.vaerys.objects.ProfileObject)1 SplitFirstObject (com.github.vaerys.objects.SplitFirstObject)1 UserLinkObject (com.github.vaerys.objects.UserLinkObject)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1