Making it work, and storing some offline data as well because whynot?
This commit is contained in:
parent
038e4cac26
commit
32f30a925a
1 changed files with 52 additions and 27 deletions
|
|
@ -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,32 +67,53 @@ 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]) {
|
||||||
|
|
||||||
// Check if the donator has already donated
|
// Check if the donator has already donated
|
||||||
if (!data[date].donators.includes(donator)) {
|
if (!data[date].donators.includes(donator)) {
|
||||||
data[date].donators.push(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
|
WRITE_DATA(data);
|
||||||
data[date].total_bits += bits;
|
})
|
||||||
data[date].donations.push(bits);
|
.catch((err) => {throw err;})
|
||||||
}
|
|
||||||
|
|
||||||
// Making new data point
|
|
||||||
else {
|
|
||||||
data[date] = {
|
|
||||||
total_bits: bits,
|
|
||||||
donators: [donator],
|
|
||||||
donations: [bits]
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
WRITE_DATA(data);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
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((_) => {});
|
||||||
};
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue