This commit is contained in:
2026-05-26 14:15:09 +09:00
parent 55d402f606
commit d6b36c43c2
33 changed files with 1496 additions and 0 deletions

104
src/classes/VoiceSession.ts Normal file
View File

@@ -0,0 +1,104 @@
import { AudioPlayerStatus, createAudioPlayer, entersState, joinVoiceChannel, NoSubscriberBehavior, VoiceConnection, VoiceConnectionStatus } from "@discordjs/voice";
import { Guild, VoiceBasedChannel } from "discord.js";
import { Logger } from "../utils/Logger";
import { createResourceFromMp3Buffer } from "../utils/Transcode";
export class VoiceSession {
private player = createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Pause } });
private generation = 0;
private isSessionEnd = false;
private lastTime = Date.now();
private endTimer?: NodeJS.Timeout | null;
private connection: VoiceConnection;
constructor(
private guild: Guild,
private channel: VoiceBasedChannel,
private endTimeMs: number,
private onSessionEnd: (guildId: string) => void
) {
this.connection = joinVoiceChannel({
guildId: this.guild.id,
channelId: this.channel.id,
adapterCreator: this.guild.voiceAdapterCreator,
selfDeaf: true,
selfMute: false,
});
this.init();
}
private async init() {
this.connection.subscribe(this.player);
await entersState(this.connection, VoiceConnectionStatus.Ready, 20_000).catch(() => {});
this.player.on(AudioPlayerStatus.Playing, () => {
this.startEndTimer();
});
this.player.on(AudioPlayerStatus.Idle, () => {
this.startEndTimer();
});
this.player.on("error", (e) => {
Logger.error("player error:"+String(e));
this.startEndTimer();
});
}
private startEndTimer() {
if (this.endTimer) clearTimeout(this.endTimer);
this.endTimer = setTimeout(() => {
this.endTimer = null;
if (
!this.isSessionEnd
&& this.player.state.status === AudioPlayerStatus.Idle
&& (Date.now() - this.lastTime) >= this.endTimeMs
) {
this.sessionEnd();
} else {
this.startEndTimer();
}
}, this.endTimeMs);
}
public async play(mp3Buf: Buffer) {
if (this.isSessionEnd) return;
const myGen = ++this.generation;
this.lastTime = Date.now();
this.startEndTimer();
const res = createResourceFromMp3Buffer(mp3Buf);
if (myGen !== this.generation || this.isSessionEnd) return;
// try { this.player.stop(true); } catch {};
try { this.player.play(res); } catch {};
}
async moveTo(newChannel: VoiceBasedChannel) {
if (this.isSessionEnd) return;
if (this.channel.id === newChannel.id) return;
this.channel = newChannel;
this.startEndTimer();
try { this.connection.disconnect(); } catch {};
try { this.connection.destroy(); } catch {};
this.connection = joinVoiceChannel({
guildId: this.guild.id,
channelId: this.channel.id,
adapterCreator: this.guild.voiceAdapterCreator,
selfDeaf: true,
selfMute: false,
});
this.connection.subscribe(this.player);
await entersState(this.connection, VoiceConnectionStatus.Ready, 20_000).catch(() => {});
}
sessionEnd() {
if (this.isSessionEnd) return;
this.isSessionEnd = true;
if (this.endTimer) clearTimeout(this.endTimer);
this.endTimer = null;
try { this.player.stop(true); } catch {};
try { this.connection.disconnect(); } catch {};
try { this.connection.destroy(); } catch {};
this.onSessionEnd(this.guild.id);
}
}