Skip to content

Commit

Permalink
Added support for pausing/skipping tracks via reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
marlind89 committed Jul 22, 2017
1 parent 7217ca3 commit 43e4be3
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ public void pause(boolean pause)
trackScheduler.pause(pause);
}

@Override
public boolean isPaused()
{
return trackScheduler.isPaused();
}

@Override
public void setLoopEnabled(boolean loopEnabled)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,12 @@ public interface MusicPlayer
* true to pause, false to play
*/
void pause(boolean pause);

/**
* Returns whether this player is paused
*
* @return
* whether this player is paused
*/
boolean isPaused();
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ public void pause(boolean pause)
player.setPaused(pause);
}

/**
* Returns whether this player is paused
*
* @return
* whether this player is paused
*/
public boolean isPaused()
{
return player.isPaused();
}

@Override
public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason endReason)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.github.langebangen.kensa.listener;

import com.github.langebangen.kensa.command.Action;
import sx.blah.discord.api.IDiscordClient;
import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.util.DiscordException;
import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.util.MessageBuilder;
import sx.blah.discord.util.MissingPermissionsException;
import sx.blah.discord.util.RateLimitException;

import com.github.langebangen.kensa.command.Action;

/**
* @author Martin.
Expand Down Expand Up @@ -47,14 +46,17 @@ protected void sendHelpMessage(IChannel channel)
* the {@link IChannel}
* @param message
* the message
*
* @return
* the sent {@link IMessage}
*/
protected void sendMessage(IChannel channel, String message)
protected IMessage sendMessage(IChannel channel, String message)
{
MessageBuilder builder = new MessageBuilder(client)
.withChannel(channel)
.withContent(message);

sendMessage(builder);
return sendMessage(builder);
}

/**
Expand All @@ -63,16 +65,12 @@ protected void sendMessage(IChannel channel, String message)
*
* @param messageBuilder
* the {@link MessageBuilder}
*
* @return
* the sent {@link IMessage}
*/
protected void sendMessage(MessageBuilder messageBuilder)
protected IMessage sendMessage(MessageBuilder messageBuilder)
{
try
{
messageBuilder.send();
}
catch(DiscordException | RateLimitException | MissingPermissionsException e)
{
e.printStackTrace();
}
return messageBuilder.send();
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
package com.github.langebangen.kensa.listener;

import com.github.langebangen.kensa.audio.MusicPlayer;
import com.github.langebangen.kensa.audio.MusicPlayerManager;
import com.github.langebangen.kensa.listener.event.*;
import com.github.langebangen.kensa.util.TrackUtils;
import com.google.inject.Inject;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sx.blah.discord.api.IDiscordClient;
import sx.blah.discord.api.events.EventSubscriber;
import sx.blah.discord.handle.impl.events.guild.channel.message.reaction.ReactionEvent;
import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.handle.obj.IGuild;
import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.util.MessageBuilder;
import sx.blah.discord.util.RequestBuilder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import com.github.langebangen.kensa.audio.MusicPlayer;
import com.github.langebangen.kensa.audio.MusicPlayerManager;
import com.github.langebangen.kensa.listener.event.ClearPlaylistEvent;
import com.github.langebangen.kensa.listener.event.CurrentTrackRequestEvent;
import com.github.langebangen.kensa.listener.event.KensaEvent;
import com.github.langebangen.kensa.listener.event.LoopPlaylistEvent;
import com.github.langebangen.kensa.listener.event.PauseEvent;
import com.github.langebangen.kensa.listener.event.PlayAudioEvent;
import com.github.langebangen.kensa.listener.event.SearchYoutubeEvent;
import com.github.langebangen.kensa.listener.event.ShowPlaylistEvent;
import com.github.langebangen.kensa.listener.event.ShufflePlaylistEvent;
import com.github.langebangen.kensa.listener.event.SkipTrackEvent;
import com.github.langebangen.kensa.util.TrackUtils;
import com.google.inject.Inject;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.vdurmont.emoji.Emoji;

/**
* @author Martin.
Expand Down Expand Up @@ -128,10 +142,11 @@ public void handleShowPlaylistEvent(ShowPlaylistEvent event)
for(AudioTrack track : playlist)
{
// The playlist size may be too large to send a message in
// as the maximum message may be 2000 characters long.
// as the maximum message may be IMessage.MAX_MESSAGE_LENGTH characters long.
// Reserving two digits for the amount of songs
String trackString = String.format("\n %d. %s", i++, TrackUtils.getReadableTrack(track));
if((trackString.length() + messageBuilder.getContent().length()) < (2000 - moreSongs.length() - 2))
if((trackString.length() + messageBuilder.getContent().length()) <
(IMessage.MAX_MESSAGE_LENGTH - moreSongs.length() - 2))
{
messageBuilder.appendContent(trackString);
}
Expand All @@ -143,7 +158,15 @@ public void handleShowPlaylistEvent(ShowPlaylistEvent event)
}
}
messageBuilder.appendContent("```");
sendMessage(messageBuilder);
IMessage message = sendMessage(messageBuilder);

RequestBuilder builder = new RequestBuilder(client);
builder.shouldBufferRequests(true);

builder.doAction(() -> addReaction(message,"⏯"))
.andThen(() -> addReaction(message,"⏭"));

builder.execute();
}
}

Expand Down Expand Up @@ -175,6 +198,28 @@ public void handlePauseEvent(PauseEvent event)
sendMessage(channel, "Invalid pause command. Specify on or off, e.g. \"!pause on\"");
}
}

@EventSubscriber
public void handleReactionEvent(ReactionEvent event)
{
if(!event.getUser().isBot())
{
Emoji emoji = event.getReaction().getUnicodeEmoji();
if(emoji != null)
{
MusicPlayer player = playerFactory.getMusicPlayer(event.getGuild());
switch(emoji.getUnicode())
{
case "⏯":
player.pause(!player.isPaused());
break;
case "⏭":
player.skipTrack();
break;
}
}
}
}

private boolean isInteger(String s)
{
Expand Down Expand Up @@ -222,4 +267,9 @@ private MusicPlayer getPlayer(KensaEvent event)
return playerFactory.getMusicPlayer(event);
}

private boolean addReaction(IMessage message, String reaction)
{
message.addReaction(reaction);
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
package com.github.langebangen.kensa.listener;

import static com.github.langebangen.kensa.storage.generated.Tables.INSULT;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Optional;
import java.util.stream.Stream;

import rita.RiMarkov;
import sx.blah.discord.api.IDiscordClient;
import sx.blah.discord.api.events.EventSubscriber;
import sx.blah.discord.handle.impl.events.guild.channel.message.MentionEvent;
import sx.blah.discord.util.MessageBuilder;

import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.langebangen.kensa.babylon.Babylon;
import com.github.langebangen.kensa.command.Action;
import com.github.langebangen.kensa.listener.event.BabylonEvent;
import com.github.langebangen.kensa.listener.event.HelpEvent;
import com.github.langebangen.kensa.listener.event.InsultEvent;
import com.github.langebangen.kensa.listener.event.InsultPersistEvent;
import com.github.langebangen.kensa.storage.Storage;
import com.github.langebangen.kensa.storage.generated.tables.Insult;
import com.github.langebangen.kensa.storage.generated.tables.records.InsultRecord;
import com.github.langebangen.kensa.util.KensaConstants;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.jooq.*;
import org.jooq.impl.DSL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rita.RiMarkov;
import sx.blah.discord.api.IDiscordClient;
import sx.blah.discord.api.events.EventSubscriber;
import sx.blah.discord.handle.impl.events.MentionEvent;
import sx.blah.discord.util.MessageBuilder;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Optional;
import java.util.stream.Stream;

import static com.github.langebangen.kensa.storage.generated.Tables.INSULT;


/**
Expand Down Expand Up @@ -115,7 +118,7 @@ public void onInsultEvent(InsultEvent event)
@EventSubscriber
public void onInsultPersistEvent(InsultPersistEvent event)
{
if(event.isAdded() == false && lastInsultId == -1)
if(!event.isAdded() && lastInsultId == -1)
{
sendMessage(event.getTextChannel(), "No previous insult to remove!");
}
Expand All @@ -126,7 +129,7 @@ public void onInsultPersistEvent(InsultPersistEvent event)
if(event.isAdded())
{
String insult = event.getInsult();
if(insult != null && insult.isEmpty() == false)
if(insult != null && !insult.isEmpty())
{
InsultRecord insultRecord = create.newRecord(INSULT);
insultRecord.setText(event.getInsult());
Expand Down

0 comments on commit 43e4be3

Please sign in to comment.