Skip to content

Commit

Permalink
Updated the ChatListener to respond to commands by private message. A…
Browse files Browse the repository at this point in the history
…lso, updated the list command to have paging. If you have more than one page of results it will ask you to specify the page number. If you provide the page number it will give back that list of sound files.
  • Loading branch information
Darkside138 committed May 7, 2016
1 parent c39f80f commit 6ae5631
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import net.dirtydeeds.discordsoundboard.beans.SoundFile;
import net.dirtydeeds.discordsoundboard.service.SoundPlayerImpl;
import net.dv8tion.jda.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.events.message.MessageReceivedEvent;
import net.dv8tion.jda.hooks.ListenerAdapter;
import net.dv8tion.jda.utils.SimpleLog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand All @@ -15,9 +17,9 @@
* This class handles listening to commands in discord text channels and responding to them.
*/
public class ChatSoundBoardListener extends ListenerAdapter {

private static final SimpleLog LOG = SimpleLog.getLog("ChatListener");

private SoundPlayerImpl soundPlayer;
private String commandCharacter = "?";
private Integer messageSizeLimit = 2000;
Expand All @@ -29,55 +31,46 @@ public ChatSoundBoardListener(SoundPlayerImpl soundPlayer, String commandCharact
}
if (messageSizeLimit != null && !messageSizeLimit.isEmpty() && messageSizeLimit.matches("^-?\\d+$")) {
this.messageSizeLimit = Integer.parseInt(messageSizeLimit);
if (this.messageSizeLimit > 1994) {
this.messageSizeLimit = 1994;
}
}
}

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

//Respond
if (message.startsWith(commandCharacter + "list")) {
Set<Map.Entry<String, SoundFile>> entrySet = soundPlayer.getAvailableSoundFiles().entrySet();

final int maxLineLength = messageSizeLimit;
StringBuilder output = new StringBuilder(maxLineLength);
if (entrySet.size() > 0) {
sb.append("```Type any of the following into the chat to play the sound: \n");
for (Map.Entry entry : entrySet) {
sb.append(commandCharacter).append(entry.getKey()).append("\n");
}
sb.append("```");

LOG.info("Responding to chat request from " + requestingUser + ".");

//if text has \n, \r or \t symbols it's better to split by \s+
final String SPLIT_REGEXP= "(?<=[ \\n])";

String[] tokens = sb.toString().split(SPLIT_REGEXP);
int lineLen = 0;
for (int i = 0; i < tokens.length; i++) {
String word = tokens[i];

if (lineLen + (word).length() > maxLineLength) {
if (i > 0) {
output.append("```");
event.getChannel().sendMessage(output.toString());

output = new StringBuilder(maxLineLength);
output.append("```");
}
lineLen = 0;
}
output.append(word);
lineLen += word.length();
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: ```?list pageNumber``` to request a specific page of results.");
} else {
replyByPrivateMessage(event, "```Type any of the following into the chat to play the sound:\n```");
replyByPrivateMessage(event, "```" + soundList.get(0) + "```");
}
} else {
sb.append("The soundboard has no available sounds to play.");
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.");
}
}
event.getChannel().sendMessage(output.toString());
//If the command is not list and starts with the specified command character try and play that "command" or sound file.
//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());
Expand All @@ -88,4 +81,55 @@ public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
}
}
}

private List<String> getCommandList(StringBuilder commandString) {
final int maxLineLength = messageSizeLimit;
List<String> soundFiles = new ArrayList<>();

//if text has \n, \r or \t symbols it's better to split by \s+
final String SPLIT_REGEXP= "(?<=[ \\n])";

String[] tokens = commandString.toString().split(SPLIT_REGEXP);
int lineLen = 0;
StringBuilder output = new StringBuilder();
output.append("```");
for (int i = 0; i < tokens.length; i++) {
String word = tokens[i];

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

output = new StringBuilder(maxLineLength);
output.append("```");
}
lineLen = 0;
}
output.append(word);
lineLen += word.length();
}
if (output.length() > 0) {
output.append("```");
}
soundFiles.add(output.toString());
return soundFiles;
}

private StringBuilder getCommandListString() {
StringBuilder sb = new StringBuilder();

Set<Map.Entry<String, SoundFile>> entrySet = soundPlayer.getAvailableSoundFiles().entrySet();

if (entrySet.size() > 0) {
for (Map.Entry entry : entrySet) {
sb.append(commandCharacter).append(entry.getKey()).append("\n");
}
}
return sb;
}

private void replyByPrivateMessage(MessageReceivedEvent event, String message) {
event.getAuthor().getPrivateChannel().sendMessage(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import net.dv8tion.jda.audio.player.FilePlayer;
import net.dv8tion.jda.entities.Guild;
import net.dv8tion.jda.entities.VoiceChannel;
import net.dv8tion.jda.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.events.message.MessageReceivedEvent;
import net.dv8tion.jda.managers.AudioManager;
import net.dv8tion.jda.player.MusicPlayer;
import net.dv8tion.jda.player.source.AudioSource;
Expand Down Expand Up @@ -108,7 +108,7 @@ public void playFileForUser(String fileName, String userName) {
* the sound back in.
* @throws Exception
*/
public void playFileForEvent(String fileName, GuildMessageReceivedEvent event) throws Exception {
public void playFileForEvent(String fileName, MessageReceivedEvent event) throws Exception {
if (event != null) {
guild = event.getGuild();
moveToUserIdsChannel(event, guild);
Expand Down Expand Up @@ -137,7 +137,7 @@ private void moveToChannel(VoiceChannel channel, Guild guild){
* @param event - The event
* @throws Exception
*/
private void moveToUserIdsChannel(GuildMessageReceivedEvent event, Guild guild) throws Exception {
private void moveToUserIdsChannel(MessageReceivedEvent event, Guild guild) throws Exception {
VoiceChannel channel = null;

outerloop:
Expand Down

0 comments on commit 6ae5631

Please sign in to comment.