Chatbot time out users

Hi,

I’m a little stuck to get the ban command working.
Checking if the token is valid then I choose to ban a person but it gives me this:
Error timing out user: Error: Request failed with status code 401.

What am I doing wrong?

async function validateToken() {
    try {
        const validateRequest = await axios.get('https://id.twitch.tv/oauth2/validate', {
            headers: {
                'Client-ID': clientId,
                'Authorization': `OAuth ${authToken}`
            }
        });

        if (validateRequest.status === 200) {
            console.log('Token is valid');
            return true;
        }
    } catch (error) {
        console.error('Token validation failed:', error);
    }

    return false;
}

async function renewToken() {
    try {
        const tokenRequest = await axios.post(`https://id.twitch.tv/oauth2/token?client_id=${clientId}&client_secret=${clientSecret}&grant_type=client_credentials`);

        if (tokenRequest.status === 200) {
            const data = tokenRequest.data;
            authToken = data.access_token;
            console.log('Token renewed:', authToken);
            return true;
        }
    } catch (error) {
        console.error('Token renewal failed:', error);
    }

    return false;
}

async function ensureValidToken() {
    const isValid = await validateToken();
    if (!isValid) {
        await renewToken();
    }
}
const messageWords = message.toLowerCase().split(' ');
    for (const word of messageWords) {
        if (bannableWords.includes(word)) {
            console.log(`Bannable word detected: ${word}`);
            try {
                await ensureValidToken();
                await axios.post(
                    `https://api.twitch.tv/helix/moderation/bans?broadcaster_id=${broadcasterId}&moderator_id=${moderatorId}`,
                    {
                        data: {
                            user_id: userId,
                            duration: 10 // Timeout duration in seconds
                        }
                    },
                    {
                        headers: {
                            'Authorization': `Bearer ${authToken}`,
                            'Client-Id': clientId,
                            'Content-Type': 'application/json'
                        }
                    }
                );
                client.say(channel, `${username} has been timed out for 10 seconds for using a bannable word.`);
            } catch (err) {
                console.error('Error timing out user:', err);
            }
            return;
        }
    }

This operation requires a user token from the moderator taking the ban action

You are trying to use a client credentials token which represents no one

Which this api method doesn’t support

You should expand your code to also report the body of the response on error. As the error body message should indicate this. The http code is half the information

1 Like

thnx I added this. Looked over this but now it works :slight_smile: