Search in sources :

Example 1 with ConsoleCommand

use of util.comm.ConsoleCommand in project Botnak by Gocnak.

the class Settings method saveConCommands.

/**
     * Console Commands
     */
public static void saveConCommands() {
    try (PrintWriter br = new PrintWriter(ccommandsFile)) {
        for (ConsoleCommand next : GUIMain.conCommands) {
            if (next != null) {
                String name = next.getTrigger();
                String action = next.getAction().toString();
                int classPerm = next.getClassPermission();
                String certainPerm = "null";
                if (next.getCertainPermissions() != null) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < next.getCertainPermissions().length; i++) {
                        sb.append(next.getCertainPermissions()[i]);
                        if (i != (next.getCertainPermissions().length - 1))
                            sb.append(",");
                    }
                    certainPerm = sb.toString();
                }
                br.println(name + "[" + action + "[" + classPerm + "[" + certainPerm);
            }
        }
    } catch (Exception e) {
        GUIMain.log(e);
    }
}
Also used : ConsoleCommand(util.comm.ConsoleCommand)

Example 2 with ConsoleCommand

use of util.comm.ConsoleCommand in project Botnak by Gocnak.

the class IRCBot method onMessage.

@Override
public void onMessage(String channel, String sender, String message) {
    if (message != null && channel != null && sender != null && Settings.accountManager.getViewer() != null) {
        String botnakUserName = Settings.accountManager.getUserAccount().getName();
        sender = sender.toLowerCase();
        if (!channel.contains(botnakUserName.toLowerCase())) {
            //in other channels
            int replyType = Settings.botReplyType.getValue();
            if (replyType == 0)
                return;
            //0 = reply to nobody (just spectate), 1 = reply to just the Botnak user, 2 = reply to everyone
            if (replyType == 1 && !sender.equalsIgnoreCase(botnakUserName))
                return;
        }
        boolean senderIsBot = sender.equalsIgnoreCase(getBot().getNick());
        boolean userIsBot = botnakUserName.equalsIgnoreCase(Settings.accountManager.getBotAccount().getName());
        //the user account is NOT the bot, just return, we don't want the bot to trigger anything
        if (senderIsBot && !userIsBot)
            return;
        //raffles
        User u = Settings.channelManager.getUser(sender, true);
        if (!raffles.isEmpty()) {
            if (!winners.contains(u.getNick().toLowerCase())) {
                for (Raffle r : raffles) {
                    if (r.isDone()) {
                        continue;
                    }
                    String key = r.getKeyword();
                    if (message.contains(key)) {
                        //TODO this will change to EXCLUDEDPerms and whatnot
                        int permBase = r.getPermission();
                        if (Permissions.hasAtLeast(Permissions.getUserPermissions(u, channel), permBase)) {
                            r.addUser(u.getLowerNick());
                        }
                    }
                }
            }
            ArrayList<Raffle> toRemove = new ArrayList<>();
            raffles.stream().filter(Raffle::isDone).forEach(r -> {
                winners.add(r.getWinner());
                toRemove.add(r);
            });
            if (!toRemove.isEmpty()) {
                toRemove.forEach(raffles::remove);
                toRemove.clear();
            }
        }
        Oauth key = Settings.accountManager.getUserAccount().getKey();
        String[] split = message.split(" ");
        //URL Checking
        boolean ytVidDetail = Settings.botShowYTVideoDetails.getValue();
        boolean twitchVOD = Settings.botShowTwitchVODDetails.getValue();
        boolean unshortenURLs = Settings.botUnshortenURLs.getValue();
        if (ytVidDetail || twitchVOD || unshortenURLs) {
            ThreadEngine.submit(() -> {
                int count = 0;
                for (String part : split) {
                    //only allowing 2 requests here; don't want spam
                    if (count > 1)
                        break;
                    if (part.startsWith("http") || part.startsWith("www")) {
                        if (ytVidDetail && (part.contains("youtu.be") || part.contains("youtube.com/watch") || part.contains("youtube.com/v") || part.contains("youtube.com/embed/"))) {
                            getBot().sendMessage(channel, APIRequests.YouTube.getVideoData(part).getResponseText());
                            count++;
                        } else if (unshortenURLs && (part.contains("bit.ly") || part.contains("tinyurl") || part.contains("goo.gl"))) {
                            getBot().sendMessage(channel, APIRequests.UnshortenIt.getUnshortened(part).getResponseText());
                            count++;
                        } else if (twitchVOD && part.contains("twitch.tv/")) {
                            if (part.contains("/v/") || part.contains("/c/") || part.contains("/b/")) {
                                getBot().sendMessage(channel, APIRequests.Twitch.getTitleOfVOD(part).getResponseText());
                                count++;
                            }
                        }
                    }
                }
            });
        }
        String first = "";
        if (split.length > 1)
            first = split[1];
        //commands
        if (message.startsWith("!")) {
            String trigger = message.substring(1).split(" ")[0].toLowerCase();
            String mess = message.substring(1);
            //sound
            if (SoundEngine.getEngine().soundTrigger(trigger, sender, channel)) {
                SoundEngine.getEngine().playSound(new Sound(SoundEngine.getEngine().getSoundMap().get(trigger)));
            }
            ConsoleCommand consoleCommand = Utils.getConsoleCommand(trigger, channel, u);
            if (consoleCommand != null) {
                Response commandResponse = null;
                switch(consoleCommand.getAction()) {
                    case ADD_FACE:
                    case CHANGE_FACE:
                        commandResponse = FaceManager.handleFace(mess);
                        if (commandResponse.isSuccessful())
                            Settings.FACES.save();
                        break;
                    case REMOVE_FACE:
                        commandResponse = FaceManager.removeFace(first);
                        if (commandResponse.isSuccessful())
                            Settings.FACES.save();
                        break;
                    case TOGGLE_FACE:
                        commandResponse = FaceManager.toggleFace(first);
                        break;
                    case ADD_SOUND:
                    case CHANGE_SOUND:
                        commandResponse = SoundEngine.getEngine().handleSound(mess, consoleCommand.getAction() == ConsoleCommand.Action.CHANGE_SOUND);
                        break;
                    case REMOVE_SOUND:
                        commandResponse = SoundEngine.getEngine().removeSound(first);
                        break;
                    case SET_SOUND_DELAY:
                        commandResponse = SoundEngine.getEngine().setSoundDelay(first);
                        break;
                    case TOGGLE_SOUND:
                        boolean individualSound = split.length > 1;
                        commandResponse = SoundEngine.getEngine().toggleSound(individualSound ? first : null, individualSound);
                        break;
                    case STOP_SOUND:
                    case STOP_ALL_SOUNDS:
                        commandResponse = SoundEngine.getEngine().stopSound(consoleCommand.getAction() == ConsoleCommand.Action.STOP_ALL_SOUNDS);
                        break;
                    case SEE_SOUND_STATE:
                        commandResponse = SoundEngine.getEngine().getSoundState(first);
                        break;
                    case ADD_KEYWORD:
                    case REMOVE_KEYWORD:
                        commandResponse = Utils.handleKeyword(mess);
                        if (commandResponse.isSuccessful())
                            Settings.KEYWORDS.save();
                        break;
                    case SET_USER_COL:
                        commandResponse = Utils.handleColor(sender, mess, u.getColor());
                        if (commandResponse.isSuccessful())
                            Settings.USER_COLORS.save();
                        break;
                    case SET_COMMAND_PERMISSION:
                        commandResponse = Utils.setCommandPermission(mess);
                        if (commandResponse.isSuccessful())
                            Settings.saveConCommands();
                        break;
                    case ADD_TEXT_COMMAND:
                        commandResponse = Utils.addCommands(mess);
                        if (commandResponse.isSuccessful())
                            Settings.COMMANDS.save();
                        break;
                    case REMOVE_TEXT_COMMAND:
                        commandResponse = Utils.removeCommands(first);
                        if (commandResponse.isSuccessful())
                            Settings.COMMANDS.save();
                        break;
                    case ADD_DONATION:
                        commandResponse = Settings.donationManager.parseDonation(split);
                        break;
                    case SET_SOUND_PERMISSION:
                        commandResponse = SoundEngine.getEngine().setSoundPermission(first);
                        break;
                    case SET_NAME_FACE:
                        if (first.startsWith("http")) {
                            commandResponse = FaceManager.downloadFace(first, Settings.nameFaceDir.getAbsolutePath(), Utils.setExtension(sender, ".png"), sender, FaceManager.FACE_TYPE.NAME_FACE);
                        }
                        break;
                    case REMOVE_NAME_FACE:
                        if (FaceManager.nameFaceMap.containsKey(sender)) {
                            try {
                                Face f = FaceManager.nameFaceMap.remove(sender);
                                if (f != null && new File(f.getFilePath()).delete())
                                    getBot().sendMessage(channel, "Removed face for user: " + sender + " !");
                            } catch (Exception e) {
                                getBot().sendMessage(channel, "Name face for user " + sender + " could not be removed due to an exception!");
                            }
                        } else {
                            getBot().sendMessage(channel, "The user " + sender + " has no name face!");
                        }
                        break;
                    case SET_STREAM_TITLE:
                        commandResponse = APIRequests.Twitch.setStreamStatus(key, channel, message, true);
                        if (commandResponse.isSuccessful()) {
                            if (GUIMain.statusGUI != null && GUIMain.statusGUI.isVisible()) {
                                GUIMain.statusGUI.updateStatusComponents();
                            }
                        }
                        break;
                    case SEE_STREAM_TITLE:
                        String title = APIRequests.Twitch.getTitleOfStream(channel);
                        if (!"".equals(title)) {
                            getBot().sendMessage(channel, "The title of the stream is: " + title);
                        } else {
                            getBot().sendMessage(channel, "The stream currently has no title!");
                        }
                        break;
                    case SEE_STREAM_GAME:
                        String game = APIRequests.Twitch.getGameOfStream(channel);
                        if ("".equals(game)) {
                            getBot().sendMessage(channel, "The streamer is currently not playing a game!");
                        } else {
                            getBot().sendMessage(channel, "The current game is: " + game);
                        }
                        break;
                    case SET_STREAM_GAME:
                        commandResponse = APIRequests.Twitch.setStreamStatus(key, channel, message, false);
                        if (commandResponse.isSuccessful()) {
                            if (GUIMain.statusGUI != null && GUIMain.statusGUI.isVisible()) {
                                GUIMain.statusGUI.updateStatusComponents();
                            }
                        }
                        break;
                    case PLAY_ADVERT:
                        if (key != null) {
                            commandResponse = playAdvert(key, first, channel);
                        }
                        break;
                    case START_RAFFLE:
                        if (split.length > 2) {
                            String timeString = split[2];
                            int time = Utils.getTime(timeString);
                            if (time < 1) {
                                getBot().sendMessage(channel, "Failed to start raffle, usage: !startraffle (key) (time) (permission?)");
                                break;
                            }
                            //TODO select a parameter in Settings GUI that defines the default raffle
                            int perm = 0;
                            if (split.length == 4) {
                                //because right now it's just "Everyone" unless specified with the int param
                                try {
                                    perm = Integer.parseInt(split[3]);
                                    perm = Utils.capNumber(0, 3, perm);
                                } catch (Exception ignored) {
                                //default to the specified value
                                }
                            }
                            Raffle r = new Raffle(getBot(), first, time, channel, perm);
                            r.start();
                            raffles.add(r);
                            //print the blarb
                            getBot().sendMessage(channel, r.getStartMessage());
                            getBot().sendMessage(channel, "NOTE: This is a promotion from " + channel.substring(1) + ". Twitch does not sponsor or endorse broadcaster promotions and is not responsible for them.");
                        } else {
                            getBot().sendMessage(channel, "Failed to start raffle, usage: !startraffle (key) (time) (permission?)");
                        }
                        break;
                    case ADD_RAFFLE_WINNER:
                        if (!winners.contains(first)) {
                            winners.add(first);
                            getBot().sendMessage(channel, "The user " + first + " has been added to the winners pool!");
                        } else {
                            getBot().sendMessage(channel, "The user " + first + " is already in the winners pool!");
                        }
                        break;
                    case STOP_RAFFLE:
                        Raffle toRemove = null;
                        for (Raffle r : raffles) {
                            if (r.getKeyword().equalsIgnoreCase(first)) {
                                r.setDone(true);
                                r.interrupt();
                                toRemove = r;
                                getBot().sendMessage(channel, "The raffle with key " + first + " has been stopped!");
                                break;
                            }
                        }
                        if (toRemove != null) {
                            raffles.remove(toRemove);
                        } else {
                            getBot().sendMessage(channel, "There is no such raffle \"" + first + "\" !");
                        }
                        break;
                    case REMOVE_RAFFLE_WINNER:
                        if (winners.contains(first)) {
                            if (winners.remove(first)) {
                                getBot().sendMessage(channel, "The user " + first + " was removed from the winners pool!");
                            }
                        } else {
                            getBot().sendMessage(channel, "The user " + first + " is not in the winners pool!");
                        }
                        break;
                    case SEE_WINNERS:
                        if (!winners.isEmpty()) {
                            StringBuilder stanSB = new StringBuilder();
                            stanSB.append("The current raffle winners are: ");
                            for (String name : winners) {
                                stanSB.append(name);
                                stanSB.append(", ");
                            }
                            getBot().sendMessage(channel, stanSB.toString().substring(0, stanSB.length() - 2) + " .");
                        } else {
                            getBot().sendMessage(channel, "There are no recorded winners!");
                        }
                        break;
                    case START_POLL:
                        if (poll != null) {
                            if (poll.isDone()) {
                                createPoll(channel, message);
                            } else {
                                getBot().sendMessage(channel, "Cannot start a poll with one currently running!");
                            }
                        } else {
                            createPoll(channel, message);
                        }
                        break;
                    case POLL_RESULT:
                        if (poll != null) {
                            poll.printResults();
                        } else {
                            getBot().sendMessage(channel, "There never was a poll!");
                        }
                        break;
                    case CANCEL_POLL:
                        if (poll != null) {
                            if (poll.isDone()) {
                                getBot().sendMessage(channel, "The poll is already finished!");
                            } else {
                                poll.interrupt();
                                getBot().sendMessage(channel, "The poll has been stopped.");
                            }
                        } else {
                            getBot().sendMessage(channel, "There is no current poll!");
                        }
                        break;
                    case VOTE_POLL:
                        if (poll != null) {
                            if (!poll.isDone()) {
                                int option;
                                try {
                                    option = Integer.parseInt(first);
                                } catch (Exception e) {
                                    break;
                                }
                                poll.addVote(sender, option);
                            }
                        }
                        break;
                    case NOW_PLAYING:
                        commandResponse = APIRequests.LastFM.getCurrentlyPlaying();
                        break;
                    case SHOW_UPTIME:
                        commandResponse = APIRequests.Twitch.getUptimeString(channel.substring(1));
                        break;
                    case SEE_PREV_SOUND_DON:
                        if (Settings.botShowPreviousDonSound.getValue()) {
                            if (Settings.loadedDonationSounds)
                                commandResponse = SoundEngine.getEngine().getLastDonationSound();
                        }
                        break;
                    case SEE_PREV_SOUND_SUB:
                        if (Settings.botShowPreviousSubSound.getValue()) {
                            if (Settings.loadedSubSounds)
                                commandResponse = SoundEngine.getEngine().getLastSubSound();
                        }
                        break;
                    case SEE_OR_SET_REPLY_TYPE:
                        commandResponse = parseReplyType(first, botnakUserName);
                        break;
                    case SEE_OR_SET_VOLUME:
                        if ("".equals(first)) {
                            getBot().sendMessage(channel, "The current Sound volume is " + String.format("%.1f", Settings.soundVolumeGain.getValue()));
                        } else {
                            try {
                                Float volume = Float.parseFloat(first);
                                volume = Utils.capNumber(0F, 100F, volume);
                                Settings.soundVolumeGain.setValue(volume);
                                getBot().sendMessage(channel, "The Sound volume was successfully set to " + String.format("%.1f", Settings.soundVolumeGain.getValue()));
                            } catch (Exception e) {
                                getBot().sendMessage(channel, "Failed to change Sound volume! Usage: \"!volume (number)\"");
                            }
                        }
                        break;
                    default:
                        break;
                }
                if (commandResponse != null && !"".equals(commandResponse.getResponseText()))
                    getBot().sendMessage(channel, commandResponse.getResponseText());
            }
            //text command
            Command c = Utils.getCommand(trigger);
            //could infinite loop (two commands calling each other over and over)
            if (c != null && !senderIsBot && c.getMessage().data.length > 0 && !c.getDelayTimer().isRunning()) {
                StringArray sa = c.getMessage();
                if (c.hasArguments()) {
                    //build arguments if it has any
                    int argAmount = c.countArguments();
                    if ((split.length - 1) < argAmount) {
                        getBot().sendMessage(channel, "Missing command arguments! Command format: " + c.printCommand());
                        return;
                    }
                    String[] definedArguments = new String[argAmount];
                    System.arraycopy(split, 1, definedArguments, 0, argAmount);
                    sa = c.buildMessage(sa, definedArguments);
                }
                //send the message
                for (String s : sa.data) {
                    getBot().sendMessage(channel, s);
                }
                c.getDelayTimer().reset();
            }
        }
    }
}
Also used : User(lib.pircbot.User) ArrayList(java.util.ArrayList) Sound(sound.Sound) ConsoleCommand(util.comm.ConsoleCommand) Oauth(irc.account.Oauth) ConsoleCommand(util.comm.ConsoleCommand) Command(util.comm.Command) Raffle(util.misc.Raffle) Face(face.Face) File(java.io.File)

Example 3 with ConsoleCommand

use of util.comm.ConsoleCommand in project Botnak by Gocnak.

the class Settings method loadConsoleCommands.

public static void loadConsoleCommands() {
    HashSet<ConsoleCommand> hardcoded = new HashSet<>();
    hardcoded.add(new ConsoleCommand("addface", ConsoleCommand.Action.ADD_FACE, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("changeface", ConsoleCommand.Action.CHANGE_FACE, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("removeface", ConsoleCommand.Action.REMOVE_FACE, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("toggleface", ConsoleCommand.Action.TOGGLE_FACE, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("addsound", ConsoleCommand.Action.ADD_SOUND, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("changesound", ConsoleCommand.Action.CHANGE_SOUND, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("removesound", ConsoleCommand.Action.REMOVE_SOUND, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("setsound", ConsoleCommand.Action.SET_SOUND_DELAY, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("togglesound", ConsoleCommand.Action.TOGGLE_SOUND, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("stopsound", ConsoleCommand.Action.STOP_SOUND, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("stopallsounds", ConsoleCommand.Action.STOP_ALL_SOUNDS, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("addkeyword", ConsoleCommand.Action.ADD_KEYWORD, Permissions.Permission.BROADCASTER.permValue, null));
    hardcoded.add(new ConsoleCommand("removekeyword", ConsoleCommand.Action.REMOVE_KEYWORD, Permissions.Permission.BROADCASTER.permValue, null));
    hardcoded.add(new ConsoleCommand("setcol", ConsoleCommand.Action.SET_USER_COL, Permissions.Permission.ALL.permValue, null));
    hardcoded.add(new ConsoleCommand("setpermission", ConsoleCommand.Action.SET_COMMAND_PERMISSION, Permissions.Permission.BROADCASTER.permValue, null));
    hardcoded.add(new ConsoleCommand("addcommand", ConsoleCommand.Action.ADD_TEXT_COMMAND, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("removecommand", ConsoleCommand.Action.REMOVE_TEXT_COMMAND, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("adddonation", ConsoleCommand.Action.ADD_DONATION, Permissions.Permission.BROADCASTER.permValue, null));
    hardcoded.add(new ConsoleCommand("setsoundperm", ConsoleCommand.Action.SET_SOUND_PERMISSION, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("setnameface", ConsoleCommand.Action.SET_NAME_FACE, Permissions.Permission.SUBSCRIBER.permValue, null));
    hardcoded.add(new ConsoleCommand("removenameface", ConsoleCommand.Action.REMOVE_NAME_FACE, Permissions.Permission.SUBSCRIBER.permValue, null));
    hardcoded.add(new ConsoleCommand("playad", ConsoleCommand.Action.PLAY_ADVERT, Permissions.Permission.BROADCASTER.permValue, null));
    hardcoded.add(new ConsoleCommand("settitle", ConsoleCommand.Action.SET_STREAM_TITLE, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("title", ConsoleCommand.Action.SEE_STREAM_TITLE, Permissions.Permission.ALL.permValue, null));
    hardcoded.add(new ConsoleCommand("setgame", ConsoleCommand.Action.SET_STREAM_GAME, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("game", ConsoleCommand.Action.SEE_STREAM_GAME, Permissions.Permission.ALL.permValue, null));
    hardcoded.add(new ConsoleCommand("startraffle", ConsoleCommand.Action.START_RAFFLE, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("addrafflewinner", ConsoleCommand.Action.ADD_RAFFLE_WINNER, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("stopraffle", ConsoleCommand.Action.STOP_RAFFLE, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("removerafflewinner", ConsoleCommand.Action.REMOVE_RAFFLE_WINNER, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("winners", ConsoleCommand.Action.SEE_WINNERS, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("startpoll", ConsoleCommand.Action.START_POLL, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("vote", ConsoleCommand.Action.VOTE_POLL, Permissions.Permission.ALL.permValue, null));
    hardcoded.add(new ConsoleCommand("pollresult", ConsoleCommand.Action.POLL_RESULT, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("cancelpoll", ConsoleCommand.Action.CANCEL_POLL, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("song", ConsoleCommand.Action.NOW_PLAYING, Permissions.Permission.ALL.permValue, null));
    hardcoded.add(new ConsoleCommand("soundstate", ConsoleCommand.Action.SEE_SOUND_STATE, Permissions.Permission.MODERATOR.permValue, null));
    hardcoded.add(new ConsoleCommand("uptime", ConsoleCommand.Action.SHOW_UPTIME, Permissions.Permission.ALL.permValue, null));
    hardcoded.add(new ConsoleCommand("lastsubsound", ConsoleCommand.Action.SEE_PREV_SOUND_SUB, Permissions.Permission.ALL.permValue, null));
    hardcoded.add(new ConsoleCommand("lastdonationsound", ConsoleCommand.Action.SEE_PREV_SOUND_DON, Permissions.Permission.ALL.permValue, null));
    hardcoded.add(new ConsoleCommand("botreply", ConsoleCommand.Action.SEE_OR_SET_REPLY_TYPE, Permissions.Permission.BROADCASTER.permValue, null));
    hardcoded.add(new ConsoleCommand("volume", ConsoleCommand.Action.SEE_OR_SET_VOLUME, Permissions.Permission.MODERATOR.permValue, null));
    if (Utils.areFilesGood(ccommandsFile.getAbsolutePath())) {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(ccommandsFile.toURI().toURL().openStream()))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] split = line.split("\\[");
                ConsoleCommand.Action a = getAction(split[1]);
                int classPerm;
                try {
                    classPerm = Integer.parseInt(split[2]);
                } catch (Exception e) {
                    classPerm = -1;
                }
                String[] customUsers = null;
                if (!split[3].equalsIgnoreCase("null")) {
                    customUsers = split[3].split(",");
                }
                GUIMain.conCommands.add(new ConsoleCommand(split[0], a, classPerm, customUsers));
            }
            if (GUIMain.conCommands.size() != hardcoded.size()) {
                //something's not right...
                for (ConsoleCommand hard : hardcoded) {
                    boolean isAdded = false;
                    for (ConsoleCommand loaded : GUIMain.conCommands) {
                        if (hard.getTrigger().equalsIgnoreCase(loaded.getTrigger())) {
                            isAdded = true;
                            //this ensures every hard coded ConCommand is loaded for Botnak
                            break;
                        }
                    }
                    if (!isAdded) {
                        //if it isn't in the file, add it
                        GUIMain.conCommands.add(hard);
                    }
                }
            }
        } catch (Exception e) {
            GUIMain.log(e);
        }
    } else {
        //first time boot/reset/deleted file etc.
        GUIMain.conCommands.addAll(hardcoded.stream().collect(Collectors.toList()));
    }
    GUIMain.log("Loaded console commands!");
}
Also used : ConsoleCommand(util.comm.ConsoleCommand)

Example 4 with ConsoleCommand

use of util.comm.ConsoleCommand in project Botnak by Gocnak.

the class Utils method setCommandPermission.

/**
     * Sets the permission of a console command based on the input received.
     * <p>
     * Ex:
     * !setpermission mod 0
     * >Everybody can now mod each other
     * <p>
     * !setpermission mod gocnak,gmansoliver
     * >Only Gocnak and Gmansoliver can mod people
     * <p>
     * etc.
     * <p>
     * Note: This WILL reset the permissions and /then/ set it to specified.
     * If you wish to add another name, you will have to retype the ones already
     * allowed!
     *
     * @param mess The entire message to dissect.
     */
public static Response setCommandPermission(String mess) {
    Response toReturn = new Response();
    if (mess == null) {
        toReturn.setResponseText("Failed to set command permission, message is null!");
        return toReturn;
    }
    String[] split = mess.split(" ");
    String trigger = split[1];
    for (ConsoleCommand c : GUIMain.conCommands) {
        if (trigger.equalsIgnoreCase(c.getTrigger())) {
            int classPerm;
            String[] certainPerm = null;
            try {
                classPerm = Integer.parseInt(split[2]);
            } catch (Exception e) {
                classPerm = -1;
                certainPerm = split[2].split(",");
            }
            c.setCertainPermission(certainPerm);
            c.setClassPermission(classPerm);
            toReturn.wasSuccessful();
            toReturn.setResponseText("Successfully set the command permission for " + trigger + " !");
            break;
        }
    }
    return toReturn;
}
Also used : ConsoleCommand(util.comm.ConsoleCommand)

Example 5 with ConsoleCommand

use of util.comm.ConsoleCommand in project Botnak by Gocnak.

the class Utils method getConsoleCommand.

/**
     * Gets the console command if the user met the trigger and permission of it.
     *
     * @param key     The name of the command.
     * @param channel The channel the user is in.
     * @return The console command, or null if the user didn't meet the requirements.
     */
public static ConsoleCommand getConsoleCommand(String key, String channel, User u) {
    String master = Settings.accountManager.getUserAccount().getName();
    if (!channel.contains(master))
        return null;
    if (u != null) {
        for (ConsoleCommand c : GUIMain.conCommands) {
            if (key.equalsIgnoreCase(c.getTrigger())) {
                int conPerm = c.getClassPermission();
                String[] certainPerms = c.getCertainPermissions();
                if (conPerm == -1) {
                    if (certainPerms != null) {
                        for (String s : certainPerms) {
                            //specified name permission
                            if (s.equalsIgnoreCase(u.getNick())) {
                                return c;
                            }
                        }
                    }
                } else {
                    //int class permission
                    if (Permissions.hasAtLeast(Permissions.getUserPermissions(u, channel), conPerm)) {
                        return c;
                    }
                }
            }
        }
    }
    return null;
}
Also used : ConsoleCommand(util.comm.ConsoleCommand)

Aggregations

ConsoleCommand (util.comm.ConsoleCommand)5 Face (face.Face)1 Oauth (irc.account.Oauth)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 User (lib.pircbot.User)1 Sound (sound.Sound)1 Command (util.comm.Command)1 Raffle (util.misc.Raffle)1