I’m lost, been looking for a couple of hours by now, what am I doing wrong?
I’m trying to get the bot to create a twitch clip with the !clip command in chat
here is the command:
if (command === '!clip') {
clipRequestCount++;
console.log('!clip triggered');
if (clipRequestCount === 1 || !clipRequestTimer) {
clipRequestTimer = setTimeout(resetClipRequestCounter, clipRequestTimeout);
}
if (clipRequestCount >= clipRequestThreshold) {
try {
const broadcasterId = await getBroadcasterId(channel);
const clipUrl = await createTwitchClip(broadcasterId);
twitchclient.say(channel, `Clip created! Watch it here: ${clipUrl}`);
resetClipRequestCounter();
} catch (error) {
twitchclient.say(channel, `Failed to create clip.`);
console.error(`Clip creation failed: ${error.message}`);
resetClipRequestCounter();
}
}
}
async function createTwitchClip(broadcasterId) {
try {
const response = await axios.post(
`https://api.twitch.tv/helix/clips?broadcaster_id=${broadcasterId}`,
null,
{
headers: {
'Authorization': `Bearer ${process.env.TWITCH_OAUTH_TOKEN}`,
'Client-Id': process.env.TWITCH_CLIENT_ID,
'Content-Type': 'application/json'
}
}
);
if (response.status === 202 && response.data.data.length > 0) {
const clipUrl = `https://clips.twitch.tv/${response.data.data[0].id}`;
return clipUrl;
} else {
throw new Error('Clip creation failed: No clip ID returned from Twitch API.');
}
} catch (error) {
console.error(`Error creating clip: ${error.message}`);
throw new Error('Clip creation failed.');
}
}
async function getBroadcasterId(channel) {
try {
const response = await axios.get(
`https://api.twitch.tv/helix/users?login=${channel}`,
{
headers: {
'Authorization': `Bearer ${process.env.TWITCH_ACCESS_TOKEN}`,
'Client-Id': process.env.TWITCH_CLIENT_ID
}
}
);
if (response.status === 200 && response.data.data.length > 0) {
return response.data.data[0].id;
} else {
throw new Error('Broadcaster not found.');
}
} catch (error) {
console.error(`Error fetching broadcaster ID: ${error.message}`);
throw new Error('Failed to fetch broadcaster ID.');
}
}
and I’m getting this error:
Error fetching broadcaster ID: Request failed with status code 401
Clip creation failed: Failed to fetch broadcaster ID.
I used twitchtokengenerator.com to generate the tokens with the (clips:edit
and user:read:broadcast
) scopes enabled