Search in sources :

Example 1 with Response

use of util.Response in project Botnak by Gocnak.

the class FaceManager method removeFace.

/**
     * Removes a face from the Face HashMap and deletes the face picture file.
     *
     * @param key The name of the face to remove.
     */
public static Response removeFace(String key) {
    Response toReturn = new Response();
    if (!faceMap.containsKey(key)) {
        toReturn.setResponseText("Could not remove the face, there is no such face \"" + key + "\"!");
        return toReturn;
    }
    try {
        Face toDelete = faceMap.get(key);
        File f = new File(toDelete.getFilePath());
        if (f.delete()) {
            faceMap.remove(key);
            toReturn.wasSuccessful();
            toReturn.setResponseText("Successfully removed face \"" + key + "\"!");
        } else {
            toReturn.setResponseText("Could not remove face due to I/O error!");
        }
    } catch (Exception e) {
        toReturn.setResponseText("Could not delete face due to Exception: " + e.getMessage());
    }
    return toReturn;
}
Also used : Response(util.Response) File(java.io.File)

Example 2 with Response

use of util.Response in project Botnak by Gocnak.

the class FaceManager method toggleFace.

/**
     * Toggles a twitch face on/off.
     * <p>
     * Ex: !toggleface RitzMitz
     * would toggle RitzMitz off/on in showing up on botnak,
     * depending on current state.
     *
     * @param faceName The face name to toggle.
     */
public static Response toggleFace(String faceName) {
    Response toReturn = new Response();
    if (faceName == null || !doneWithTwitchFaces) {
        if (doneWithTwitchFaces)
            toReturn.setResponseText("Failed to toggle face, the face name is null!");
        else
            toReturn.setResponseText("Failed to toggle face, not done checking Twitch faces!");
        return toReturn;
    }
    Set<Map.Entry<Integer, TwitchFace>> set = twitchFaceMap.entrySet();
    for (Map.Entry<Integer, TwitchFace> entry : set) {
        TwitchFace fa = entry.getValue();
        String regex = fa.getRegex();
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(faceName);
        if (m.find()) {
            boolean newStatus = !fa.isEnabled();
            fa.setEnabled(newStatus);
            toReturn.setResponseText("Toggled the face " + faceName + (newStatus ? " ON" : " OFF"));
            toReturn.wasSuccessful();
            return toReturn;
        }
    }
    String errorMessage = "Could not find face " + faceName + " in the loaded Twitch faces";
    if (Settings.ffzFacesEnable.getValue()) {
        Set<Map.Entry<String, ArrayList<FrankerFaceZ>>> channels = ffzFaceMap.entrySet();
        for (Map.Entry<String, ArrayList<FrankerFaceZ>> entry : channels) {
            ArrayList<FrankerFaceZ> faces = entry.getValue();
            for (FrankerFaceZ f : faces) {
                if (f.getRegex().equalsIgnoreCase(faceName)) {
                    boolean newStatus = !f.isEnabled();
                    f.setEnabled(newStatus);
                    toReturn.setResponseText("Toggled the FrankerFaceZ face " + f.getRegex() + (newStatus ? " ON" : " OFF"));
                    toReturn.wasSuccessful();
                    return toReturn;
                }
            }
        }
        errorMessage += " or loaded FrankerFaceZ faces";
    }
    errorMessage += "!";
    toReturn.setResponseText(errorMessage);
    return toReturn;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Response(util.Response) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with Response

use of util.Response in project Botnak by Gocnak.

the class FaceManager method downloadFace.

/**
     * Downloads a face off of the internet using the given URL and stores it in the given
     * directory with the given filename and extension. The regex (or "name") of the face is put in the map
     * for later use/comparison.
     * <p>
     *
     * @param url       The URL to the face.
     * @param directory The directory to save the face in.
     * @param name      The name of the file for the face, including the extension.
     * @param regex     The regex pattern ("name") of the face.
     * @param type      What type of face it is.
     */
public static Response downloadFace(String url, String directory, String name, String regex, FACE_TYPE type) {
    Response toReturn = new Response();
    if (directory == null || name == null || directory.equals("") || name.equals("")) {
        toReturn.setResponseText("Failed to download face, the directory or name is null!");
        return toReturn;
    }
    try {
        File toSave = new File(directory + File.separator + name);
        if (download(url, toSave, type)) {
            if (type == FACE_TYPE.NORMAL_FACE) {
                Face face = new Face(regex, toSave.getAbsolutePath());
                name = Utils.removeExt(name);
                //put it
                faceMap.put(name, face);
                toReturn.setResponseText("Successfully added the normal face: " + name + " !");
            } else {
                Face face = new Face(regex, toSave.getAbsolutePath());
                name = Utils.removeExt(name);
                nameFaceMap.put(name, face);
                toReturn.setResponseText("Successfully added the nameface for user: " + name + " !");
            }
            toReturn.wasSuccessful();
        } else {
            toReturn.setResponseText("Failed to download the face, perhaps a bad URL!");
        }
    } catch (Exception e) {
        toReturn.setResponseText("Failed to download face due to Exception: " + e.getMessage());
    }
    return toReturn;
}
Also used : Response(util.Response) File(java.io.File)

Example 4 with Response

use of util.Response in project Botnak by Gocnak.

the class SoundEngine method stopSound.

public Response stopSound(boolean all) {
    Response toReturn = new Response();
    if (all) {
        Collection<SoundEntry> coll = getCurrentPlayingSounds();
        if (!coll.isEmpty()) {
            coll.forEach(SoundEntry::close);
            toReturn.setResponseText("Successfully stopped all playing sounds!");
            toReturn.wasSuccessful();
        } else {
            toReturn.setResponseText("There are no sounds currently playing!");
        }
    } else {
        //first sound it can find
        SoundEntry sound = getCurrentPlayingSound();
        if (sound != null) {
            sound.close();
            toReturn.wasSuccessful();
            toReturn.setResponseText("Successfully stopped the first found playing sound!");
        } else {
            toReturn.setResponseText("There are no sounds currently playing!");
        }
    }
    return toReturn;
}
Also used : Response(util.Response)

Example 5 with Response

use of util.Response in project Botnak by Gocnak.

the class SoundEngine method handleSound.

/**
     * Handles the adding/changing of a sound, its permission, and/or its files.
     *
     * @param s      The string from the chat to manipulate.
     * @param change True for changing a sound, false for adding.
     */
public Response handleSound(String s, boolean change) {
    Response toReturn = new Response();
    if (!Settings.defaultSoundDir.getValue().equals("null") && !Settings.defaultSoundDir.getValue().equals("")) {
        try {
            String[] split = s.split(" ");
            //both commands have this in common.
            String name = split[1].toLowerCase();
            int perm;
            if (split.length > 3) {
                //!add/changesound sound 0 sound(,maybe,more)
                try {
                    perm = Integer.parseInt(split[2]);
                } catch (Exception e) {
                    toReturn.setResponseText("Failed to handle sound, could not parse the permission!");
                    return toReturn;
                }
                String files = split[3];
                if (perm < 0 || perm > 4) {
                    toReturn.setResponseText("Failed to handle sound due to a bad permission!");
                    return toReturn;
                }
                if (!files.contains(",")) {
                    //isn't multiple
                    //this can be !addsound sound 0 sound or !changesound sound 0 newsound
                    String filename = Settings.defaultSoundDir.getValue() + File.separator + Utils.setExtension(files, ".wav");
                    if (Utils.areFilesGood(filename)) {
                        if (soundMap.containsKey(name)) {
                            //they could technically change the permission here as well
                            if (!change) {
                                //!addsound
                                soundMap.put(name, new // add it tooo it maaan
                                Sound(// add it tooo it maaan
                                perm, Utils.addStringsToArray(soundMap.get(name).getSounds().data, filename)));
                                toReturn.setResponseText("Successfully added the sound \"" + filename + "\" to the command \"!" + name + "\" !");
                                toReturn.wasSuccessful();
                            } else {
                                //!changesound
                                //replace it
                                soundMap.put(name, new Sound(perm, filename));
                                toReturn.setResponseText("Successfully changed the sound \"!" + name + "\" !");
                                toReturn.wasSuccessful();
                            }
                        } else {
                            //*gasp* A NEW SOUND!?
                            if (!change) {
                                //can't have !changesound act like !addsound
                                soundMap.put(name, new Sound(perm, filename));
                                toReturn.setResponseText("Successfully added the new sound \"!" + name + "\" !");
                                toReturn.wasSuccessful();
                            } else {
                                toReturn.setResponseText("The sound \"!" + name + "\" does not exist; cannot change it!");
                            }
                        }
                    } else {
                        toReturn.setResponseText("Failed to handle sound, the file \"" + filename + "\" does not exist!");
                    }
                } else {
                    //is multiple
                    //this can be !addsound sound 0 multi,sound or !changesound sound 0 multi,sound
                    ArrayList<String> list = new ArrayList<>();
                    String[] filesSplit = files.split(",");
                    for (String str : filesSplit) {
                        list.add(Settings.defaultSoundDir.getValue() + File.separator + Utils.setExtension(str, ".wav"));
                    }
                    //calls the areFilesGood boolean in it (filters bad files already)
                    filesSplit = Utils.checkFiles(list.toArray(new String[list.size()]));
                    //recycle time!
                    list.clear();
                    if (!change) {
                        //adding sounds
                        if (soundMap.containsKey(name)) {
                            //adding sounds, so get the old ones V
                            Collections.addAll(list, soundMap.get(name).getSounds().data);
                        }
                        //checks for repetition, will add anyway if list is empty
                        Utils.checkAndAdd(list, filesSplit);
                        soundMap.put(name, new Sound(perm, list.toArray(new String[list.size()])));
                        toReturn.setResponseText("Successfully added multiple sounds!");
                        toReturn.wasSuccessful();
                        return toReturn;
                    } else {
                        //!changesound, so replace it if it's in there
                        if (soundMap.containsKey(name)) {
                            soundMap.put(name, new Sound(perm, filesSplit));
                            toReturn.setResponseText("Successfully changed the sound \"!" + name + "\" to have the new files!");
                            toReturn.wasSuccessful();
                        } else {
                            toReturn.setResponseText("Failed to change sound, the sound \"" + name + "\" does not exist!");
                        }
                    }
                }
            } else if (split.length == 3) {
                //add/changesound sound perm/newsound
                if (split[2].length() == 1) {
                    //ASSUMING it's a permission change.
                    try {
                        //I mean come on. What sound will have a 1 char name?
                        perm = Integer.parseInt(split[2]);
                        if (perm != -1 && perm >= 0 && perm < 5) {
                            if (change) {
                                //because adding just a sound name and a permission is silly
                                //A pretty bad one...
                                soundMap.put(name, new Sound(perm, soundMap.get(name).getSounds().data));
                                toReturn.setResponseText("Successfully changed the sound \"!" + name + "\" to have the new permission: " + perm);
                                toReturn.wasSuccessful();
                            } else {
                                toReturn.setResponseText("Failed to add sound, cannot add a permission as a sound!");
                            }
                        } else {
                            toReturn.setResponseText("Failed to change the permission, please give a permission from 0 to 4!");
                        }
                    } catch (NumberFormatException e) {
                        //maybe it really is a 1-char-named sound?
                        String test = Settings.defaultSoundDir.getValue() + File.separator + Utils.setExtension(split[2], ".wav");
                        if (Utils.areFilesGood(test)) {
                            //wow...
                            if (change) {
                                soundMap.put(name, new Sound(soundMap.get(name).getPermission(), test));
                                toReturn.setResponseText("Successfully changed the sound for \"!" + name + "\" !");
                                toReturn.wasSuccessful();
                            } else {
                                //adding a 1 char sound that exists to the pool...
                                soundMap.put(name, new Sound(soundMap.get(name).getPermission(), Utils.addStringsToArray(soundMap.get(name).getSounds().data, test)));
                                toReturn.setResponseText("Successfully added the sound \"" + split[2] + "\" to the command \"!" + name + "\" !");
                                toReturn.wasSuccessful();
                            }
                        } else {
                            toReturn.setResponseText("Failed to handle sound, invalid permission!");
                        }
                    }
                } else {
                    //it's a/some new file(s) as replacement/to add!
                    if (split[2].contains(",")) {
                        //multiple
                        String[] filesSplit = split[2].split(",");
                        ArrayList<String> list = new ArrayList<>();
                        for (String str : filesSplit) {
                            list.add(Settings.defaultSoundDir.getValue() + File.separator + Utils.setExtension(str, ".wav"));
                        }
                        //calls the areFilesGood boolean in it (filters bad files already)
                        filesSplit = Utils.checkFiles(list.toArray(new String[list.size()]));
                        if (!change) {
                            //!addsound soundname more,sounds
                            if (soundMap.containsKey(name)) {
                                filesSplit = Utils.addStringsToArray(soundMap.get(name).getSounds().data, filesSplit);
                                soundMap.put(name, new Sound(soundMap.get(name).getPermission(), filesSplit));
                                toReturn.wasSuccessful();
                                toReturn.setResponseText("Successfully added " + filesSplit.length + " new sounds to the sound \"!" + name + "\" !");
                            } else {
                                //use default permission
                                soundMap.put(name, new Sound(filesSplit));
                                toReturn.setResponseText("Successfully added " + filesSplit.length + " new sounds to the new command \"!" + name + "\" !");
                                toReturn.wasSuccessful();
                            }
                        } else {
                            //!changesound soundname new,sounds
                            if (soundMap.containsKey(name)) {
                                //!changesound isn't !addsound
                                soundMap.put(name, new Sound(soundMap.get(name).getPermission(), filesSplit));
                                toReturn.setResponseText("Successfully changed the sound \"!" + name + "\" to have the " + filesSplit.length + " new files!");
                                toReturn.wasSuccessful();
                            } else {
                                toReturn.setResponseText("Cannot change the sound, \"" + name + "\" doesn't exist!");
                            }
                        }
                    } else {
                        //singular
                        String test = Settings.defaultSoundDir.getValue() + File.separator + Utils.setExtension(split[2], ".wav");
                        if (Utils.areFilesGood(test)) {
                            if (!change) {
                                //!addsound sound newsound
                                if (soundMap.containsKey(name)) {
                                    //getting the old permission/files
                                    soundMap.put(name, new Sound(soundMap.get(name).getPermission(), Utils.addStringsToArray(soundMap.get(name).getSounds().data, test)));
                                    toReturn.setResponseText("Successfully added the sound \"" + split[2] + "\" to the command \"!" + name + "\" !");
                                    toReturn.wasSuccessful();
                                } else {
                                    //use default permission
                                    soundMap.put(name, new Sound(test));
                                    toReturn.setResponseText("Successfully added new sound \"!" + name + "\" !");
                                    toReturn.wasSuccessful();
                                }
                            } else {
                                //!changesound sound newsound
                                if (soundMap.containsKey(name)) {
                                    //!changesound isn't !addsound
                                    soundMap.put(name, new Sound(soundMap.get(name).getPermission(), test));
                                    toReturn.setResponseText("Successfully changed the sound \"" + name + "\" to have the new sound \"" + split[2] + "\" !");
                                    toReturn.wasSuccessful();
                                } else {
                                    toReturn.setResponseText("Cannot change sound, \"" + name + "\" does not exist!");
                                }
                            }
                        } else {
                            toReturn.setResponseText("Cannot handle sound, the file \"" + split[2] + "\" does not exist!");
                        }
                    }
                }
            }
        } catch (Exception e) {
            toReturn.setResponseText("Failed to handle sound due to Exception: " + e.getMessage());
        }
    } else {
        toReturn.setResponseText("Failed to handle sound, the default sound directory is null!");
    }
    return toReturn;
}
Also used : Response(util.Response)

Aggregations

Response (util.Response)14 File (java.io.File)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 FaceManager (face.FaceManager)1 BotnakTrayIcon (gui.BotnakTrayIcon)1 ChatPane (gui.ChatPane)1 CombinedChatPane (gui.CombinedChatPane)1 DraggableTabbedPane (gui.DraggableTabbedPane)1 ListenerUserChat (gui.listeners.ListenerUserChat)1 NewTabListener (gui.listeners.NewTabListener)1 IRCBot (irc.IRCBot)1 IRCViewer (irc.IRCViewer)1 Message (irc.message.Message)1 MessageQueue (irc.message.MessageQueue)1 java.awt (java.awt)1 java.awt.event (java.awt.event)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1