Skip to content

Commit

Permalink
feat(bot): handle voice resuming
Browse files Browse the repository at this point in the history
  • Loading branch information
Snazzah committed Oct 3, 2024
1 parent 8245572 commit 2b8c292
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions apps/bot/src/modules/recorder/recording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ export default class Recording {
if (!alreadyConnected) {
this.connection?.removeAllListeners('connect');
this.connection?.removeAllListeners('disconnect');
this.connection?.removeAllListeners('resumed');
this.connection?.removeAllListeners('error');
this.connection?.removeAllListeners('warn');
this.connection?.removeAllListeners('debug');
Expand All @@ -478,11 +479,12 @@ export default class Recording {
connection.on('ready', this.onConnectionReady.bind(this));
connection.on('connect', this.onConnectionConnect.bind(this));
connection.on('disconnect', this.onConnectionDisconnect.bind(this));
connection.on('error', (err) => {
connection.on('resumed', this.onConnectionResumed.bind(this));
connection.on('error', (err: any) => {
this.writeToLog(`Error: ${err}`, 'connection');
this.recorder.logger.error(`Error in connection for recording ${this.id}`, err);
});
connection.on('warn', (m) => {
connection.on('warn', (m: string) => {
this.writeToLog(`Warning: ${m}`, 'connection');
this.recorder.logger.debug(`Warning in connection for recording ${this.id}`, m);
});
Expand Down Expand Up @@ -571,11 +573,17 @@ export default class Recording {

async onConnectionReady() {
if (!this.active) return;
this.writeToLog(`Voice connection ready (state=${this.connection?.ws?.readyState})`, 'connection');
this.recorder.logger.debug(`Recording ${this.id} ready`);
this.writeToLog(`Voice connection ready (state=${this.connection?.ws?.readyState}, mode=${this.connection?.mode})`, 'connection');
this.recorder.logger.debug(`Recording ${this.id} ready (mode=${this.connection?.mode})`);
this.pushToActivity('Automatically reconnected.');
}

async onConnectionResumed() {
if (!this.active) return;
this.writeToLog(`Voice connection resumed (seq=${this.connection?.wsSequence})`, 'connection');
this.recorder.logger.debug(`Recording ${this.id} resumed`);
}

async onConnectionDisconnect(err?: Error) {
if (!this.active) return;
this.writeToLog(`Got disconnected, ${err}`);
Expand Down Expand Up @@ -667,7 +675,6 @@ export default class Recording {

async onData(data: Buffer, userID: string, timestamp: number) {
if (!this.active) return;
data = Buffer.from(data);
if (!userID) return;

let recordingUser = this.users[userID];
Expand Down Expand Up @@ -820,6 +827,7 @@ export default class Recording {
}

const startedTimestamp = this.startedAt ? Math.floor(this.startedAt.valueOf() / 1000) : null;
const voiceRegion = this.connection?.endpoint?.hostname;
return {
content: '',
embeds: [
Expand Down

0 comments on commit 2b8c292

Please sign in to comment.