'i'm trying to make my discord bot to record audio using prism-media as said in the example of @discordjs/voice but i'm getting error

I'm making a discord bot that records the voice channel

the libraries [email protected], @discordjs/[email protected], [email protected]

my imports

const { createWriteStream } = require("node:fs")
const { pipeline } = require("node:stream")

const { Client, Intents } = require("discord.js");
const { joinVoiceChannel, getVoiceConnection, EndBehaviorType } = require("@discordjs/voice");
const prism = require('prism-media')

const { token } = require("./secrets.json");

the part of my code that errors

        const oggStream = new prism.opus.OggLogicalBitstream(
            {
                opusHead: new prism.opus.OpusHead({
                    channelCount: 2,
                    sampleRate: 48000,
                }),
                pageSizeControl: {
                    maxPackets: 10,
                },
            }
        );

This is identical to what is shown on the github page of @discordjs/voice https://github.com/discordjs/voice/blob/309ac8596cac422cf22e51331869e011c720124c/examples/recorder/src/createListeningStream.ts#L19

the error

C:\Users\user\OneDrive\Documents\source\discord_bot\index.js:60
                opusHead: new prism.opus.OpusHead({
                          ^

TypeError: prism.opus.OpusHead is not a constructor
    at Client.<anonymous> (C:\Users\user\OneDrive\Documents\source\discord_bot\index.js:60:27)
    at Client.emit (node:events:390:28)
    at VoiceStateUpdate.handle (C:\Users\user\OneDrive\Documents\source\discord_bot\node_modules\discord.js\src\client\actions\VoiceStateUpdate.js:38:14)
    at Object.module.exports [as VOICE_STATE_UPDATE] (C:\Users\user\OneDrive\Documents\source\discord_bot\node_modules\discord.js\src\client\websocket\handlers\VOICE_STATE_UPDATE.js:4:35)
    at WebSocketManager.handlePacket (C:\Users\user\OneDrive\Documents\source\discord_bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:351:31)
    at WebSocketShard.onPacket (C:\Users\user\OneDrive\Documents\source\discord_bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\user\OneDrive\Documents\source\discord_bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\user\OneDrive\Documents\source\discord_bot\node_modules\ws\lib\event-target.js:199:18)
    at WebSocket.emit (node:events:390:28)
    at Receiver.receiverOnMessage (C:\Users\user\OneDrive\Documents\source\discord_bot\node_modules\ws\lib\websocket.js:1137:20)


Solution 1:[1]

The issue is with your imports:

const { createWriteStream } = require("node:fs") 
const { pipeline } = require("node:stream") 
const { Client, Intents } = require("discord.js"); 
const { joinVoiceChannel, getVoiceConnection, EndBehaviorType } = require("@discordjs/voice"); 
const { opus } = require("prism-media");  // THIS IS THE WRONG BIT
const { token } = require("./secrets.json"); 

You are importing opus from prism-media. You don't have a prism variable. Yet you call it here:

const oggStream = new prism.opus.OggLogicalBitstream( //WRONG
    {
        opusHead: new prism.opus.OpusHead({ //WRONG
            channelCount: 2,
            sampleRate: 48000,
        }),
        pageSizeControl: {
            maxPackets: 10,
        },
        crc: false
    }
);

You didn't import prism though, just prism.opus. Change your code to:

const oggStream = new opus.OggLogicalBitstream(
    {
        opusHead: new opus.OpusHead({
            channelCount: 2,
            sampleRate: 48000,
        }),
        pageSizeControl: {
            maxPackets: 10,
        },
        crc: false
    }
);

Or you can change the import, but that may break other existing code.

const prism = require("prism-media");          // THIS INSTEAD OF
const { opus } = require("prism-media");  // THIS

Solution 2:[2]

Use [email protected].

For this:

npm i [email protected]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Akif9748