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

fix: 再接続の制御を除去 #959

Merged
merged 4 commits into from
Aug 6, 2023
Merged
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
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 @@
VoiceConnectionStatus,
createAudioPlayer,
createAudioResource,
entersState,
joinVoiceChannel
} from '@discordjs/voice';
import {
Expand Down Expand Up @@ -121,55 +120,16 @@
this.connection = null;
}

onDisconnected(shouldReconnect: () => boolean): void {
onDisconnected(handler: () => void): void {

Check warning on line 123 in src/adaptor/discord/voice.ts

View check run for this annotation

Codecov / codecov/patch

src/adaptor/discord/voice.ts#L123

Added line #L123 was not covered by tests
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();
});

Check warning on line 132 in src/adaptor/discord/voice.ts

View check run for this annotation

Codecov / codecov/patch

src/adaptor/discord/voice.ts#L129-L132

Added lines #L129 - L132 were not covered by tests
}
}

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;
}