tmijs / tmi.js

💬 Javascript library for the Twitch Messaging Interface. (Twitch.tv)
https://tmijs.com
MIT License
1.54k stars 217 forks source link

Mod and Broadcaster only command fix! #415

Closed joshuaepstein closed 3 years ago

joshuaepstein commented 3 years ago

You may also want to check that the broadcaster can use it. They are not considered a moderator in the chat.

let isMod = user.mod || user['user-type'] === 'mod';
let isBroadcaster = channel.slice(1) === user.username;
let isModUp = isMod || isBroadcaster;

Then use isModUp for command checking:

if(isModUp) {
    // Something for moderators or higher
}

Originally posted by @AlcaDesign in https://github.com/tmijs/tmi.js/issues/283#issuecomment-381250643

This isnt working for me, do you have a fix

AlcaDesign commented 3 years ago

It'd be nice if you were more specific about why it wasn't working. Maybe you could explore the values and figure that out but here's a new solution:

client.on('message', (channel, tags, message, self) => {
    if(self) return;
    const badges = tags.badges || {};
    const isBroadcaster = badges.broadcaster;
    const isMod = badges.moderator;
    const isModUp = isBroadcaster || isMod;
});

Also you can join the Twitch API Discord for help related questions. Issues are for issues with the library.

Zamiell commented 3 years ago

For reference, this is how you determine if the bot is a moderator:

const botUserState = client.userstate[channel];
const amMod = botUserState !== undefined && botUserState.mod === true;

The "userstate" object is automatically updated by the client when the "join" event happens or the "chat" event happens, so you don't have to micro-manage it.