fixed bugs and got the multiple-channel thing to FINALLY WORK

This commit is contained in:
Avi 2020-10-19 04:15:19 +00:00
parent e6c91a6619
commit c2a5c22f94
3 changed files with 78 additions and 31 deletions

View file

@ -1,8 +1,9 @@
# Million Stats # Million Stats
This is an app for the #counttoamillion channel in Hack Club's Slack workspace. You can also clone this for anything that involves counting to a very high number :) This is an app for the #counttoamillion channel in Hack Club's Slack workspace, where you and a team of other cool kids together try to count to a million, base ten, by ones. No one can count consecutively. No purpose, just a fun team-building game :)
Every midnight, it gives a report on the progress the channel's made and the average daily speed:
![shows the day's increase, the average daily speed, and the predicted amount of time needed to reach the next thousand/tenthousand](https://cdn.glitch.com/95c2faf1-779e-4e08-b219-8a122cdceefe%2Fbotdemo.png?v=1594948839444) Every midnight, the bot gives a report on the progress the channel's made and the average daily speed:
![a couple of messages from different people showing numbers going the day's increase, the average daily speed, and the predicted amount of time needed to reach the next thousand/tenthousand](./counttoamillion.png)
## Modifying this thing ## Modifying this thing

View file

@ -33,12 +33,24 @@ function extractNumber(txt) {
async function fetchLatest(id) { async function fetchLatest(id) {
try { try {
const result = await app.client.conversations.history({ if (id == "CDJMS683D") {
token: token, result = await app.client.conversations.history({
channel: id, token: token,
limit: 1, channel: id,
}); limit: 1
const number = extractNumber(result.messages[0].text) });
} else if (id == "C01BZ7V0207") {
result = await app.client.conversations.replies({
token: token,
channel: id,
ts: '1602496865.003000',
// thread_ts: 1602496865,
limit: 1
});
}
let index = id == "CDJMS683D" ? 0 : 1
const number = extractNumber(result.messages[index].text);
// console.log(number);
return number; return number;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
@ -48,13 +60,27 @@ async function fetchLatest(id) {
async function fetchOldest(id) { async function fetchOldest(id) {
try { try {
let last24Hrs; let last24Hrs;
const result = await app.client.conversations.history({ let result;
token: token, if (id == "CDJMS683D") {
channel: id, result = await app.client.conversations.history({
oldest: Math.floor(Date.now() / 1000) - 86400, //debug: 1596326400, actual: Math.floor(Date.now() / 1000) - 86400 token: token,
inclusive: false channel: id,
}); oldest: Math.floor(Date.now() / 1000) - 86400, //debug: 1596326400, actual: Math.floor(Date.now() / 1000) - 86400
const number = extractNumber(result.messages[result.messages.length - 2].text); inclusive: false
});
} else if (id == "C01BZ7V0207") {
result = await app.client.conversations.replies({
token: token,
channel: id,
ts: '1602496865.003000',
limit: 10,
oldest: Math.floor(Date.now() / 1000) - 86400, //debug: 1596326400, actual: Math.floor(Date.now() / 1000) - 86400
inclusive: false
});
// console.log(result);
}
let index = id == "CDJMS683D" ? result.messages.length - 2 : 2
const number = extractNumber(result.messages[index].text);
return number - 1; return number - 1;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
@ -64,12 +90,12 @@ async function fetchOldest(id) {
async function publishMessage(id, text) { async function publishMessage(id, text) {
try { try {
let options = { let options = {
token: token, token: token,
channel: id, channel: id,
text: text text: text
} }
if (id == "CDJMS683D") { if (id == "C01BZ7V0207") {
options['thread_ts'] = 1602496865003000 options['thread_ts'] = '1602496865.003000'
} }
const result = await app.client.chat.postMessage(options); const result = await app.client.chat.postMessage(options);
} catch (error) { } catch (error) {
@ -122,10 +148,11 @@ async function addData(db, object) {
async function getStats(id) { async function getStats(id) {
try { try {
let obj;
if (id == "CDJMS683D") { if (id == "CDJMS683D") {
let obj = await base('stats').find('rec2XI8QAsPr7EMVB'); obj = await base('stats').find('rec2XI8QAsPr7EMVB');
} else if (id == "C01BZ7V0207") { } else if (id == "C01BZ7V0207") {
let obj = await base('thread stats').find('recBljY8vBdaNK8kq'); obj = await base('thread_stats').find('recBljY8vBdaNK8kq');
} }
return { return {
id: obj.id, id: obj.id,
@ -141,13 +168,20 @@ async function report(channel) {
let latest = await fetchLatest(channel); let latest = await fetchLatest(channel);
let diff = latest - oldest; let diff = latest - oldest;
let db = channel == "CDJMS683D" ? 'increase' : 'thread'; let db = channel == "CDJMS683D" ? 'increase' : 'thread';
addData(db, { let dataArray = {
"Date": moment().subtract(1, "days").format("YYYY-MM-DD"), "Date": moment().subtract(1, "days").format("YYYY-MM-DD"),
"increase": diff, "increase": diff
"stats": [ }
if (channel == "CDJMS683D") {
dataArray.stats = [
"rec2XI8QAsPr7EMVB" "rec2XI8QAsPr7EMVB"
] ]
}); } else if (channel == "C01BZ7V0207") {
dataArray.thread_stats = [
"recBljY8vBdaNK8kq"
]
}
addData(db, dataArray);
let newStats = await getStats(channel); let newStats = await getStats(channel);
averageSpeed = newStats.fields.average.toFixed(3); averageSpeed = newStats.fields.average.toFixed(3);
let thousandsGoal = Math.ceil(latest / 1000) * 1000; let thousandsGoal = Math.ceil(latest / 1000) * 1000;
@ -175,9 +209,16 @@ async function report(channel) {
"* \n :fastparrot: KEEP IT GOING GUYS!"; "* \n :fastparrot: KEEP IT GOING GUYS!";
if (pastThousandsGoal > oldest && pastThousandsGoal <= latest) { if (pastThousandsGoal > oldest && pastThousandsGoal <= latest) {
let messageWithCelebration = ":tada: YAY! We've went past " + pastThousandsGoal + "! :tada: \n" + message; let messageWithCelebration = ":tada: YAY! We've went past " + pastThousandsGoal + "! :tada: \n" + message;
publishMessage(channel, messageWithCelebration); //'C017W4PHYKS' for debugging, channel for actual
publishMessage(channel, messageWithCelebration);
// publishMessage('C017W4PHYKS', messageWithCelebration);
//'C017W4PHYKS' for debugging, channel for actual
} else { } else {
publishMessage(channel, message); //'C017W4PHYKS' for debugging, channel for actual // publishMessage('C017W4PHYKS', message);
publishMessage(channel, message);
//'C017W4PHYKS' for debugging, channel for actual
} }
}; };
@ -215,8 +256,13 @@ app.event('message', async (body) => {
// Start your app // Start your app
try { try {
await app.start(process.env.PORT || 3000); await app.start(process.env.PORT || 3000);
let j = schedule.scheduleJob('0 0 * * *', () => { report(main) }); // */15 * * * * * for debugging, 0 0 * * * actual let j = schedule.scheduleJob('0 0 * * *', () => { report(main) });
// let j = schedule.scheduleJob('*/15 * * * * *', () => { report(main) });
let k = schedule.scheduleJob('0 0 * * *', () => { report(thread) }); let k = schedule.scheduleJob('0 0 * * *', () => { report(thread) });
// let k = schedule.scheduleJob('*/15 * * * * *', () => { report(thread) });
// */15 * * * * * for debugging, 0 0 * * * actual
} catch (error) { } catch (error) {
console.error(error); console.error(error);
publishMessage('C017W4PHYKS', error) publishMessage('C017W4PHYKS', error)

BIN
counttoamillion.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 135 KiB