I am currently working on a Twitch chatbot, and I'm trying to
implement a function which will see if a user is a moderator in a chat
or a global moderator. The function should return true if the user is
a moderator in the channel or a global moderator in the code.
Otherwise, return false. The array of global moderators is
['toximixes', 'lucidzbot']
.
So far, checking if the user is a moderator works fine on what I'm using (tmi.js), but seeing if the username exists inside of the global moderators array is for some reason not working.
Here is the current function I am using:
function isMod(user) {
if (user.mod == true) {
return true;
} else {
chnls.forEach(function(val,i){
if (val == user.username) {
return true;
}
});
}
return false;
}
The function takes one argument, and that is the userstate object as provided by tmi.js when a message is sent, but for simplicity and debugging purposes I am using these 3 objects:
{
username:"demo",
mod: false
}, // should be false
{
username:"demo-2",
mod: true
}, // should be true
{
username:"toximixes",
mod: false
} // should be true
The first two objects work perfectly, but for some reason the third
object stays as false. This is an issue because tmi.js does not
consider the broadcaster (me in this situation) as a mod, and so when
I run commands in my chat, my account appears as mod:false
.
If that happens, the function is supposed to check my username against the array of global moderators, and if it finds a match, return true. But, for some reason, it's returning false and no errors appear for me in the console or in the chat.
Here's what I've tried:
===
) then tried the other (==
);forEach
to check each item against the username;forEach
again to see if the item includes
the username at allBut so far none of what I'm trying is working. Despite me having all permissions in the chat, the function -- and by extension the entire bot -- treats me pretty much the same as a regular viewer.
In fact, this isn't just happening for my username, the function does this for every non-Twitch-mod even if they are supposed to be a global-mod. What's going on? And how can I fix this?
You actually don't need to iterate over the array. Array.prototype.includes() works fine.
Try this :
function isMod(user) {
if (user.mod == true) {
return true;
} else if (chnls.includes(user.username)) {
return true
}
return false;
}