aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/soundchan/LocalAudioManager.java
blob: 4db7010f4af2c0fd800acc8495219e6e57fe98bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package soundchan;

import net.dv8tion.jda.api.entities.MessageChannel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class LocalAudioManager {

    /*
      A list of local files for the sound bot to play.
     */

    public Map<String, String> filenameDict;
    public Map<String, String> usernameDict;
    private String filepath;
    private String userSoundFilepath;

    public LocalAudioManager(String filepath_in){
        filepath = filepath_in;
        userSoundFilepath = null;
        filenameDict = PopulateFiles();
    }

    /**
     * Constructor for when there is a file listing users and sounds to play for them
     * @param filepath_in Path to folder where sounds are located
     * @param userSoundFile Path to file with users and sounds
     */
    public LocalAudioManager(String filepath_in, String userSoundFile) {
        filepath = filepath_in;
        userSoundFilepath = userSoundFile;
        filenameDict = PopulateFiles();
        usernameDict = MapUserAudio();
    }

    /**
     * Gives filepath to sound to play either from a command or when given a username
     * @param command Command or username to play soundbite
     * @return Path to sound, or "" if no sound for given command
     */
    public String GetFilePath(String command){
        String path;
        try{
            path = filepath + "/" + filenameDict.get(command);
        }catch(Exception ex){
            System.out.println("File " + command + " not found!");
            path = "";
        }
        if(path.contentEquals("") || path.contentEquals(filepath + "/null")) {
            try {
                path = filepath + "/" + usernameDict.get(command);
            } catch (Exception ex) {
                System.out.println("File " + command + " not found!");
                path = "";
            }
        }
        if(path.contentEquals(filepath + "/null")) {
            return "";
        }
        return path;
    }

    public void ListSounds(MessageChannel channel){
        Set<String> localSounds = filenameDict.keySet();
        String toPrint = "The following sounds you can play are:\n```";
        for (String sound:
                localSounds) {
            toPrint = toPrint + " * " + sound + "\n";
        }
        toPrint = toPrint + "```";
        channel.sendMessage(toPrint).queue();
    }

    /**
     * Lists users with sounds that will play when they join the voice channel
     * @param channel Text channel messaged on
     */
    public void ListUserAudio(MessageChannel channel) {
        Set<String> userSounds = usernameDict.keySet();
        String toPrint = "The following users have sounds that will play when they join the voice channel:\n```";
        for (String user : userSounds) {
            String sound = usernameDict.get(user);
            toPrint =  toPrint + " * " + user + "\t" + sound.substring(0, sound.indexOf('.')) + "\n";
        }
        toPrint = toPrint + "```";
        channel.sendMessage(toPrint).queue();
    }

    /**
     * Updates the map of sound files
     */
    public void UpdateFiles() {
        filenameDict = PopulateFiles();
    }

    /**
     * Updates the map of usernames to sound files
     */
    public void UpdateUserAudio() {
        if(userSoundFilepath != null | userSoundFilepath.contentEquals("")) {
            usernameDict = MapUserAudio();
        }
    }

    /**
     * Creates a map of the sounds in the sound directory
     * @return A map with the filename (without extension) is the key for the filename (with extension)
     */
    private Map<String, String> PopulateFiles(){
        File folder = new File(filepath);
        File[] listOfFiles = folder.listFiles();

        Map<String, String> fileDict = new HashMap<>();

        for (File file : listOfFiles) {
            if (file.isFile()) {
                String filename = file.getName();
                fileDict.put(filename.substring(0, filename.indexOf('.')), filename);
            }
        }
        return fileDict;
    }

    /**
     * Reads in users and their respective sounds from file, then builds a map of users to the filenames. This assumes
     * filenames for the sounds are valid, but doesn't check for them.
     * @return A map with the usernames as the keys for the filename of the sound
     */
    private Map<String, String> MapUserAudio() {
        Properties userSoundProp = LoadProperties(userSoundFilepath);
        Set<String> users = userSoundProp.stringPropertyNames();

        Map<String, String> userDict = new HashMap<>();

        for(String user : users) {
            String soundFile = userSoundProp.getProperty(user);
            userDict.put(user, soundFile);
        }
        return userDict;
    }

    /**
     * Builds a property object from a file.
     * @param filename File to be read
     * @return Property object with information from file
     */
    private static Properties LoadProperties(String filename){
        Properties properties = new Properties();
        InputStream input = null;
        File file = new File(filename);
        if(file.exists() && !file.isDirectory()) {
            try {
                input = new FileInputStream(filename);
                properties.load(input);
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    input.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            return properties;
        } else {
            return properties;
        }
    }
}