refactored admin actions and updated licenses
This commit is contained in:
		@@ -2,7 +2,7 @@
 | 
			
		||||
 *
 | 
			
		||||
 * Project: Eight Track, File: TrackManager.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,
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 *
 | 
			
		||||
 * Project: Eight Track, File: DiscordGuild.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,
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 *
 | 
			
		||||
 * Project: Eight Track, File: GuildPlaylist.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,
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 *
 | 
			
		||||
 * Project: Eight Track, File: GuildPlaylistSong.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,
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 *
 | 
			
		||||
 * Project: Eight Track, File: GuildSong.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,
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 *
 | 
			
		||||
 * Project: Eight Track, File: Log.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,
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 *
 | 
			
		||||
 * Project: Eight Track, File: Song.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,
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 *
 | 
			
		||||
 * Project: Eight Track, File: GuildLogRepository.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,
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 *
 | 
			
		||||
 * Project: Eight Track, File: GuildPlaylistRepository.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,
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 *
 | 
			
		||||
 * Project: Eight Track, File: GuildPlaylistSongRepository.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,
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 *
 | 
			
		||||
 * Project: Eight Track, File: GuildSongRepository.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,
 | 
			
		||||
@@ -56,7 +56,7 @@ public interface GuildSongRepository extends CrudRepository<GuildSong, Long> {
 | 
			
		||||
  
 | 
			
		||||
  List<GuildSong> findByGuildAndUuidIn(DiscordGuild guild, Set<String> uuid);
 | 
			
		||||
  
 | 
			
		||||
  @Query("SELECT gs FROM GuildSong gs WHERE gs.guild.guildId = ?1 AND gs.uuid = ?2")
 | 
			
		||||
  @Query("SELECT gs FROM GuildSong gs WHERE gs.guild.guildId = ?1 AND gs.uuid in ?2")
 | 
			
		||||
  List<GuildSong> findByGuildAndUuidIn(Long guildId, Set<String> uuid);
 | 
			
		||||
  
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,39 @@
 | 
			
		||||
/**
 | 
			
		||||
 *
 | 
			
		||||
 * Project: Eight Track, File: AbstractAdminEventHandler.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.admin;
 | 
			
		||||
 | 
			
		||||
import net.locusworks.discord.eighttrack.events.AbstractEvent;
 | 
			
		||||
 | 
			
		||||
public abstract class AbstractAdminEventHandler extends AbstractEvent {
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
  public AbstractAdminEventHandler(String command) {
 | 
			
		||||
    super(command);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,130 @@
 | 
			
		||||
/**
 | 
			
		||||
 *
 | 
			
		||||
 * Project: Eight Track, File: AdminPlaylistDeleteHandler.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.admin.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.Member;
 | 
			
		||||
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.GuildPlaylist;
 | 
			
		||||
import net.locusworks.discord.eighttrack.events.admin.AbstractAdminEventHandler;
 | 
			
		||||
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 AdminPlaylistDeleteHandler extends AbstractAdminEventHandler {
 | 
			
		||||
  
 | 
			
		||||
  private ApplicationLogger logger;
 | 
			
		||||
 | 
			
		||||
  @Autowired
 | 
			
		||||
  private RepositoryService guildRepoService;
 | 
			
		||||
 | 
			
		||||
  @Autowired
 | 
			
		||||
  private ReactionHandler reactionHandler;
 | 
			
		||||
 | 
			
		||||
  public AdminPlaylistDeleteHandler() {
 | 
			
		||||
    super("delete");
 | 
			
		||||
    logger = ApplicationLoggerFactory.getLogger(AdminPlaylistDeleteHandler.class);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public void executeEvent(GuildMusicHandler musicHandler, GuildMessageReceivedEvent event, List<String> commands) {
 | 
			
		||||
    if (!isAdmin(event)) return;
 | 
			
		||||
 | 
			
		||||
    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 = guildRepoService.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, event.getGuild().getIdLong()));
 | 
			
		||||
 | 
			
		||||
      reactionHandler.addReactionListener( event.getGuild().getIdLong(), msg, handler);
 | 
			
		||||
    });
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  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() {
 | 
			
		||||
    return "delete";
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,94 @@
 | 
			
		||||
/**
 | 
			
		||||
 *
 | 
			
		||||
 * Project: Eight Track, File: AdminPlaylistListHandler.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.admin.handlers;
 | 
			
		||||
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import org.apache.commons.lang3.StringUtils;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
import net.dv8tion.jda.api.entities.Member;
 | 
			
		||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
 | 
			
		||||
import net.locusworks.discord.eighttrack.database.entities.GuildPlaylist;
 | 
			
		||||
import net.locusworks.discord.eighttrack.events.admin.AbstractAdminEventHandler;
 | 
			
		||||
import net.locusworks.discord.eighttrack.handlers.GuildMusicHandler;
 | 
			
		||||
import net.locusworks.discord.eighttrack.services.RepositoryService;
 | 
			
		||||
 | 
			
		||||
@Component
 | 
			
		||||
public class AdminPlaylistListHandler extends AbstractAdminEventHandler {
 | 
			
		||||
  @Autowired
 | 
			
		||||
  private RepositoryService guildRepoService;
 | 
			
		||||
 | 
			
		||||
  public AdminPlaylistListHandler() {
 | 
			
		||||
    super("list");
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  @Override
 | 
			
		||||
  public void executeEvent(GuildMusicHandler musicHandler, GuildMessageReceivedEvent event, List<String> commands) {
 | 
			
		||||
    if (!isAdmin(event)) return;
 | 
			
		||||
    
 | 
			
		||||
    List<GuildPlaylist> userPlaylist = guildRepoService.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());
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  @Override
 | 
			
		||||
  public String help() {
 | 
			
		||||
    return "list" ;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -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)";
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -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,
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 *
 | 
			
		||||
 * Project: Eight Track, File: ReactionListener.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,
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 *
 | 
			
		||||
 * Project: Eight Track, File: RepositoryService.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,
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 *
 | 
			
		||||
 * Project: Eight Track, File: Reactions.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