Bot status changes temporary - javascript

I have PresenceUpdate Set so when a specific user streams the bot status updates and it announces in the channel. It did announce and it worked fine but the bot status seems to change for only ~20 seconds then it would go back to its default.
I've tried fixing it using:
For Loop
While Loop
Recursion function
Can anyone help me out and fix this? Here is my code:
client.on("presenceUpdate", (oldPresence, newPresence) => {
let announcement = "Announcement Message";
if (!newPresence.activities) return false;
newPresence.activities.forEach(activity => {
if (activity.type == "STREAMING") {
if(newPresence.user.id == "ID OF USER STREAMING") {
console.log(`${newPresence.user.tag} is streaming at ${activity.url}.`);
client.channels.cache.get("My Channel ID").send(announcement);
client.user.setActivity("to the server", { type: "STREAMING", url: "Twitch Link"});
};
};
});
});
EDIT: Okay so i managed to fix it by removing the part where if they r not streaming the status returns to default. HOWEVER, now it is stuck on streaming even when the user is no longer streaming. How do i solve this? The code has been also updated.

You could store the streaming user's id somewhere, and then check whenever the presence is update if it is this same user.
Here is an example :
if (client.streamingUserID !== undefined && client.streamingUserID !== newPresence.user.id) return;
newPresence.activities.forEach(activity => {
if (activity.type == "STREAMING") {
if(newPresence.user.id == "ID OF USER STREAMING") {
console.log(`${newPresence.user.tag} is streaming at ${activity.url}.`);
client.channels.cache.get("My Channel ID").send(announcement);
client.user.setActivity("to the server", { type: "STREAMING", url: "Twitch Link"});
client.streamingUserID = newPresence.user.id;
};
} else {
// set back activity to default and
delete client.streamingUserID;
}
});

Related

Send message to disconnected user?

so, as you may know there is an admin-ui library (Idk what to call it) in socket.io.
what I want to do is if a client is kicked, send a message to that client (or have the client detect when it is disconnected)
i have tried:
socket.on('disconnect', function(){
alert('disconnected');
});
but that does nothing...
Found a fix using the socket attribute connected and setting an interval to check if the client is connected to a socket:
// client
let offline_alerted = false;
setInterval(function () {
if (socket.connected === false) {
if (offline_alerted === false) {
alert("Disconnected :(");
offline_alerted = true;
}
} else if (offline_alerted === true) {
alert("Reconnected!");
offline_alerted = false;
}
}, 1000);

Notification API permission being reset

I am trying to make a chatroom and have everything up and running, but am getting stuck when it comes to desktop notifications. Whenever I request permission for notifications and get the access set to 'granted', the permission will reset to 'default' not allowing notifications to be displayed. Is there a way to get the permission to stay set as 'granted' or would I have to get them to keep allowing notifications every time a new message is sent? Is it a protection involved with running it as a local file (on ChromeOS)? Here is the code for the part about notifications:
function showNotification() {
var title = "New Message!";
var messageValueNotif = document.querySelectorAll(".message");
if (messageValueNotif.length !== 0) {
messageValueNotif = messageValueNotif[messageValueNotif.length - 1].innerHTML;
} else {
return false;
}
var currentFavicon = document.querySelector('link').href;
var notif = new Notification(title, {
body: messageValueNotif,
icon: currentFavicon
});
notif.onclick = () => {
notif.close();
window.parent.focus();
}
}
function requestAndShowPermission() {
if (Notification.permission === 'granted') {
showNotification();
} else if (Notification.permission !== 'denied') {
Notification.requestPermission().then(function(permission) {
showNotification()
});
}
}
window.onload = requestAndShowPermission();
It is apparently a file protection with ChromeOS.

How catch disabling web push notification?

How can I catch with serviceworker or simple js code, when user who previously allowed the web push notifications for my site disable them? For Firefox and Chrome browsers.
You can use the Permissions API to detect the current state of given permissions, as well as listen for changes.
This article has more detail. Here's a relevant code snippet:
navigator.permissions.query({name: 'push'}).then(function(status) {
// Initial permission status is `status.state`
status.onchange = function() {
// Status changed to `this.state`
};
});
You can try this:
navigator.permissions.query({name:'notifications'}).then(function(status) {
//alert(status.state); // status.state == { "prompt", "granted", "denied" }
status.onchange = function() {
if(this.state=="denied" || this.state=="prompt"){ push_unsubscribe(); }
};
});
or this:
navigator.permissions.query({name:'push', userVisibleOnly:true}).then(function(status) {
//alert(status.state); // status.state == { "prompt", "granted", "denied" }
status.onchange = function() {
if(this.state=="denied" || this.state=="prompt"){ push_unsubscribe(); }
};
});
I use it to send information to the server that the user has canceled or disabled notifications. Note that the code is not executed if the user does so in the site settings.

Sending browser notification to specific users (web)

I would want to send out notifications to the list of users from database.
I found a solution to send push notifications:
// request permission on page load
document.addEventListener('DOMContentLoaded', function () {
if (!Notification) {
alert('Desktop notifications not available in your browser. Try Chromium.');
return;
}
if (Notification.permission !== "granted")
Notification.requestPermission();
});
function notifyMe() {
if (Notification.permission !== "granted")
Notification.requestPermission();
else {
var notification = new Notification('Notification title', {
icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
body: "Hey there! You've been notified!",
});
notification.onclick = function () {
window.open("https://stackoverflow.com/a/13328397/1269037");
};
}
}
(Source: Chrome desktop notification example ) How can i configure it to send specific notifications to specific users ?
Let us assume that I want to send notifications to all users of the table user_notification.
I tried using
<?php
$sqx = "SELECT * FROM `user_js` ";
?>
in the code but it does not help either
step 1 : try adding the following into your HTML response :
<button onclick="notifyMe()">Notify me!</button>
step 2 : if you can see the button and you get a notification when you click on it, try to move on and trigger it as soon as the page loads.
see if this link will help you : http://www.w3schools.com/jsref/event_onload.asp
principally, this should work :
<body onload="notifyMe()">
step 3 : if all works well, you'll have to add a PHP condition that will add this "onload=..." only to the users you want.

Notifications API, showing multiple times

I'm using the chrome(Mozilla, etc) notifications API:
Notification.requestPermission(function (permission) {
// Whatever the user answers, we make sure we store the information
if (!('permission' in Notification)) {
Notification.permission = permission;
}
// If the user is okay, let's create a notification
if (permission === "granted") {
var options = {
body: data.nome + ': ' + data.corpo,
icon: data.img,
dir : "ltr"
};
var notification = new Notification("Weeazy",options);
}
});
When i'm with 2 or more windows opened, the notification shows multiple times (1 per window).
Someone knows how i can prevent this? I just want to show 1 notification, even with 2, or 999 windows opened, like Facebook does. I hope my code example can help! Thanks!

Categories