six
six
DIAdiscord.js - Imagine an app
Created by six on 3/10/2024 in #djs-voice
multiple event listeners
so this is the code i have
async create({
channel,
member,
}: {
channel: discord.VoiceChannel;
member: discord.GuildMember;
}) {
// first join the channel.
const connection: VoiceConnection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
selfDeaf: false,
});

// get the receiver.
const receiver: VoiceReceiver = connection.receiver;
// start listener loop
const opusStream = receiver.subscribe(member.user.id, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 1500,
},
});

// initiate the buffer chunks.
let buffers: Uint8Array[] = [];

// start listening on the decoder now.
opusStream
.pipe(
new prism.opus.Decoder({
frameSize: 960,
channels: 2,
rate: 48000,
})
)
.on("data", (chunk: Uint8Array) => {
buffers.push(chunk);
});

// Now finally when the data stream ends, run STT.
opusStream.on("end", async () => {
// Create specialID for this recording.
const id = this.client.random.id();
// Using concat create the full buffer.
const buffer = Buffer.concat(buffers);
buffers = [];

// Write to file
await fs.promises.writeFile(`./audio/${id}.pcm`, buffer);
pcmToWav(`./audio/${id}.pcm`, `./audio/${id}.wav`, 48000, 2);

console.log(id);
});
}
async create({
channel,
member,
}: {
channel: discord.VoiceChannel;
member: discord.GuildMember;
}) {
// first join the channel.
const connection: VoiceConnection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
selfDeaf: false,
});

// get the receiver.
const receiver: VoiceReceiver = connection.receiver;
// start listener loop
const opusStream = receiver.subscribe(member.user.id, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 1500,
},
});

// initiate the buffer chunks.
let buffers: Uint8Array[] = [];

// start listening on the decoder now.
opusStream
.pipe(
new prism.opus.Decoder({
frameSize: 960,
channels: 2,
rate: 48000,
})
)
.on("data", (chunk: Uint8Array) => {
buffers.push(chunk);
});

// Now finally when the data stream ends, run STT.
opusStream.on("end", async () => {
// Create specialID for this recording.
const id = this.client.random.id();
// Using concat create the full buffer.
const buffer = Buffer.concat(buffers);
buffers = [];

// Write to file
await fs.promises.writeFile(`./audio/${id}.pcm`, buffer);
pcmToWav(`./audio/${id}.pcm`, `./audio/${id}.wav`, 48000, 2);

console.log(id);
});
}
for some reason the end listener count is 2?, and it seems to increase every time i speak?
27 replies
DIAdiscord.js - Imagine an app
Created by six on 2/20/2024 in #djs-voice
Opus stream
are there any easy ways to convert the opus stream you receive from discord into wav buffer?
6 replies
DIAdiscord.js - Imagine an app
Created by six on 1/2/2024 in #djs-voice
forcefully stopping player
hey i just wanted to know if theres a way to forcefully stop playing a resource for example
player.play(createAudioResource(audioPath));
player.play(createAudioResource(audioPath));
6 replies
DIAdiscord.js - Imagine an app
Created by six on 10/26/2023 in #djs-voice
Help with audio stream.
Im trying to make a bot which would capture the incoming audio from a voice channel and play it back in realtime, how can i do this?, i tried doing this but it didnt work
receiver.speaking.on("start", (userId) => {
const opusStream = receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 1000,
},
});

const resource = createAudioResource(
createReadStream(opusStream, {
inputType: StreamType.OggOpus,
})
);

const player = createAudioPlayer();
player.play(resource);
connection.subscribe(player);
});
receiver.speaking.on("start", (userId) => {
const opusStream = receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 1000,
},
});

const resource = createAudioResource(
createReadStream(opusStream, {
inputType: StreamType.OggOpus,
})
);

const player = createAudioPlayer();
player.play(resource);
connection.subscribe(player);
});
31 replies
DIAdiscord.js - Imagine an app
Created by six on 10/26/2023 in #djs-voice
doubt about voice status
whenever i create a connection with joinVoiceChannel why is the stauts sometimes "signalling" and sometimes "disconnected"
5 replies
DIAdiscord.js - Imagine an app
Created by six on 10/26/2023 in #djs-voice
doubts about joinVoiceChannel
hey so i have a server which hosts multiple bots at the same time and if i want to specifically use joinVoiceChannel function on one of the bots which is running how can i do that?, is there any way to specify the client object which i want the function to use
2 replies