FINISHED basic support for mcchat (#12)

This commit is contained in:
Norbi Peti 2017-06-14 22:10:26 +02:00
parent 1ad49dfe23
commit 6cd81a6776
5 changed files with 24 additions and 22 deletions

View file

@ -247,7 +247,7 @@ public class DiscordPlugin extends JavaPlugin implements IListener<ReadyEvent> {
try { try {
if (channel == chatchannel) if (channel == chatchannel)
MCChatListener.resetLastMessage(); // If this is a chat message, it'll be set again MCChatListener.resetLastMessage(); // If this is a chat message, it'll be set again
final String content = TBMCCoreAPI.IsTestServer() && channel != chatchannel final String content = TBMCCoreAPI.IsTestServer() && channel != chatchannel || channel == botroomchannel // Both are the same for testing
? "*The following message is from a test server*\n" + message : message; ? "*The following message is from a test server*\n" + message : message;
return perform( return perform(
() -> embed == null ? channel.sendMessage(content) : channel.sendMessage(content, embed, false)); () -> embed == null ? channel.sendMessage(content) : channel.sendMessage(content, embed, false));

View file

@ -27,7 +27,9 @@ public abstract class DiscordCommandBase {
DiscordCommandBase command = commands.get(cmd); DiscordCommandBase command = commands.get(cmd);
if (command == null) { if (command == null) {
DiscordPlugin.sendMessageToChannel(message.getChannel(), DiscordPlugin.sendMessageToChannel(message.getChannel(),
"Unknown command: " + cmd + " with args: " + args + "\nDo @ChromaBot help for help"); "Unknown command: " + cmd + " with args: " + args + "\nDo '"
+ (message.getChannel().isPrivate() ? "" : message.getClient().getOurUser().mention() + " ")
+ "help' for help");
return; return;
} }
try { try {

View file

@ -21,7 +21,9 @@ public class MCChatCommand extends DiscordCommandBase {
try (final DiscordPlayer user = DiscordPlayer.getUser(message.getAuthor().getStringID(), DiscordPlayer.class)) { try (final DiscordPlayer user = DiscordPlayer.getUser(message.getAuthor().getStringID(), DiscordPlayer.class)) {
PlayerData<Boolean> mcchat = user.minecraftChat(); PlayerData<Boolean> mcchat = user.minecraftChat();
mcchat.set(!mcchat.getOrDefault(false)); mcchat.set(!mcchat.getOrDefault(false));
message.reply("Minecraft chat " + (mcchat.get() ? "enabled." : "disabled.")); message.reply(
"Minecraft chat " + (mcchat.get() ? "enabled. Use '" + message.getClient().getOurUser().mention()
+ " mcchat' (with the mention) to disable." : "disabled."));
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Error while setting mcchat for user" + message.getAuthor().getName(), e); TBMCCoreAPI.SendException("Error while setting mcchat for user" + message.getAuthor().getName(), e);
} }

View file

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Random; import java.util.Random;
import buttondevteam.discordplugin.DiscordPlayer;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.commands.DiscordCommandBase; import buttondevteam.discordplugin.commands.DiscordCommandBase;
import sx.blah.discord.api.events.IListener; import sx.blah.discord.api.events.IListener;
@ -29,15 +30,9 @@ public class CommandListener {
"It'll be ready next time you ask", // Ali "It'll be ready next time you ask", // Ali
}; };
private static final String[] serverReadyQuestions = new String[] { private static final String[] serverReadyQuestions = new String[] { "when will the server be open",
"when will the server be open", "when will the server be ready", "when will the server be done", "when will the server be complete",
"when will the server be ready", "when will the server be finished", "when's the server ready", "when's the server open" };
"when will the server be done",
"when will the server be complete",
"when will the server be finished",
"when's the server ready",
"when's the server open"
};
private static final Random serverReadyRandom = new Random(); private static final Random serverReadyRandom = new Random();
private static final ArrayList<Short> usableServerReadyStrings = new ArrayList<Short>(serverReadyStrings.length) { private static final ArrayList<Short> usableServerReadyStrings = new ArrayList<Short>(serverReadyStrings.length) {
@ -65,6 +60,9 @@ public class CommandListener {
return; return;
if (channel.getStringID().equals(DiscordPlugin.chatchannel.getStringID())) if (channel.getStringID().equals(DiscordPlugin.chatchannel.getStringID()))
return; // The chat code already handles this - Right now while testing botchannel is the same as chatchannel return; // The chat code already handles this - Right now while testing botchannel is the same as chatchannel
if (DiscordPlayer.getUser(event.getAuthor().getStringID(), DiscordPlayer.class).minecraftChat()
.getOrDefault(false)) // Let the MCChatListener handle it
return;
runCommand(event.getMessage(), true); runCommand(event.getMessage(), true);
} }
}, new IListener<MessageReceivedEvent>() { }, new IListener<MessageReceivedEvent>() {
@ -80,7 +78,9 @@ public class CommandListener {
next = usableServerReadyStrings.remove(serverReadyRandom.nextInt(usableServerReadyStrings.size())); next = usableServerReadyStrings.remove(serverReadyRandom.nextInt(usableServerReadyStrings.size()));
DiscordPlugin.sendMessageToChannel(event.getMessage().getChannel(), serverReadyStrings[next]); DiscordPlugin.sendMessageToChannel(event.getMessage().getChannel(), serverReadyStrings[next]);
} }
if (!event.getMessage().getChannel().isPrivate()) if (!event.getMessage().getChannel().isPrivate()
|| DiscordPlayer.getUser(event.getAuthor().getStringID(), DiscordPlayer.class).minecraftChat()
.getOrDefault(false))
return; return;
if (event.getMessage().getAuthor().isBot()) if (event.getMessage().getAuthor().isBot())
return; return;

View file

@ -15,7 +15,6 @@ import buttondevteam.discordplugin.*;
import buttondevteam.lib.*; import buttondevteam.lib.*;
import buttondevteam.lib.chat.Channel; import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.player.ChromaGamerBase;
import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayer;
import sx.blah.discord.api.events.IListener; import sx.blah.discord.api.events.IListener;
import sx.blah.discord.api.internal.json.objects.EmbedObject; import sx.blah.discord.api.internal.json.objects.EmbedObject;
@ -124,9 +123,9 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
@Override // Discord @Override // Discord
public void handle(MessageReceivedEvent event) { public void handle(MessageReceivedEvent event) {
final IUser author = event.getMessage().getAuthor(); final IUser author = event.getMessage().getAuthor();
final DiscordPlayer user = DiscordPlayer.getUser(author.getStringID(), DiscordPlayer.class);
if (!event.getMessage().getChannel().getStringID().equals(DiscordPlugin.chatchannel.getStringID()) if (!event.getMessage().getChannel().getStringID().equals(DiscordPlugin.chatchannel.getStringID())
&& !(event.getMessage().getChannel().isPrivate() && DiscordPlayer && !(event.getMessage().getChannel().isPrivate() && user.minecraftChat().getOrDefault(false)))
.getUser(author.getStringID(), DiscordPlayer.class).minecraftChat().getOrDefault(false)))
return; return;
resetLastMessage(); resetLastMessage();
lastlist++; lastlist++;
@ -137,8 +136,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
String dmessage = event.getMessage().getContent(); String dmessage = event.getMessage().getContent();
synchronized (this) { synchronized (this) {
try { try {
DiscordPlayer dp = ChromaGamerBase.getUser(author.getStringID(), DiscordPlayer.class); final DiscordSenderBase dsender = getSender(event.getMessage().getChannel(), author, user);
final DiscordSenderBase dsender = getSender(event.getMessage().getChannel(), author, dp);
for (IUser u : event.getMessage().getMentions()) { for (IUser u : event.getMessage().getMentions()) {
dmessage = dmessage.replace(u.mention(false), "@" + u.getName()); // TODO: IG Formatting dmessage = dmessage.replace(u.mention(false), "@" + u.getName()); // TODO: IG Formatting
@ -148,7 +146,7 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
if (dmessage.startsWith("/")) { if (dmessage.startsWith("/")) {
DiscordPlugin.perform(() -> { DiscordPlugin.perform(() -> {
if (!event.getMessage().isDeleted()) if (!event.getMessage().isDeleted() && !event.getChannel().isPrivate())
event.getMessage().delete(); event.getMessage().delete();
}); });
final String cmd = dmessage.substring(1).toLowerCase(); final String cmd = dmessage.substring(1).toLowerCase();