使用 Discord 机器人接收用户的音频

我正在开发一个不和谐项目,在该项目中我需要录制用户声音,我正在关注此文档。


到目前为止,这就是我写的:


const fs = require('fs');

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

const client = new Discord.Client();


client.once('ready', () => {

    console.log('Ready!');

});


client.on('message', async message => {

    if (message.content === 'a' && message.member.voice.channel) {

        const connection = await message.member.voice.channel.join();

        const audio = connection.receiver.createStream('user_id?', { mode: 'pcm' });

        audio.pipe(fs.createWriteStream('user_audio'));

    }

});


client.login('token');

但问题是 user_audio 文件总是空的!


慕虎7371278
浏览 130回答 1
1回答

Qyouu

这是discord.js 中的一个错误,为了解决这个问题,我们需要播放音频......const fs = require('fs');const Discord = require('discord.js');const client = new Discord.Client();const { Readable } = require('stream');const SILENCE_FRAME = Buffer.from([0xF8, 0xFF, 0xFE]);class Silence extends Readable {  _read() {    this.push(SILENCE_FRAME);    this.destroy();  }}client.once('ready', () => {    console.log('Ready!');});client.on('message', async message => {    if (message.content === 's' && message.member.voice.channel) {        const connection = await message.member.voice.channel.join();        const audio = connection.receiver.createStream(message, { mode: 'pcm', end: 'manual' });        audio.pipe(fs.createWriteStream('user_audio'));        connection.play(new Silence(), { type: 'opus' });        console.log(message.member.user.id);    }});client.login('token');
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript