Fixes & debug & /lenny

Fixed cmd handling for real
Added debug command (not the best for debugging commands)
Stop send/receive threads on shutdown
Allow /lenny for the unconnected
This commit is contained in:
Norbi Peti 2018-06-06 00:12:55 +02:00
parent b350be608a
commit 0497d6c8ac
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
5 changed files with 67 additions and 8 deletions

View file

@ -233,6 +233,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
ChromaBot.getInstance().updatePlayerList();
try {
SafeMode = true; // Stop interacting with Discord
MCChatListener.stop();
ChromaBot.delete();
dc.changePresence(StatusType.IDLE, ActivityType.PLAYING, "Chromacraft"); //No longer using the same account for testing
dc.logout();

View file

@ -0,0 +1,26 @@
package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.listeners.CommandListener;
import sx.blah.discord.handle.obj.IMessage;
public class DebugCommand extends DiscordCommandBase {
@Override
public String getCommandName() {
return "debug";
}
@Override
public boolean run(IMessage message, String args) {
if (message.getAuthor().hasRole(DiscordPlugin.mainServer.getRoleByID(126030201472811008L)))
message.reply("Debug " + (CommandListener.debug() ? "enabled" : "disabled"));
else
message.reply("You need to be a moderator to use this command.");
return true;
}
@Override
public String[] getHelpText() {
return new String[]{"Switches debug mode."};
}
}

View file

@ -8,6 +8,8 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.stream.Collectors;
import static buttondevteam.discordplugin.listeners.CommandListener.debug;
public abstract class DiscordCommandBase {
public abstract String getCommandName();
@ -24,9 +26,11 @@ public abstract class DiscordCommandBase {
commands.put("role", new RoleCommand());
commands.put("mcchat", new MCChatCommand());
commands.put("channelcon", new ChannelconCommand());
commands.put("debug", new DebugCommand());
}
public static void runCommand(String cmd, String args, IMessage message) {
debug("F"); //Not sure if needed
DiscordCommandBase command = commands.get(cmd);
if (command == null) {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
@ -35,6 +39,7 @@ public abstract class DiscordCommandBase {
+ "help' for help");
return;
}
debug("G");
try {
if (!command.run(message, args))
DiscordPlugin.sendMessageToChannel(message.getChannel(), Arrays.stream(command.getHelpText()).collect(Collectors.joining("\n")));
@ -43,6 +48,7 @@ public abstract class DiscordCommandBase {
DiscordPlugin.sendMessageToChannel(message.getChannel(),
"An internal error occured while executing this command. For more technical details see the server-issues channel on the dev Discord.");
}
debug("H");
}
protected String[] splitargs(String args) {

View file

@ -160,8 +160,10 @@ public class CommandListener {
* @return Whether it ran the command (always true if mentionedonly is false)
*/
public static boolean runCommand(IMessage message, boolean mentionedonly) {
debug("A");
if (DiscordPlugin.SafeMode)
return true;
debug("B");
final StringBuilder cmdwithargs = new StringBuilder(message.getContent());
final String mention = DiscordPlugin.dc.getOurUser().mention(false);
final String mentionNick = DiscordPlugin.dc.getOurUser().mention(true);
@ -169,26 +171,41 @@ public class CommandListener {
gotmention = checkanddeletemention(cmdwithargs, mentionNick, message) || gotmention;
for (String mentionRole : (Iterable<String>) message.getRoleMentions().stream().filter(r -> DiscordPlugin.dc.getOurUser().hasRole(r)).map(r -> r.mention())::iterator)
gotmention = checkanddeletemention(cmdwithargs, mentionRole, message) || gotmention; // Delete all mentions
debug("C");
if (mentionedonly && !gotmention) {
message.getChannel().setTypingStatus(false);
return false;
}
debug("D");
message.getChannel().setTypingStatus(true);
int index = cmdwithargs.indexOf(" ");
String cmdwithargsString = cmdwithargs.toString().trim(); //Remove spaces between mention and command
int index = cmdwithargsString.indexOf(" ");
String cmd;
String args;
if (index == -1) {
cmd = cmdwithargs.toString();
cmd = cmdwithargsString;
args = "";
} else {
cmd = cmdwithargs.substring(0, index);
args = cmdwithargs.substring(index + 1).trim(); //In case there are multiple spaces
}
cmd = cmdwithargsString.substring(0, index);
args = cmdwithargsString.substring(index + 1).trim(); //In case there are multiple spaces
}
debug("E");
DiscordCommandBase.runCommand(cmd.toLowerCase(), args, message);
message.getChannel().setTypingStatus(false);
return true;
}
private static boolean debug = false;
public static void debug(String debug) {
if (CommandListener.debug) //Debug
System.out.println(debug);
}
public static boolean debug() {
return debug = !debug;
}
private static boolean checkanddeletemention(StringBuilder cmdwithargs, String mention, IMessage message) {
if (message.getContent().startsWith(mention)) // TODO: Resolve mentions: Compound arguments, either a mention or text
if (cmdwithargs.length() > mention.length() + 1)

View file

@ -45,6 +45,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
private BukkitTask sendtask;
private LinkedBlockingQueue<AbstractMap.SimpleEntry<TBMCChatEvent, Instant>> sendevents = new LinkedBlockingQueue<>();
private Runnable sendrunnable;
private static Thread sendthread;
@EventHandler // Minecraft
public void onMCChat(TBMCChatEvent ev) {
@ -54,6 +55,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
if (sendtask != null)
return;
sendrunnable = () -> {
sendthread = Thread.currentThread();
processMCToDiscord();
sendtask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, sendrunnable);
};
@ -121,13 +123,13 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
|| ((DiscordSenderBase) e.getSender()).getChannel().getLongID() != ch.getLongID();
if ((e.getChannel() == Channel.GlobalChat || e.getChannel().ID.equals("rp"))
&& (!e.isFromcmd() || isdifferentchannel.test(DiscordPlugin.chatchannel)))
&& (e.isFromcmd() || isdifferentchannel.test(DiscordPlugin.chatchannel)))
doit.accept(lastmsgdata == null
? lastmsgdata = new LastMsgData(DiscordPlugin.chatchannel, null, null)
: lastmsgdata);
for (LastMsgData data : lastmsgPerUser) {
if (data.dp.isMinecraftChatEnabled() && isdifferentchannel.test(data.channel)
if (data.dp.isMinecraftChatEnabled() && (e.isFromcmd() || isdifferentchannel.test(data.channel))
&& e.shouldSendTo(getSender(data.channel, data.user, data.dp)))
doit.accept(data);
}
@ -168,7 +170,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
}
private static final String[] UnconnectedCmds = new String[]{"list", "u", "shrug", "tableflip", "unflip", "mwiki",
"yeehaw"};
"yeehaw", "lenny"};
private static LastMsgData lastmsgdata;
private static short lastlist = 0;
@ -286,10 +288,16 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
action.accept(data.channel);
}
public static void stop() {
if (sendthread != null) sendthread.interrupt();
if (recthread != null) recthread.interrupt();
}
private BukkitTask rectask;
private LinkedBlockingQueue<MessageReceivedEvent> recevents = new LinkedBlockingQueue<>();
private IMessage lastmsgfromd; // Last message sent by a Discord user, used for clearing checkmarks
private Runnable recrun;
private static Thread recthread;
@Override // Discord
public void handle(MessageReceivedEvent ev) {
@ -314,6 +322,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
if (rectask != null)
return;
recrun = () -> { //Don't return in a while loop next time
recthread = Thread.currentThread();
processDiscordToMC();
rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, recrun); //Continue message processing
};