Some additions and fixes...

- Made the bot only react when msg starts with the ping unless we're in
PM
- Now removing color codes from AFK messages
- Removed /t as an unconnnected cmd as it doesn't work properly
- Added /yeehaw to the unconnected cmds
- Allowing bot commands in mc chat
This commit is contained in:
Norbi Peti 2016-12-21 22:37:01 +01:00
parent 4570b964b3
commit 2bcb47b7b5
4 changed files with 43 additions and 30 deletions

View file

@ -98,10 +98,10 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
dc.changeStatus(Status.game("on TBMC")); dc.changeStatus(Status.game("on TBMC"));
} else { } else {
botchannel = devServer.getChannelByID("239519012529111040"); // bot-room botchannel = devServer.getChannelByID("239519012529111040"); // bot-room
annchannel = devServer.getChannelByID("239519012529111040"); // bot-room annchannel = botchannel; // bot-room
genchannel = devServer.getChannelByID("239519012529111040"); // bot-room genchannel = botchannel; // bot-room
botroomchannel = devServer.getChannelByID("239519012529111040");// bot-room botroomchannel = botchannel;// bot-room
issuechannel = devServer.getChannelByID("239519012529111040"); // bot-room issuechannel = botchannel; // bot-room
chatchannel = devServer.getChannelByID("248185455508455424"); // minecraft_chat_test chatchannel = devServer.getChannelByID("248185455508455424"); // minecraft_chat_test
dc.changeStatus(Status.game("testing")); dc.changeStatus(Status.game("testing"));
} }

View file

@ -19,7 +19,7 @@ public class CommandListener {
final IChannel channel = event.getMessage().getChannel(); final IChannel channel = event.getMessage().getChannel();
if (!channel.getID().equals(DiscordPlugin.botchannel.getID()) && !channel.isPrivate()) if (!channel.getID().equals(DiscordPlugin.botchannel.getID()) && !channel.isPrivate())
return; return;
runCommand(event.getMessage()); runCommand(event.getMessage(), true);
} }
}, new IListener<MessageReceivedEvent>() { }, new IListener<MessageReceivedEvent>() {
@Override @Override
@ -28,25 +28,38 @@ public class CommandListener {
return; return;
if (event.getMessage().getAuthor().isBot()) if (event.getMessage().getAuthor().isBot())
return; return;
runCommand(event.getMessage()); runCommand(event.getMessage(), false);
} }
} }; } };
} }
private static void runCommand(IMessage message) { /**
* Runs a ChromaBot command.
*
* @param message
* The Discord message
* @param mentionedonly
* Only run the command if ChromaBot is mentioned at the start of the message
* @return Whether it ran the command (always true if mentionedonly is false)
*/
public static boolean runCommand(IMessage message, boolean mentionedonly) {
message.getChannel().setTypingStatus(true); message.getChannel().setTypingStatus(true);
String cmdwithargs = message.getContent(); final StringBuilder cmdwithargs = new StringBuilder(message.getContent());
final String mention = DiscordPlugin.dc.getOurUser().mention(false); final String mention = DiscordPlugin.dc.getOurUser().mention(false);
final String mentionNick = DiscordPlugin.dc.getOurUser().mention(true); final String mentionNick = DiscordPlugin.dc.getOurUser().mention(true);
cmdwithargs = checkanddeletemention(cmdwithargs, mention, message); boolean gotmention = checkanddeletemention(cmdwithargs, mention, message);
cmdwithargs = checkanddeletemention(cmdwithargs, mentionNick, message); gotmention = checkanddeletemention(cmdwithargs, mentionNick, message);
for (String mentionRole : (Iterable<String>) message.getRoleMentions().stream().map(r -> r.mention())::iterator) for (String mentionRole : (Iterable<String>) message.getRoleMentions().stream().map(r -> r.mention())::iterator)
cmdwithargs = checkanddeletemention(cmdwithargs, mentionRole, message); gotmention = checkanddeletemention(cmdwithargs, mentionRole, message);
int index = cmdwithargs.indexOf(' '); if (mentionedonly && !gotmention) {
message.getChannel().setTypingStatus(false);
return false;
}
int index = cmdwithargs.indexOf(" ");
String cmd; String cmd;
String args; String args;
if (index == -1) { if (index == -1) {
cmd = cmdwithargs; cmd = cmdwithargs.toString();
args = ""; args = "";
} else { } else {
cmd = cmdwithargs.substring(0, index); cmd = cmdwithargs.substring(0, index);
@ -54,17 +67,20 @@ public class CommandListener {
} }
DiscordCommandBase.runCommand(cmd, args, message); DiscordCommandBase.runCommand(cmd, args, message);
message.getChannel().setTypingStatus(false); message.getChannel().setTypingStatus(false);
return true;
} }
private static String checkanddeletemention(String cmdwithargs, String mention, IMessage message) { 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 (message.getContent().startsWith(mention)) // TODO: Resolve mentions: Compound arguments, either a mention or text
if (cmdwithargs.length() > mention.length() + 1) if (cmdwithargs.length() > mention.length() + 1)
cmdwithargs = cmdwithargs.substring( cmdwithargs = cmdwithargs.delete(0,
cmdwithargs.charAt(mention.length()) == ' ' ? mention.length() + 1 : mention.length()); cmdwithargs.charAt(mention.length()) == ' ' ? mention.length() + 1 : mention.length());
else else
cmdwithargs = "help"; cmdwithargs.replace(0, cmdwithargs.length(), "help");
else
return false;
if (cmdwithargs.length() == 0) if (cmdwithargs.length() == 0)
cmdwithargs = "help"; cmdwithargs.replace(0, cmdwithargs.length(), "help");
return cmdwithargs; return true;
} }
} }

View file

@ -35,14 +35,13 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
return; return;
if (e.getChannel().equals(Channel.GlobalChat)) if (e.getChannel().equals(Channel.GlobalChat))
DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel, DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel,
"<" + (e.getSender() instanceof Player DiscordPlugin.sanitizeString("<" + (e.getSender() instanceof Player //
? DiscordPlugin.sanitizeString(((Player) e.getSender()).getDisplayName()) ? ((Player) e.getSender()).getDisplayName() //
: DiscordPlugin.sanitizeString(e.getSender().getName())) + "> " : e.getSender().getName()) + "> " + e.getMessage()));
+ DiscordPlugin.sanitizeString(e.getMessage()));
} }
private static final String[] UnconnectedCmds = new String[] { "list", "u", "shrug", "tableflip", "unflip", "mwiki", private static final String[] UnconnectedCmds = new String[] { "list", "u", "shrug", "tableflip", "unflip", "mwiki",
"t" }; "yeehaw" };
public static final HashMap<String, DiscordSender> UnconnectedSenders = new HashMap<>(); public static final HashMap<String, DiscordSender> UnconnectedSenders = new HashMap<>();
public static final HashMap<String, DiscordPlayerSender> ConnectedSenders = new HashMap<>(); public static final HashMap<String, DiscordPlayerSender> ConnectedSenders = new HashMap<>();
@ -55,6 +54,8 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
if (!event.getMessage().getChannel().getID().equals(DiscordPlugin.chatchannel.getID()) if (!event.getMessage().getChannel().getID().equals(DiscordPlugin.chatchannel.getID())
/* && !(event.getMessage().getChannel().isPrivate() && privatechat) */) /* && !(event.getMessage().getChannel().isPrivate() && privatechat) */)
return; return;
if (CommandListener.runCommand(event.getMessage(), true))
return;
String dmessage = event.getMessage().getContent(); String dmessage = event.getMessage().getContent();
try { try {
Optional<? extends Player> player = Bukkit.getOnlinePlayers().stream().filter(p -> { // TODO: Support offline players Optional<? extends Player> player = Bukkit.getOnlinePlayers().stream().filter(p -> { // TODO: Support offline players
@ -89,7 +90,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
"Sorry, you need to be online on the server and have your accounts connected, you can only access these commands:\n" "Sorry, you need to be online on the server and have your accounts connected, you can only access these commands:\n"
+ Arrays.stream(UnconnectedCmds).map(uc -> "/" + uc) + Arrays.stream(UnconnectedCmds).map(uc -> "/" + uc)
.collect(Collectors.joining(", ")) .collect(Collectors.joining(", "))
+ "\nTo connect your accounts, use @ChromaBot connect in " + "\nTo connect your accounts, use @ChromaBot connect here or in "
+ DiscordPlugin.botchannel.mention()); + DiscordPlugin.botchannel.mention());
return; return;
} }

View file

@ -56,14 +56,10 @@ public class MCListener implements Listener {
DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel, e.getDeathMessage()); DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel, e.getDeathMessage());
} }
/*@EventHandler
public void onPlayerYEEHAW(TBMCYEEHAWEvent e) {
DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel, e.getSender() + " YEEHAWs");
}*/ // It's broadcasted now
@EventHandler @EventHandler
public void onPlayerAFK(AfkStatusChangeEvent e) { public void onPlayerAFK(AfkStatusChangeEvent e) {
DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel, DiscordPlugin.sendMessageToChannel(DiscordPlugin.chatchannel,
e.getAffected().getBase().getDisplayName() + " is " + (e.getValue() ? "now" : "no longer") + " AFK."); DiscordPlugin.sanitizeString(e.getAffected().getBase().getDisplayName()) + " is "
+ (e.getValue() ? "now" : "no longer") + " AFK.");
} }
} }