· 5 years ago · Nov 23, 2020, 05:06 PM
1// var api = require('twitch-api-v5');
2var fetch = require('node-fetch');
3var irc = require("tmi.js");
4var esaGame = "";
5var bsgGame = "";
6var esaTitle = "";
7var bsgTitle = "";
8var interval = "5"; //in minutes
9
10const OPTIONS = {
11 options: {
12 debug: true
13 },
14 connection: {
15 //server: "irc.swiftirc.net",
16 //port: 6667,
17 random: "chat",
18 reconnect: true
19 },
20 identity: {
21 username: "nuhh",
22 password: "nuhh"
23 },
24 channels: ["#botikelt"]
25};
26
27var client = new irc.client(OPTIONS);
28// Connect the client to the server..
29client.connect();
30
31logic();
32setInterval(function() {
33 logic();
34}, interval * 60 * 1000);
35
36
37function logic() {
38 getGame();
39 setTimeout(function() {
40 if (esaGame !== bsgGame) {
41 setGame();
42 } else {
43 console.log("No update");
44 }
45 }, (2 * 1000));
46}
47
48function getGame() {
49 client.api({ //esamarathon
50 url: "https://api.twitch.tv/helix/channels?broadcaster_id=54739364",
51 headers: {
52 'Client-ID': '<key>',
53 'Authorization': 'Bearer <key>'
54 }
55 }, (err, res, body) => {
56 // console.log(body);
57 esaGame = body.data[0].game_name;
58 esaTitle = body.data[0].title;
59 console.log("ESA Game saved: " + esaGame);
60 });
61 client.api({ //bsg_marathon
62 url: "https://api.twitch.tv/helix/channels?broadcaster_id=30685577",
63 headers: {
64 'Client-ID': '<key>',
65 'Authorization': 'Bearer <key>'
66 }
67 }, (err, res, body) => {
68 // console.log(body);
69 bsgGame = body.data[0].game_name;
70 bsgTitle = body.data[0].title;
71 console.log("BSG Game saved: " + bsgGame);
72 });
73}
74
75function setGame() {
76
77 postRequest('https://api.twitch.tv/kraken/channels/30685577')
78 // .then(data => console.log(data)) // Result from the `response.json()` call
79 .then(data => console.log(""))
80 .catch(error => console.error(error))
81
82 function postRequest(url, data) {
83 return fetch(url, {
84 method: 'PUT', // 'GET', 'PUT', 'DELETE', etc.
85 body: '{"channel": {"game": "' + esaGame + '", "status": "' + esaTitle + '"}}', // Coordinate the body type with 'Content-Type'
86 headers: {
87 'Client-ID': '<key>',
88 'Authorization': 'OAuth <key>', //scope channel_editor
89 'Accept': 'application/vnd.twitchtv.v5+json',
90 'Content-Type': 'application/json'
91 },
92 })
93 .then(response => response.json());
94 }
95 console.log("UPDATE: Changed BSG game to " + esaGame)
96}
97// fetch('https://api.twitch.tv/helix/channels?broadcaster_id=26082881', {
98// method: 'GET',
99// headers: {
100// 'Client-ID': '<key, but commented out anyways>',
101// 'Authorization': 'Bearer <key but it is commented out anyways>'
102// }
103// })
104// .then(res => res.json())
105// .then(res => {
106// console.log(res) // I keep getting "OAuth token is missing"
107// console.log("De game is "+res.data[0].game_name);
108// console.log(res.data[0].title);
109// });
110