Relaying audio from one Discord voice channel to another

Viewed 343

What I want to achieve:

(This is for private server/purpose, all parties are aware and okay with being recorded and the bots/project won't need to be scaled)

2 Voice channels A and B. Audio from A should be transmitted to B. So as a result B can hear A, but can't really communicate besides that.

My approach:

Single project with 2 discord bot clients, one is recording VC A, another is playing/streaming back that recording in VC B.

I'm not familiar with discord.js, but I was able to achieve recording and playing audio separately, but not exactly sure how to combine the two. Most/all samples I've found for playing audio just plays local or remote files, nothing really about streaming live audio. Can I just somehow pass the OggLogicalBitstream to the playback bot, or is there something more involved? Also would I need to do anything manually for memory management? As in, does recording just put everything into memory until it runs out, or does it just store a short rolling buffer of audio in memory?

Code samples for what I achieved so far (Mostly from discordjs/voice samples):

Creating listening stream

export function createListeningStream(receiver: VoiceReceiver, userId: string, user?: User) : prism.opus.OggLogicalBitstream {
const opusStream = receiver.subscribe(userId, {
    end: {
        behavior: EndBehaviorType.AfterSilence,
        duration: 100,
    },
});

const oggStream = new prism.opus.OggLogicalBitstream({
    opusHead: new prism.opus.OpusHead({
        channelCount: 2,
        sampleRate: 48000,
    }),
    pageSizeControl: {
        maxPackets: 10,
    },
});

return oggStream;
}

Playing audio from remote file:

playSong() {
    const resource = createAudioResource('https://urlToSome/file.mp3', {
        inputType: StreamType.Arbitrary,
    });
    this.player.play(resource);
    return entersState(this.player, AudioPlayerStatus.Playing, 5e3);
}
0 Answers
Related