Jinan Dangor
Jinan Dangor

Reputation: 142

Discord Bot - Cross-Channel Communication

Recently, to facilitate playing games, I wanted to see if I could make a Discord Bot that would allow all of the audio from one channel to be transmitted to another. The idea was that you'd have a kind of 'audience' channel for people to chat in while listening to the 'performance' in another channel, with everyone being able to hear the performance while the audience can converse amongst themselves.

If I'm interpreting this page of the Discord.js API Documentation correctly, this should work. The 'Advanced Usage' section seems to literally provide a code snippet for what I want to do.

I can get the bot to record audio to my computer, and play audio from my computer, but streaming voice from one source to another (even within the same channel) is appearing impossible to me.

My code is below. Is there any way to get my Discord bot to transmit one or more user voices from one channel to another?

const Discord = require('discord.js');
const client = new Discord.Client();
const {prefix, token, help, transmit, recieve, end} = require('./config.json');

client.once('ready', () => {
    console.log('Ready!');
});

let transmitting = false;
let t_channel;
let recieving = false;
let r_channel;
let t_connection;
let r_connection;
let sender;
let audio;

client.on('message', message => {
    // Take command input
    if (!message.content.startsWith(prefix) || message.author.bot) return;
    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();
    
    switch (command) {
        case `${help}`:
            message.channel.send(`Commands: ${transmit}, ${recieve}, ${end}`);
            break
        case `${transmit}`:
            message.channel.send('Transmitting');
            transmitting = true;
            t_channel = message.member.voice.channel;
            sender = message.author;
            break;
        case `${recieve}`:
            message.channel.send('Receiving');
            recieving = true;
            r_channel = message.member.voice.channel;
            break;
        case `${end}`:
            message.channel.send('Ending');
            transmitting = false;
            recieving = false;
            end_broadcast();
            break;
    }

    if (transmitting && recieving) {
        broadcast();
    }
});

async function broadcast() {
    t_connection = await t_channel.join();
    r_connection = await r_channel.join();
    audio = t_connection.receiver.createStream(sender);
    r_connection.play(audio, { type: 'opus' });
    console.log("Working!");
}

async function end_broadcast() {
    audio.destroy();
    r_channel.leave();
    t_connection.disconnect;
    r_connection.disconnect;
}

client.login(token);

Upvotes: 2

Views: 6552

Answers (2)

rowisacode
rowisacode

Reputation: 11

I'm still working on my code for this for a podcast type bot, right now it can send only one users voice at a time. So if a music bot was playing, and i talk over it, it would completely override the bot one with mine, and when im done talking it stays on me as the bots voice activity hasn't changed. I'll put this into a github repository. If anyone finds out how to successfully merge 2 or more audio stream please put it into a issue and tell me now and what i need to change.

And for your case, you just need to add another 'receiver' and 'send', also make sure under the .on('speaking', you put if(user.id===<client/bot>.user.id) return so it doesn't pick up it's self

Upvotes: 0

ApatheticEnthusiasts
ApatheticEnthusiasts

Reputation: 51

By design Discord only allows each account to be on one voice channel at a time.

I just started researching this, but I came across this: Q: Can I be in two discord voice channels at once? A: Yes, you can run multiple accounts. These are legitimate instances of discord that each handle their own auth token, so you can open as many clients as you want and run an account per-client, meaning you can sign in to as many accounts as you want.

I think you could accomplish this by running two separate bots, with separate accounts. Basically Bot A listens and records on voice channel 1, then Bot B plays the recording on voice channel 2. It's basically a producer (Bot A) and consumer (Bot B) model.

I would be interested in contributing, as I would like to attempt this for my Among Us server!

Upvotes: 5

Related Questions