Added support for broadcast toggles

#89
This commit is contained in:
Norbi Peti 2019-02-16 14:08:37 +01:00
parent 5a9986de4f
commit 0cbc22eb72
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
4 changed files with 97 additions and 59 deletions

View file

@ -3,6 +3,9 @@ package buttondevteam.discordplugin.commands;
import buttondevteam.core.component.channel.Channel; import buttondevteam.core.component.channel.Channel;
import buttondevteam.discordplugin.*; import buttondevteam.discordplugin.*;
import buttondevteam.discordplugin.mcchat.MCChatCustom; import buttondevteam.discordplugin.mcchat.MCChatCustom;
import buttondevteam.lib.TBMCSystemChatEvent;
import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayer;
import lombok.val; import lombok.val;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -10,61 +13,80 @@ import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.handle.obj.Permissions; import sx.blah.discord.handle.obj.Permissions;
import sx.blah.discord.util.PermissionUtils; import sx.blah.discord.util.PermissionUtils;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class ChannelconCommand extends DiscordCommandBase { @CommandClass(helpText = {"---- Channel connect ---", //
@Override "This command allows you to connect a Minecraft channel to a Discord channel (just like how the global chat is connected to #minecraft-chat).", //
public String getCommandName() { "You need to have access to the MC channel and have manage permissions on the Discord channel.", //
return "channelcon"; "You also need to have your Minecraft account connected. In #bot use /connect <mcname>.", //
} "Call this command from the channel you want to use.", //
"Usage: @Bot channelcon <mcchannel>", //
"Use the ID (command) of the channel, for example `g` for the global chat.", //
"To remove a connection use @ChromaBot channelcon remove in the channel.", //
"Mentioning the bot is needed in this case because the / prefix only works in #bot.", //
"Invite link: <https://discordapp.com/oauth2/authorize?client_id=226443037893591041&scope=bot&permissions=268509264>" //
})
public class ChannelconCommand extends ICommand2DC {
@Command2.Subcommand
public boolean remove(Command2DCSender sender) {
val message = sender.getMessage();
if (checkPerms(message)) return true;
if (MCChatCustom.removeCustomChat(message.getChannel()))
message.reply("channel connection removed.");
else
message.reply("this channel isn't connected.");
return true;
}
@Override @Command2.Subcommand
public boolean run(IMessage message, String args) { public boolean toggle(Command2DCSender sender, @Command2.OptionalArg String toggle) {
if (args.length() == 0) val message = sender.getMessage();
return false; if (checkPerms(message)) return true;
if (!PermissionUtils.hasPermissions(message.getChannel(), message.getAuthor(), Permissions.MANAGE_CHANNEL)) { val cc = MCChatCustom.getCustomChat(message.getChannel());
message.reply("you need to have manage permissions for this channel!"); assert cc != null; //It's not null
return true; Supplier<String> togglesString = () -> Arrays.stream(ChannelconBroadcast.values()).map(t -> t.toString().toLowerCase() + ": " + ((cc.toggles & t.flag) == 0 ? "disabled" : "enabled")).collect(Collectors.joining("\n"))
} + "\n\n" + TBMCSystemChatEvent.BroadcastTarget.stream().map(TBMCSystemChatEvent.BroadcastTarget::getName).collect(Collectors.joining("\n"));
if (MCChatCustom.hasCustomChat(message.getChannel())) { if (toggle == null) {
if (args.toLowerCase().startsWith("remove")) { message.reply("toggles:\n" + togglesString.get());
if (MCChatCustom.removeCustomChat(message.getChannel())) return true;
message.reply("channel connection removed."); }
else String arg = toggle.toUpperCase();
message.reply("wait what, couldn't remove channel connection."); val b = Arrays.stream(ChannelconBroadcast.values()).filter(t -> t.toString().equals(arg)).findAny();
return true; if (!b.isPresent()) {
} val bt = TBMCSystemChatEvent.BroadcastTarget.get(arg);
if (args.toLowerCase().startsWith("toggle")) { if (bt == null) {
val cc = MCChatCustom.getCustomChat(message.getChannel()); message.reply("cannot find toggle. Toggles:\n" + togglesString.get());
assert cc != null; //It's not null return true;
Supplier<String> togglesString = () -> Arrays.stream(ChannelconBroadcast.values()).map(t -> t.toString().toLowerCase() + ": " + ((cc.toggles & t.flag) == 0 ? "disabled" : "enabled")).collect(Collectors.joining("\n")); }
String[] argsa = args.split(" "); final boolean add;
if (argsa.length < 2) { if (add = !cc.brtoggles.contains(bt))
message.reply("toggles:\n" + togglesString.get()); cc.brtoggles.add(bt);
return true; else
} cc.brtoggles.remove(bt);
String arg = argsa[1].toUpperCase(); return respond(sender, "'" + bt.getName() + "' " + (add ? "en" : "dis") + "abled");
val b = Arrays.stream(ChannelconBroadcast.values()).filter(t -> t.toString().equals(arg)).findAny(); }
if (!b.isPresent()) { //A B | F
message.reply("cannot find toggle. Toggles:\n" + togglesString.get()); //------- A: original - B: mask - F: new
return true; //0 0 | 0
} //0 1 | 1
//A B | F //1 0 | 1
//------- A: original - B: mask - F: new //1 1 | 0
//0 0 | 0 // XOR
//0 1 | 1 cc.toggles ^= b.get().flag;
//1 0 | 1 message.reply("'" + b.get().toString().toLowerCase() + "' " + ((cc.toggles & b.get().flag) == 0 ? "disabled" : "enabled"));
//1 1 | 0 return true;
// XOR }
cc.toggles ^= b.get().flag;
message.reply("'" + b.get().toString().toLowerCase() + "' " + ((cc.toggles & b.get().flag) == 0 ? "disabled" : "enabled")); @Command2.Subcommand
return true; public boolean def(Command2DCSender sender, String args) {
} val message = sender.getMessage();
message.reply("this channel is already connected to a Minecraft channel. Use `@ChromaBot channelcon remove` to remove it."); if (checkPerms(message)) return true;
return true; if (MCChatCustom.hasCustomChat(message.getChannel()))
} return respond(sender, "this channel is already connected to a Minecraft channel. Use `@ChromaBot channelcon remove` to remove it.");
val chan = Channel.getChannels().filter(ch -> ch.ID.equalsIgnoreCase(args) || (Arrays.stream(ch.IDs().get()).anyMatch(cid -> cid.equalsIgnoreCase(args)))).findAny(); val chan = Channel.getChannels().filter(ch -> ch.ID.equalsIgnoreCase(args) || (Arrays.stream(ch.IDs().get()).anyMatch(cid -> cid.equalsIgnoreCase(args)))).findAny();
if (!chan.isPresent()) { //TODO: Red embed that disappears over time (kinda like the highlight messages in OW) if (!chan.isPresent()) { //TODO: Red embed that disappears over time (kinda like the highlight messages in OW)
message.reply("MC channel with ID '" + args + "' not found! The ID is the command for it without the /."); message.reply("MC channel with ID '" + args + "' not found! The ID is the command for it without the /.");
@ -87,13 +109,21 @@ public class ChannelconCommand extends DiscordCommandBase {
message.reply("sorry, this MC chat is already connected to a different channel, multiple channels are not supported atm."); message.reply("sorry, this MC chat is already connected to a different channel, multiple channels are not supported atm.");
return true; return true;
}*/ //TODO: "Channel admins" that can connect channels? }*/ //TODO: "Channel admins" that can connect channels?
MCChatCustom.addCustomChat(message.getChannel(), groupid, chan.get(), message.getAuthor(), dcp, 0); MCChatCustom.addCustomChat(message.getChannel(), groupid, chan.get(), message.getAuthor(), dcp, 0, new ArrayList<>());
message.reply("alright, connection made to group `" + groupid + "`!"); message.reply("alright, connection made to group `" + groupid + "`!");
return true; return true;
} }
@Override private boolean checkPerms(IMessage message) {
public String[] getHelpText() { if (!PermissionUtils.hasPermissions(message.getChannel(), message.getAuthor(), Permissions.MANAGE_CHANNEL)) {
message.reply("you need to have manage permissions for this channel!");
return true;
}
return false;
}
@Override
public String[] getHelpText(Method method) {
return new String[]{ // return new String[]{ //
"---- Channel connect ---", // "---- Channel connect ---", //
"This command allows you to connect a Minecraft channel to a Discord channel (just like how the global chat is connected to #minecraft-chat).", // "This command allows you to connect a Minecraft channel to a Discord channel (just like how the global chat is connected to #minecraft-chat).", //

View file

@ -2,6 +2,7 @@ package buttondevteam.discordplugin.mcchat;
import buttondevteam.core.component.channel.Channel; import buttondevteam.core.component.channel.Channel;
import buttondevteam.discordplugin.DiscordConnectedPlayer; import buttondevteam.discordplugin.DiscordConnectedPlayer;
import buttondevteam.lib.TBMCSystemChatEvent;
import lombok.NonNull; import lombok.NonNull;
import lombok.val; import lombok.val;
import sx.blah.discord.handle.obj.IChannel; import sx.blah.discord.handle.obj.IChannel;
@ -18,8 +19,8 @@ public class MCChatCustom {
*/ */
static ArrayList<CustomLMD> lastmsgCustom = new ArrayList<>(); static ArrayList<CustomLMD> lastmsgCustom = new ArrayList<>();
public static void addCustomChat(IChannel channel, String groupid, Channel mcchannel, IUser user, DiscordConnectedPlayer dcp, int toggles) { public static void addCustomChat(IChannel channel, String groupid, Channel mcchannel, IUser user, DiscordConnectedPlayer dcp, int toggles, List<TBMCSystemChatEvent.BroadcastTarget> brtoggles) {
val lmd = new CustomLMD(channel, user, groupid, mcchannel, dcp, toggles); val lmd = new CustomLMD(channel, user, groupid, mcchannel, dcp, toggles, brtoggles);
lastmsgCustom.add(lmd); lastmsgCustom.add(lmd);
} }
@ -46,14 +47,16 @@ public class MCChatCustom {
public final Channel mcchannel; public final Channel mcchannel;
public final DiscordConnectedPlayer dcp; public final DiscordConnectedPlayer dcp;
public int toggles; public int toggles;
public List<TBMCSystemChatEvent.BroadcastTarget> brtoggles;
private CustomLMD(@NonNull IChannel channel, @NonNull IUser user, private CustomLMD(@NonNull IChannel channel, @NonNull IUser user,
@NonNull String groupid, @NonNull Channel mcchannel, @NonNull DiscordConnectedPlayer dcp, int toggles) { @NonNull String groupid, @NonNull Channel mcchannel, @NonNull DiscordConnectedPlayer dcp, int toggles, List<TBMCSystemChatEvent.BroadcastTarget> brtoggles) {
super(channel, user); super(channel, user);
groupID = groupid; groupID = groupid;
this.mcchannel = mcchannel; this.mcchannel = mcchannel;
this.dcp = dcp; this.dcp = dcp;
this.toggles = toggles; this.toggles = toggles;
this.brtoggles = brtoggles;
} }
} }
} }

View file

@ -172,7 +172,7 @@ public class MCChatUtils {
if (event.shouldSendTo(getSender(data.channel, data.user))) if (event.shouldSendTo(getSender(data.channel, data.user)))
action.accept(data.channel); action.accept(data.channel);
MCChatCustom.lastmsgCustom.stream().filter(clmd -> { MCChatCustom.lastmsgCustom.stream().filter(clmd -> {
if ((clmd.toggles & ChannelconBroadcast.BROADCAST.flag) == 0) if (!clmd.brtoggles.contains(event.getTarget()))
return false; return false;
return event.shouldSendTo(clmd.dcp); return event.shouldSendTo(clmd.dcp);
}).map(clmd -> clmd.channel).forEach(action); }).map(clmd -> clmd.channel).forEach(action);

View file

@ -5,6 +5,7 @@ import buttondevteam.discordplugin.DPUtils;
import buttondevteam.discordplugin.DiscordConnectedPlayer; import buttondevteam.discordplugin.DiscordConnectedPlayer;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.TBMCSystemChatEvent;
import buttondevteam.lib.architecture.Component; import buttondevteam.lib.architecture.Component;
import buttondevteam.lib.architecture.ConfigData; import buttondevteam.lib.architecture.ConfigData;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
@ -14,7 +15,9 @@ import org.bukkit.Bukkit;
import sx.blah.discord.handle.obj.IChannel; import sx.blah.discord.handle.obj.IChannel;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
public class MinecraftChatModule extends Component { public class MinecraftChatModule extends Component {
private @Getter MCChatListener listener; private @Getter MCChatListener listener;
@ -56,11 +59,12 @@ public class MinecraftChatModule extends Component {
val user = DiscordPlugin.dc.fetchUser(did); val user = DiscordPlugin.dc.fetchUser(did);
val groupid = chcon.getString("groupid"); val groupid = chcon.getString("groupid");
val toggles = chcon.getInt("toggles"); val toggles = chcon.getInt("toggles");
val brtoggles = chcon.getStringList("brtoggles");
if (!mcch.isPresent() || ch == null || user == null || groupid == null) if (!mcch.isPresent() || ch == null || user == null || groupid == null)
continue; continue;
Bukkit.getScheduler().runTask(getPlugin(), () -> { //<-- Needed because of occasional ConcurrentModificationExceptions when creating the player (PermissibleBase) Bukkit.getScheduler().runTask(getPlugin(), () -> { //<-- Needed because of occasional ConcurrentModificationExceptions when creating the player (PermissibleBase)
val dcp = new DiscordConnectedPlayer(user, ch, UUID.fromString(chcon.getString("mcuid")), chcon.getString("mcname")); val dcp = new DiscordConnectedPlayer(user, ch, UUID.fromString(chcon.getString("mcuid")), chcon.getString("mcname"));
MCChatCustom.addCustomChat(ch, groupid, mcch.get(), user, dcp, toggles); MCChatCustom.addCustomChat(ch, groupid, mcch.get(), user, dcp, toggles, brtoggles.stream().map(TBMCSystemChatEvent.BroadcastTarget::get).filter(Objects::nonNull).collect(Collectors.toList()));
}); });
} }
} }
@ -79,7 +83,8 @@ public class MinecraftChatModule extends Component {
chconc.set("mcname", chcon.dcp.getName()); chconc.set("mcname", chcon.dcp.getName());
chconc.set("groupid", chcon.groupID); chconc.set("groupid", chcon.groupID);
chconc.set("toggles", chcon.toggles); chconc.set("toggles", chcon.toggles);
chconc.set("brtoggles", chcon.brtoggles.stream().map(TBMCSystemChatEvent.BroadcastTarget::getName).collect(Collectors.toList()));
} }
MCChatListener.stop(true); MCChatListener.stop(true);
} //TODO: Use ComponentManager.isEnabled() at other places too, instead of SafeMode }
} }