discord.js playing audio on multiple servers - node.js

I made a test test bot for discord.js using Node.js / ffmpeg, to play a radio station in a channel on discord and there are no problems there, however i was thinking about how it would stream the same station to multiple servers/channels efficiently.
For example, if I am playing it on "Discord server one":
var voiceChannel = message.member.voiceChannel;
voiceChannel.join().then(connection => {
console.log("joined channel");
const dispatcher = connection.playArbitraryInput('http://philae.shoutca.st:8950/live', { volume: 0.5 });
dispatcher.on("end", end => {
console.log("left channel");
voiceChannel.leave();
});
}).catch(err => console.log(err));
However if i want to play it on another server that my bot is a member of, do i need to create the audio stream again?
If so that kinda sux. My bot would have to encode the stream seperatly for each instance?
I would like to know if there is any way i can reuse the audio stream and restream it so that if I had my bot in 100 channels its only one download instance of the stream and 100 uploads
I haope i am making sense but let me make some fake code to try help explain:
lets say instead i could do something like this:
let cachedAudioStream = cacheArbitraryInput('http://philae.shoutca.st:8950/live', {});
then for every outgoing instance:
connection1.playArbitraryInput(cachedAudioStream, { volume: 0.5 });
connection2.playArbitraryInput(cachedAudioStream, { volume: 0.8 });
connection3.playArbitraryInput(cachedAudioStream, { volume: 1 });
and so on
Thanks

Since the stream is managed by Discord.js (you only give a link, and Discord.js uses FFMPEG to handle it), you can't do it without modifying the Discord.js code.
Your question is relevant but I don't think it's feasible, at least if you uses Discord.js. You can still open an issue on their Github repository to ask them.

discord.js has a 'Voice Broadcast' for stuff like radio bots. Yes, you do have to connect the call to the broadcast but that's all.

Related

Why does my Discord.js bot take more than five minutes to respond on a server with less than 500 users?

I've just developed a bot for adding roles to a user who starts streaming on my server. When I tested the bot on a isolated server with only a few channels and users the bot responded instantly. But now I have set up my bot on my primary Discord server with roughly 25 channels and about ~500 users and now the response time is more than five minutes before I see it add or remove a role.
I am connecting via FIOS gigabit and running on a I9 with DDR4 4266MHz RAM.
When I first start the node server I notice the delay is much less, however still very slow responses of around a minute or two before it adds or removes the roles. If I allow the server to run for ten minutes then the response time is around five minutes before it adds a role. I do not know if it continues to get worse over time. But what could be causing this delay? I see my user status change to streaming (purple dot) in the online users list immediately, but my discord bot is not aware of this for a very long time!
This is my code, I have tried my best to optimize it but I've only been learning Discord.js for about a week and am still very unfamiliar with it:
// require the discord.js module
const Discord = require('discord.js');
// create a new Discord client
const client = new Discord.Client();
let role = '';
client.once('ready', () => {
const myGuild = client.guilds.cache.get('123456789123456789');
role = myGuild.roles.cache.find(role => role.name === 'streaming');
console.log('Ready!');
});
client.on("presenceUpdate", (oldPresence, newPresence) => {
if (!newPresence.activities){ return false; }
newPresence.activities.forEach(activity => {
if (activity.type == "STREAMING") {
//console.log(`${newPresence.user.tag} is streaming at ${activity.url}.`);
newPresence.member.roles.add(role)
} else {
newPresence.member.roles.remove(role)
};
});
});
// login to Discord with your app's token
client.login('123456789123456789.123456789.123456789123456789');
Can someone please explain why my bot is becoming so sluggish? It is almost unusable and kind of discouraging.
The default maxListeners is set to 10 and it may be worth exploring setting that value higher.
Additionally, I am curious if there is a memory leak on your application.
It sounds like it from the sluggishness that is experienced over time.
Try using a profiling tool like Clinic.js to monitor the state of the application over time.

Playing custom notification sound using Cloud Functions - iOS

I'm sending notification to my client app using Cloud Functions and GoogleCloudMessaging in this way:
const notificationContent = {
notification: {
title: `${senderName} has sent you a message`,
body: `${messageString}`,
icon: "default",
sound: "customNotificationSound",
},
};
return admin.messaging()
.sendToDevice(notifToken, notificationContent)
.then((result) =>{
console.log("write done correctly");
});
I want to use a custom notification sound instead of the default one; so I followed some guides online like this, this and this but they don't seem to answer my question.
Since I'm sending the push notification from the cloud function, do I still need to load the sound file in the client Main Bundle (I tried it and in fact it doesn't seem to work).
Or do I have to upload it somewhere else?
P.s. also the sound file extension is .wav so there shouldn't be problems for that matter.
I solved this and the answer is Yes, you still need to upload the music file in your main bundle even tough you're sending the push notification from cloud functions

Send an unsolicited message to Discord channel using nodejs

Looking for a way to send completely unsolicited messages to my discord channel using discord.js.
Background: I have a nodejs app running on my desktop that is monitoring a few websites and I need to notify my team so they can deal with things as the problem evolves. It's an ongoing issue which I'm not getting into right now. Yes, I want to pay for a pro website monitor, but the bean-counters yadda yadda...
I've read through the docs but it seems everything is geared towards event-driven behaviour from the discord side e.g. a new user arrives, a typed command is entered etc
I don't want that. I just want to send a notification when the server that I'm monitoring does X thing.
Here is what I have so far:
const Discord = require('discord.js');
config.discord.client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
if(shitgoesdown){
new Discord.Message(config.discord.client, data, config.discord.channel);
}
I can't seem to find anything on how to just point this thing at my server. The docs are great and all but they are super dense and right now it's just information overload. If someone could just point at the sign that would be great.
You can fetch the channel using client.channels.fetch(/* Channel ID */) and send a message in the callback using channel.send().
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
// Code to execute when something happens
client.channels.fetch(/* Channel ID */).then(channel => {
channel.send('Some message');
});

sharing messages between telegram bots

I'm using node-telegram-bot-api package
and I have two telegram bots like this:
const TelegramBot = require('node-telegram-bot-api');
let bot1 = new TelegramBot(token1, { polling: true });
let bot2 = new TelegramBot(token2, { polling: true });
I'm kind of making a connection between these two bots, In a way that messages that are recieved from bot1 should be sent to user by bot2 and vice versa
something like:
bot1.on('message', (msg) => {
if (msg.video) { // video in this case
bot2.sendVideo(user2_chatid, msg.video.file_id);
}
});
In the above code I am trying to send the video to user2 using bot2, once bot1 receives a video
using file_id to reference sticker is all right, but for photos, videos, voices etc I get this error
wrong file identifier/HTTP URL specified
Looks like telegram is kind of private about the bots and the data they can share, means that file_id is something private and only accessible by the bot it self
How can I share these data between telegram bots?
and I strongly prefer a way other than saving photos and videos somewhere on the server then sending them
https://core.telegram.org/bots/api#sending-files
file_id is unique for each individual bot and can't be transferred from one bot to another.
so I guess the answer Is no, unless I would save the file somewhere, upload it again using bot2 and then send it
for me a better solution was to use the same bot (bot1) for both users, so I can easily send the received file to any user who has started a chat with the bot

Send MediaStream through NodeJS

I have the MediaStream object returned from getUserMedia and can show it in my own screen.
The thing is, I don't know how to *send / pipe / stream/ * that MediaStream from Point A through NodeJS using socket.io to Point B.
My code right now is:
// Cámara
if (navigator.getUserMedia) {
navigator.getUserMedia({audio: true, video: true}, function(stream) {
video.src = URL.createObjectURL(stream) || window.URL.createObjectURL(stream);
webcamstream = stream;
}, onVideoFail);
} else {
alert ('failed');
}
});
function onVideoFail(e) {
console.log('webcam fail!', e);
};
I need the way to make this stream being sent constantly to other user using NodeJS.
The comment made in the answer for Audio and video conference with NodeJS are still valid
if you were to send the streams through socket.io instead of a peer connection, in any case that would be the raw video content (bytes). You would lose:
the streaming part (RTP/RTCP), and corresponding packet loss cancellation
the bandwidth adaptation
the encryption (DTLS)
the media engine (jitter correction, …)
over webRTC.
why not implement an SFU in node.js? Use node.js/socket.io for the signaling (i.e. the initial handshake) but also use node.js as a peer connection endpoint which relays/routes the media stream to (an)other(s) peer(s)? You would have an intermediate server like you seem to want, and all the advantages of webRTC.
another solution is to use an MCU, google webrtc+mcu and you will find many.
this might be a little late, but there's now a library called wrtc/node-webrtc. use that! https://github.com/node-webrtc/node-webrtc

Resources