Refactored how commands are handled
This commit is contained in:
@ -9,11 +9,11 @@ import net.dv8tion.jda.api.requests.ErrorResponse;
|
|||||||
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
||||||
import net.locusworks.discord.eighttrack.handlers.ReactionHandler;
|
import net.locusworks.discord.eighttrack.handlers.ReactionHandler;
|
||||||
|
|
||||||
public abstract class AbstractEventHandler {
|
public abstract class AbstractEvent {
|
||||||
|
|
||||||
private String command;
|
private String command;
|
||||||
|
|
||||||
public AbstractEventHandler(String command) {
|
protected AbstractEvent(String command) {
|
||||||
this.command = command;
|
this.command = command;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,13 +47,25 @@ public abstract class AbstractEventHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void sendMessage(GuildMessageReceivedEvent event, String msg) {
|
protected void sendMessage(GuildMessageReceivedEvent event, String msg) {
|
||||||
|
sendMessage(event, msg, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void sendMessage(GuildMessageReceivedEvent event, String msg, boolean wait) {
|
||||||
|
if (!wait) {
|
||||||
event.getChannel().sendMessage(msg).queue();
|
event.getChannel().sendMessage(msg).queue();
|
||||||
|
} else {
|
||||||
|
event.getChannel().sendMessage(msg).complete();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void sendMessage(GuildMessageReceivedEvent event, String msgFmt, Object... args) {
|
protected void sendMessage(GuildMessageReceivedEvent event, String msgFmt, Object... args) {
|
||||||
sendMessage(event, String.format(msgFmt, args));
|
sendMessage(event, String.format(msgFmt, args));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void sendMessage(GuildMessageReceivedEvent event, boolean wait, String msgFmt, Object... args) {
|
||||||
|
sendMessage(event, String.format(msgFmt, args), wait);
|
||||||
|
}
|
||||||
|
|
||||||
protected void deleteMessage(Message msg, ReactionHandler reactionHandler, Long guildId) {
|
protected void deleteMessage(Message msg, ReactionHandler reactionHandler, Long guildId) {
|
||||||
try {
|
try {
|
||||||
msg.delete().complete();
|
msg.delete().complete();
|
@ -0,0 +1,12 @@
|
|||||||
|
package net.locusworks.discord.eighttrack.events.main;
|
||||||
|
|
||||||
|
import net.locusworks.discord.eighttrack.events.AbstractEvent;
|
||||||
|
|
||||||
|
public abstract class AbstractMainEventHandler extends AbstractEvent {
|
||||||
|
|
||||||
|
public AbstractMainEventHandler(String command) {
|
||||||
|
super(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,17 +1,17 @@
|
|||||||
package net.locusworks.discord.eighttrack.events.handlers;
|
package net.locusworks.discord.eighttrack.events.main.handlers;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||||
import net.locusworks.discord.eighttrack.events.AbstractEventHandler;
|
import net.locusworks.discord.eighttrack.events.main.AbstractMainEventHandler;
|
||||||
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
||||||
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
||||||
import net.locusworks.logger.ApplicationLogger;
|
import net.locusworks.logger.ApplicationLogger;
|
||||||
import net.locusworks.logger.ApplicationLoggerFactory;
|
import net.locusworks.logger.ApplicationLoggerFactory;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class NextSongHandler extends AbstractEventHandler {
|
public class NextSongHandler extends AbstractMainEventHandler {
|
||||||
|
|
||||||
private ApplicationLogger logger;
|
private ApplicationLogger logger;
|
||||||
|
|
@ -1,17 +1,17 @@
|
|||||||
package net.locusworks.discord.eighttrack.events.handlers;
|
package net.locusworks.discord.eighttrack.events.main.handlers;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||||
import net.locusworks.discord.eighttrack.events.AbstractEventHandler;
|
import net.locusworks.discord.eighttrack.events.main.AbstractMainEventHandler;
|
||||||
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
||||||
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
||||||
import net.locusworks.logger.ApplicationLogger;
|
import net.locusworks.logger.ApplicationLogger;
|
||||||
import net.locusworks.logger.ApplicationLoggerFactory;
|
import net.locusworks.logger.ApplicationLoggerFactory;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class PlaySongHandler extends AbstractEventHandler {
|
public class PlaySongHandler extends AbstractMainEventHandler {
|
||||||
|
|
||||||
private ApplicationLogger logger;
|
private ApplicationLogger logger;
|
||||||
|
|
@ -0,0 +1,75 @@
|
|||||||
|
package net.locusworks.discord.eighttrack.events.main.handlers;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||||
|
import net.locusworks.discord.eighttrack.events.main.AbstractMainEventHandler;
|
||||||
|
import net.locusworks.discord.eighttrack.events.playlist.AbstractPlaylistEventHandler;
|
||||||
|
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
||||||
|
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
||||||
|
import net.locusworks.logger.ApplicationLogger;
|
||||||
|
import net.locusworks.logger.ApplicationLoggerFactory;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class PlaylistHandler extends AbstractMainEventHandler {
|
||||||
|
|
||||||
|
private ApplicationLogger logger;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RepositoryService guildRepoService;
|
||||||
|
|
||||||
|
private Map<String, AbstractPlaylistEventHandler> handlers;
|
||||||
|
|
||||||
|
public PlaylistHandler() {
|
||||||
|
super("-playlist");
|
||||||
|
logger = ApplicationLoggerFactory.getLogger(PlaylistHandler.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private void init(List<AbstractPlaylistEventHandler> eventHandlers) {
|
||||||
|
handlers = new TreeMap<>();
|
||||||
|
for(AbstractPlaylistEventHandler apeh : eventHandlers) {
|
||||||
|
handlers.put(apeh.getCommand(), apeh);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void executeEvent(GuildMusicHandler musicHandler, GuildMessageReceivedEvent event, List<String> commands) {
|
||||||
|
if (commands == null || commands.isEmpty()) {
|
||||||
|
event.getChannel().sendMessage("Missing command for -playlist. Valid commands are: add, delete, list, play").queue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
String command = commands.remove(0);
|
||||||
|
|
||||||
|
AbstractPlaylistEventHandler apeh = handlers.get(command);
|
||||||
|
if (apeh == null) {
|
||||||
|
sendMessage(event, help());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
apeh.executeEvent(musicHandler, event, commands);
|
||||||
|
|
||||||
|
} catch (Exception ex) {
|
||||||
|
logger.error("Unable to display playlist: " + ex.getMessage(), ex);
|
||||||
|
guildRepoService.logEntry(event.getGuild().getIdLong(), ex, "Unable to display playlist: %s", ex.getMessage());
|
||||||
|
sendMessage(event, "Sorry I am unable to display the playlist: " + ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String help() {
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for(Entry<String, AbstractPlaylistEventHandler> entry : handlers.entrySet()) {
|
||||||
|
sb.append(entry.getValue().help()).append("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return "-playlist | " + sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,17 +1,17 @@
|
|||||||
package net.locusworks.discord.eighttrack.events.handlers;
|
package net.locusworks.discord.eighttrack.events.main.handlers;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||||
import net.locusworks.discord.eighttrack.events.AbstractEventHandler;
|
import net.locusworks.discord.eighttrack.events.main.AbstractMainEventHandler;
|
||||||
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
||||||
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
||||||
import net.locusworks.logger.ApplicationLogger;
|
import net.locusworks.logger.ApplicationLogger;
|
||||||
import net.locusworks.logger.ApplicationLoggerFactory;
|
import net.locusworks.logger.ApplicationLoggerFactory;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class RepeatSongHandler extends AbstractEventHandler {
|
public class RepeatSongHandler extends AbstractMainEventHandler {
|
||||||
|
|
||||||
private ApplicationLogger logger;
|
private ApplicationLogger logger;
|
||||||
|
|
@ -0,0 +1,81 @@
|
|||||||
|
package net.locusworks.discord.eighttrack.events.main.handlers;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||||
|
import net.locusworks.discord.eighttrack.database.entities.GuildSong;
|
||||||
|
import net.locusworks.discord.eighttrack.events.main.AbstractMainEventHandler;
|
||||||
|
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
||||||
|
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
||||||
|
import net.locusworks.logger.ApplicationLogger;
|
||||||
|
import net.locusworks.logger.ApplicationLoggerFactory;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class SongListHandler extends AbstractMainEventHandler {
|
||||||
|
|
||||||
|
private ApplicationLogger logger;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RepositoryService guildRepoService;
|
||||||
|
|
||||||
|
public SongListHandler() {
|
||||||
|
super("-list");
|
||||||
|
logger = ApplicationLoggerFactory.getLogger(SongListHandler.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void executeEvent(GuildMusicHandler musicHandler, GuildMessageReceivedEvent event, List<String> commands) {
|
||||||
|
try {
|
||||||
|
listSongs(event, commands);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
logger.error("Unable to play next song: " + ex.getMessage(), ex);
|
||||||
|
guildRepoService.logEntry(event.getGuild().getIdLong(), ex, "Unable to play next song: %s", ex.getMessage());
|
||||||
|
sendMessage(event, "Sorry I am unable to play next song: " + ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void listSongs(GuildMessageReceivedEvent event, List<String> commands) throws Exception {
|
||||||
|
List<GuildSong> gsList = guildRepoService.getGuildSongRepo().findByGuild(event.getGuild().getIdLong());
|
||||||
|
|
||||||
|
if (gsList == null || gsList.isEmpty()) {
|
||||||
|
sendMessage(event, "There is no music for this guild.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int longestSong = 0;
|
||||||
|
int longestArtist = 0;
|
||||||
|
for(GuildSong gs : gsList) {
|
||||||
|
if (gs.getSong().getTitle().length() > longestSong) longestSong = gs.getSong().getTitle().length();
|
||||||
|
if (gs.getSong().getArtist().length() > longestArtist) longestArtist = gs.getSong().getArtist().length();
|
||||||
|
}
|
||||||
|
|
||||||
|
sendMessage(event, true, "**Currently available songs for %s**\n\n", event.getGuild().getName());
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
String fmt = "%6s | %-" + longestSong +"s | %-" + longestArtist +"s | %s%n";
|
||||||
|
StringBuilder sb = new StringBuilder("```");
|
||||||
|
sb.append(String.format(fmt, "Track", "Title", "Artist", "id"));
|
||||||
|
sb.append(StringUtils.repeat("-", 27 + longestSong + longestArtist)).append("\n");
|
||||||
|
|
||||||
|
for(GuildSong gs : gsList) {
|
||||||
|
String msg = String.format(fmt, ++count, gs.getSong().getTitle(), gs.getSong().getArtist(), gs.getUuid());
|
||||||
|
if (sb.length() + msg.length() > 2000) {
|
||||||
|
sb.append("```");
|
||||||
|
sendMessage(event, sb.toString(), true);
|
||||||
|
sb = new StringBuilder("```");
|
||||||
|
} else {
|
||||||
|
sb.append(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sb.append("```");
|
||||||
|
sendMessage(event, sb.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String help() {
|
||||||
|
return "-list (lists the sonds to play)";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,17 +1,17 @@
|
|||||||
package net.locusworks.discord.eighttrack.events.handlers;
|
package net.locusworks.discord.eighttrack.events.main.handlers;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||||
import net.locusworks.discord.eighttrack.events.AbstractEventHandler;
|
import net.locusworks.discord.eighttrack.events.main.AbstractMainEventHandler;
|
||||||
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
||||||
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
||||||
import net.locusworks.logger.ApplicationLogger;
|
import net.locusworks.logger.ApplicationLogger;
|
||||||
import net.locusworks.logger.ApplicationLoggerFactory;
|
import net.locusworks.logger.ApplicationLoggerFactory;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class StopSongHandler extends AbstractEventHandler {
|
public class StopSongHandler extends AbstractMainEventHandler {
|
||||||
|
|
||||||
private ApplicationLogger logger;
|
private ApplicationLogger logger;
|
||||||
|
|
@ -1,17 +1,17 @@
|
|||||||
package net.locusworks.discord.eighttrack.events.handlers;
|
package net.locusworks.discord.eighttrack.events.main.handlers;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||||
import net.locusworks.discord.eighttrack.events.AbstractEventHandler;
|
import net.locusworks.discord.eighttrack.events.main.AbstractMainEventHandler;
|
||||||
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
||||||
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
||||||
import net.locusworks.logger.ApplicationLogger;
|
import net.locusworks.logger.ApplicationLogger;
|
||||||
import net.locusworks.logger.ApplicationLoggerFactory;
|
import net.locusworks.logger.ApplicationLoggerFactory;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class UpNextSongHandler extends AbstractEventHandler {
|
public class UpNextSongHandler extends AbstractMainEventHandler {
|
||||||
|
|
||||||
private ApplicationLogger logger;
|
private ApplicationLogger logger;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package net.locusworks.discord.eighttrack.events.handlers;
|
package net.locusworks.discord.eighttrack.events.main.handlers;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -21,7 +21,7 @@ import net.locusworks.discord.eighttrack.audio.Mp3UploadResults;
|
|||||||
import net.locusworks.discord.eighttrack.database.entities.DiscordGuild;
|
import net.locusworks.discord.eighttrack.database.entities.DiscordGuild;
|
||||||
import net.locusworks.discord.eighttrack.database.entities.GuildSong;
|
import net.locusworks.discord.eighttrack.database.entities.GuildSong;
|
||||||
import net.locusworks.discord.eighttrack.database.entities.Song;
|
import net.locusworks.discord.eighttrack.database.entities.Song;
|
||||||
import net.locusworks.discord.eighttrack.events.AbstractEventHandler;
|
import net.locusworks.discord.eighttrack.events.main.AbstractMainEventHandler;
|
||||||
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
||||||
import net.locusworks.discord.eighttrack.services.ConfigurationService;
|
import net.locusworks.discord.eighttrack.services.ConfigurationService;
|
||||||
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
||||||
@ -29,7 +29,7 @@ import net.locusworks.logger.ApplicationLogger;
|
|||||||
import net.locusworks.logger.ApplicationLoggerFactory;
|
import net.locusworks.logger.ApplicationLoggerFactory;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class UploadHandler extends AbstractEventHandler {
|
public class UploadHandler extends AbstractMainEventHandler {
|
||||||
|
|
||||||
private ApplicationLogger logger;
|
private ApplicationLogger logger;
|
||||||
|
|
@ -0,0 +1,12 @@
|
|||||||
|
package net.locusworks.discord.eighttrack.events.playlist;
|
||||||
|
|
||||||
|
import net.locusworks.discord.eighttrack.events.AbstractEvent;
|
||||||
|
|
||||||
|
public abstract class AbstractPlaylistEventHandler extends AbstractEvent {
|
||||||
|
|
||||||
|
|
||||||
|
public AbstractPlaylistEventHandler(String command) {
|
||||||
|
super(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,120 @@
|
|||||||
|
package net.locusworks.discord.eighttrack.events.playlist.handlers;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.time.OffsetDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import net.dv8tion.jda.api.EmbedBuilder;
|
||||||
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||||
|
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||||
|
import net.locusworks.discord.eighttrack.database.entities.DiscordGuild;
|
||||||
|
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylist;
|
||||||
|
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylistSong;
|
||||||
|
import net.locusworks.discord.eighttrack.database.entities.GuildSong;
|
||||||
|
import net.locusworks.discord.eighttrack.events.playlist.AbstractPlaylistEventHandler;
|
||||||
|
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
||||||
|
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class PlaylistAddHandler extends AbstractPlaylistEventHandler {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RepositoryService guildRepoService;
|
||||||
|
|
||||||
|
public PlaylistAddHandler() {
|
||||||
|
super("add");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void executeEvent(GuildMusicHandler musicHandler, GuildMessageReceivedEvent event, List<String> commands) {
|
||||||
|
if(!checkCommands(event, commands,
|
||||||
|
event.getAuthor().getAsMention() + " you have to provide the playlist to create a new playlist (no spaces in name) and optionally a list of uuids for songs to add")) return;
|
||||||
|
|
||||||
|
Long guildId = event.getGuild().getIdLong();
|
||||||
|
|
||||||
|
DiscordGuild guild = guildRepoService.getGuildRepo().findByGuildId(guildId);
|
||||||
|
if (guild == null) {
|
||||||
|
event.getChannel().sendMessage("Unable to find guild in local database. Please contact administrator").queue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String playlist = commands.remove(0);
|
||||||
|
|
||||||
|
long userId = event.getMember().getIdLong();
|
||||||
|
|
||||||
|
GuildPlaylist gpl = guildRepoService.getGuildPlaylistRepo().findByGuildAndUserIdAndPlaylist(guild, userId, playlist);
|
||||||
|
boolean newList = false;
|
||||||
|
if (gpl != null && commands.isEmpty()) {
|
||||||
|
event.getChannel().sendMessage(event.getAuthor().getAsMention() + " a playlist with the name " + playlist + " already exist for you.").queue();
|
||||||
|
return;
|
||||||
|
} else if (gpl == null) {
|
||||||
|
newList = true;
|
||||||
|
gpl = new GuildPlaylist();
|
||||||
|
gpl.setDateAdded(new Date());
|
||||||
|
gpl.setGuild(guild);
|
||||||
|
gpl.setPlaylist(playlist);
|
||||||
|
gpl.setUserId(userId);
|
||||||
|
guildRepoService.getGuildPlaylistRepo().save(gpl);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (commands.isEmpty() && newList) {
|
||||||
|
event.getChannel().sendMessage(event.getAuthor().getAsMention() + " playlist " + playlist + " successfully created.").queue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<String> songIds = guildRepoService.getGuildPlaylistSongRepo()
|
||||||
|
.findByGuildPlaylistAndSongIds(gpl, commands).stream()
|
||||||
|
.map(g -> g.getGuildSong().getUuid())
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
|
List<GuildPlaylistSong> gplsList = new ArrayList<>();
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for(String id : commands.stream().collect(Collectors.toSet())) {
|
||||||
|
GuildSong gs = guildRepoService.getGuildSongRepo().findByUuid(id);
|
||||||
|
if (songIds.contains(id)) {
|
||||||
|
sb.append(String.format("**%s** by __%s__ already exists in this playlist.%n", gs.getSong().getTitle(), gs.getSong().getArtist()));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gs == null) {
|
||||||
|
sb.append("Song with id `" + id + "` not found. Please check id\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
GuildPlaylistSong gpls = new GuildPlaylistSong();
|
||||||
|
gpls.setDateAdded(new Date());
|
||||||
|
gpls.setGuildPlaylist(gpl);
|
||||||
|
gpls.setGuildSong(gs);
|
||||||
|
gplsList.add(gpls);
|
||||||
|
|
||||||
|
sb.append(String.format("**%s** by __%s__ added to playlist.%n", gs.getSong().getTitle(), gs.getSong().getArtist()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!gplsList.isEmpty()) {
|
||||||
|
guildRepoService.getGuildPlaylistSongRepo().saveAll(gplsList);
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageEmbed embed = new EmbedBuilder()
|
||||||
|
.setTitle("Results for adding playlist " + playlist)
|
||||||
|
.setAuthor(event.getMember().getEffectiveName(), null, event.getAuthor().getAvatarUrl())
|
||||||
|
.setColor(Color.GREEN)
|
||||||
|
.setDescription(sb.toString())
|
||||||
|
.setFooter("", event.getGuild().getSelfMember().getUser().getAvatarUrl())
|
||||||
|
.setTimestamp(OffsetDateTime.now())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
event.getChannel().sendMessage(embed).queue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String help() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,144 @@
|
|||||||
|
package net.locusworks.discord.eighttrack.events.playlist.handlers;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.time.OffsetDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import net.dv8tion.jda.api.EmbedBuilder;
|
||||||
|
import net.dv8tion.jda.api.entities.Message;
|
||||||
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||||
|
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||||
|
import net.locusworks.discord.eighttrack.database.entities.DiscordGuild;
|
||||||
|
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylist;
|
||||||
|
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylistSong;
|
||||||
|
import net.locusworks.discord.eighttrack.database.entities.GuildSong;
|
||||||
|
import net.locusworks.discord.eighttrack.events.playlist.AbstractPlaylistEventHandler;
|
||||||
|
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
||||||
|
import net.locusworks.discord.eighttrack.handlers.ReactionHandler;
|
||||||
|
import net.locusworks.discord.eighttrack.listeners.ReactionListener;
|
||||||
|
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
||||||
|
import net.locusworks.discord.eighttrack.utils.Reactions;
|
||||||
|
import net.locusworks.logger.ApplicationLogger;
|
||||||
|
import net.locusworks.logger.ApplicationLoggerFactory;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class PlaylistDeleteHandler extends AbstractPlaylistEventHandler {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RepositoryService guildRepoService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ReactionHandler reactionHandler;
|
||||||
|
|
||||||
|
private ApplicationLogger logger;
|
||||||
|
public PlaylistDeleteHandler() {
|
||||||
|
super("delete");
|
||||||
|
logger = ApplicationLoggerFactory.getLogger(PlaylistDeleteHandler.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void executeEvent(GuildMusicHandler musicHandler, GuildMessageReceivedEvent event, List<String> commands) {
|
||||||
|
if(!checkCommands(event, commands,
|
||||||
|
event.getAuthor().getAsMention() + " you have to provide the playlist name and optionally a list of uuids for songs to remove")) return;
|
||||||
|
|
||||||
|
Long guildId = event.getGuild().getIdLong();
|
||||||
|
|
||||||
|
DiscordGuild guild = guildRepoService.getGuildRepo().findByGuildId(guildId);
|
||||||
|
if (guild == null) {
|
||||||
|
event.getChannel().sendMessage("Unable to find guild in local database. Please contact administrator").queue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String playlist = commands.remove(0);
|
||||||
|
|
||||||
|
long userId = event.getMember().getIdLong();
|
||||||
|
|
||||||
|
GuildPlaylist gpl = guildRepoService.getGuildPlaylistRepo().findByGuildAndUserIdAndPlaylist(guild, userId, playlist);
|
||||||
|
|
||||||
|
if (commands.isEmpty()) {
|
||||||
|
|
||||||
|
MessageEmbed embed = new EmbedBuilder()
|
||||||
|
.setColor(Color.RED)
|
||||||
|
.setTitle("Delete Playlist " + playlist)
|
||||||
|
.setDescription("Are you sure you want to delete the playlist " + playlist)
|
||||||
|
.setTimestamp(OffsetDateTime.now())
|
||||||
|
.setFooter(event.getGuild().getSelfMember().getEffectiveName(), event.getGuild().getSelfMember().getUser().getAvatarUrl())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
event.getChannel().sendMessage(embed).queue((msg) -> {
|
||||||
|
|
||||||
|
ReactionListener<String> handler = new ReactionListener<>(userId, msg.getId());
|
||||||
|
handler.setExpiresIn(TimeUnit.MINUTES, 1);
|
||||||
|
handler.registerReaction(Reactions.CHECK_MARK_BUTTON, (ret) -> deletePlayListConfirm(gpl, event.getMember().getAsMention(), msg));
|
||||||
|
handler.registerReaction(Reactions.CROSS_MARK_BUTTON, (ret) -> deleteMessage(msg, reactionHandler, event.getGuild().getIdLong()));
|
||||||
|
|
||||||
|
reactionHandler.addReactionListener(guildId, msg, handler);
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<GuildPlaylistSong> songs = guildRepoService.getGuildPlaylistSongRepo().findByGuildPlaylistAndSongIds(gpl, commands);
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder("Are you sure you want to delete the following songs from playlist " + playlist + "?\n");
|
||||||
|
for (GuildPlaylistSong gpls : songs) {
|
||||||
|
GuildSong gs = gpls.getGuildSong();
|
||||||
|
sb.append(String.format("**%s** by __%s__%n", gs.getSong().getTitle(), gs.getSong().getArtist()));
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageEmbed embed = new EmbedBuilder()
|
||||||
|
.setColor(Color.RED)
|
||||||
|
.setTitle("Delete Playlist Songs")
|
||||||
|
.setDescription(sb.toString())
|
||||||
|
.setTimestamp(OffsetDateTime.now())
|
||||||
|
.setFooter(event.getGuild().getSelfMember().getEffectiveName(), event.getGuild().getSelfMember().getUser().getAvatarUrl())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
event.getChannel().sendMessage(embed).queue((msg) -> {
|
||||||
|
|
||||||
|
ReactionListener<String> handler = new ReactionListener<>(userId, msg.getId());
|
||||||
|
handler.setExpiresIn(TimeUnit.MINUTES, 1);
|
||||||
|
handler.registerReaction(Reactions.CHECK_MARK_BUTTON, (ret) -> deletePlayListSongConfirm(songs, event.getMember().getAsMention(), msg));
|
||||||
|
handler.registerReaction(Reactions.CROSS_MARK_BUTTON, (ret) -> deleteMessage(msg, reactionHandler, event.getGuild().getIdLong()));
|
||||||
|
|
||||||
|
reactionHandler.addReactionListener(guildId, msg, handler);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deletePlayListSongConfirm(List<GuildPlaylistSong> songs, String user, Message msg) {
|
||||||
|
try {
|
||||||
|
guildRepoService.getGuildPlaylistSongRepo().deleteAll(songs);
|
||||||
|
msg.getChannel().sendMessage(user + ", songs removed successfully fom playlist").complete();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
msg.getChannel().sendMessage("Sorry " + user + " I was unable to remove songs from the playlist. Reason: " + ex.getMessage()).complete();
|
||||||
|
logger.error("Unable to delete songs from playlist : " + ex.getMessage());
|
||||||
|
logger.error(ex);
|
||||||
|
guildRepoService.logEntry(msg.getGuild().getIdLong(), ex, "Unable to delete playlist: %s", ex.getMessage());
|
||||||
|
} finally {
|
||||||
|
deleteMessage(msg, reactionHandler, msg.getGuild().getIdLong());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deletePlayListConfirm(GuildPlaylist gpl, String user, Message msg) {
|
||||||
|
String playlist = gpl.getPlaylist();
|
||||||
|
try {
|
||||||
|
guildRepoService.getGuildPlaylistRepo().delete(gpl);
|
||||||
|
msg.getChannel().sendMessage(user + " " + playlist + " deleted successfully").complete();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
msg.getChannel().sendMessage("Sorry " + user + " I was unable to remove playlist " + playlist + ". Reason: " + ex.getMessage()).complete();
|
||||||
|
logger.error("Unable to delete playlist " + playlist + ": " + ex.getMessage());
|
||||||
|
logger.error(ex);
|
||||||
|
guildRepoService.logEntry(msg.getGuild().getIdLong(), ex, "Unable to delete playlist: %s", ex.getMessage());
|
||||||
|
} finally {
|
||||||
|
deleteMessage(msg, reactionHandler, msg.getGuild().getIdLong());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String help() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,97 @@
|
|||||||
|
package net.locusworks.discord.eighttrack.events.playlist.handlers;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||||
|
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylist;
|
||||||
|
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylistSong;
|
||||||
|
import net.locusworks.discord.eighttrack.database.entities.GuildSong;
|
||||||
|
import net.locusworks.discord.eighttrack.events.playlist.AbstractPlaylistEventHandler;
|
||||||
|
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
||||||
|
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class PlaylistListHandler extends AbstractPlaylistEventHandler {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RepositoryService guildRepoService;
|
||||||
|
|
||||||
|
public PlaylistListHandler() {
|
||||||
|
super("list");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void executeEvent(GuildMusicHandler musicHandler, GuildMessageReceivedEvent event, List<String> commands) {
|
||||||
|
if (commands == null || commands.isEmpty()) {
|
||||||
|
List<GuildPlaylist> userPlaylist = guildRepoService.getGuildPlaylistRepo().findByGuildAndUserIdFetchSongs(event.getGuild().getIdLong(), event.getMember().getIdLong());
|
||||||
|
if (userPlaylist.isEmpty()) {
|
||||||
|
event.getChannel().sendMessage(event.getMember().getAsMention() + " you have no defined playlists on this server").queue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int longestPlaylist = 10;
|
||||||
|
for(GuildPlaylist gpl : userPlaylist) {
|
||||||
|
if (gpl.getPlaylist().length() > longestPlaylist) longestPlaylist = gpl.getPlaylist().length();
|
||||||
|
}
|
||||||
|
|
||||||
|
String fmt = "%6s | %-" + longestPlaylist +"s | %s%n";
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder("```");
|
||||||
|
sb.append(String.format(fmt, "", "Playlist", "Song Count"));
|
||||||
|
sb.append(StringUtils.repeat("-", 24 + longestPlaylist)).append("\n");
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
for(GuildPlaylist gpl : userPlaylist) {
|
||||||
|
sb.append(String.format(fmt, ++count, gpl.getPlaylist(), gpl.getGuildPlaylistSongList().size()));
|
||||||
|
}
|
||||||
|
sb.append("```");
|
||||||
|
event.getChannel().sendMessage("**" + "Currently available playlists for " + event.getMember().getAsMention() + "**").queue((succcess)->
|
||||||
|
event.getChannel().sendMessage(sb.toString()).queue());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String playlist = commands.remove(0);
|
||||||
|
|
||||||
|
GuildPlaylist gpl = guildRepoService.getGuildPlaylistRepo().findByGuildUserPlaylistFetchSongs(event.getGuild().getIdLong(), event.getMember().getIdLong(), playlist);
|
||||||
|
if (gpl == null) {
|
||||||
|
event.getChannel().sendMessage(event.getMember().getAsMention() + " you have no defined playlists on this server by the name of " + playlist).queue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gpl.getGuildPlaylistSongList().isEmpty()) {
|
||||||
|
event.getChannel().sendMessage(event.getMember().getAsMention() + " you have no defined songs for playlist " + playlist).queue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int longestSong = 0;
|
||||||
|
int longestArtist = 0;
|
||||||
|
for(GuildPlaylistSong gpls : gpl.getGuildPlaylistSongList()) {
|
||||||
|
GuildSong gs = gpls.getGuildSong();
|
||||||
|
if (gs.getSong().getTitle().length() > longestSong) longestSong = gs.getSong().getTitle().length();
|
||||||
|
if (gs.getSong().getArtist().length() > longestArtist) longestArtist = gs.getSong().getArtist().length();
|
||||||
|
}
|
||||||
|
|
||||||
|
String fmt = "%6s | %-" + longestSong +"s | %-" + longestArtist +"s | %s%n";
|
||||||
|
int count = 0;
|
||||||
|
StringBuilder sb = new StringBuilder("```");
|
||||||
|
sb.append(String.format(fmt, "Track", "Title", "Artist", "id"));
|
||||||
|
sb.append(StringUtils.repeat("-", 27 + longestSong + longestArtist)).append("\n");
|
||||||
|
|
||||||
|
for(GuildPlaylistSong gpls : gpl.getGuildPlaylistSongList()) {
|
||||||
|
GuildSong gs = gpls.getGuildSong();
|
||||||
|
sb.append(String.format(fmt, ++count, gs.getSong().getTitle(), gs.getSong().getArtist(), gs.getUuid()));
|
||||||
|
}
|
||||||
|
sb.append("```");
|
||||||
|
event.getChannel().sendMessage("**" + "Current songs in playlists " + playlist + " for " + event.getMember().getAsMention() + "**").queue((succcess)->
|
||||||
|
event.getChannel().sendMessage(sb.toString()).queue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String help() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
package net.locusworks.discord.eighttrack.events.playlist.handlers;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||||
|
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylist;
|
||||||
|
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylistSong;
|
||||||
|
import net.locusworks.discord.eighttrack.database.entities.Song;
|
||||||
|
import net.locusworks.discord.eighttrack.events.playlist.AbstractPlaylistEventHandler;
|
||||||
|
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
|
||||||
|
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
||||||
|
import net.locusworks.logger.ApplicationLogger;
|
||||||
|
import net.locusworks.logger.ApplicationLoggerFactory;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class PlaylistPlayHandler extends AbstractPlaylistEventHandler {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RepositoryService guildRepoService;
|
||||||
|
|
||||||
|
private ApplicationLogger logger;
|
||||||
|
public PlaylistPlayHandler() {
|
||||||
|
super("play");
|
||||||
|
logger = ApplicationLoggerFactory.getLogger(PlaylistPlayHandler.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void executeEvent(GuildMusicHandler musicHandler, GuildMessageReceivedEvent event, List<String> commands) {
|
||||||
|
if (commands == null || commands.isEmpty()) {
|
||||||
|
event.getChannel().sendMessage(event.getAuthor().getAsMention() + " please provide the name of the playlist to play").queue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String playlist = commands.remove(0);
|
||||||
|
|
||||||
|
|
||||||
|
GuildPlaylist currentPlaylist = guildRepoService.getGuildPlaylistRepo().findByGuildAndUserIdAndPlaylist(event.getGuild().getIdLong(), event.getMember().getIdLong(), playlist);
|
||||||
|
|
||||||
|
if (currentPlaylist == null) {
|
||||||
|
sendMessage(event, event.getAuthor().getAsMention() + ", no playlist with the name of " + playlist + " exists");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
musicHandler.setCurrentPlaylist(currentPlaylist);
|
||||||
|
|
||||||
|
musicHandler.getTrackManager().clearTracks();
|
||||||
|
|
||||||
|
for(GuildPlaylistSong gpls : guildRepoService.getGuildPlaylistSongRepo().findByGuildPlaylist(currentPlaylist)) {
|
||||||
|
Song s = gpls.getGuildSong().getSong();
|
||||||
|
musicHandler.getTrackManager().queueLast(s.getFilePath());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
musicHandler.isPlaying(false);
|
||||||
|
musicHandler.play(event);
|
||||||
|
musicHandler.isPlaying(true);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
sendMessage(event, "Sorry " + event.getAuthor().getAsMention() + " I was unable to remove songs from the playlist. Reason: " + ex.getMessage(), true);
|
||||||
|
logger.error("Unable to play playlist : " + ex.getMessage());
|
||||||
|
logger.error(ex);
|
||||||
|
guildRepoService.logEntry(event.getGuild().getIdLong(), ex, "Unable to play playlist: %s", ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String help() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -51,7 +51,7 @@ import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
|||||||
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
|
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
|
||||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||||
import net.locusworks.discord.eighttrack.database.entities.DiscordGuild;
|
import net.locusworks.discord.eighttrack.database.entities.DiscordGuild;
|
||||||
import net.locusworks.discord.eighttrack.events.AbstractEventHandler;
|
import net.locusworks.discord.eighttrack.events.main.AbstractMainEventHandler;
|
||||||
import net.locusworks.discord.eighttrack.services.ConfigurationService;
|
import net.locusworks.discord.eighttrack.services.ConfigurationService;
|
||||||
import net.locusworks.discord.eighttrack.services.GuildMusicService;
|
import net.locusworks.discord.eighttrack.services.GuildMusicService;
|
||||||
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
||||||
@ -71,10 +71,10 @@ public class DiscordEventHandler extends ListenerAdapter {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ReactionHandler reactionHandler;
|
private ReactionHandler reactionHandler;
|
||||||
|
|
||||||
private Map<String, AbstractEventHandler> events;
|
private Map<String, AbstractMainEventHandler> events;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private void setup(ConfigurationService cService, List<AbstractEventHandler> eventHandlers) throws IOException {
|
private void setup(ConfigurationService cService, List<AbstractMainEventHandler> eventHandlers) throws IOException {
|
||||||
this.musicDir = cService.getMusicDirectory();
|
this.musicDir = cService.getMusicDirectory();
|
||||||
if (!Files.exists(this.musicDir.toAbsolutePath())) {
|
if (!Files.exists(this.musicDir.toAbsolutePath())) {
|
||||||
Files.createDirectories(this.musicDir.toAbsolutePath());
|
Files.createDirectories(this.musicDir.toAbsolutePath());
|
||||||
@ -82,7 +82,7 @@ public class DiscordEventHandler extends ListenerAdapter {
|
|||||||
|
|
||||||
events = new TreeMap<>();
|
events = new TreeMap<>();
|
||||||
|
|
||||||
for(AbstractEventHandler handler : eventHandlers) {
|
for(AbstractMainEventHandler handler : eventHandlers) {
|
||||||
events.put(handler.getCommand(), handler);
|
events.put(handler.getCommand(), handler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -165,19 +165,13 @@ public class DiscordEventHandler extends ListenerAdapter {
|
|||||||
|
|
||||||
String command = commands.remove(0);
|
String command = commands.remove(0);
|
||||||
|
|
||||||
AbstractEventHandler aeh = events.get(command.toLowerCase());
|
AbstractMainEventHandler aeh = events.get(command.toLowerCase());
|
||||||
if (aeh != null) {
|
if (aeh != null) {
|
||||||
aeh.executeEvent(gmh, event, commands);
|
aeh.executeEvent(gmh, event, commands);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(command) {
|
switch(command) {
|
||||||
case "-list":
|
|
||||||
gmh.list(event);
|
|
||||||
return;
|
|
||||||
case "-playlist":
|
|
||||||
gmh.playlist(event, commands);
|
|
||||||
return;
|
|
||||||
case "+playlist":
|
case "+playlist":
|
||||||
gmh.adminPlaylist(event, commands);
|
gmh.adminPlaylist(event, commands);
|
||||||
return;
|
return;
|
||||||
@ -189,7 +183,7 @@ public class DiscordEventHandler extends ListenerAdapter {
|
|||||||
if (!command.equalsIgnoreCase("-help")) return;
|
if (!command.equalsIgnoreCase("-help")) return;
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (Entry<String, AbstractEventHandler> entry : events.entrySet()) {
|
for (Entry<String, AbstractMainEventHandler> entry : events.entrySet()) {
|
||||||
sb.append(String.format("%s | %s%n", entry.getKey(), entry.getValue().help()));
|
sb.append(String.format("%s | %s%n", entry.getKey(), entry.getValue().help()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,13 +30,10 @@ package net.locusworks.discord.eighttrack.handlers;
|
|||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
@ -59,11 +56,8 @@ import net.dv8tion.jda.api.managers.AudioManager;
|
|||||||
import net.dv8tion.jda.api.requests.ErrorResponse;
|
import net.dv8tion.jda.api.requests.ErrorResponse;
|
||||||
import net.locusworks.discord.eighttrack.audio.EightTrackAudioSendHandler;
|
import net.locusworks.discord.eighttrack.audio.EightTrackAudioSendHandler;
|
||||||
import net.locusworks.discord.eighttrack.audio.TrackManager;
|
import net.locusworks.discord.eighttrack.audio.TrackManager;
|
||||||
import net.locusworks.discord.eighttrack.database.entities.DiscordGuild;
|
|
||||||
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylist;
|
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylist;
|
||||||
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylistSong;
|
|
||||||
import net.locusworks.discord.eighttrack.database.entities.GuildSong;
|
import net.locusworks.discord.eighttrack.database.entities.GuildSong;
|
||||||
import net.locusworks.discord.eighttrack.database.entities.Song;
|
|
||||||
import net.locusworks.discord.eighttrack.listeners.ReactionListener;
|
import net.locusworks.discord.eighttrack.listeners.ReactionListener;
|
||||||
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
import net.locusworks.discord.eighttrack.services.RepositoryService;
|
||||||
import net.locusworks.discord.eighttrack.utils.Reactions;
|
import net.locusworks.discord.eighttrack.utils.Reactions;
|
||||||
@ -97,31 +91,6 @@ public class GuildMusicHandler {
|
|||||||
this.trackManager = new TrackManager();
|
this.trackManager = new TrackManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void playlist(GuildMessageReceivedEvent event, List<String> commands) throws Exception {
|
|
||||||
|
|
||||||
if (commands == null || commands.isEmpty()) {
|
|
||||||
event.getChannel().sendMessage("Missing command for -playlist. Valid commands are: add, delete, list, play").queue();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String command = commands.remove(0);
|
|
||||||
|
|
||||||
switch(command) {
|
|
||||||
case "add":
|
|
||||||
addPlayList(event, commands);
|
|
||||||
return;
|
|
||||||
case "delete":
|
|
||||||
deletePlayList(event, commands);
|
|
||||||
return;
|
|
||||||
case "list":
|
|
||||||
listPlayList(event, commands);
|
|
||||||
return;
|
|
||||||
case "play":
|
|
||||||
playPlayList(event, commands);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getCurrentVoiceChannelId() {
|
public Long getCurrentVoiceChannelId() {
|
||||||
return voiceChannelId;
|
return voiceChannelId;
|
||||||
}
|
}
|
||||||
@ -284,43 +253,6 @@ public class GuildMusicHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void list(GuildMessageReceivedEvent event) {
|
|
||||||
List<GuildSong> gsList = guildSongRepoService.getGuildSongRepo().findByGuild(guildId);
|
|
||||||
|
|
||||||
if (gsList == null || gsList.isEmpty()) {
|
|
||||||
event.getChannel().sendMessage("There is no music for this guild.").queue();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int longestSong = 0;
|
|
||||||
int longestArtist = 0;
|
|
||||||
for(GuildSong gs : gsList) {
|
|
||||||
if (gs.getSong().getTitle().length() > longestSong) longestSong = gs.getSong().getTitle().length();
|
|
||||||
if (gs.getSong().getArtist().length() > longestArtist) longestArtist = gs.getSong().getArtist().length();
|
|
||||||
}
|
|
||||||
|
|
||||||
event.getChannel().sendMessage(String.format("**Currently available songs for %s**\n\n", event.getGuild().getName())).complete();
|
|
||||||
|
|
||||||
int count = 0;
|
|
||||||
String fmt = "%6s | %-" + longestSong +"s | %-" + longestArtist +"s | %s%n";
|
|
||||||
StringBuilder sb = new StringBuilder("```");
|
|
||||||
sb.append(String.format(fmt, "Track", "Title", "Artist", "id"));
|
|
||||||
sb.append(StringUtils.repeat("-", 27 + longestSong + longestArtist)).append("\n");
|
|
||||||
|
|
||||||
for(GuildSong gs : gsList) {
|
|
||||||
String msg = String.format(fmt, ++count, gs.getSong().getTitle(), gs.getSong().getArtist(), gs.getUuid());
|
|
||||||
if (sb.length() + msg.length() > 2000) {
|
|
||||||
sb.append("```");
|
|
||||||
event.getChannel().sendMessage(sb.toString()).complete();
|
|
||||||
sb = new StringBuilder("```");
|
|
||||||
} else {
|
|
||||||
sb.append(msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sb.append("```");
|
|
||||||
event.getChannel().sendMessage(sb.toString()).queue();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void adminPlaylist(GuildMessageReceivedEvent event, List<String> commands) throws IOException {
|
public void adminPlaylist(GuildMessageReceivedEvent event, List<String> commands) throws IOException {
|
||||||
if(!isAdmin(event)) return;
|
if(!isAdmin(event)) return;
|
||||||
if (!checkCommands(event, commands, event.getMember().getAsMention() + ", valid commands are +playst list, play, delete")) return;
|
if (!checkCommands(event, commands, event.getMember().getAsMention() + ", valid commands are +playst list, play, delete")) return;
|
||||||
@ -353,96 +285,6 @@ public class GuildMusicHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void playPlayList(GuildMessageReceivedEvent event, List<String> commands) throws Exception {
|
|
||||||
if (commands == null || commands.isEmpty()) {
|
|
||||||
event.getChannel().sendMessage(event.getAuthor().getAsMention() + " please provide the name of the playlist to play").queue();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
String playlist = commands.remove(0);
|
|
||||||
currentPlaylist = guildSongRepoService.getGuildPlaylistRepo().findByGuildAndUserIdAndPlaylist(guildId, event.getMember().getIdLong(), playlist);
|
|
||||||
if (currentPlaylist == null) {
|
|
||||||
event.getChannel().sendMessage(event.getAuthor().getAsMention() + ", no playlist with the name of " + playlist + " exists").queue();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
trackManager.clearTracks();
|
|
||||||
|
|
||||||
for(GuildPlaylistSong gpls : guildSongRepoService.getGuildPlaylistSongRepo().findByGuildPlaylist(currentPlaylist)) {
|
|
||||||
Song s = gpls.getGuildSong().getSong();
|
|
||||||
trackManager.queueLast(s.getFilePath());
|
|
||||||
}
|
|
||||||
|
|
||||||
playing.set(false);
|
|
||||||
play(event);
|
|
||||||
playing.set(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void listPlayList(GuildMessageReceivedEvent event, List<String> commands) {
|
|
||||||
if (commands == null || commands.isEmpty()) {
|
|
||||||
List<GuildPlaylist> userPlaylist = guildSongRepoService.getGuildPlaylistRepo().findByGuildAndUserIdFetchSongs(event.getGuild().getIdLong(), event.getMember().getIdLong());
|
|
||||||
if (userPlaylist.isEmpty()) {
|
|
||||||
event.getChannel().sendMessage(event.getMember().getAsMention() + " you have no defined playlists on this server").queue();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int longestPlaylist = 10;
|
|
||||||
for(GuildPlaylist gpl : userPlaylist) {
|
|
||||||
if (gpl.getPlaylist().length() > longestPlaylist) longestPlaylist = gpl.getPlaylist().length();
|
|
||||||
}
|
|
||||||
|
|
||||||
String fmt = "%6s | %-" + longestPlaylist +"s | %s%n";
|
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder("```");
|
|
||||||
sb.append(String.format(fmt, "", "Playlist", "Song Count"));
|
|
||||||
sb.append(StringUtils.repeat("-", 24 + longestPlaylist)).append("\n");
|
|
||||||
|
|
||||||
int count = 0;
|
|
||||||
for(GuildPlaylist gpl : userPlaylist) {
|
|
||||||
sb.append(String.format(fmt, ++count, gpl.getPlaylist(), gpl.getGuildPlaylistSongList().size()));
|
|
||||||
}
|
|
||||||
sb.append("```");
|
|
||||||
event.getChannel().sendMessage("**" + "Currently available playlists for " + event.getMember().getAsMention() + "**").queue((succcess)->
|
|
||||||
event.getChannel().sendMessage(sb.toString()).queue());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String playlist = commands.remove(0);
|
|
||||||
|
|
||||||
GuildPlaylist gpl = guildSongRepoService.getGuildPlaylistRepo().findByGuildUserPlaylistFetchSongs(event.getGuild().getIdLong(), event.getMember().getIdLong(), playlist);
|
|
||||||
if (gpl == null) {
|
|
||||||
event.getChannel().sendMessage(event.getMember().getAsMention() + " you have no defined playlists on this server by the name of " + playlist).queue();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (gpl.getGuildPlaylistSongList().isEmpty()) {
|
|
||||||
event.getChannel().sendMessage(event.getMember().getAsMention() + " you have no defined songs for playlist " + playlist).queue();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int longestSong = 0;
|
|
||||||
int longestArtist = 0;
|
|
||||||
for(GuildPlaylistSong gpls : gpl.getGuildPlaylistSongList()) {
|
|
||||||
GuildSong gs = gpls.getGuildSong();
|
|
||||||
if (gs.getSong().getTitle().length() > longestSong) longestSong = gs.getSong().getTitle().length();
|
|
||||||
if (gs.getSong().getArtist().length() > longestArtist) longestArtist = gs.getSong().getArtist().length();
|
|
||||||
}
|
|
||||||
|
|
||||||
String fmt = "%6s | %-" + longestSong +"s | %-" + longestArtist +"s | %s%n";
|
|
||||||
int count = 0;
|
|
||||||
StringBuilder sb = new StringBuilder("```");
|
|
||||||
sb.append(String.format(fmt, "Track", "Title", "Artist", "id"));
|
|
||||||
sb.append(StringUtils.repeat("-", 27 + longestSong + longestArtist)).append("\n");
|
|
||||||
|
|
||||||
for(GuildPlaylistSong gpls : gpl.getGuildPlaylistSongList()) {
|
|
||||||
GuildSong gs = gpls.getGuildSong();
|
|
||||||
sb.append(String.format(fmt, ++count, gs.getSong().getTitle(), gs.getSong().getArtist(), gs.getUuid()));
|
|
||||||
}
|
|
||||||
sb.append("```");
|
|
||||||
event.getChannel().sendMessage("**" + "Current songs in playlists " + playlist + " for " + event.getMember().getAsMention() + "**").queue((succcess)->
|
|
||||||
event.getChannel().sendMessage(sb.toString()).queue());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void deleteSong(GuildMessageReceivedEvent event, List<String> commands) {
|
private void deleteSong(GuildMessageReceivedEvent event, List<String> commands) {
|
||||||
if(!checkCommands(event, commands,
|
if(!checkCommands(event, commands,
|
||||||
event.getAuthor().getAsMention() + ", you have to provide the list of uuids for songs to remove")) return;
|
event.getAuthor().getAsMention() + ", you have to provide the list of uuids for songs to remove")) return;
|
||||||
@ -545,14 +387,12 @@ public class GuildMusicHandler {
|
|||||||
|
|
||||||
ReactionListener<String> handler = new ReactionListener<>(userId, msg.getId());
|
ReactionListener<String> handler = new ReactionListener<>(userId, msg.getId());
|
||||||
handler.setExpiresIn(TimeUnit.MINUTES, 1);
|
handler.setExpiresIn(TimeUnit.MINUTES, 1);
|
||||||
handler.registerReaction(Reactions.CHECK_MARK_BUTTON, (ret) -> deletePlayListConfirm(gpl, event.getMember().getAsMention(), msg));
|
//handler.registerReaction(Reactions.CHECK_MARK_BUTTON, (ret) -> deletePlayListConfirm(gpl, event.getMember().getAsMention(), msg));
|
||||||
handler.registerReaction(Reactions.CROSS_MARK_BUTTON, (ret) -> deleteMessage(msg));
|
handler.registerReaction(Reactions.CROSS_MARK_BUTTON, (ret) -> deleteMessage(msg));
|
||||||
|
|
||||||
reactionHandler.addReactionListener(guildId, msg, handler);
|
reactionHandler.addReactionListener(guildId, msg, handler);
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteGuildSongsConfirm(List<GuildSong> songs, String user, Message msg) {
|
private void deleteGuildSongsConfirm(List<GuildSong> songs, String user, Message msg) {
|
||||||
@ -568,181 +408,6 @@ public class GuildMusicHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deletePlayList(GuildMessageReceivedEvent event, List<String> commands) {
|
|
||||||
if(!checkCommands(event, commands,
|
|
||||||
event.getAuthor().getAsMention() + " you have to provide the playlist name and optionally a list of uuids for songs to remove")) return;
|
|
||||||
|
|
||||||
Long guildId = event.getGuild().getIdLong();
|
|
||||||
|
|
||||||
DiscordGuild guild = guildSongRepoService.getGuildRepo().findByGuildId(guildId);
|
|
||||||
if (guild == null) {
|
|
||||||
event.getChannel().sendMessage("Unable to find guild in local database. Please contact administrator").queue();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String playlist = commands.remove(0);
|
|
||||||
|
|
||||||
long userId = event.getMember().getIdLong();
|
|
||||||
|
|
||||||
GuildPlaylist gpl = guildSongRepoService.getGuildPlaylistRepo().findByGuildAndUserIdAndPlaylist(guild, userId, playlist);
|
|
||||||
|
|
||||||
if (commands.isEmpty()) {
|
|
||||||
|
|
||||||
MessageEmbed embed = new EmbedBuilder()
|
|
||||||
.setColor(Color.RED)
|
|
||||||
.setTitle("Delete Playlist " + playlist)
|
|
||||||
.setDescription("Are you sure you want to delete the playlist " + playlist)
|
|
||||||
.setTimestamp(OffsetDateTime.now())
|
|
||||||
.setFooter(event.getGuild().getSelfMember().getEffectiveName(), event.getGuild().getSelfMember().getUser().getAvatarUrl())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
event.getChannel().sendMessage(embed).queue((msg) -> {
|
|
||||||
|
|
||||||
ReactionListener<String> handler = new ReactionListener<>(userId, msg.getId());
|
|
||||||
handler.setExpiresIn(TimeUnit.MINUTES, 1);
|
|
||||||
handler.registerReaction(Reactions.CHECK_MARK_BUTTON, (ret) -> deletePlayListConfirm(gpl, event.getMember().getAsMention(), msg));
|
|
||||||
handler.registerReaction(Reactions.CROSS_MARK_BUTTON, (ret) -> deleteMessage(msg));
|
|
||||||
|
|
||||||
reactionHandler.addReactionListener(guildId, msg, handler);
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<GuildPlaylistSong> songs = guildSongRepoService.getGuildPlaylistSongRepo().findByGuildPlaylistAndSongIds(gpl, commands);
|
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder("Are you sure you want to delete the following songs from playlist " + playlist + "?\n");
|
|
||||||
for (GuildPlaylistSong gpls : songs) {
|
|
||||||
GuildSong gs = gpls.getGuildSong();
|
|
||||||
sb.append(String.format("**%s** by __%s__%n", gs.getSong().getTitle(), gs.getSong().getArtist()));
|
|
||||||
}
|
|
||||||
|
|
||||||
MessageEmbed embed = new EmbedBuilder()
|
|
||||||
.setColor(Color.RED)
|
|
||||||
.setTitle("Delete Playlist Songs")
|
|
||||||
.setDescription(sb.toString())
|
|
||||||
.setTimestamp(OffsetDateTime.now())
|
|
||||||
.setFooter(event.getGuild().getSelfMember().getEffectiveName(), event.getGuild().getSelfMember().getUser().getAvatarUrl())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
event.getChannel().sendMessage(embed).queue((msg) -> {
|
|
||||||
|
|
||||||
ReactionListener<String> handler = new ReactionListener<>(userId, msg.getId());
|
|
||||||
handler.setExpiresIn(TimeUnit.MINUTES, 1);
|
|
||||||
handler.registerReaction(Reactions.CHECK_MARK_BUTTON, (ret) -> deletePlayListSongConfirm(songs, event.getMember().getAsMention(), msg));
|
|
||||||
handler.registerReaction(Reactions.CROSS_MARK_BUTTON, (ret) -> deleteMessage(msg));
|
|
||||||
|
|
||||||
reactionHandler.addReactionListener(guildId, msg, handler);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void deletePlayListSongConfirm(List<GuildPlaylistSong> songs, String user, Message msg) {
|
|
||||||
try {
|
|
||||||
guildSongRepoService.getGuildPlaylistSongRepo().deleteAll(songs);
|
|
||||||
msg.getChannel().sendMessage(user + ", songs removed successfully fom playlist").complete();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
msg.getChannel().sendMessage("Sorry " + user + " I was unable to remove songs from the playlist. Reason: " + ex.getMessage()).complete();
|
|
||||||
logger.error("Unable to delete songs from playlist : " + ex.getMessage());
|
|
||||||
logger.error(ex);
|
|
||||||
} finally {
|
|
||||||
deleteMessage(msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void deletePlayListConfirm(GuildPlaylist gpl, String user, Message msg) {
|
|
||||||
String playlist = gpl.getPlaylist();
|
|
||||||
try {
|
|
||||||
guildSongRepoService.getGuildPlaylistRepo().delete(gpl);
|
|
||||||
msg.getChannel().sendMessage(user + " " + playlist + " deleted successfully").complete();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
msg.getChannel().sendMessage("Sorry " + user + " I was unable to remove playlist " + playlist + ". Reason: " + ex.getMessage()).complete();
|
|
||||||
logger.error("Unable to delete playlist " + playlist + ": " + ex.getMessage());
|
|
||||||
logger.error(ex);
|
|
||||||
} finally {
|
|
||||||
deleteMessage(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addPlayList(GuildMessageReceivedEvent event, List<String> commands) {
|
|
||||||
if(!checkCommands(event, commands,
|
|
||||||
event.getAuthor().getAsMention() + " you have to provide the playlist to create a new playlist (no spaces in name) and optionally a list of uuids for songs to add")) return;
|
|
||||||
|
|
||||||
Long guildId = event.getGuild().getIdLong();
|
|
||||||
|
|
||||||
DiscordGuild guild = guildSongRepoService.getGuildRepo().findByGuildId(guildId);
|
|
||||||
if (guild == null) {
|
|
||||||
event.getChannel().sendMessage("Unable to find guild in local database. Please contact administrator").queue();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String playlist = commands.remove(0);
|
|
||||||
|
|
||||||
long userId = event.getMember().getIdLong();
|
|
||||||
|
|
||||||
GuildPlaylist gpl = guildSongRepoService.getGuildPlaylistRepo().findByGuildAndUserIdAndPlaylist(guild, userId, playlist);
|
|
||||||
boolean newList = false;
|
|
||||||
if (gpl != null && commands.isEmpty()) {
|
|
||||||
event.getChannel().sendMessage(event.getAuthor().getAsMention() + " a playlist with the name " + playlist + " already exist for you.").queue();
|
|
||||||
return;
|
|
||||||
} else if (gpl == null) {
|
|
||||||
newList = true;
|
|
||||||
gpl = new GuildPlaylist();
|
|
||||||
gpl.setDateAdded(new Date());
|
|
||||||
gpl.setGuild(guild);
|
|
||||||
gpl.setPlaylist(playlist);
|
|
||||||
gpl.setUserId(userId);
|
|
||||||
guildSongRepoService.getGuildPlaylistRepo().save(gpl);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (commands.isEmpty() && newList) {
|
|
||||||
event.getChannel().sendMessage(event.getAuthor().getAsMention() + " playlist " + playlist + " successfully created.").queue();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Set<String> songIds = guildSongRepoService.getGuildPlaylistSongRepo()
|
|
||||||
.findByGuildPlaylistAndSongIds(gpl, commands).stream()
|
|
||||||
.map(g -> g.getGuildSong().getUuid())
|
|
||||||
.collect(Collectors.toSet());
|
|
||||||
|
|
||||||
List<GuildPlaylistSong> gplsList = new ArrayList<>();
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
for(String id : commands.stream().collect(Collectors.toSet())) {
|
|
||||||
GuildSong gs = guildSongRepoService.getGuildSongRepo().findByUuid(id);
|
|
||||||
if (songIds.contains(id)) {
|
|
||||||
sb.append(String.format("**%s** by __%s__ already exists in this playlist.%n", gs.getSong().getTitle(), gs.getSong().getArtist()));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (gs == null) {
|
|
||||||
sb.append("Song with id `" + id + "` not found. Please check id\n");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
GuildPlaylistSong gpls = new GuildPlaylistSong();
|
|
||||||
gpls.setDateAdded(new Date());
|
|
||||||
gpls.setGuildPlaylist(gpl);
|
|
||||||
gpls.setGuildSong(gs);
|
|
||||||
gplsList.add(gpls);
|
|
||||||
|
|
||||||
sb.append(String.format("**%s** by __%s__ added to playlist.%n", gs.getSong().getTitle(), gs.getSong().getArtist()));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!gplsList.isEmpty()) {
|
|
||||||
guildSongRepoService.getGuildPlaylistSongRepo().saveAll(gplsList);
|
|
||||||
}
|
|
||||||
|
|
||||||
MessageEmbed embed = new EmbedBuilder()
|
|
||||||
.setTitle("Results for adding playlist " + playlist)
|
|
||||||
.setAuthor(event.getMember().getEffectiveName(), null, event.getAuthor().getAvatarUrl())
|
|
||||||
.setColor(Color.GREEN)
|
|
||||||
.setDescription(sb.toString())
|
|
||||||
.setFooter("", event.getGuild().getSelfMember().getUser().getAvatarUrl())
|
|
||||||
.setTimestamp(OffsetDateTime.now())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
event.getChannel().sendMessage(embed).queue();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void loadRandomSong() throws Exception {
|
private void loadRandomSong() throws Exception {
|
||||||
List<GuildSong> gsList = null;
|
List<GuildSong> gsList = null;
|
||||||
|
|
||||||
@ -801,4 +466,12 @@ public class GuildMusicHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setCurrentPlaylist(GuildPlaylist currentPlaylist) {
|
||||||
|
this.currentPlaylist = currentPlaylist;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TrackManager getTrackManager() {
|
||||||
|
return this.trackManager;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user