gpt4 book ai didi

javascript - 使用 Discord bot 接收来自用户的音频

转载 作者:行者123 更新时间:2023-12-04 08:30:52 27 4
gpt4 key购买 nike

我正在做一个不和谐的项目,在那个项目中我需要录制用户的声音,我正在关注 this文档。
到目前为止,这是我写的:

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 文件总是空的!

最佳答案

这是discord.js中的一个bug,为了解决这个问题,我们需要播放一段音频...

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('NzMwMDYyNTk5OTc0MTU4MzQ3.XwSBxw.7ljjhLtLE4Xbw9f40yTwVm2Ml8w');

关于javascript - 使用 Discord bot 接收来自用户的音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65026152/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com