Making it work, and storing some offline data as well because whynot?

This commit is contained in:
Tyler-A 2020-01-08 15:11:43 -06:00
parent 038e4cac26
commit 32f30a925a

View file

@ -1,7 +1,7 @@
// //
// Twitch.ts // Twitch.ts
// //
// Written by: Tyler Akins (2020/01/06) // Written by: Tyler Akins (2020/01/06 - 2020/01/08)
// //
@ -17,25 +17,28 @@ import {
} from "./utils"; } from "./utils";
const IS_LIVE = async (channel_name: string): Promise<boolean> => { const IS_LIVE = async (): Promise<boolean> => {
// Fetch the data from the Twitch API to check if the user is live // Fetch the data from the Twitch API to check if the user is live
let response = await requests.get( let response = await requests.get(
TWITCH_URI, TWITCH_URI,
{ {
qs: { user_login: channel_name}, qs: {
"user_login": config.CHANNEL
},
headers: { headers: {
"Client-ID": config.CLIENT_ID "Client-ID": config.CLIENT_ID
}, },
json: true json: true
} }
); );
console.log(response)
return response.data.length !== 0; return response.data.length !== 0;
}; };
export const START_BOT = async () => { export const START_BOT = async () => {
const client = tmi.Client({ const client = tmi.Client({
@ -54,7 +57,8 @@ export const START_BOT = async () => {
channels: [config.CHANNEL] channels: [config.CHANNEL]
}); });
client.on("cheer", (channel: string, ctx: tmi.Userstate, message: string) => {
client.on("cheer", (ch: string, ctx: tmi.Userstate, msg: string) => {
let bits: number = parseInt(ctx.bits); let bits: number = parseInt(ctx.bits);
let donator: string = ctx.username; let donator: string = ctx.username;
@ -63,9 +67,15 @@ export const START_BOT = async () => {
let data: data = LOAD_DATA(); let data: data = LOAD_DATA();
// Ensure the channel is live when the bits are donated IS_LIVE()
if (!IS_LIVE(channel)) { return; }; .then((response: boolean) => {
// Ensure the user is live
if (response) {
data["offline"].total_bits += bits;
data["offline"].donations.push(bits);
data["offline"].donators.push(donator)
} else {
// Data already exists // Data already exists
if (data[date]) { if (data[date]) {
@ -88,7 +98,22 @@ export const START_BOT = async () => {
donations: [bits] donations: [bits]
}; };
}; };
};
WRITE_DATA(data); WRITE_DATA(data);
})
.catch((err) => {throw err;})
}); });
client.on("disconnected", (reason: string) => {
console.log(`* Disconnected from Twitch w/ reason: ${reason}`)
});
client.on("connected", (addr: string, port: number) => {
console.log(`* Connected to Twitch on ${addr}:${port}`)
});
client.connect().catch((_) => {});
}; };