Fix channel position

This commit is contained in:
Sebastien Clement 2021-02-18 17:07:35 +01:00
parent 023afb7323
commit 1a4899c1cf
2 changed files with 19 additions and 8 deletions

View File

@ -17,12 +17,14 @@ import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceJoinEvent; import net.dv8tion.jda.api.events.guild.voice.GuildVoiceJoinEvent;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent; import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceMoveEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException; import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.managers.GuildManager; import net.dv8tion.jda.api.managers.GuildManager;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import java.awt.*; import java.awt.*;
@ -149,6 +151,13 @@ public class BotListener extends ListenerAdapter {
autoVoiceChannel.leave(event.getChannelLeft()); autoVoiceChannel.leave(event.getChannelLeft());
} }
@Override
public void onGuildVoiceMove(@NotNull GuildVoiceMoveEvent event) {
super.onGuildVoiceMove(event);
AutoVoiceChannel autoVoiceChannel = AutoVoiceChannel.getInstance(event.getGuild());
autoVoiceChannel.leave(event.getChannelLeft());
}
@Override @Override
public void onMessageReceived(MessageReceivedEvent event) { public void onMessageReceived(MessageReceivedEvent event) {
@ -206,6 +215,7 @@ public class BotListener extends ListenerAdapter {
} }
@Override @Override
public void onGuildJoin(GuildJoinEvent event) { public void onGuildJoin(GuildJoinEvent event) {
logger.info("Join new guild! (" + event.getGuild().getName() + " " + event.getGuild().getMembers().size() + " Members)"); logger.info("Join new guild! (" + event.getGuild().getName() + " " + event.getGuild().getMembers().size() + " Members)");

View File

@ -36,13 +36,14 @@ public class AutoVoiceChannel {
if (guild == null) if (guild == null)
return; return;
GuildPreferenceEntity pref = SettingsUtils.getInstance().getPreference(guild); GuildPreferenceEntity pref = SettingsUtils.getInstance().getPreference(guild);
if ( pref.isAutoVoice() && voiceChannel.getId().equals(pref.getAutoVoiceChannelID())) { if (pref.isAutoVoice() && voiceChannel.getId().equals(pref.getAutoVoiceChannelID())) {
logger.info("Creating new voice channel for Guild : " + guild.getName()); logger.info("Creating new voice channel for Guild : " + guild.getName());
VoiceChannel newChannel = voiceChannel.createCopy().complete(); VoiceChannel newChannel = voiceChannel.createCopy().complete();
int next = getNextNumber(); int next = getNextNumber();
String title = pref.getAutoVoiceChannelTitle(); String title = pref.getAutoVoiceChannelTitle();
title = title.replace("@count", Integer.toString(next)); title = title.replace("@count", Integer.toString(next));
newChannel.getManager().setName(title).queue(); newChannel.getManager().setName(title).setPosition(voiceChannel.getPosition()).queue();
createdChannels.put(next, newChannel.getId()); createdChannels.put(next, newChannel.getId());
moveMembers(voiceChannel.getMembers(), newChannel); moveMembers(voiceChannel.getMembers(), newChannel);
} }
@ -50,10 +51,10 @@ public class AutoVoiceChannel {
} }
public void leave(VoiceChannel voiceChannel) { public void leave(VoiceChannel voiceChannel) {
if (voiceChannel.getMembers().isEmpty()){ if (voiceChannel.getMembers().isEmpty()) {
String id = voiceChannel.getId(); String id = voiceChannel.getId();
for(Map.Entry<Integer, String> entry : createdChannels.entrySet()) { for (Map.Entry<Integer, String> entry : createdChannels.entrySet()) {
if (entry.getValue().equals(id)){ if (entry.getValue().equals(id)) {
logger.info("Auto created channel is empty, deleting it ..."); logger.info("Auto created channel is empty, deleting it ...");
voiceChannel.delete().reason("Auto-remove empty voice channel").queue(); voiceChannel.delete().reason("Auto-remove empty voice channel").queue();
} }
@ -71,15 +72,15 @@ public class AutoVoiceChannel {
return 999; return 999;
} }
private void moveMembers(List<Member> members, VoiceChannel destination){ private void moveMembers(List<Member> members, VoiceChannel destination) {
logger.debug("Moving Members to new voice channel..."); logger.debug("Moving Members to new voice channel...");
RestAction<Void> restAction = null; RestAction<Void> restAction = null;
for(Member member : members){ for (Member member : members) {
if (restAction == null) if (restAction == null)
restAction = destination.getGuild().moveVoiceMember(member, destination); restAction = destination.getGuild().moveVoiceMember(member, destination);
restAction = restAction.and(destination.getGuild().moveVoiceMember(member, destination)); restAction = restAction.and(destination.getGuild().moveVoiceMember(member, destination));
} }
if(restAction != null){ if (restAction != null) {
restAction.queue(); restAction.queue();
} }
} }