summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorromangraef <roman.graef@gmail.com>2018-02-08 20:18:50 +0100
committerromangraef <roman.graef@gmail.com>2018-02-08 20:18:50 +0100
commit0f29a5d757e85f4f0b0a9f0d729abc7c4574fc9a (patch)
tree351cc8228c0e55a8256e9e08005e972caead884f /src
downloadprivateroombot-0f29a5d757e85f4f0b0a9f0d729abc7c4574fc9a.tar.gz
privateroombot-0f29a5d757e85f4f0b0a9f0d729abc7c4574fc9a.tar.bz2
privateroombot-0f29a5d757e85f4f0b0a9f0d729abc7c4574fc9a.zip
Initial commit
Diffstat (limited to 'src')
-rw-r--r--src/main/java/de/romjaki/privateroombot/Config.java22
-rw-r--r--src/main/java/de/romjaki/privateroombot/Main.java25
-rw-r--r--src/main/java/de/romjaki/privateroombot/VoiceChannelJoinListener.java58
3 files changed, 105 insertions, 0 deletions
diff --git a/src/main/java/de/romjaki/privateroombot/Config.java b/src/main/java/de/romjaki/privateroombot/Config.java
new file mode 100644
index 0000000..738070b
--- /dev/null
+++ b/src/main/java/de/romjaki/privateroombot/Config.java
@@ -0,0 +1,22 @@
+package de.romjaki.privateroombot;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+
+public class Config {
+
+ public static Config CONFIG;
+
+ static {
+ try {
+ CONFIG = Main.gson.fromJson(new FileReader(new File("config.json")), Config.class);
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ }
+ }
+
+ String token;
+ String from_channel_id;
+ String category_id;
+}
diff --git a/src/main/java/de/romjaki/privateroombot/Main.java b/src/main/java/de/romjaki/privateroombot/Main.java
new file mode 100644
index 0000000..353fae3
--- /dev/null
+++ b/src/main/java/de/romjaki/privateroombot/Main.java
@@ -0,0 +1,25 @@
+package de.romjaki.privateroombot;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import net.dv8tion.jda.core.AccountType;
+import net.dv8tion.jda.core.JDA;
+import net.dv8tion.jda.core.JDABuilder;
+
+import javax.security.auth.login.LoginException;
+
+import static de.romjaki.privateroombot.Config.CONFIG;
+
+public class Main {
+
+ public static Gson gson = new GsonBuilder()
+ .create();
+
+ public static JDA jda;
+ public static void main(String[] args) throws LoginException {
+ jda = new JDABuilder(AccountType.BOT)
+ .setToken(CONFIG.token)
+ .addEventListener(new VoiceChannelJoinListener())
+ .buildAsync();
+ }
+}
diff --git a/src/main/java/de/romjaki/privateroombot/VoiceChannelJoinListener.java b/src/main/java/de/romjaki/privateroombot/VoiceChannelJoinListener.java
new file mode 100644
index 0000000..5f55beb
--- /dev/null
+++ b/src/main/java/de/romjaki/privateroombot/VoiceChannelJoinListener.java
@@ -0,0 +1,58 @@
+package de.romjaki.privateroombot;
+
+import net.dv8tion.jda.core.Permission;
+import net.dv8tion.jda.core.entities.Category;
+import net.dv8tion.jda.core.entities.Guild;
+import net.dv8tion.jda.core.entities.Member;
+import net.dv8tion.jda.core.entities.VoiceChannel;
+import net.dv8tion.jda.core.events.guild.voice.GuildVoiceJoinEvent;
+import net.dv8tion.jda.core.events.guild.voice.GuildVoiceLeaveEvent;
+import net.dv8tion.jda.core.events.guild.voice.GuildVoiceMoveEvent;
+import net.dv8tion.jda.core.hooks.ListenerAdapter;
+
+import static de.romjaki.privateroombot.Config.CONFIG;
+
+public class VoiceChannelJoinListener extends ListenerAdapter {
+ @Override
+ public void onGuildVoiceJoin(GuildVoiceJoinEvent event) {
+ super.onGuildVoiceJoin(event);
+ if (!event.getChannelJoined().getId().equals(CONFIG.from_channel_id)) {
+ return;
+ }
+ Guild guild = event.getGuild();
+ Member member = event.getMember();
+ Category category = guild.getCategoryById(CONFIG.category_id);
+ VoiceChannel newChannel = (VoiceChannel) category.createVoiceChannel(member.getEffectiveName() + " - privat").complete();
+ newChannel.createPermissionOverride(member).setAllow(Permission.VOICE_MOVE_OTHERS).queue();
+ guild.getController().moveVoiceMember(member, newChannel).queue();
+ }
+
+ @Override
+ public void onGuildVoiceMove(GuildVoiceMoveEvent event) {
+ super.onGuildVoiceMove(event);
+ VoiceChannelJoinListener.this.onGuildVoiceLeave(
+ new GuildVoiceLeaveEvent(
+ event.getJDA(),
+ event.getResponseNumber(),
+ event.getMember(),
+ event.getChannelLeft()
+ )
+ );
+ }
+
+ @Override
+ public void onGuildVoiceLeave(GuildVoiceLeaveEvent event) {
+ super.onGuildVoiceLeave(event);
+ if (!event.getChannelLeft().getParent().getId().equals(CONFIG.category_id)) {
+ return;
+ }
+ VoiceChannel channel = event.getChannelLeft();
+ if (!channel.getMembers().isEmpty()) {
+ return;
+ }
+ if (channel.getId().equals(CONFIG.from_channel_id)) {
+ return;
+ }
+ channel.delete().queue();
+ }
+}