Supporting clean installs, broadcast toggles, command improvements #92
4 changed files with 97 additions and 59 deletions
|
@ -3,6 +3,9 @@ package buttondevteam.discordplugin.commands;
|
|||
import buttondevteam.core.component.channel.Channel;
|
||||
import buttondevteam.discordplugin.*;
|
||||
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 lombok.val;
|
||||
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.util.PermissionUtils;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ChannelconCommand extends DiscordCommandBase {
|
||||
@Override
|
||||
public String getCommandName() {
|
||||
return "channelcon";
|
||||
}
|
||||
@CommandClass(helpText = {"---- 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).", //
|
||||
"You need to have access to the MC channel and have manage permissions on the Discord channel.", //
|
||||
"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
|
||||
public boolean run(IMessage message, String args) {
|
||||
if (args.length() == 0)
|
||||
return false;
|
||||
if (!PermissionUtils.hasPermissions(message.getChannel(), message.getAuthor(), Permissions.MANAGE_CHANNEL)) {
|
||||
message.reply("you need to have manage permissions for this channel!");
|
||||
return true;
|
||||
}
|
||||
if (MCChatCustom.hasCustomChat(message.getChannel())) {
|
||||
if (args.toLowerCase().startsWith("remove")) {
|
||||
if (MCChatCustom.removeCustomChat(message.getChannel()))
|
||||
message.reply("channel connection removed.");
|
||||
else
|
||||
message.reply("wait what, couldn't remove channel connection.");
|
||||
return true;
|
||||
}
|
||||
if (args.toLowerCase().startsWith("toggle")) {
|
||||
val cc = MCChatCustom.getCustomChat(message.getChannel());
|
||||
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"));
|
||||
String[] argsa = args.split(" ");
|
||||
if (argsa.length < 2) {
|
||||
message.reply("toggles:\n" + togglesString.get());
|
||||
return true;
|
||||
}
|
||||
String arg = argsa[1].toUpperCase();
|
||||
val b = Arrays.stream(ChannelconBroadcast.values()).filter(t -> t.toString().equals(arg)).findAny();
|
||||
if (!b.isPresent()) {
|
||||
message.reply("cannot find toggle. Toggles:\n" + togglesString.get());
|
||||
return true;
|
||||
}
|
||||
//A B | F
|
||||
//------- A: original - B: mask - F: new
|
||||
//0 0 | 0
|
||||
//0 1 | 1
|
||||
//1 0 | 1
|
||||
//1 1 | 0
|
||||
// XOR
|
||||
cc.toggles ^= b.get().flag;
|
||||
message.reply("'" + b.get().toString().toLowerCase() + "' " + ((cc.toggles & b.get().flag) == 0 ? "disabled" : "enabled"));
|
||||
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 toggle(Command2DCSender sender, @Command2.OptionalArg String toggle) {
|
||||
val message = sender.getMessage();
|
||||
if (checkPerms(message)) return true;
|
||||
val cc = MCChatCustom.getCustomChat(message.getChannel());
|
||||
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"))
|
||||
+ "\n\n" + TBMCSystemChatEvent.BroadcastTarget.stream().map(TBMCSystemChatEvent.BroadcastTarget::getName).collect(Collectors.joining("\n"));
|
||||
if (toggle == null) {
|
||||
message.reply("toggles:\n" + togglesString.get());
|
||||
return true;
|
||||
}
|
||||
String arg = toggle.toUpperCase();
|
||||
val b = Arrays.stream(ChannelconBroadcast.values()).filter(t -> t.toString().equals(arg)).findAny();
|
||||
if (!b.isPresent()) {
|
||||
val bt = TBMCSystemChatEvent.BroadcastTarget.get(arg);
|
||||
if (bt == null) {
|
||||
message.reply("cannot find toggle. Toggles:\n" + togglesString.get());
|
||||
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: original - B: mask - F: new
|
||||
//0 0 | 0
|
||||
//0 1 | 1
|
||||
//1 0 | 1
|
||||
//1 1 | 0
|
||||
// XOR
|
||||
cc.toggles ^= b.get().flag;
|
||||
message.reply("'" + b.get().toString().toLowerCase() + "' " + ((cc.toggles & b.get().flag) == 0 ? "disabled" : "enabled"));
|
||||
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();
|
||||
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 /.");
|
||||
|
@ -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.");
|
||||
return true;
|
||||
}*/ //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 + "`!");
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getHelpText() {
|
||||
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
|
||||
public String[] getHelpText(Method method) {
|
||||
return new String[]{ //
|
||||
"---- 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).", //
|
||||
|
|
|
@ -2,6 +2,7 @@ package buttondevteam.discordplugin.mcchat;
|
|||
|
||||
import buttondevteam.core.component.channel.Channel;
|
||||
import buttondevteam.discordplugin.DiscordConnectedPlayer;
|
||||
import buttondevteam.lib.TBMCSystemChatEvent;
|
||||
import lombok.NonNull;
|
||||
import lombok.val;
|
||||
import sx.blah.discord.handle.obj.IChannel;
|
||||
|
@ -18,8 +19,8 @@ public class MCChatCustom {
|
|||
*/
|
||||
static ArrayList<CustomLMD> lastmsgCustom = new ArrayList<>();
|
||||
|
||||
public static void addCustomChat(IChannel channel, String groupid, Channel mcchannel, IUser user, DiscordConnectedPlayer dcp, int toggles) {
|
||||
val lmd = new CustomLMD(channel, user, groupid, mcchannel, dcp, 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, brtoggles);
|
||||
lastmsgCustom.add(lmd);
|
||||
}
|
||||
|
||||
|
@ -46,14 +47,16 @@ public class MCChatCustom {
|
|||
public final Channel mcchannel;
|
||||
public final DiscordConnectedPlayer dcp;
|
||||
public int toggles;
|
||||
public List<TBMCSystemChatEvent.BroadcastTarget> brtoggles;
|
||||
|
||||
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);
|
||||
groupID = groupid;
|
||||
this.mcchannel = mcchannel;
|
||||
this.dcp = dcp;
|
||||
this.toggles = toggles;
|
||||
this.brtoggles = brtoggles;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -172,7 +172,7 @@ public class MCChatUtils {
|
|||
if (event.shouldSendTo(getSender(data.channel, data.user)))
|
||||
action.accept(data.channel);
|
||||
MCChatCustom.lastmsgCustom.stream().filter(clmd -> {
|
||||
if ((clmd.toggles & ChannelconBroadcast.BROADCAST.flag) == 0)
|
||||
if (!clmd.brtoggles.contains(event.getTarget()))
|
||||
return false;
|
||||
return event.shouldSendTo(clmd.dcp);
|
||||
}).map(clmd -> clmd.channel).forEach(action);
|
||||
|
|
|
@ -5,6 +5,7 @@ import buttondevteam.discordplugin.DPUtils;
|
|||
import buttondevteam.discordplugin.DiscordConnectedPlayer;
|
||||
import buttondevteam.discordplugin.DiscordPlugin;
|
||||
import buttondevteam.lib.TBMCCoreAPI;
|
||||
import buttondevteam.lib.TBMCSystemChatEvent;
|
||||
import buttondevteam.lib.architecture.Component;
|
||||
import buttondevteam.lib.architecture.ConfigData;
|
||||
import com.google.common.collect.Lists;
|
||||
|
@ -14,7 +15,9 @@ import org.bukkit.Bukkit;
|
|||
import sx.blah.discord.handle.obj.IChannel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MinecraftChatModule extends Component {
|
||||
private @Getter MCChatListener listener;
|
||||
|
@ -56,11 +59,12 @@ public class MinecraftChatModule extends Component {
|
|||
val user = DiscordPlugin.dc.fetchUser(did);
|
||||
val groupid = chcon.getString("groupid");
|
||||
val toggles = chcon.getInt("toggles");
|
||||
val brtoggles = chcon.getStringList("brtoggles");
|
||||
if (!mcch.isPresent() || ch == null || user == null || groupid == null)
|
||||
continue;
|
||||
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"));
|
||||
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("groupid", chcon.groupID);
|
||||
chconc.set("toggles", chcon.toggles);
|
||||
chconc.set("brtoggles", chcon.brtoggles.stream().map(TBMCSystemChatEvent.BroadcastTarget::getName).collect(Collectors.toList()));
|
||||
}
|
||||
MCChatListener.stop(true);
|
||||
} //TODO: Use ComponentManager.isEnabled() at other places too, instead of SafeMode
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue