how ask browser if audio device allowed and then if video allowed? - audio

I got some fails on the way to solve it.
So I think it's worth to share my solution.
https://stackblitz.com/edit/typescript-wfvqpl

try {
await navigator.mediaDevices.getUserMedia({
audio:true,
});
log('audio is permited');
try {
let stream = await navigator.mediaDevices.getUserMedia({
audio:true,
video:true
});
log('video and audio is permited');
} catch (e) {
log('video is not permited');
}
} catch (e) {
let videoConstraint = false;
}

Related

node ffmpeg module stuck more than one file

when I read more than one file it will be stuck and also hang my pc. I need to restart my pc
on one file or 5 files it will work perfectly but not more than 5 files
if anyone know this issue let me know
const ffmpegPath = require('#ffmpeg-installer/ffmpeg').path
const ffmpeg = require('fluent-ffmpeg')
ffmpeg.setFfmpegPath(ffmpegPath)
const testFolder = './videos/';
const fs = require('fs');
fs.readdir(testFolder, async(err, files) => {
try {
for(let i = 0; i < 10; i++){
if(files[i] != '1 Surah Fatiha Dr Israr Ahmed Urdu - 81of81.mp4'){
let converter = await ffmpeg(`./videos/${files[i]}`)
await converter.setStartTime('00:00:00').setDuration('30').output(`./outputfolder/${files[i]}`).on('end', function(err) {
if(err) {
console.log(`err durinng conversation \n ${err}`)
}
else{
console.log(`Done ${files[i]}`);
}
}).on('error', function(err){
console.log(`error: ${files[i]}`, err)
}).run()
}
}
} catch (error) {
console.log(error)
}
});
Your computer is crashing because fluent-ffmpeg is not asynchronous, this makes your code run in a loop running ffmpeg several times without waiting for the previous video to complete processing, consequently consuming all your processing power.
I created an asynchronous function called renderVideo and changed the for loop to the for of loop, as only it is able to wait for an asynchronous function.
The code looked like this:
const ffmpegPath = require('#ffmpeg-installer/ffmpeg').path
const ffmpeg = require('fluent-ffmpeg')
ffmpeg.setFfmpegPath(ffmpegPath)
const fs = require('fs');
const testFolder = './videos/';
fs.readdir(testFolder, async(err, videos) => {
try {
for(let video of videos){
if(video != '1 Surah Fatiha Dr Israr Ahmed Urdu - 81of81.mp4'){
await renderVideo(video)
}
}
} catch (error) {
console.log(error)
}
function renderVideo(video){
return new Promise((resolve, reject)=>{
let converter = ffmpeg(`./videos/${video}`)
converter
.setStartTime('00:00:00')
.setDuration('30')
.output(`./outputfolder/${video}`)
.on('end', (done)=> {
resolve(done)
})
.on('error', (err)=> {
reject(`The video ${video} return with error: ${err}`)
})
.run()
})
}
})
I also changed the names of some variables to make sense in the current code.

Catching NodeJS errors using a discord.js command handler

I'm creating a Discord bot, but have some problems catching the errors that my bot sends.
I'm using a custom command handler, that is working pretty well.
Fs.readFile(`./database/prefixes/prefixes.json`, "utf8", (err: Error, data): void => {
data = JSON.parse(data);
const prefix: string = data[message.author.id] == undefined ? "ma!" : data[message.author.id];
const msg: string = message.content;
const args: string[] = message.content.slice(prefix.length).trim().split(" ");
const cmd: string = args.shift().toLowerCase();
if (!msg.startsWith(prefix)) {
return checkCustomCommands();
}
let ops: {} = {
queue: queue
}
try {
require(checkFolders(cmd)).run(Client, message, args, ops); // maybe catching just there?
Logger.log(`${message.author.tag} just used the ${cmd} power in ${message.guild.name}.`);
} catch (err) {
Logger.log(`The command ${message.author.tag} tried to call in ${message.guild.name} doesen't seem to exist.`);
}
});
function checkCustomCommands() {
let content = JSON.parse(Fs.readFileSync('./database/commands/commands.json', 'utf8'));
try {
if (content[message.guild.id][message.content] == undefined) {
return;
} else {
message.channel.send(content[message.guild.id][message.content]);
}
} catch (err) {
return;
}
}
function checkFolders(command) {
let folders = ["moderation", "fun", "music", "info", "game"];
var files: string[];
var finalPath: string;
folders.forEach(folder => {
files = Fs.readdirSync(`./src/commands/${folder}`);
files.forEach(file => {
if (file.split(".")[0] == command) {
return finalPath = `./../commands/${folder}/${file.split(".")[0]}.js`;
}
});
});
return finalPath;
}
But sometimes, my bot sends various errors that I can't catch into the commands files, resulting in my bot being shutdown. Is there any way to prevent that inside the command handler, or anything I can do to catch these errors?
Thanks a lot, I can provide more code if needed, even though the biggest chunk of the command handler is just there ^
I'm quite late to this, but I feel like I should answer my own question, now that I'm able to do so haha
The main way to catch these errors is by listening to the unhandledRejection event, via Node process.
Here's how I did it:
async function handleRejections() {
process.on("unhandledRejection", (error: Error) => {
const errorEmbed: Discord.MessageEmbed = new Discord.MessageEmbed()
.setDescription("<:no:835565213322575963> An error has been detected... \n" + `\`\`\`${error.stack}\`\`\``)
.setTimestamp()
.setFooter(client.user.username, client.user.displayAvatarURL())
.setColor("DARK_RED")
logError(client, errorEmbed);
});
}
Be sure to call the function at the very beginning of the project code, even before the bot starts.
I personally log these errors into a Discord channel, on my server, so that I can check them easily. That's it! Hope that helps. Keep on coding :D

How to make discord bot say a string/phrase in a voice channel. (Like a text-to-speech thing)

Someone I know told me I should use some sort of npm package that sends streams then use VoiceConnection.playStream but told me he doesn't know any packages that do so.
I am using discord.js.
You can use say to generate the audio file from a text, export it, and then play the sound in a voice channel.
const say = require('say');
...
function tts(voiceChannel, text) {
if (!FS.existsSync('./temp')){
FS.mkdirSync('./temp');
}
const timestamp = new Date().getTime();
const soundPath = `./temp/${timestamp}.wav`;
say.export(text, null, 1, soundPath, (err) => {
if (err) {
console.error(err);
return;
}else{
voiceChannel.join().then((connection) => {
connection.playFile(soundPath).on('end', () => {
connection.disconnect();
FS.unlinkSync(soundPath);
}).on('error', (err) => {
console.error(err);
connection.disconnect();
FS.unlinkSync(soundPath);
});
}).catch((err) => {
console.error(err);
});
}
});
}
Unfortunately say.export() doesn't work on Linux.
Not exactly sure if you mean TTS or using Voice Channel directly :(
But if you meant TTS:
message.channel.send("Meow! I'm a baby loli kitten!", {
tts: true
})
If you need a package, you could try text-to-speech-js, hope I helped in any way~ meow!

How to fix "Stream is not generating quickly enough" error when the first song stream finishes?

I was making a discord.js bot and stumbled upon a problem with playing streams from youtube.
After the first stream finished playing dispatcher errors with "Stream is not generating quickly enough" and won't play any other streams until I restart the bot.
I'm using these modules:
discord.js#11.4.0
ffmpeg#0.0.4
ffmped-binaries#3.2.2-3
opusscript#0.0.6
ytdl-core#0.25.0
I tried installing other versions of ytdl-core but that didn't help me.
Here's my code so far:
const yt = require("ytdl-core");
function play(bot, msg) {
if (msg.guild.queue.songs.length < 1) {
msg.guild.queue.playing = false;
return msg.channel.send("Queue is empty");
}
if (!msg.guild.voiceConnection) {
msg.member.voiceChannel.join().then(con => {
let song = msg.guild.queue.songs.shift();
msg.channel.send(`Playing: **${song.title}**!`);
msg.guild.queue.playing = true;
msg.guild.queue.dispatcher = con.playStream(yt(song.url))
.on("end", reason => {
console.log(reason);
bot.queue[msg.guild.id].dispatcher.stop();
setTimeout(play, 500, bot, msg);
})
.on("error", err => {
console.log(err);
bot.queue[msg.guild.id].dispatcher = null;
setTimeout(play, 500, bot, msg);
});
});
}
}
exports.run = async(bot, msg, args, ops) => {
if (!msg.member.voiceChannel) return msg.channel.send("Connect to a voice channel first!");
if (!args[0]) return msg.channel.send("Specify youtube url first!");
yt.getInfo(args[0], (err, info) => {
if (err) return msg.channel.send(err);
if (!msg.guild.queue) {
msg.guild.queue = {};
msg.guild.queue.playing = false;
msg.guild.queue.songs = [];
msg.guild.queue.dispatcher = null;
}
msg.guild.queue.songs.push({
url: info.video_url,
title: info.title,
requester: msg.author.username
});
if (msg.guild.queue.playing) {
msg.channel.send(`Added **${info.title}** to queue list!`);
} else {
play(bot, msg);
}
});
}
I guess easiest fix would be to use discord.js master / v12-dev wich can be installed by doing npm i discordjs/discord.js the master version has a full rewrite of the Voice Functions, wich of course has some breaking changes with it, but overall the issue with stream is not generating fast enough from ytdl is fixed in master.

Bot receiver listen only to itself

i have problem with createReceiver. I have code:
client.on('message', message => {
// Voice only works in guilds, if the message does not come from a guild,
// we ignore it
if (!message.guild) return;
if (message.content === '/q') {
// Only try to join the sender's voice channel if they are in one themselves
const voiceChannel = message.member.voiceChannel;
if (message.member.voiceChannel) {
message.member.voiceChannel.join()
.then(connection => { // Connection is an instance of VoiceConnection
connection.on('error', console.error);
// const dispatcher = connection.playFile('C:/Denwer/ddradio/lyubov.mp3');
connection.on('speaking', (user, speaking) => {
if(speaking) {
const receiver = connection.createReceiver();
const stream = receiver.createPCMStream(user);
stream.on('data', chunk => {
console.log(chunk.length);
});
}
});
})
.catch(console.log);
} else {
message.reply('');
}
}
})
console.log(chunk.length); working only when playing music, those. only when the bot speaking. He does not hear other members. Please tell me what the problem is
I had this same problem, I don't know how to completely fix it forever, but if you play a short audio file, like a beep, when the bot joins the channel, your code should work.

Resources