Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Botだけになったら自動で抜ける機能実装 #9

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
api("commons-io:commons-io:2.16.1")
api("com.ibm.icu:icu4j:75.1")
api("com.atilika.kuromoji:kuromoji-ipadic:0.9.0")
api("it.unimi.dsi:fastutil:8.5.14")

api("org.jetbrains:annotations:23.0.0")
}
Expand Down
81 changes: 6 additions & 75 deletions core/src/main/java/dev/felnull/itts/core/discord/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,16 @@
import dev.felnull.itts.core.ITTSRuntimeUse;
import dev.felnull.itts.core.ImmortalityTimer;
import dev.felnull.itts.core.discord.command.*;
import dev.felnull.itts.core.savedata.BotStateData;
import dev.felnull.itts.core.tts.TTSInstance;
import dev.felnull.itts.core.tts.saidtext.StartupSaidText;
import dev.felnull.itts.core.voice.VoiceType;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.channel.middleman.AudioChannel;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.api.managers.Presence;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
Expand All @@ -35,6 +26,11 @@ public class Bot implements ITTSRuntimeUse {
*/
protected final List<BaseCommand> baseCommands = new ArrayList<>();

/**
* 接続制御
*/
private final ConnectControl connectControl = new ConnectControl();

/**
* JDA
*/
Expand All @@ -49,7 +45,7 @@ public void start() {
this.jda = JDABuilder.createDefault(getConfigManager().getConfig().getBotToken())
.enableIntents(GatewayIntent.MESSAGE_CONTENT, GatewayIntent.GUILD_PRESENCES)
.enableCache(CacheFlag.ACTIVITY)
.addEventListeners(new DCEventListener(this))
.addEventListeners(new DCEventListener(this), this.connectControl.getAdaptor())
.build();

updateCommands(this.jda);
Expand All @@ -63,71 +59,6 @@ public void run() {
updateActivityAsync();
}
}, 0, 1000 * 10);

try {
this.jda.awaitReady();
reconnect();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

private void reconnect() {
CompletableFuture.runAsync(() -> {
Map<Long, BotStateData> allData = getSaveDataManager().getAllBotStateData();
long selfId = getJDA().getSelfUser().getIdLong();

allData.forEach((guildId, data) -> {
Guild guild = getJDA().getGuildById(guildId);

if (guild != null && data.getConnectedAudioChannel() >= 0 && data.getReadAroundTextChannel() >= 0) {
try {
AudioChannel audioChannel = guild.getChannelById(AudioChannel.class, data.getConnectedAudioChannel());

if (audioChannel == null) {
data.setConnectedAudioChannel(-1);
getITTSLogger().info("Failed to reconnect (Audio channel does not exist): {}", guild.getName());
return;
}

MessageChannel chatChannel = guild.getTextChannelById(data.getReadAroundTextChannel());

if (chatChannel == null) {
data.setReadAroundTextChannel(-1);
data.setConnectedAudioChannel(-1);
getITTSLogger().info("Failed to reconnect (Message channel does not exist): {}", guild.getName());
return;
}

getTTSManager().setReadAroundChannel(guild, chatChannel);

try {
guild.getAudioManager().openAudioConnection(audioChannel);
} catch (InsufficientPermissionException ex) {
data.setConnectedAudioChannel(-1);
getITTSLogger().info("Failed to reconnect (No permission): {}", guild.getName());
return;
}

getTTSManager().connect(guild, audioChannel);

TTSInstance ti = getTTSManager().getTTSInstance(guildId);
VoiceType vt = getVoiceManager().getVoiceType(guildId, selfId);

if (ti != null && vt != null) {
if (getTTSManager().canSpeak(guild)) {
ti.sayText(new StartupSaidText(vt.createVoice(guildId, selfId)));
}
}

getITTSLogger().info("Reconnected: {}", guild.getName());
} catch (Exception ex) {
getITTSLogger().error("Failed to reconnect: {}", guild.getName(), ex);
}
}
});

}, getAsyncExecutor());
}

private void registeringCommands() {
Expand Down
Loading
Loading