chandywerks.dev

Audio Capture Discord Bot on a Pi

Raspberry Pi

A rasberry pi and a USB audio capture device mounted next to a TV on the wall

In our computer room we have a TV but you can't hear it with headphones on. With a Raspberry Pi and a Griffin iMic USB audio adapter I'm able to capture stereo audio and stream it into Discord.

Capture and stream the audio with mic and discord.js

const mic = require('mic');
const Discord = require('discord.js');

const config = {
mic: {
rate: 48000,
channels: 2,
device: "plughw:CARD=system,DEV=0"
},
discord: {
token: "<bot-token>",
guild: "<server-name>",
channel: "<channel-name>"
}
}

const client = new Discord.Client();
client.login(config.discord.token)
.catch( () => {
console.log('Unable to log in with provided token.');
process.exit();
});

// Start audio capture
let micInstance = mic(config.mic);
let audioStream = micInstance.getAudioStream();

client.on('ready', () => {
// Filter channels down to voice channels in the guild
let channels = client.channels.filter( channel => {
return channel.guild.name === config.discord.guild && channel.type === 'voice';
});

// Find channel by name
let channel = channels.find('name', config.discord.channel);

if(!channel) {
console.log(`Unable to find voice channel ${config.discord.channel} in ${config.discord.guild}`);
process.exit();
}

// Connect to the channel
channel.join()
.then( connection => {
console.log('Connected...');

// Start the audio stream
connection.playConvertedStream( audioStream );
micInstance.start();
})
.catch( error => {
console.log('Unable to connect to the voice channel', error);
process.exit();
});
});