Discord wierd script
Helloo, i want to make this bot :
A bot who join my vocal, who records what I say and when I stop talking, he leaves and send me the mp3 file, after that, he joins again and speak this mp3
2 Replies
- What are your intents?
GuildVoiceStates
is required to receive voice data!
- Show what dependencies you are using -- generateDependencyReport()
is exported from @discordjs/voice
.
- Try looking at common examples: https://github.com/discordjs/voice-examples.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!my script looks like : it creates a .ogg file and after a .mp3 but but the mp3 file are only gressions
well
it's good
require('dotenv').config();
const { Client, GatewayIntentBits } = require('discord.js');
const { joinVoiceChannel } = require('@discordjs/voice');
const fs = require('fs');
const path = require('path');
const ffmpeg = require('fluent-ffmpeg');
const prism = require('prism-media');
const token = process.env.TOKEN;
const guildID = process.env.GUILD_ID;
const voiceChannelID = process.env.VOICE_CHANNEL_ID;
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates]
});
client.once('ready', async () => {
console.log(`â
Connecté en tant que ${client.user.tag}`);
const channel = client.channels.cache.get(voiceChannelID);
if (!channel) {
console.error("â Salon vocal introuvable !");
return;
}
const connection = joinVoiceChannel({
channelId: voiceChannelID,
guildId: guildID,
adapterCreator: channel.guild.voiceAdapterCreator,
selfDeaf: false,
});
console.log(`đ€ Rejoint le salon vocal: ${channel.name}`);
// On attend que quelquâun (autre que le bot) parle
const receiver = connection.receiver;
const onSpeaking = (userId) => {
if (userId === client.user.id) return; // Ignore le bot
console.log(`đïž Utilisateur ${userId} parle. DĂ©but de l'enregistrement de 5 secondes...`);
receiver.speaking.off('start', onSpeaking); // On ne réagit qu'une seule fois
recordAudioFor5Seconds(receiver, userId, connection);
};
receiver.speaking.on('start', onSpeaking);
});
function recordAudioFor5Seconds(receiver, userId, connection) {
// Prépare le dossier de sauvegarde
const recordingsDir = path.join(__dirname, 'recordings');
if (!fs.existsSync(recordingsDir)) {
fs.mkdirSync(recordingsDir);
}
// Chemins des fichiers temporaires
const oggFilePath = path.join(recordingsDir, `recording_${userId}_${Date.now()}.ogg`);
const mp3FilePath = oggFilePath.replace('.ogg', '.mp3');
// Créer un flux audio pour l'utilisateur
const opusStream = receiver.subscribe(userId, { end: { behavior: 'manual' } });
// Crée un encodeur Opus qui envoie les paquets audio vers un fichier OGG
const oggWriteStream = fs.createWriteStream(oggFilePath);
const opusEncoder = new prism.opus.Decoder({ rate: 48000, channels: 2, frameSize: 960 });
// Pipe l'Opus vers le fichier OGG
opusStream.pipe(opusEncoder).pipe(oggWriteStream);
// ArrĂȘte l'enregistrement aprĂšs 5 secondes
setTimeout(() => {
console.log("â±ïž 5 secondes Ă©coulĂ©es. ArrĂȘt de l'enregistrement...");
opusStream.destroy(); // ArrĂȘter le flux
oggWriteStream.end(); // Fin de l'écriture dans le fichier
// Lorsque l'écriture est terminée, lance la conversion en MP3
oggWriteStream.on('finish', () => {
console.log(`â
Enregistrement sauvegardé dans ${oggFilePath}`);
ffmpeg(oggFilePath)
.toFormat('mp3')
.on('end', () => {
console.log(`đ¶ Conversion terminĂ©e : ${mp3FilePath}`);
connection.destroy();
console.log("đ Le bot a quittĂ© le salon vocal.");
process.exit(0);
})
.on('error', (err) => {
console.error('â Erreur lors de la conversion :', err);
connection.destroy();
process.exit(1);
})
.save(mp3FilePath);
});
}, 5000);
}
client.login(token);
require('dotenv').config();
const { Client, GatewayIntentBits } = require('discord.js');
const { joinVoiceChannel } = require('@discordjs/voice');
const fs = require('fs');
const path = require('path');
const ffmpeg = require('fluent-ffmpeg');
const prism = require('prism-media');
const token = process.env.TOKEN;
const guildID = process.env.GUILD_ID;
const voiceChannelID = process.env.VOICE_CHANNEL_ID;
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates]
});
client.once('ready', async () => {
console.log(`â
Connecté en tant que ${client.user.tag}`);
const channel = client.channels.cache.get(voiceChannelID);
if (!channel) {
console.error("â Salon vocal introuvable !");
return;
}
const connection = joinVoiceChannel({
channelId: voiceChannelID,
guildId: guildID,
adapterCreator: channel.guild.voiceAdapterCreator,
selfDeaf: false,
});
console.log(`đ€ Rejoint le salon vocal: ${channel.name}`);
// On attend que quelquâun (autre que le bot) parle
const receiver = connection.receiver;
const onSpeaking = (userId) => {
if (userId === client.user.id) return; // Ignore le bot
console.log(`đïž Utilisateur ${userId} parle. DĂ©but de l'enregistrement de 5 secondes...`);
receiver.speaking.off('start', onSpeaking); // On ne réagit qu'une seule fois
recordAudioFor5Seconds(receiver, userId, connection);
};
receiver.speaking.on('start', onSpeaking);
});
function recordAudioFor5Seconds(receiver, userId, connection) {
// Prépare le dossier de sauvegarde
const recordingsDir = path.join(__dirname, 'recordings');
if (!fs.existsSync(recordingsDir)) {
fs.mkdirSync(recordingsDir);
}
// Chemins des fichiers temporaires
const oggFilePath = path.join(recordingsDir, `recording_${userId}_${Date.now()}.ogg`);
const mp3FilePath = oggFilePath.replace('.ogg', '.mp3');
// Créer un flux audio pour l'utilisateur
const opusStream = receiver.subscribe(userId, { end: { behavior: 'manual' } });
// Crée un encodeur Opus qui envoie les paquets audio vers un fichier OGG
const oggWriteStream = fs.createWriteStream(oggFilePath);
const opusEncoder = new prism.opus.Decoder({ rate: 48000, channels: 2, frameSize: 960 });
// Pipe l'Opus vers le fichier OGG
opusStream.pipe(opusEncoder).pipe(oggWriteStream);
// ArrĂȘte l'enregistrement aprĂšs 5 secondes
setTimeout(() => {
console.log("â±ïž 5 secondes Ă©coulĂ©es. ArrĂȘt de l'enregistrement...");
opusStream.destroy(); // ArrĂȘter le flux
oggWriteStream.end(); // Fin de l'écriture dans le fichier
// Lorsque l'écriture est terminée, lance la conversion en MP3
oggWriteStream.on('finish', () => {
console.log(`â
Enregistrement sauvegardé dans ${oggFilePath}`);
ffmpeg(oggFilePath)
.toFormat('mp3')
.on('end', () => {
console.log(`đ¶ Conversion terminĂ©e : ${mp3FilePath}`);
connection.destroy();
console.log("đ Le bot a quittĂ© le salon vocal.");
process.exit(0);
})
.on('error', (err) => {
console.error('â Erreur lors de la conversion :', err);
connection.destroy();
process.exit(1);
})
.save(mp3FilePath);
});
}, 5000);
}
client.login(token);