From 6ae563106b3a3f3d731b606556d2d24b8a7a2c3d Mon Sep 17 00:00:00 2001 From: dfurrer Date: Sat, 7 May 2016 09:50:11 -0500 Subject: [PATCH] Updated the ChatListener to respond to commands by private message. Also, 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. --- .../ChatSoundBoardListener.java | 126 ++++++++++++------ .../service/SoundPlayerImpl.java | 6 +- 2 files changed, 88 insertions(+), 44 deletions(-) diff --git a/src/main/java/net/dirtydeeds/discordsoundboard/ChatSoundBoardListener.java b/src/main/java/net/dirtydeeds/discordsoundboard/ChatSoundBoardListener.java index b8ab80d..0f02199 100644 --- a/src/main/java/net/dirtydeeds/discordsoundboard/ChatSoundBoardListener.java +++ b/src/main/java/net/dirtydeeds/discordsoundboard/ChatSoundBoardListener.java @@ -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; @@ -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; @@ -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> 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 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()); @@ -88,4 +81,55 @@ public void onGuildMessageReceived(GuildMessageReceivedEvent event) { } } } + + private List getCommandList(StringBuilder commandString) { + final int maxLineLength = messageSizeLimit; + List 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> 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); + } } diff --git a/src/main/java/net/dirtydeeds/discordsoundboard/service/SoundPlayerImpl.java b/src/main/java/net/dirtydeeds/discordsoundboard/service/SoundPlayerImpl.java index 3307cb4..31a80ea 100644 --- a/src/main/java/net/dirtydeeds/discordsoundboard/service/SoundPlayerImpl.java +++ b/src/main/java/net/dirtydeeds/discordsoundboard/service/SoundPlayerImpl.java @@ -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; @@ -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); @@ -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: