diff --git a/src/Twitch.ts b/src/Twitch.ts index eac8d5d..2c925f6 100644 --- a/src/Twitch.ts +++ b/src/Twitch.ts @@ -1,7 +1,7 @@ // // 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"; -const IS_LIVE = async (channel_name: string): Promise => { +const IS_LIVE = async (): Promise => { // Fetch the data from the Twitch API to check if the user is live let response = await requests.get( TWITCH_URI, { - qs: { user_login: channel_name}, + qs: { + "user_login": config.CHANNEL + }, headers: { "Client-ID": config.CLIENT_ID }, json: true } ); - console.log(response) + return response.data.length !== 0; }; + export const START_BOT = async () => { const client = tmi.Client({ @@ -54,7 +57,8 @@ export const START_BOT = async () => { 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 donator: string = ctx.username; @@ -63,32 +67,53 @@ export const START_BOT = async () => { let data: data = LOAD_DATA(); - // Ensure the channel is live when the bits are donated - if (!IS_LIVE(channel)) { return; }; + IS_LIVE() + .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 - if (data[date]) { + // Data already exists + if (data[date]) { - // Check if the donator has already donated - if (!data[date].donators.includes(donator)) { - data[date].donators.push(donator); + // Check if the donator has already donated + if (!data[date].donators.includes(donator)) { + data[date].donators.push(donator); + }; + + // Add the bits to the total and the array of donations + data[date].total_bits += bits; + data[date].donations.push(bits); + } + + // Making new data point + else { + data[date] = { + total_bits: bits, + donators: [donator], + donations: [bits] + }; + }; }; - // Add the bits to the total and the array of donations - data[date].total_bits += bits; - data[date].donations.push(bits); - } - - // Making new data point - else { - data[date] = { - total_bits: bits, - donators: [donator], - 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((_) => {}); }; \ No newline at end of file