Skip to content

Commit

Permalink
fix: 再接続の制御を除去 (#959)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuroXina authored Aug 6, 2023
1 parent f053514 commit 9ebe81d
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 52 deletions.
50 changes: 5 additions & 45 deletions src/adaptor/discord/voice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
VoiceConnectionStatus,
createAudioPlayer,
createAudioResource,
entersState,
joinVoiceChannel
} from '@discordjs/voice';
import {
Expand Down Expand Up @@ -121,55 +120,16 @@ export class DiscordVoiceConnection<K extends string | number | symbol>
this.connection = null;
}

onDisconnected(shouldReconnect: () => boolean): void {
onDisconnected(handler: () => void): void {
if (!this.connection) {
throw new Error(
'You must invoke `connect` before to register disconnection handler'
);
}
this.connection.on(
VoiceConnectionStatus.Disconnected,
() => void this.makeDisconnectionHandler(shouldReconnect)
);
}
private makeDisconnectionHandler(shouldReconnect: () => boolean) {
return async () => {
if (!this.connection) {
return;
}
try {
await Promise.race([
entersState(
this.connection,
VoiceConnectionStatus.Signalling,
TIMEOUT_MS
),
entersState(
this.connection,
VoiceConnectionStatus.Connecting,
TIMEOUT_MS
)
]);
// 再接続に成功。
return;
} catch (error) {
console.error(error);
this.destroy();
}
if (!shouldReconnect()) {
return;
}
try {
const newConn = new DiscordVoiceConnection(
this.channel,
this.audioRecord
);
this.connection = newConn.connection;
this.player = newConn.player;
} catch (error) {
console.error(error);
}
};
this.connection.on(VoiceConnectionStatus.Disconnected, () => {
handler();
this.destroy();
});
}
}

Expand Down
1 change: 0 additions & 1 deletion src/service/command/gyokuon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export class GyokuonCommand implements CommandResponder<typeof SCHEMA> {
connectionVC.connect();
connectionVC.onDisconnected(() => {
this.doingGyokuon = false;
return false;
});

if (isShort) {
Expand Down
1 change: 0 additions & 1 deletion src/service/command/kaere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ export class KaereCommand implements CommandResponder<typeof SCHEMA> {
connection.connect();
connection.onDisconnected(() => {
this.doingKaere = false;
return false;
});
await this.deps.stdout.sendEmbed({
title: '提督、もうこんな時間だよ',
Expand Down
1 change: 0 additions & 1 deletion src/service/command/party.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ export class PartyCommand implements CommandResponder<typeof SCHEMA> {
this.connection.connect();
this.connection.onDisconnected(() => {
this.connection = null;
return false;
});
await message.reply(partyStarting);
await this.connection.playToEnd(this.generateNextKey());
Expand Down
6 changes: 2 additions & 4 deletions src/service/voice-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ export interface VoiceConnection<K> {
unpause(): void;

/**
* 回復できない接続解除が発生した時に、同じチャンネルへ再接続するかどうかのハンドラを登録する。ボイスチャンネルが削除されたなど、必ず再接続できないこともある。
*
* @param shouldReconnect - この関数が `true` を返す場合は、回復できない接続解除でも同じチャンネルへの再接続を試みる。
* 接続解除が発生した時のハンドラを登録する。
*/
onDisconnected(shouldReconnect: () => boolean): void;
onDisconnected(handler: () => void): void;
}

0 comments on commit 9ebe81d

Please sign in to comment.