refactored admin actions and updated licenses
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
*
|
||||
* Project: Eight Track, File: AdminPlaylistHandler.java
|
||||
*
|
||||
* Copyright 2019-2020 Locusworks LLC.
|
||||
* All rights reserved. Federal copyright law prohibits unauthorized reproduction by
|
||||
* any means and imposes fines up to $25,000 for violation. No part of this material
|
||||
* may be reproduced, transmitted, transcribed, stored in a retrieval system, copied,
|
||||
* modified, duplicated, adapted or translated into another program language in any
|
||||
* form or by any means, electronic, mechanical, photocopying, recording, or
|
||||
* otherwise, without the prior written permission from Locusworks. Locusworks
|
||||
* affirms that Eight-Track(R) software and data is subject to United States
|
||||
* Government Purpose Rights. Contact Locusworks, 1313 Lawnview Drive
|
||||
* Forney TX 75126, (802) 488-0438, for commercial licensing opportunities.
|
||||
*
|
||||
* IN NO EVENT SHALL LOCUSWORKS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
||||
* INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT
|
||||
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF LOCUSWORKS HAS BEEN
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. NO RESPONSIBILITY IS ASSUMED BY
|
||||
* LOCUSWORKS FOR ITS USE, OR FOR ANY INFRINGEMENTS OF PATENTS OR OTHER RIGHTS OF
|
||||
* THIRD PARTIES RESULTING FROM ITS USE. LOCUSWORKS SPECIFICALLY DISCLAIMS ANY
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND
|
||||
* ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
|
||||
* IS". LOCUSWORKS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
|
||||
* ENHANCEMENTS, OR MODIFICATIONS.
|
||||
*/
|
||||
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.admin.AbstractAdminEventHandler;
|
||||
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 AdminPlaylistHandler extends AbstractMainEventHandler {
|
||||
|
||||
private ApplicationLogger logger;
|
||||
|
||||
@Autowired
|
||||
private RepositoryService guildRepoService;
|
||||
|
||||
private Map<String, AbstractAdminEventHandler> handlers;
|
||||
|
||||
public AdminPlaylistHandler() {
|
||||
super("+playlist");
|
||||
logger = ApplicationLoggerFactory.getLogger(AdminPlaylistHandler.class);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private void init(List<AbstractAdminEventHandler> eventHandlers) {
|
||||
handlers = new TreeMap<>();
|
||||
for(AbstractAdminEventHandler apeh : eventHandlers) {
|
||||
handlers.put(apeh.getCommand(), apeh);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void executeEvent(GuildMusicHandler musicHandler, GuildMessageReceivedEvent event, List<String> commands) {
|
||||
if (!isAdmin(event)) return;
|
||||
|
||||
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);
|
||||
|
||||
AbstractAdminEventHandler 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, AbstractAdminEventHandler> entry : handlers.entrySet()) {
|
||||
sb.append(entry.getValue().help()).append("\n");
|
||||
}
|
||||
|
||||
return "+playlist | " + sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
/**
|
||||
*
|
||||
* Project: Eight Track, File: DeleteSongHandler.java
|
||||
*
|
||||
* Copyright 2019-2020 Locusworks LLC.
|
||||
* All rights reserved. Federal copyright law prohibits unauthorized reproduction by
|
||||
* any means and imposes fines up to $25,000 for violation. No part of this material
|
||||
* may be reproduced, transmitted, transcribed, stored in a retrieval system, copied,
|
||||
* modified, duplicated, adapted or translated into another program language in any
|
||||
* form or by any means, electronic, mechanical, photocopying, recording, or
|
||||
* otherwise, without the prior written permission from Locusworks. Locusworks
|
||||
* affirms that Eight-Track(R) software and data is subject to United States
|
||||
* Government Purpose Rights. Contact Locusworks, 1313 Lawnview Drive
|
||||
* Forney TX 75126, (802) 488-0438, for commercial licensing opportunities.
|
||||
*
|
||||
* IN NO EVENT SHALL LOCUSWORKS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
||||
* INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT
|
||||
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF LOCUSWORKS HAS BEEN
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. NO RESPONSIBILITY IS ASSUMED BY
|
||||
* LOCUSWORKS FOR ITS USE, OR FOR ANY INFRINGEMENTS OF PATENTS OR OTHER RIGHTS OF
|
||||
* THIRD PARTIES RESULTING FROM ITS USE. LOCUSWORKS SPECIFICALLY DISCLAIMS ANY
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND
|
||||
* ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
|
||||
* IS". LOCUSWORKS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
|
||||
* ENHANCEMENTS, OR MODIFICATIONS.
|
||||
*/
|
||||
package net.locusworks.discord.eighttrack.events.main.handlers;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
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.Message;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
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.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 DeleteSongHandler extends AbstractMainEventHandler {
|
||||
|
||||
private ApplicationLogger logger;
|
||||
|
||||
@Autowired
|
||||
private RepositoryService guildRepoService;
|
||||
|
||||
@Autowired
|
||||
private ReactionHandler reactionHandler;
|
||||
|
||||
public DeleteSongHandler() {
|
||||
super("+delete");
|
||||
logger = ApplicationLoggerFactory.getLogger(DeleteSongHandler.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void executeEvent(GuildMusicHandler musicHandler, GuildMessageReceivedEvent event, List<String> commands) {
|
||||
if(!isAdmin(event)) return;
|
||||
|
||||
if (commands == null || commands.isEmpty()) {
|
||||
event.getChannel().sendMessage(event.getMember().getAsMention() + ", Missing command for +delete. Valid command is : +delete uuids...").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
deleteSong(event, commands);
|
||||
}
|
||||
|
||||
private void deleteSong(GuildMessageReceivedEvent event, List<String> commands) {
|
||||
if(!checkCommands(event, commands,
|
||||
event.getAuthor().getAsMention() + ", you have to provide the list of uuids for songs to remove")) return;
|
||||
|
||||
List<GuildSong> songs = guildRepoService.getGuildSongRepo().findByGuildAndUuidIn(event.getGuild().getIdLong(), commands.stream().collect(Collectors.toSet()));
|
||||
if (songs.isEmpty()) {
|
||||
event.getChannel().sendMessage(event.getAuthor().getAsMention() + ", there are no songs for this guild").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder("Are you sure you want to delete the following songs?\n");
|
||||
for (GuildSong gs : songs) {
|
||||
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<>(event.getAuthor().getIdLong(), msg.getId());
|
||||
handler.setExpiresIn(TimeUnit.MINUTES, 1);
|
||||
handler.registerReaction(Reactions.CHECK_MARK_BUTTON, (ret) -> deleteGuildSongsConfirm(songs, event.getMember().getAsMention(), msg));
|
||||
handler.registerReaction(Reactions.CROSS_MARK_BUTTON, (ret) -> deleteMessage(msg, reactionHandler, event.getGuild().getIdLong()));
|
||||
|
||||
reactionHandler.addReactionListener(event.getGuild().getIdLong(), msg, handler);
|
||||
});
|
||||
}
|
||||
|
||||
private void deleteGuildSongsConfirm(List<GuildSong> songs, String user, Message msg) {
|
||||
try {
|
||||
guildRepoService.getGuildSongRepo().deleteAll(songs);
|
||||
msg.getChannel().sendMessage(user + ", songs were deleted successfully").complete();
|
||||
} catch (Exception ex) {
|
||||
msg.getChannel().sendMessage("Sorry " + user + " I was unable to remove the selected songs. Reason: " + ex.getMessage()).complete();
|
||||
logger.error("Unable to delete songs: " + ex.getMessage());
|
||||
logger.error(ex);
|
||||
guildRepoService.logEntry(msg.getGuild().getIdLong(), ex, "Unable to delete songs: %s", ex.getMessage());
|
||||
} finally {
|
||||
deleteMessage(msg, reactionHandler, msg.getGuild().getIdLong());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String help() {
|
||||
return "-next <id of song to play> (will stop song if one already playing)";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user