Skip to content

Commit

Permalink
Implement restart command
Browse files Browse the repository at this point in the history
Now Kensa is able to restart himself.
Also now it is possible to restrict actions
to specific Discord roles
  • Loading branch information
marlind89 committed Nov 17, 2018
1 parent 01d4491 commit ba7ee6b
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 14 deletions.
16 changes: 14 additions & 2 deletions src/main/java/com/github/langebangen/kensa/KensaApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import sx.blah.discord.api.IDiscordClient;
import sx.blah.discord.api.events.EventDispatcher;
import sx.blah.discord.api.internal.Opus;
import sx.blah.discord.handle.obj.IVoiceChannel;
import sx.blah.discord.util.DiscordException;

/**
Expand All @@ -34,18 +35,29 @@ public class KensaApp
public static void main(String[] args)
throws DiscordException
{
if(args.length != 1)
if(args.length < 1)
{
System.out.println("The bot token must be provided in main args.");
System.exit(0);
}

Injector injector = Guice.createInjector(new KensaModule(args[0]));
long voiceChannelId = 0;
if (args.length > 1)
{
String voiceChannelToConnectTo = args[1];
if (voiceChannelToConnectTo != null && !voiceChannelToConnectTo.isEmpty())
{
voiceChannelId = Long.parseLong(voiceChannelToConnectTo);
}
}

Injector injector = Guice.createInjector(new KensaModule(args[0], voiceChannelId));

IDiscordClient dcClient = injector.getInstance(IDiscordClient.class);
registerListeners(dcClient, injector);
dcClient.login();


logger.info("Opus version:" + Opus.INSTANCE.opus_get_version_string());
}

Expand Down
31 changes: 22 additions & 9 deletions src/main/java/com/github/langebangen/kensa/command/Action.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.github.langebangen.kensa.command;

import sx.blah.discord.handle.obj.IGuild;
import sx.blah.discord.handle.obj.IRole;
import sx.blah.discord.handle.obj.IUser;

import com.github.langebangen.kensa.role.KensaRole;

/**
* Class describing the actions Kensa supports.
*
Expand All @@ -24,23 +30,23 @@ public enum Action
CLEAR ("clear", "Clears the playlist."),
BABYLON ("babylon", "Chooses a delicious babylon dish for you so you don't have to!"),
INSULT ("insult", "Insults the specified person. The person should be mentioned for this to work. Use !insult add to add an insult. " +
"!insult remove to remove the previous insult from the insult list");
"!insult remove to remove the previous insult from the insult list"),
RESTART ("restart", "Restarts kensa", KensaRole.ADMIN);

private final String action;
private final String description;
private final KensaRole requiresRole;

/**
* Constructor.
*
* @param command
* the command
* @param description
* the description
*/
Action(String command, String description)
{
this(command, description, null);
}

Action(String command, String description, KensaRole requiresRole)
{
this.action = "!" + command;
this.description = description;
this.requiresRole = requiresRole;
}

/**
Expand Down Expand Up @@ -86,4 +92,11 @@ public static Action getAction(String actionValue)
return null;
}

public boolean hasPermission(IUser user, IGuild guild)
{
return requiresRole == null ||
user.getRolesForGuild(guild)
.stream()
.anyMatch(x -> x.getName().equals(requiresRole.GetRoleName()));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.langebangen.kensa.listener;

import com.github.langebangen.kensa.command.Action;
import com.github.langebangen.kensa.command.Command;
import com.github.langebangen.kensa.listener.event.*;
import com.google.inject.Inject;
Expand All @@ -16,6 +17,7 @@
import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.handle.obj.IUser;
import sx.blah.discord.handle.obj.IVoiceChannel;
import sx.blah.discord.util.audio.AudioPlayer;

import java.io.File;
Expand All @@ -24,6 +26,8 @@
import java.util.Arrays;
import java.util.Random;

import javax.inject.Named;

/**
* EventListener which listens on events from discord.
*
Expand All @@ -38,6 +42,7 @@ public class EventListener
private final File messageFile;
private final Random random;
private final RiMarkov markov;
private final long latestVoiceChannelId;

/**
* Constructor.
Expand All @@ -48,9 +53,11 @@ public class EventListener
* the {@link RiMarkov}
*/
@Inject
public EventListener(IDiscordClient client, RiMarkov markov)
public EventListener(IDiscordClient client, RiMarkov markov,
@Named("latestVoiceChannelId") long latestVoiceChannelId)
{
super(client);
this.latestVoiceChannelId = latestVoiceChannelId;
this.random = new Random();
this.messageFile = new File("messages.txt");
this.markov = markov;
Expand All @@ -66,6 +73,15 @@ public EventListener(IDiscordClient client, RiMarkov markov)
public void onReady(ReadyEvent event)
{
logger.info("Logged in successfully.!");
if (latestVoiceChannelId > 0)
{
IVoiceChannel voiceChannel = client.getVoiceChannelByID(latestVoiceChannelId);
logger.info("Rejoining channel " + voiceChannel.getName());
if (voiceChannel != null)
{
voiceChannel.join();
}
}
}

/**
Expand All @@ -84,11 +100,19 @@ public void onMessageReceivedEvent(MessageReceivedEvent event)
Command command = Command.parseCommand(content);
if(command != null)
{

String argument = command.getArgument();
AudioPlayer player = AudioPlayer.getAudioPlayerForGuild(message.getGuild());
EventDispatcher dispatcher = client.getDispatcher();
KensaEvent kensaEvent = null;
switch(command.getAction())
Action action = command.getAction();

if (!action.hasPermission(message.getAuthor(), message.getGuild())){
sendMessage(textChannel, "You don't have permission do to that, you filthy fool!");
return;
}

switch(action)
{
/* Text channel commands */
case HELP:
Expand Down Expand Up @@ -157,6 +181,9 @@ else if(insultType.equals("remove"))
case CLEAR:
kensaEvent = new ClearPlaylistEvent(textChannel, player);
break;
case RESTART:
kensaEvent = new RestartKensaEvent(textChannel);
break;
}

if(kensaEvent != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

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

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
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.handle.obj.IVoiceChannel;
import sx.blah.discord.util.MessageBuilder;

import org.jooq.DSLContext;
Expand All @@ -26,6 +30,7 @@
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.listener.event.RestartKensaEvent;
import com.github.langebangen.kensa.storage.Storage;
import com.github.langebangen.kensa.storage.generated.tables.records.InsultRecord;
import com.github.langebangen.kensa.util.KensaConstants;
Expand Down Expand Up @@ -151,4 +156,37 @@ public void onInsultPersistEvent(InsultPersistEvent event)
logger.error("Error when persisting insult.", e);
}
}

@EventSubscriber
public void onRestartKensaEvent(RestartKensaEvent event)
{
sendMessage(event.getTextChannel(), "Restarting...");

String voiceChannelId = "";
IVoiceChannel connectedVoiceChannel = event.getTextChannel()
.getGuild().getConnectedVoiceChannel();
if (connectedVoiceChannel != null)
{
voiceChannelId = " " + connectedVoiceChannel.getLongID();
}

event.getTextChannel().getGuild().getConnectedVoiceChannel().getLongID();
List<String> command = new ArrayList<>();
command.add("/bin/bash");
command.add("-c");
command.add("sleep 5 && ~/kensa/kensa.sh" + voiceChannelId);
ProcessBuilder builder = new ProcessBuilder(command);

try
{
builder.start();
System.exit(0);
}
catch(IOException e)
{
String message = "Failed to restart Kensa!";
logger.error(message , e);
sendMessage(event.getTextChannel(), message);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.langebangen.kensa.listener.event;

import sx.blah.discord.handle.obj.IChannel;

public class RestartKensaEvent extends KensaEvent
{
public RestartKensaEvent(IChannel textChannel)
{
super(textChannel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.name.Names;

import rita.RiMarkov;
import sx.blah.discord.api.ClientBuilder;
import sx.blah.discord.api.IDiscordClient;
Expand All @@ -17,15 +19,18 @@ public class KensaModule
extends AbstractModule
{
private final String token;
private long voiceChannelId;

public KensaModule(String token)
public KensaModule(String token, long voiceChannelId)
{
this.token = token;
this.voiceChannelId = voiceChannelId;
}

@Override
protected void configure()
{
bindConstant().annotatedWith(Names.named("latestVoiceChannelId")).to(voiceChannelId);
}

@Provides
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/github/langebangen/kensa/role/KensaRole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.langebangen.kensa.role;

public enum KensaRole
{
ADMIN("KensaAdmin");

private final String roleName;
KensaRole(String roleName)
{
this.roleName = roleName;
}

public String GetRoleName()
{
return roleName;
}
}

0 comments on commit ba7ee6b

Please sign in to comment.