Skip to content

Commit

Permalink
Updated the chat listener with some additions from Zasa. Also, refact…
Browse files Browse the repository at this point in the history
…ored some code in SoundPlayer to better handle commands coming from private messages. Changed the SoundFile bean to not keep File objects which reduced memory usage by quite a bit.
  • Loading branch information
Darkside138 committed May 11, 2016
1 parent b93234f commit 9a96be8
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ChatSoundBoardListener extends ListenerAdapter {
private SoundPlayerImpl soundPlayer;
private String commandCharacter = "?";
private Integer messageSizeLimit = 2000;
private boolean muted;

public ChatSoundBoardListener(SoundPlayerImpl soundPlayer, String commandCharacter, String messageSizeLimit) {
this.soundPlayer = soundPlayer;
Expand All @@ -35,52 +36,91 @@ public ChatSoundBoardListener(SoundPlayerImpl soundPlayer, String commandCharact
this.messageSizeLimit = 1994;
}
}
muted=false;
}

@Override
public void onMessageReceived(MessageReceivedEvent event) {
String message = event.getMessage().getContent();
String requestingUser = event.getAuthor().getUsername();
final int maxLineLength = messageSizeLimit;

//Respond
if (message.startsWith(commandCharacter + "list")) {
StringBuilder commandString = getCommandListString();
List<String> soundList = getCommandList(commandString);

LOG.info("Responding to " + message + " command. Requested by " + requestingUser + ".");
if (message.equals(commandCharacter + "list")) {
if (commandString.length() > maxLineLength) {
replyByPrivateMessage(event, "You have " + soundList.size() + " pages of soundFiles. Reply: ```" + commandCharacter + "list pageNumber``` to request a specific page of results.");
if(!event.getAuthor().isBot()) {
String message = event.getMessage().getContent();
String requestingUser = event.getAuthor().getUsername();
final int maxLineLength = messageSizeLimit;

//Respond
if (message.startsWith(commandCharacter + "list")) {
StringBuilder commandString = getCommandListString();
List<String> soundList = getCommandList(commandString);

LOG.info("Responding to " + message + " command. Requested by " + requestingUser + ".");
if (message.equals(commandCharacter + "list")) {
if (commandString.length() > maxLineLength) {
replyByPrivateMessage(event, "You have " + soundList.size() + " pages of soundFiles. Reply: ```" + commandCharacter + "list pageNumber``` to request a specific page of results.");
} else {
replyByPrivateMessage(event, "Type any of the following into the chat to play the sound:");
replyByPrivateMessage(event, soundList.get(0));
}
} else {
String[] messageSplit = message.split(" ");
try {
Integer pageNumber = Integer.parseInt(messageSplit[1]);
replyByPrivateMessage(event, soundList.get(pageNumber - 1));
} catch (IndexOutOfBoundsException e) {
replyByPrivateMessage(event, "The page number you entered is not valid.");
} catch (NumberFormatException e) {
replyByPrivateMessage(event, "The page number argument must be a number.");
}
}
//If the command is not list and starts with the specified command character try and play that "command" or sound file.
} else if (message.startsWith(commandCharacter + "help")) {
LOG.info("Responding to help command. Requested by " + requestingUser + ".");
replyByPrivateMessage(event, "Type ```" + commandCharacter + "list``` to get a list of available sound files. Type ```" + commandCharacter + "soundFileName``` to play the a sound from the list.");
} else if(message.startsWith(commandCharacter + "volume")) {
int newVol = Integer.parseInt(message.substring(8));
if(newVol >= 1 && newVol <= 100) {
muted = false;
soundPlayer.setSoundPlayerVolume(newVol);
replyByPrivateMessage(event, "*Volume set to " + newVol + "%*");
LOG.info("Volume set to " + newVol + "% by " + requestingUser + ".");
} else if(newVol == 0) {
muted = true;
soundPlayer.setSoundPlayerVolume(newVol);
replyByPrivateMessage(event, requestingUser + " muted me.");
LOG.info("Bot muted by " + requestingUser + ".");
soundPlayer.playFileForUser("stop",requestingUser);
}
} else if (message.startsWith(commandCharacter + "stop")) {
if (soundPlayer.stop()) {
replyByPrivateMessage(event, "Playback stopped.");
} else {
replyByPrivateMessage(event, "Type any of the following into the chat to play the sound:\n");
replyByPrivateMessage(event, soundList.get(0));
replyByPrivateMessage(event, "Nothing was playing.");
}

} else if (message.startsWith(commandCharacter) && message.length() >= 2) {
if(!muted) {
try {
String fileNameRequested = message.substring(1, message.length());
LOG.info("Attempting to play file: " + fileNameRequested + ". Requested by " + requestingUser + ".");
soundPlayer.playFileForEvent(fileNameRequested, event);
} catch (Exception e) {
e.printStackTrace();
}
} else {
replyByPrivateMessage(event, "I seem to be muted! Try " + commandCharacter + "help");
LOG.info("Attempting to play a sound file while muted. Requested by " + requestingUser + ".");
}
} else {
String[] messageSplit = message.split(" ");
try {
Integer pageNumber = Integer.parseInt(messageSplit[1]);
replyByPrivateMessage(event, soundList.get(pageNumber - 1));
} catch (IndexOutOfBoundsException e) {
replyByPrivateMessage(event, "The page number you entered is not valid.");
} catch (NumberFormatException e) {
replyByPrivateMessage(event, "The page number argument must be a number.");
if (message.startsWith(commandCharacter) || event.isPrivate()) {
nonRecognizedCommand(event, requestingUser);
}
}
//If the command is not list and starts with the specified command character try and play that "command" or sound file.
} else if (message.startsWith(commandCharacter + "help")) {
LOG.info("Responding to help command. Requested by " + requestingUser + ".");
replyByPrivateMessage(event, "Type ```" + commandCharacter + "list``` to get a list of available sound files. Type ```" + commandCharacter + "soundFileName``` to play the a sound from the list.");
} else if (message.startsWith(commandCharacter)) {
try {
String fileNameRequested = message.substring(1, message.length());
LOG.info("Attempting to play file: " + fileNameRequested + ". Requested by " + requestingUser + ".");
soundPlayer.playFileForEvent(fileNameRequested, event);
} catch (Exception e) {
e.printStackTrace();
}
}
}

private void nonRecognizedCommand(MessageReceivedEvent event, String requestingUser) {
replyByPrivateMessage(event, "Hello @" + requestingUser + ". I don't know how to respond to this message!");
replyByPrivateMessage(event, "You can type " + commandCharacter + "list to see a list of all playable Sounds. Type " + commandCharacter + "volume 0 - 100 to set the bots volume.");
LOG.info("Responding to PM of " + requestingUser + ". Unknown Command. Sending help text.");
}

private List<String> getCommandList(StringBuilder commandString) {
final int maxLineLength = messageSizeLimit;
Expand All @@ -92,13 +132,13 @@ private List<String> getCommandList(StringBuilder commandString) {
String[] tokens = commandString.toString().split(SPLIT_REGEXP);
int lineLen = 0;
StringBuilder output = new StringBuilder();
output.append("```");
output.append("```\n");
for (int i = 0; i < tokens.length; i++) {
String word = tokens[i];

if (lineLen + (word).length() > maxLineLength) {
if (i > 0) {
output.append("```");
output.append("```\n");
soundFiles.add(output.toString());

output = new StringBuilder(maxLineLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
public class SoundFile {

private final String soundFileId;
private final File soundFile;
private final String soundFileLocation;
private final String category;

public SoundFile(String soundFileId, File soundFile, String category) {
public SoundFile(String soundFileId, String soundFileLocation, String category) {
this.soundFileId = soundFileId;
this.soundFile = soundFile;
this.soundFileLocation = soundFileLocation;
this.category = category;
}

Expand All @@ -27,18 +27,13 @@ public String getSoundFileId() {
}

public String getSoundFileLocation() {
return soundFile.toString();
return soundFileLocation;
}

public String getCategory() {
return category;
}

@JsonIgnore
public File getSoundFile() {
return soundFile;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Loading

0 comments on commit 9a96be8

Please sign in to comment.