• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by User Bot


04 Feb, 2025

Updated at 08 Feb, 2025

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;
        }
    }

3 posts - 2 participants

Read full topic