Created
January 24, 2021 01:48
-
-
Save azechi/a496e7ee51fb11e730eb9eb8c781cac1 to your computer and use it in GitHub Desktop.
discord.js, voice channel, streaming, raw PCM, child process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const BOT_TOKEN = process.env.DISCORD_BOT_TOKEN; | |
const CHANNEL_ID = process.env.VOICE_CHANNEL_ID; | |
const { spawn } = require('child_process'); | |
const Discord = require('discord.js'); | |
const prism = require('prism-media'); | |
const client = new Discord.Client(); | |
client.on('ready', async () => { | |
console.log(`Logged in as ${client.user.tag}`); | |
const ch = await client.channels.fetch(CHANNEL_ID); | |
const con = await ch.join(); | |
const encoder = new prism.opus.Encoder({ | |
rate: 48000, channels: 2, frameSize: 960 | |
}) | |
const cmd = spawn('node', ['raw_pcm_producer.js']); | |
const p = cmd.stdout.pipe(encoder); | |
const dispatcher = con.play(p, {type: 'opus'}) | |
dispatcher.on('start', () => console.log('voice is now playing!')); | |
dispatcher.on('finish', () => console.log('voice has finished playing!')); | |
dispatcher.on('error', console.error); | |
}); | |
process.on('SIGINT', ()=> { | |
client.destroy(); | |
process.exit(); | |
}); | |
client.login(BOT_TOKEN); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "voice", | |
"version": "1.0.0", | |
"description": "", | |
"main": "client.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"dependencies": { | |
"@discordjs/opus": "^0.3.3", | |
"discord.js": "^12.5.1" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const prism = require('prism-media'); | |
fs | |
.createReadStream('./audio.ogg') | |
.pipe(new prism.opus.OggDemuxer()) | |
.pipe(new prism.opus.Decoder({ rate: 48000, channels: 2, frameSize: 960 })) | |
.pipe(process.stdout); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment