update 2026

This commit is contained in:
Henry Jameson 2026-01-13 22:16:12 +02:00
commit 97a0f93836
4 changed files with 180 additions and 1349 deletions

171
index.js
View file

@ -1,98 +1,101 @@
import { ApiClient } from 'twitch';
import { ClientCredentialsAuthProvider } from 'twitch-auth';
import { NgrokAdapter } from 'twitch-webhooks-ngrok';
import { SimpleAdapter, WebHookListener } from 'twitch-webhooks';
import fetch from 'node-fetch';
const bearer = process.env.BEARER
const instance = process.env.INSTANCE
const botBearer = process.env.BEARER;
const userBearer = process.env.USER_BEARER;
const instance = process.env.INSTANCE;
const videoId = process.env.VIDEO_ID;
const clientId = process.env.CLIENT_ID;
const clientSecret = process.env.CLIENT_SECRET;
const userName = process.env.USER;
const hostName = process.env.HOSTNAME;
const globalState = { currentlyLive: false };
if (!(clientId && clientSecret && userName)) process.exit(1)
const post = async ({
status, spoiler_text, visibility = 'direct', content_type = 'text/plain'
}) => await fetch(`https://${instance}/api/v1/statuses`, {
"headers": {
"accept": '*/*',
"accept-language": 'en-US,en;q=0.9',
"authorization": `Bearer ${bearer}`,
"cache-control": 'no-cache',
"content-type": 'application/json',
"pragma": 'no-cache',
"sec-fetch-dest": 'empty',
"sec-fetch-mode": 'cors',
"sec-fetch-site": 'same-origin'
},
"referrerPolicy": "same-origin",
"body": JSON.stringify({
spoiler_text, status, source: 'HJ\'s twitch bot', visibility, content_type
}),
"method": "POST",
"mode": "cors",
"credentials": "include"
const headers = (bearer) => ({
accept: '*/*',
authorization: `Bearer ${bearer}`,
'cache-control': 'no-cache',
'content-type': 'application/json',
pragma: 'no-cache',
});
const authProvider = new ClientCredentialsAuthProvider(clientId, clientSecret);
const apiClient = new ApiClient({ authProvider });
const post = ({
status,
spoiler_text,
visibility = 'direct',
content_type = 'text/plain',
}) =>
fetch(`https://${instance}/api/v1/statuses`, {
headers: headers(botBearer),
referrerPolicy: 'same-origin',
body: JSON.stringify({
spoiler_text,
status,
source: "HJ's twitch bot",
visibility,
content_type,
}),
method: 'POST',
mode: 'cors',
credentials: 'include',
});
console.info('Getting userid...')
const userId = await apiClient.helix.users.getUserByName(userName);
const scrobble = ({ title, external_link }) =>
fetch(`https://${instance}/api/v1/pleroma/scrobble`, {
headers: headers(userBearer),
referrerPolicy: 'same-origin',
body: JSON.stringify({
title,
external_link,
}),
method: 'POST',
mode: 'cors',
credentials: 'include',
});
const adapter = hostName
? new SimpleAdapter({
hostName,
listenerPort: 8090
})
: new NgrokAdapter()
// await post({
// spoiler_text: 'Debug',
// status: '@hj twitch bot initialized',
// });
const listener = new WebHookListener(apiClient, adapter);
console.info('Starting listener...')
await listener.listen();
process.on('SIGINT', () => {
process.exit();
});
await post ({
spoiler_text: 'Debug',
status: '@hj twitch bot initialized'
})
const loop = async () => {
const response = await fetch(
`https://tube.ebin.club/api/v1/videos/${videoId}`,
);
const { state } = await response.json();
const isLive = state.id === 1; // published
const stateChange = isLive !== globalState.currentlyLive;
globalState.currentlyLive = isLive;
// we need to track the previous status of the stream because there are other state changes than the live/offline switch
let prevStream = await apiClient.helix.streams.getStreamByUserId(userId);
console.info('Subscribing to events...')
console.log(prevStream)
const subscription = await listener.subscribeToStreamChanges(userId, async stream => {
if (stream) {
console.log(stream)
if (!prevStream) {
console.log(`${stream.userDisplayName} just went live with title: ${stream.title}`);
const status = [
'Hej, hj just went live on twitch :annoying_dog_hole:',
'',
`Streaming ${stream.gameName}: ${stream.title}`,
'https://www.twitch.tv/hjkos',
'',
'Follow this bot and hit the bell to get notifications when hj is live'
].join('\n')
if (stateChange) {
if (isLive) {
await post({
spoiler_text: 'Stream announcment',
status,
visibility: 'public'
})
}
} else {
// no stream, no display name
const user = await apiClient.helix.users.getUserById(userId);
console.log(`${user.displayName} just went offline`);
}
prevStream = stream ?? null;
});
console.info('Ready!')
status: [
'Hej, hj just went live on peertube :annoying_dog_hole:',
'',
`Streaming ${stream.gameName}: ${stream.title}`,
`https://tube.ebin.club/w/${videoId}`,
'',
'Follow this bot and hit the bell to get notifications when hj is live',
].join('\n'),
visibility: 'public',
});
process.on('SIGINT', function() {
subscription.stop()
process.exit()
});
await scrobble({
title: 'Streaming on PeerTube',
external_link: `https://tube.ebin.club/w/${videoId}`,
});
} else {
await scrobble({
title: 'Stream over',
});
}
}
setTimeout(loop, 30 * 1000);
};
loop();
console.info('Ready!');