Home > Enterprise >  DiscordJSv14 Recording / Receiving Voice Audio
DiscordJSv14 Recording / Receiving Voice Audio

Time:12-03

So I'm working on a Discord bot using JS and DiscordJSv14, and want the bot to use audio from a voice chat to send to another bot, some type of listen and snitch bot.

So far I got the Bot's connecting to the voice call but can't get any of the packets to send to the other bot.

Here's some code I've got so far:

const connection = joinVoiceChannel({
    channelId: C.id,
    guildId: guild.id,
    adapterCreator: guild.voiceAdapterCreator,
    selfDeaf: false,
    selfMute: false,
    group: this.client.user.id,
});

if (isListener) {
    console.log("Listener Is Joining Voice And Listening...");

    const encoder = new OpusEncoder(48000, 2);

    let subscription = connection.receiver.subscribe(ID); // ID = Bot's ID
    // Basically client.user.id

    subscription.on("data", (chunk) => {
        console.log(encoder.decode(chunk));
    });
}

Console won't log anything about the chunk Using DiscordJSv14 @discordjs/opus

CodePudding user response:

To receive audio data from a voice channel in DiscordJS v14, you can use the VoiceReceiver class to subscribe to audio events and receive audio data as Buffer objects. The VoiceReceiver class provides an on method that allows you to register a callback function to be executed whenever audio data is received.

To receive audio data, you can use the subscribe method on the VoiceReceiver instance to register a listener for audio events. This method returns a VoiceData object that emits "data" events whenever audio data is received. You can then use the on method on this object to register a callback function that will be executed whenever audio data is received.

Here is an example of how you could use the VoiceReceiver class to receive audio data from a voice channel in DiscordJS v14:

// Get the voice receiver for the voice channel
const voiceReceiver = connection.receiver;

// Subscribe to audio events from the voice receiver
const subscription = voiceReceiver.subscribe(client.user.id);

// Register a callback to receive audio data
subscription.on("data", (chunk) => {
// Log the received audio data
console.log(chunk);
});

In this example, the subscribe method is used to register a listener for audio events from the VoiceReceiver instance. The on method is then used to register a callback function that will be executed whenever audio data is received. This callback function simply logs the received audio data to the console.

Note that the audio data received by this callback function will be in the form of a Buffer object, which is a binary data type that represents raw data in Node.js. You can use the Buffer class to manipulate and process this data as needed.

CodePudding user response:

Everything worked fine I just needed to add the following code below.

this.client = new Client({
      intents: [GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildMessages, GatewayIntentBits.Guilds],
});
  • Related