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,139 @@
/**
*
* Project: Pseudo-Bot, File: ReactionListener.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.listeners;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import net.dv8tion.jda.api.entities.Message;
public class ReactionListener<T> {
private final Map<String, Consumer<Message>> reactions;
private final long userId;
private volatile T data;
private Long expiresIn, lastAction;
private boolean active;
public ReactionListener(long userId, T data) {
this.data = data;
this.userId = userId;
reactions = new LinkedHashMap<>();
active = true;
lastAction = System.currentTimeMillis();
expiresIn = TimeUnit.MINUTES.toMillis(5);
}
public boolean isActive() {
return active;
}
public void disable() {
this.active = false;
}
/**
* The time after which this listener expires which is now + specified time
* Defaults to now+5min
*
* @param timeUnit time units
* @param time amount of time units
*/
public void setExpiresIn(TimeUnit timeUnit, long time) {
expiresIn = timeUnit.toMillis(time);
}
/**
* Check if this listener has specified emote
*
* @param emote the emote to check for
* @return does this listener do anything with this emote?
*/
public boolean hasReaction(String emote) {
return reactions.containsKey(emote);
}
/**
* React to the reaction :')
*
* @param emote the emote used
* @param message the message bound to the reaction
*/
public void react(String emote, Message message) {
if (hasReaction(emote)) reactions.get(emote).accept(message);
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
/**
* Register a consumer for a specified emote
* Multiple emote's will result in overriding the old one
*
* @param emote the emote to respond to
* @param consumer the behaviour when emote is used
*/
public void registerReaction(String emote, Consumer<Message> consumer) {
reactions.put(emote, consumer);
}
/**
* @return list of all emotes used in this reaction listener
*/
public Set<String> getEmotes() {
return reactions.keySet();
}
/**
* updates the timestamp when the reaction was last accessed
*/
public void updateLastAction() {
lastAction = System.currentTimeMillis();
}
/**
* When does this reaction listener expire?
*
* @return timestamp in millis
*/
public Long getExpiresInTimestamp() {
return lastAction + expiresIn;
}
public long getUserId() {
return userId;
}
}