package Modules; import Controll.GuildController; import Controll.SuperModule; import net.dv8tion.jda.api.entities.VoiceChannel; import net.dv8tion.jda.api.events.GenericEvent; import net.dv8tion.jda.api.events.guild.GenericGuildEvent; import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import java.util.HashSet; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.json.simple.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class VoiceLobby extends SuperModule { public static final String command = "lobby"; private static final String description = "Create temporary channels.\n" + "`lobby create -n NAME -l USERLIMIT` creates new temporary channel, without parameter default values will be used.\n" + "`lobby limit USERLIMIT` set new user limit.\n" + "`lobby name NAME` set new channel name.\n" + "`lobby setLobbyChannel CATEGORY_ID` to set Category where lobbies will spawn. \n" + "`lobby setLaunchpad VOICE_CHANNEL_ID` to set a Launchpad channel (user who join this channel will get moved in a new temporary channel)"; private long groupCategory; private long launchpadChannel; private HashSet tmpChannel = new HashSet<>(); private Logger logger = LoggerFactory.getLogger(this.getClass()); public VoiceLobby(GuildController guildController, JSONObject config){ super(command, guildController, config); if (!getConfig().containsKey("groupCategory")){ getConfig().put("groupCategory", "123"); safeConfig(getConfig()); } if(!getConfig().containsKey("LaunchpadChannel")){ getConfig().put("LaunchpadChannel", "123"); safeConfig(getConfig()); } //long groupCategory this.groupCategory = Long.parseLong(getConfig().get("groupCategory").toString()); this.launchpadChannel = Long.parseLong(getConfig().get("LaunchpadChannel").toString()); } @Override public String getCommand() { return command; } @Override public String getDescription() { return description; } @Override public void execute(GenericEvent genericEvent) { if (!(genericEvent instanceof GenericGuildEvent)) return; GenericGuildEvent event = (GenericGuildEvent) genericEvent; if (event instanceof GuildMessageReceivedEvent) { GuildMessageReceivedEvent thisEvent = (GuildMessageReceivedEvent) event; if (!isCommand(thisEvent.getMessage())) return; logger.debug("Lobby command triggered"); //////////////////////// // CREATE TMP CHANNEL //////////////////////// if (checkForCommand(thisEvent.getMessage().getContentRaw(), "create")) { //check if user is in Voice Channel if (thisEvent.getMember().getVoiceState().inVoiceChannel()) { //Pattern Matcher name = Pattern.compile("(?is:-n [^ ]+)").matcher(thisEvent.getMessage().getContentRaw()); Matcher limit = Pattern.compile("(?is:-l [0-9]+)").matcher(thisEvent.getMessage().getContentRaw()); //TODO Matcher privateChannel = Pattern.compile("(?is:-p)").matcher(thisEvent.getMessage().getContentRaw()); //Create Voice channel VoiceChannel newChannel = event.getGuild().createVoiceChannel(name.find() ? name.group(0).replace("-n ", "").replaceAll(" $", "") : "TEMP") .setParent(event.getGuild().getCategoryById(groupCategory)) .setUserlimit(limit.find() ? Integer.parseInt(limit.group(0).replace("-l ", "").replaceAll(" $", "")) : 3).complete(); // move User event.getGuild().moveVoiceMember(((GuildMessageReceivedEvent) event).getMember(), newChannel).queue(); tmpChannel.add(newChannel.getIdLong()); } else { thisEvent.getChannel().sendMessage("Please Join a Voice Channel first!").queue(e -> e.delete().queueAfter(1, TimeUnit.MINUTES)); } //////////////////////// // Set Limit //////////////////////// } else if (thisEvent.getMessage().getContentRaw().matches("(?is:" + getGuildController().getPREFIX() + command + " limit [0-9]+)")) { //check if user is in Voice Channel and a temp channel if (thisEvent.getMember().getVoiceState().inVoiceChannel() && thisEvent.getMember().getVoiceState().getChannel().getParent().getIdLong() == groupCategory) { //Set new Limit thisEvent.getMember().getVoiceState().getChannel().getManager().setUserLimit(Integer.parseInt( thisEvent.getMessage().getContentRaw().replaceAll(".* limit ", "")) ).queue(); thisEvent.getChannel().sendMessage( "Set limit to " + thisEvent.getMessage().getContentRaw().replaceAll(".* limit ", "")) .queue(e -> e.delete().queueAfter(1, TimeUnit.MINUTES)); } else { thisEvent.getChannel().sendMessage("You are not in a lobby channel!").queue(e -> e.delete().queueAfter(1, TimeUnit.MINUTES)); } //////////////////////// // Set name //////////////////////// } else if (thisEvent.getMessage().getContentRaw().matches("(?is:" + getGuildController().getPREFIX() + command + " name [^ ]+)")) { //check if user is in Voice Channel and a temp channel if (thisEvent.getMember().getVoiceState().inVoiceChannel() && thisEvent.getMember().getVoiceState().getChannel().getParent().getIdLong() == groupCategory) { //Set new Limit thisEvent.getMember().getVoiceState().getChannel().getManager().setName( thisEvent.getMessage().getContentRaw().replaceAll(".* name ", "") ).queue(); thisEvent.getChannel().sendMessage( "Set name to " + thisEvent.getMessage().getContentRaw().replaceAll(".* name ", "")) .queue(e -> e.delete().queueAfter(1, TimeUnit.MINUTES)); } else { thisEvent.getChannel().sendMessage("You are not in a lobby channel!").queue(e -> e.delete().queueAfter(1, TimeUnit.MINUTES)); } //////////////////////// // Set Lobby Category //////////////////////// } else if (checkForAdmin(thisEvent.getMember()) && checkForCommand(thisEvent.getMessage().getContentRaw(), "setLobbyChannel")) { Matcher m = Pattern.compile("(?is:[0-9]+)").matcher(thisEvent.getMessage().getContentRaw()); if (m.find() && thisEvent.getGuild().getCategoryById(m.group(0)) != null) { getConfig().replace("groupCategory", m.group(0) + ""); thisEvent.getChannel().sendMessage("Set groupCategory to: " + m.group(0)).queue(); safeConfig(getConfig()); } else { thisEvent.getChannel().sendMessage("no valid Category!").queue(); } thisEvent.getMessage().delete().queue(); //////////////////////// // Set Launchpad //////////////////////// } else if (checkForAdmin(thisEvent.getMember()) && checkForCommand(thisEvent.getMessage().getContentRaw(), "setLaunchpad")) { if (thisEvent.getMessage().getContentRaw().matches("(?is:.*[0-9]+.*)")) { // check if a Voice channel could be given Matcher m = Pattern.compile("(?is:[0-9]+)").matcher(thisEvent.getMessage().getContentRaw()); if (m.find() && thisEvent.getGuild().getVoiceChannelById(m.group(0)) != null) { getConfig().put("LaunchpadChannel", m.group(0)); safeConfig(getConfig()); this.launchpadChannel = Long.parseLong(getConfig().get("LaunchpadChannel").toString()); thisEvent.getChannel().sendMessage("Launchpad set to `" + m.group(0) + "` (" + thisEvent.getGuild().getVoiceChannelById(m.group(0)).getName() + ")").queue(); return; } } thisEvent.getChannel().sendMessage("Something went wrong, are you sure that you mentioned an VoicechannelID?").queue(e -> e.delete().queueAfter(1, TimeUnit.MINUTES)); } } else if (event instanceof GuildVoiceUpdateEvent) { GuildVoiceUpdateEvent thisEvent = (GuildVoiceUpdateEvent) event; //check if user left a temp channel if ((thisEvent.getChannelLeft() != null && thisEvent.getChannelLeft().getParent().getIdLong() == groupCategory)) { //check if channel is now empty if (thisEvent.getChannelLeft().getMembers().size() < 1 && tmpChannel.contains(thisEvent.getChannelLeft().getIdLong())) { logger.debug("may remove channel: " + thisEvent.getChannelLeft().getId()); //wait for 30 seconds and check again //then delete channel if still empty new Timer().schedule(new TimerTask() { @Override public void run() { if (thisEvent.getChannelLeft().getMembers().size() < 1) { logger.debug("remove channel: " + thisEvent.getChannelLeft().getId()); thisEvent.getChannelLeft().delete().queue(); tmpChannel.remove(thisEvent.getChannelLeft().getIdLong()); } else { logger.debug("dont remove channel: " + thisEvent.getChannelLeft().getId()); } } }, TimeUnit.SECONDS.convert(30, TimeUnit.MILLISECONDS)); } } if ((thisEvent.getChannelJoined() != null && thisEvent.getChannelJoined().getIdLong() == launchpadChannel)) { logger.debug("Triggered Launchpad"); //Create Voice channel VoiceChannel newChannel = event.getGuild().createVoiceChannel("Temp-Lobby", event.getGuild().getCategoryById(groupCategory)) .complete(); // move User event.getGuild().moveVoiceMember(thisEvent.getEntity(), newChannel).queue(); tmpChannel.add(newChannel.getIdLong()); } } } @Override public String showSettings() { return "LOBBYCHANNEL: `" + ((getGuildController().getJda().getCategoryById(groupCategory) == null) ? "none" : getGuildController().getJda().getCategoryById(groupCategory).getName()) + "`" + "LaunchpadChannel: `"+ launchpadChannel + "`"; } }