Added chat channel & mixed case cmds support

Needs testing
This commit is contained in:
Norbi Peti 2017-08-04 13:33:26 +02:00
parent b0c791e622
commit 67074017d2
3 changed files with 39 additions and 18 deletions

View file

@ -9,7 +9,11 @@ import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.IDiscordSender; import buttondevteam.lib.chat.IDiscordSender;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import sx.blah.discord.handle.obj.IChannel; import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.handle.obj.IUser; import sx.blah.discord.handle.obj.IUser;
@ -19,6 +23,7 @@ public abstract class DiscordSenderBase implements IDiscordSender {
*/ */
protected IUser user; protected IUser user;
protected IChannel channel; protected IChannel channel;
private @Getter @Setter @NonNull Channel mcchannel = Channel.GlobalChat;
protected DiscordSenderBase(IUser user, IChannel channel) { protected DiscordSenderBase(IUser user, IChannel channel) {
this.user = user; this.user = user;

View file

@ -146,7 +146,7 @@ public class CommandListener {
cmd = cmdwithargs.substring(0, index); cmd = cmdwithargs.substring(0, index);
args = cmdwithargs.substring(index + 1); args = cmdwithargs.substring(index + 1);
} }
DiscordCommandBase.runCommand(cmd, args, message); DiscordCommandBase.runCommand(cmd.toLowerCase(), args, message);
message.getChannel().setTypingStatus(false); message.getChannel().setTypingStatus(false);
return true; return true;
} }

View file

@ -1,12 +1,8 @@
package buttondevteam.discordplugin.listeners; package buttondevteam.discordplugin.listeners;
import java.awt.Color; import java.awt.Color;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays; import java.util.function.*;
import java.util.HashMap;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -212,7 +208,13 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
dmessage = dmessage.replace(u.mention(true), "@" + (nick != null ? nick : u.getName())); dmessage = dmessage.replace(u.mention(true), "@" + (nick != null ? nick : u.getName()));
} }
if (dmessage.startsWith("/")) { BiConsumer<Channel, String> sendChatMessage = (channel, msg) -> TBMCChatAPI.SendChatMessage(channel,
dsender,
msg + (event.getMessage().getAttachments().size() > 0 ? "\n" + event.getMessage()
.getAttachments().stream().map(a -> a.getUrl()).collect(Collectors.joining("\n"))
: ""));
if (dmessage.startsWith("/")) { // Ingame command
DiscordPlugin.perform(() -> { DiscordPlugin.perform(() -> {
if (!event.getMessage().isDeleted() && !event.getChannel().isPrivate()) if (!event.getMessage().isDeleted() && !event.getChannel().isPrivate())
event.getMessage().delete(); event.getMessage().delete();
@ -239,21 +241,35 @@ public class MCChatListener implements Listener, IListener<MessageReceivedEvent>
dsender.sendMessage("Stop it. You know the answer."); dsender.sendMessage("Stop it. You know the answer.");
lastlist = 0; lastlist = 0;
} else { } else {
String topcmd = null; // TODO: Channels int spi = cmd.indexOf(' ');
VanillaCommandListener.runBukkitOrVanillaCommand(dsender, cmd); final String topcmd = spi == -1 ? cmd : cmd.substring(0, spi);
Optional<Channel> ch = Channel.getChannels().stream().filter(c -> c.ID.equalsIgnoreCase(topcmd))
.findAny();
if (!ch.isPresent())
VanillaCommandListener.runBukkitOrVanillaCommand(dsender, cmd);
else // TO!DO: Only allow talking in general in public chat - A to-do from before I went to Greece
{
Channel chc = ch.get();
if (!chc.ID.equals(Channel.GlobalChat.ID) && !event.getMessage().getChannel().isPrivate())
dsender.sendMessage(
"You can only talk in global in the public chat. DM `mcchat` to enable private chat to talk in the other channels.");
else {
if (spi == -1) // Switch channels
{
dsender.setMcchannel(chc);
dsender.sendMessage("You're now talking in: "
+ DiscordPlugin.sanitizeString(dsender.getMcchannel().DisplayName));
} else // Send single message
sendChatMessage.accept(chc, cmd.substring(spi + 1));
}
}
} }
lastlistp = (short) Bukkit.getOnlinePlayers().size(); lastlistp = (short) Bukkit.getOnlinePlayers().size();
} else { } else {// Not a command
if (dmessage.length() == 0 && event.getMessage().getAttachments().size() == 0) if (dmessage.length() == 0 && event.getMessage().getAttachments().size() == 0)
TBMCChatAPI.SendChatMessage(Channel.GlobalChat, dsender, "pinned a message on Discord."); // TODO: Not chat message TBMCChatAPI.SendChatMessage(Channel.GlobalChat, dsender, "pinned a message on Discord."); // TODO: Not chat message
else else
TBMCChatAPI sendChatMessage.accept(dsender.getMcchannel(), dmessage);
.SendChatMessage(Channel.GlobalChat,
dsender, dmessage
+ (event.getMessage().getAttachments().size() > 0
? "\n" + event.getMessage().getAttachments().stream()
.map(a -> a.getUrl()).collect(Collectors.joining("\n"))
: ""));
event.getMessage().getChannel().getMessageHistory().stream().forEach(m -> { event.getMessage().getChannel().getMessageHistory().stream().forEach(m -> {
try { try {
final IReaction reaction = m.getReactionByUnicode(DiscordPlugin.DELIVERED_REACTION); final IReaction reaction = m.getReactionByUnicode(DiscordPlugin.DELIVERED_REACTION);