tmijs / tmi.js

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

Permissions not working (Question) #384

Closed HypeLevels closed 4 years ago

HypeLevels commented 4 years ago

Its just a question, im new to this and i made a simple clear chat command,

if (message == prefix + "limpar") {
        client.clear(Canal)
        client.say(Canal, "Chat limpo com sucesso!")
}

But EVERY USER can use it... when use the following code

if (user.mod && message == prefix + "limpar") {
        client.clear(Canal)
        client.say(Canal, "Chat limpo com sucesso!")
}

Only moderators can use, the streamer cant, how do i fix that?

AlcaDesign commented 4 years ago

It's better to use the badges property. It can tell you if the user is a broadcaster, moderator, subscriber/founder, etc.

const prefix = '!';
client.on('message', (channel, tags, message, self) => {
    // If message is from this bot or is not a command
    if(self || !message.startsWith(prefix)) return;
    // Parse message
    const args = message.slice(prefix.length).split(' ');
    const command = args.shift().toLowerCase();
    // Permissions
    const badges = tags.badges || {};
    const isBroadcaster = badges.broadcaster;
    const isMod = badges.moderator;
    const isModUp = isBroadcaster || isMod;
    // Commands
    if(command === 'clear' && isModUp) {
        client.clear(channel);
    }
});