refactored admin actions and updated licenses
This commit is contained in:
@@ -68,7 +68,6 @@ public class DiscordEventHandler extends ListenerAdapter {
|
||||
@Autowired
|
||||
private RepositoryService guildSongRepoService;
|
||||
|
||||
@Autowired
|
||||
private ReactionHandler reactionHandler;
|
||||
|
||||
private Map<String, AbstractMainEventHandler> events;
|
||||
@@ -155,7 +154,7 @@ public class DiscordEventHandler extends ListenerAdapter {
|
||||
|
||||
if (!GuildMusicService.getMap().containsKey(event.getGuild().getIdLong())) {
|
||||
GuildMusicService.getMap().put(event.getGuild().getIdLong(),
|
||||
new GuildMusicHandler(event.getGuild().getIdLong(), reactionHandler, guildSongRepoService));
|
||||
new GuildMusicHandler(event.getGuild().getIdLong(), guildSongRepoService));
|
||||
}
|
||||
|
||||
GuildMusicHandler gmh = GuildMusicService.getMap().get(event.getGuild().getIdLong());
|
||||
@@ -171,15 +170,6 @@ public class DiscordEventHandler extends ListenerAdapter {
|
||||
return;
|
||||
}
|
||||
|
||||
switch(command) {
|
||||
case "+playlist":
|
||||
gmh.adminPlaylist(event, commands);
|
||||
return;
|
||||
case "+delete":
|
||||
gmh.delete(event, commands);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!command.equalsIgnoreCase("-help")) return;
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
@@ -27,40 +27,28 @@
|
||||
*/
|
||||
package net.locusworks.discord.eighttrack.handlers;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.io.IOException;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
|
||||
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
import net.dv8tion.jda.api.entities.ChannelType;
|
||||
import net.dv8tion.jda.api.entities.GuildChannel;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import net.dv8tion.jda.api.entities.VoiceChannel;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
|
||||
import net.dv8tion.jda.api.managers.AudioManager;
|
||||
import net.dv8tion.jda.api.requests.ErrorResponse;
|
||||
import net.locusworks.discord.eighttrack.audio.EightTrackAudioSendHandler;
|
||||
import net.locusworks.discord.eighttrack.audio.TrackManager;
|
||||
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylist;
|
||||
import net.locusworks.discord.eighttrack.database.entities.GuildSong;
|
||||
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;
|
||||
|
||||
@@ -74,20 +62,18 @@ public class GuildMusicHandler {
|
||||
|
||||
private Consumer<Long> callback;
|
||||
private RepositoryService guildSongRepoService;
|
||||
private ReactionHandler reactionHandler;
|
||||
private long guildId;
|
||||
|
||||
private GuildPlaylist currentPlaylist;
|
||||
|
||||
private TrackManager trackManager;
|
||||
|
||||
public GuildMusicHandler(long guildId, ReactionHandler reactionHandler, RepositoryService guildSongRepoService) throws IOException {
|
||||
public GuildMusicHandler(long guildId, RepositoryService guildSongRepoService) throws IOException {
|
||||
this.logger = ApplicationLoggerFactory.getLogger(GuildMusicHandler.class);
|
||||
this.playing = new AtomicBoolean(false);
|
||||
this.lastPlayed = OffsetDateTime.now();
|
||||
this.guildSongRepoService = guildSongRepoService;
|
||||
this.guildId = guildId;
|
||||
this.reactionHandler = reactionHandler;
|
||||
this.trackManager = new TrackManager();
|
||||
}
|
||||
|
||||
@@ -253,161 +239,6 @@ public class GuildMusicHandler {
|
||||
}
|
||||
}
|
||||
|
||||
public void adminPlaylist(GuildMessageReceivedEvent event, List<String> commands) throws IOException {
|
||||
if(!isAdmin(event)) return;
|
||||
if (!checkCommands(event, commands, event.getMember().getAsMention() + ", valid commands are +playst list, play, delete")) return;
|
||||
|
||||
String command = commands.remove(0);
|
||||
switch (command) {
|
||||
case "list":
|
||||
listAllPlaylists(event);
|
||||
return;
|
||||
case "delete":
|
||||
deleteUserPlayList(event, commands);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void delete(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 song uuids...").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
String command = commands.remove(0);
|
||||
|
||||
switch(command) {
|
||||
case "song":
|
||||
deleteSong(event, commands);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
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 = guildSongRepoService.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.addReactionListener(guildId, msg, handler);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void listAllPlaylists(GuildMessageReceivedEvent event) throws IOException {
|
||||
List<GuildPlaylist> userPlaylist = guildSongRepoService.getGuildPlaylistRepo().findByGuildFetchSongs(event.getGuild().getIdLong());
|
||||
if (userPlaylist == null || userPlaylist.isEmpty()) {
|
||||
event.getChannel().sendMessage(event.getAuthor().getAsMention() + ", there are no playlists for this guild").queue();
|
||||
return;
|
||||
}
|
||||
int longestPlaylist = 10;
|
||||
int longestUserName = 10;
|
||||
Map<Long, String> userMap = new HashMap<>();
|
||||
for(GuildPlaylist gpl : userPlaylist) {
|
||||
if (gpl.getPlaylist().length() > longestPlaylist) longestPlaylist = gpl.getPlaylist().length();
|
||||
|
||||
Member m = event.getGuild().getMemberById(gpl.getUserId());
|
||||
if (m == null) continue;
|
||||
|
||||
if (m.getUser().getAsTag().length() > longestUserName) longestUserName = m.getUser().getAsTag().length();
|
||||
userMap.put(gpl.getUserId(), m.getUser().getAsTag());
|
||||
}
|
||||
|
||||
String fmt = "%6s | %-" + longestPlaylist +"s | %-" + longestUserName + "s | %s%n";
|
||||
|
||||
StringBuilder sb = new StringBuilder("```");
|
||||
sb.append(String.format(fmt, "", "Playlist", "User", "Song Count"));
|
||||
sb.append(StringUtils.repeat("-", 24 + longestPlaylist + longestUserName)).append("\n");
|
||||
|
||||
int count = 0;
|
||||
for(GuildPlaylist gpl : userPlaylist) {
|
||||
sb.append(String.format(fmt, ++count, gpl.getPlaylist(), userMap.get(gpl.getUserId()), gpl.getGuildPlaylistSongList().size()));
|
||||
}
|
||||
sb.append("```");
|
||||
event.getChannel().sendMessage("**Playlists for " + event.getGuild().getName() + "**").queue((succcess)->
|
||||
event.getChannel().sendMessage(sb.toString()).queue());
|
||||
}
|
||||
|
||||
private void deleteUserPlayList(GuildMessageReceivedEvent event, List<String> commands) {
|
||||
if (!checkCommands(event, commands, 2, "Must provide playlist and user's tag (in that order)")) return;
|
||||
|
||||
String playlist = commands.remove(0);
|
||||
String userTag = commands.remove(0);
|
||||
|
||||
Member m = event.getGuild().getMemberByTag(userTag);
|
||||
if (m == null) {
|
||||
sendMessage(event, String.format("Unable to find user with tag %s", userTag));
|
||||
return;
|
||||
}
|
||||
|
||||
GuildPlaylist gpl = guildSongRepoService.getGuildPlaylistRepo().findByGuildAndUserIdAndPlaylist(event.getGuild().getIdLong(), m.getIdLong(), playlist);
|
||||
if (gpl == null) {
|
||||
sendMessage(event, String.format("Unable to find play list %s for user %s", playlist, userTag));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
long userId = event.getMember().getIdLong();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private void deleteGuildSongsConfirm(List<GuildSong> songs, String user, Message msg) {
|
||||
try {
|
||||
guildSongRepoService.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);
|
||||
} finally {
|
||||
deleteMessage(msg);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadRandomSong() throws Exception {
|
||||
List<GuildSong> gsList = null;
|
||||
|
||||
@@ -433,39 +264,6 @@ public class GuildMusicHandler {
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isAdmin(GuildMessageReceivedEvent event) {
|
||||
if (!event.getMessage().getMember().hasPermission(Permission.ADMINISTRATOR, Permission.MANAGE_SERVER)) {
|
||||
event.getChannel().sendMessage(String.format("Sorry <@%s> i can't do that. *psst: you have to have manage server, or administrator role*", event.getMember().getIdLong())).queue();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean checkCommands(GuildMessageReceivedEvent event, List<String> commands, String msg) {
|
||||
return checkCommands(event, commands, 0, msg);
|
||||
}
|
||||
|
||||
private boolean checkCommands(GuildMessageReceivedEvent event, List<String> commands, int minParams, String msg) {
|
||||
if (commands == null || commands.isEmpty() || commands.size() < minParams) {
|
||||
event.getChannel().sendMessage(msg).queue();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void sendMessage(GuildMessageReceivedEvent event, String msg) {
|
||||
event.getChannel().sendMessage(msg).queue();
|
||||
}
|
||||
|
||||
private void deleteMessage(Message msg) {
|
||||
try {
|
||||
msg.delete().complete();
|
||||
reactionHandler.removeReactionListener(guildId, msg.getIdLong());
|
||||
} catch (ErrorResponseException ex) {
|
||||
if (ex.getErrorResponse() != ErrorResponse.UNKNOWN_MESSAGE) throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
public void setCurrentPlaylist(GuildPlaylist currentPlaylist) {
|
||||
this.currentPlaylist = currentPlaylist;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
*
|
||||
* Project: Eight Track, File: ReactionHandler.java
|
||||
*
|
||||
* Copyright 2019-2019 Locusworks LLC.
|
||||
* 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,
|
||||
|
||||
Reference in New Issue
Block a user