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,47 +13,62 @@ 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>", //
@Override "Use the ID (command) of the channel, for example `g` for the global chat.", //
public boolean run(IMessage message, String args) { "To remove a connection use @ChromaBot channelcon remove in the channel.", //
if (args.length() == 0) "Mentioning the bot is needed in this case because the / prefix only works in #bot.", //
return false; "Invite link: <https://discordapp.com/oauth2/authorize?client_id=226443037893591041&scope=bot&permissions=268509264>" //
if (!PermissionUtils.hasPermissions(message.getChannel(), message.getAuthor(), Permissions.MANAGE_CHANNEL)) { })
message.reply("you need to have manage permissions for this channel!"); public class ChannelconCommand extends ICommand2DC {
return true; @Command2.Subcommand
} public boolean remove(Command2DCSender sender) {
if (MCChatCustom.hasCustomChat(message.getChannel())) { val message = sender.getMessage();
if (args.toLowerCase().startsWith("remove")) { if (checkPerms(message)) return true;
if (MCChatCustom.removeCustomChat(message.getChannel())) if (MCChatCustom.removeCustomChat(message.getChannel()))
message.reply("channel connection removed."); message.reply("channel connection removed.");
else else
message.reply("wait what, couldn't remove channel connection."); message.reply("this channel isn't connected.");
return true; return true;
} }
if (args.toLowerCase().startsWith("toggle")) {
@Command2.Subcommand
public boolean toggle(Command2DCSender sender, @Command2.OptionalArg String toggle) {
val message = sender.getMessage();
if (checkPerms(message)) return true;
val cc = MCChatCustom.getCustomChat(message.getChannel()); val cc = MCChatCustom.getCustomChat(message.getChannel());
assert cc != null; //It's not null assert cc != null; //It's not null
Supplier<String> togglesString = () -> Arrays.stream(ChannelconBroadcast.values()).map(t -> t.toString().toLowerCase() + ": " + ((cc.toggles & t.flag) == 0 ? "disabled" : "enabled")).collect(Collectors.joining("\n")); 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(" "); + "\n\n" + TBMCSystemChatEvent.BroadcastTarget.stream().map(TBMCSystemChatEvent.BroadcastTarget::getName).collect(Collectors.joining("\n"));
if (argsa.length < 2) { if (toggle == null) {
message.reply("toggles:\n" + togglesString.get()); message.reply("toggles:\n" + togglesString.get());
return true; return true;
} }
String arg = argsa[1].toUpperCase(); String arg = toggle.toUpperCase();
val b = Arrays.stream(ChannelconBroadcast.values()).filter(t -> t.toString().equals(arg)).findAny(); val b = Arrays.stream(ChannelconBroadcast.values()).filter(t -> t.toString().equals(arg)).findAny();
if (!b.isPresent()) { if (!b.isPresent()) {
val bt = TBMCSystemChatEvent.BroadcastTarget.get(arg);
if (bt == null) {
message.reply("cannot find toggle. Toggles:\n" + togglesString.get()); message.reply("cannot find toggle. Toggles:\n" + togglesString.get());
return true; return true;
} }
final boolean add;
if (add = !cc.brtoggles.contains(bt))
cc.brtoggles.add(bt);
else
cc.brtoggles.remove(bt);
return respond(sender, "'" + bt.getName() + "' " + (add ? "en" : "dis") + "abled");
}
//A B | F //A B | F
//------- A: original - B: mask - F: new //------- A: original - B: mask - F: new
//0 0 | 0 //0 0 | 0
@ -62,9 +80,13 @@ public class ChannelconCommand extends DiscordCommandBase {
message.reply("'" + b.get().toString().toLowerCase() + "' " + ((cc.toggles & b.get().flag) == 0 ? "disabled" : "enabled")); message.reply("'" + b.get().toString().toLowerCase() + "' " + ((cc.toggles & b.get().flag) == 0 ? "disabled" : "enabled"));
return true; return true;
} }
message.reply("this channel is already connected to a Minecraft channel. Use `@ChromaBot channelcon remove` to remove it.");
return true; @Command2.Subcommand
} public boolean def(Command2DCSender sender, String args) {
val message = sender.getMessage();
if (checkPerms(message)) 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;
} }
private boolean checkPerms(IMessage message) {
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 @Override
public String[] getHelpText() { 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 }
} }