Skip to content
This repository has been archived by the owner on Jul 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #219 from book000/feat/comeback-afk
Browse files Browse the repository at this point in the history
feat: AFKから別チャンネルに移動した場合に参加する
  • Loading branch information
yuuahp authored Jun 13, 2023
2 parents 8795b06 + fde7e48 commit 9cc81bc
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 17 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/jaoafa/jdavcspeaker/Event/AutoJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.jaoafa.jdavcspeaker.Lib.MultipleServer;
import com.jaoafa.jdavcspeaker.Main;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.channel.middleman.AudioChannel;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.managers.AudioManager;
Expand All @@ -28,6 +29,10 @@ public void onGuildVoiceUpdate(@NotNull GuildVoiceUpdateEvent event) {
return; // 自身がいずれかのVCに参加している
}

if (isAfkChannel(event.getChannelJoined())) {
return; // AFKチャンネルに接続した場合は除外
}

AudioManager audioManager = event.getGuild().getAudioManager();
audioManager.openAudioConnection(event.getChannelJoined());

Expand All @@ -40,4 +45,9 @@ public void onGuildVoiceUpdate(@NotNull GuildVoiceUpdateEvent event) {
.setColor(LibEmbedColor.success);
MultipleServer.getVCChannel(event.getGuild()).sendMessageEmbeds(embed.build()).queue();
}

boolean isAfkChannel(AudioChannel channel) {
if (channel.getGuild().getAfkChannel() == null) return false;
return channel.getIdLong() == channel.getGuild().getAfkChannel().getIdLong();
}
}
109 changes: 92 additions & 17 deletions src/main/java/com/jaoafa/jdavcspeaker/Event/AutoMove.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ public void onGuildVoiceUpdate(@NotNull GuildVoiceUpdateEvent event) {
AudioChannel oldChannel = event.getOldValue();
AudioChannel newChannel = event.getNewValue();
if (oldChannel == null || newChannel == null) return; // 移動以外は除外
long connectedUsers = oldChannel.getMembers().stream()
.filter(member -> !member.getUser().isBot())
.count();
long newUsers = newChannel.getMembers().stream()
.filter(member -> !member.getUser().isBot())
.count();
long connectedUsers = getConnectedUsers(oldChannel);
long newUsers = getConnectedUsers(newChannel);

new LibFlow("AutoMove")
.action("%s: %s (%d) -> %s (%s)".formatted(
Expand All @@ -41,29 +37,46 @@ public void onGuildVoiceUpdate(@NotNull GuildVoiceUpdateEvent event) {
newChannel.getName(),
newUsers));

if (event.getGuild().getSelfMember().getVoiceState() == null ||
event.getGuild().getSelfMember().getVoiceState().getChannel() == null) {
return; // 自身がどのVCにも参加していない
MoveInfo info = new MoveInfo(event, oldChannel, newChannel, connectedUsers, newUsers);
runJoined(info);
runNotJoined(info);
}

/**
* Bot自身がVCに参加している場合に動作。
* <p>
* 移動先がAFKでなく、移動先にBot自身がおらず、移動先に参加しているユーザーが移動元より多い場合、移動する。
*
* @param info MoveInfo
*/
void runJoined(MoveInfo info) {
GuildVoiceUpdateEvent event = info.event();
AudioChannel oldChannel = info.oldChannel();
AudioChannel newChannel = info.newChannel();
long connectedUsers = info.connectedUsers();
long newUsers = info.newUsers();

AudioChannel connectedChannel = getConnectedChannel(event);
if (connectedChannel == null) {
return; // VCに参加していない
}
AudioChannel connectedChannel = event.getGuild().getSelfMember().getVoiceState().getChannel();

if (event.getMember().getUser().isBot()) {
return;
return; // Botが移動した場合は終了
}

if (event.getGuild().getSelfMember().getVoiceState().getChannel() != oldChannel) {
if (isAfkChannel(newChannel)) {
return; // 移動先がAFKチャンネルの場合は終了
}

if (connectedChannel.getIdLong() != oldChannel.getIdLong()) {
return; // 移動元チャンネルに自身が入っていない
}

if (connectedUsers >= newUsers) {
return; // 自身がいるチャンネルの人数より、移動先の人数の方が少ない、もしくは同じ場合終了
}

if (event.getGuild().getAfkChannel() == null &&
event.getGuild().getAfkChannel().getIdLong() == newChannel.getIdLong()) {
return; // 移動先がAFKチャンネルの場合終了
}

AudioManager audioManager = event.getGuild().getAudioManager();
audioManager.openAudioConnection(event.getChannelJoined());

Expand All @@ -79,4 +92,66 @@ public void onGuildVoiceUpdate(@NotNull GuildVoiceUpdateEvent event) {
.setColor(LibEmbedColor.success);
MultipleServer.getVCChannel(event.getGuild()).sendMessageEmbeds(embed.build()).queue();
}

/**
* Bot自身がVCに参加していない場合に動作。
* <p>
* 移動元がAFKの場合、移動する。
*
* @param info MoveInfo
*/
void runNotJoined(MoveInfo info) {
GuildVoiceUpdateEvent event = info.event();
AudioChannel oldChannel = info.oldChannel();
AudioChannel newChannel = info.newChannel();

AudioChannel connectedChannel = getConnectedChannel(event);
if (connectedChannel != null) {
return; // VCに参加している
}

if (!isAfkChannel(oldChannel)) {
return; // 移動元がAFKチャンネルでない
}

if (isAfkChannel(newChannel)) {
return; // 移動先がAFKチャンネルの場合は終了
}

if (event.getMember().getUser().isBot()) {
return; // Botが移動した場合は終了
}


AudioManager audioManager = event.getGuild().getAudioManager();
audioManager.openAudioConnection(newChannel);

if (MultipleServer.getVCChannel(event.getGuild()) == null) return;

new LibFlow("AutoMove").success("自動接続しました: %s", newChannel.getName());
EmbedBuilder embed = new EmbedBuilder()
.setTitle(":white_check_mark: AutoJoin (Triggered by move from AFK)")
.setDescription("<#%s> に接続しました。".formatted(newChannel.getId()))
.setColor(LibEmbedColor.success);
MultipleServer.getVCChannel(event.getGuild()).sendMessageEmbeds(embed.build()).queue();
}

record MoveInfo(GuildVoiceUpdateEvent event, AudioChannel oldChannel, AudioChannel newChannel, long connectedUsers,
long newUsers) {
}

int getConnectedUsers(AudioChannel channel) {
return (int) channel.getMembers().stream()
.filter(member -> !member.getUser().isBot())
.count();
}

AudioChannel getConnectedChannel(GuildVoiceUpdateEvent event) {
return event.getGuild().getAudioManager().getConnectedChannel();
}

boolean isAfkChannel(AudioChannel channel) {
if (channel.getGuild().getAfkChannel() == null) return false;
return channel.getIdLong() == channel.getGuild().getAfkChannel().getIdLong();
}
}

0 comments on commit 9cc81bc

Please sign in to comment.