Added ability to delete playlists
Some checks failed
Locusworks Team/eight-track/pipeline/head There was a failure building this commit

This commit is contained in:
Isaac Parenteau
2019-10-11 20:55:22 -05:00
parent daf447e839
commit 1fca679a79
5 changed files with 456 additions and 8 deletions

View File

@@ -0,0 +1,133 @@
/**
*
* Project: Pseudo-Bot, File: ReactionHandler.java
*
* Copyright 2019 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 PSEUDO-BOT(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.handlers;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.stereotype.Component;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageReaction;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.internal.utils.PermissionUtil;
import net.locusworks.discord.eighttrack.listeners.ReactionListener;
@Component
public class ReactionHandler {
private final ConcurrentHashMap<Long, ConcurrentHashMap<Long, ReactionListener<?>>> reactions;
private ReactionHandler() {
reactions = new ConcurrentHashMap<>();
}
public synchronized void addReactionListener(long guildId, Message message, ReactionListener<?> handler) {
addReactionListener(guildId, message, handler, true);
}
public synchronized void addReactionListener(long guildId, Message message, ReactionListener<?> handler, boolean queue) {
if (handler == null) {
return;
}
if (message.getChannelType().equals(ChannelType.TEXT)) {
if (!PermissionUtil.checkPermission(message.getTextChannel(), message.getGuild().getSelfMember(), Permission.MESSAGE_ADD_REACTION)) {
return;
}
}
if (!reactions.containsKey(guildId)) {
reactions.put(guildId, new ConcurrentHashMap<>());
}
if (!reactions.get(guildId).containsKey(message.getIdLong())) {
for (String emote : handler.getEmotes()) {
RestAction<Void> action = message.addReaction(emote);
if (queue) action.queue(); else action.complete();
}
}
reactions.get(guildId).put(message.getIdLong(), handler);
}
public synchronized void removeReactionListener(long guildId, long messageId) {
if (!reactions.containsKey(guildId)) return;
reactions.get(guildId).remove(messageId);
}
/**
* Handles the reaction
*
* @param channel TextChannel of the message
* @param messageId id of the message
* @param userId id of the user reacting
* @param reaction the reaction
*/
public void handle(TextChannel channel, long messageId, long userId, MessageReaction reaction) {
ReactionListener<?> listener = reactions.get(channel.getGuild().getIdLong()).get(messageId);
if (!listener.isActive() || listener.getExpiresInTimestamp() < System.currentTimeMillis()) {
reactions.get(channel.getGuild().getIdLong()).remove(messageId);
} else if ((listener.hasReaction(reaction.getReactionEmote().getName())) && listener.getUserId() == userId) {
reactions.get(channel.getGuild().getIdLong()).get(messageId).updateLastAction();
Message message = channel.retrieveMessageById(messageId).complete();
listener.react(reaction.getReactionEmote().getName(), message);
}
}
/**
* Do we have an event for a message?
*
* @param guildId discord guild-id of the message
* @param messageId id of the message
* @return do we have an handler?
*/
public boolean canHandle(long guildId, long messageId) {
return reactions.containsKey(guildId) && reactions.get(guildId).containsKey(messageId);
}
public synchronized void removeGuild(long guildId) {
reactions.remove(guildId);
}
/**
* Delete expired handlers
*/
public synchronized void cleanCache() {
long now = System.currentTimeMillis();
for (Iterator<Map.Entry<Long, ConcurrentHashMap<Long, ReactionListener<?>>>> iterator = reactions.entrySet().iterator(); iterator.hasNext(); ) {
Map.Entry<Long, ConcurrentHashMap<Long, ReactionListener<?>>> mapEntry = iterator.next();
mapEntry.getValue().values().removeIf(listener -> !listener.isActive() || listener.getExpiresInTimestamp() < now);
if (mapEntry.getValue().values().isEmpty()) {
reactions.remove(mapEntry.getKey());
}
}
}
}