|
| 1 | +package net.discordjug.javabot.systems.custom_vc; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import lombok.RequiredArgsConstructor; |
| 6 | +import net.discordjug.javabot.data.config.BotConfig; |
| 7 | +import net.discordjug.javabot.util.ExceptionLogger; |
| 8 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 9 | +import net.dv8tion.jda.api.Permission; |
| 10 | +import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel; |
| 11 | +import net.dv8tion.jda.api.entities.channel.middleman.StandardGuildChannel; |
| 12 | +import net.dv8tion.jda.api.entities.channel.unions.AudioChannelUnion; |
| 13 | +import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent; |
| 14 | +import net.dv8tion.jda.api.events.session.ReadyEvent; |
| 15 | +import net.dv8tion.jda.api.hooks.ListenerAdapter; |
| 16 | +import net.dv8tion.jda.api.requests.restaction.ChannelAction; |
| 17 | + |
| 18 | +/** |
| 19 | + * Automatically creates a temporary voice channel when a user joins the {@link ModerationConfig#getCustomVoiceChannel() custom voice channel template} and deletes that voice channel when there are no users left. |
| 20 | + */ |
| 21 | +@RequiredArgsConstructor |
| 22 | +public class CustomVCListener extends ListenerAdapter { |
| 23 | + private final BotConfig botConfig; |
| 24 | + private final CustomVCRepository repository; |
| 25 | + private final CustomVCButtonHandler buttonHandler; |
| 26 | + |
| 27 | + @Override |
| 28 | + public void onGuildVoiceUpdate(GuildVoiceUpdateEvent event) { |
| 29 | + AudioChannelUnion vcJoined = event.getChannelJoined(); |
| 30 | + long customVoiceChannelId = botConfig.get(event.getGuild()).getModerationConfig().getCustomVoiceChannelId(); |
| 31 | + if (vcJoined != null && vcJoined.getIdLong() == customVoiceChannelId) { |
| 32 | + createCustomVC(event, vcJoined); |
| 33 | + } |
| 34 | + AudioChannelUnion vcLeft = event.getChannelLeft(); |
| 35 | + if (vcLeft != null && repository.isCustomVoiceChannel(vcLeft.getIdLong()) && vcLeft.getMembers().isEmpty()) { |
| 36 | + vcLeft.delete().queue(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private void createCustomVC(GuildVoiceUpdateEvent event, AudioChannelUnion vcJoined) { |
| 41 | + ChannelAction<? extends StandardGuildChannel> copy = vcJoined.createCopy(); |
| 42 | + copy |
| 43 | + .setName("custom-" + event.getMember().getId()) |
| 44 | + .addMemberPermissionOverride( |
| 45 | + event.getMember().getIdLong(), |
| 46 | + List.of(Permission.MANAGE_CHANNEL, Permission.VIEW_CHANNEL), |
| 47 | + List.of()) |
| 48 | + .queue(newChannel -> { |
| 49 | + repository.addCustomVoiceChannel(newChannel.getIdLong(), event.getMember().getIdLong()); |
| 50 | + if (!(newChannel instanceof VoiceChannel newVC)) { |
| 51 | + ExceptionLogger.capture(new IllegalStateException("expected VoiceChannel to be created, got " + newChannel.getClass().getCanonicalName())); |
| 52 | + newChannel.delete().queue(); |
| 53 | + return; |
| 54 | + } |
| 55 | + event.getGuild().moveVoiceMember(event.getMember(), newVC).queue(); |
| 56 | + newVC.sendMessageEmbeds(new EmbedBuilder() |
| 57 | + .setTitle("Your personal Voice Channel") |
| 58 | + .setDescription(""" |
| 59 | + This is your personal, temporary voice channel. |
| 60 | + You can configure this channel using the button below, the `/vc-control` command or the Discord settings. |
| 61 | + This channel will be deleted as soon as all people leave this channel. |
| 62 | + """) |
| 63 | + .build()) |
| 64 | + .addContent(event.getMember().getAsMention()) |
| 65 | + .addActionRow(buttonHandler.createMakePrivateButton()) |
| 66 | + .queue(); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onReady(ReadyEvent event) { |
| 72 | + for (long channelId : repository.getAllCustomVoiceChannels()) { |
| 73 | + VoiceChannel vc = event.getJDA().getVoiceChannelById(channelId); |
| 74 | + if (vc == null) { |
| 75 | + repository.removeCustomVoiceChannel(channelId); |
| 76 | + } else if (vc.getMembers().isEmpty()) { |
| 77 | + vc.delete().queue(); |
| 78 | + repository.removeCustomVoiceChannel(channelId); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments