Преглед на файлове

bot: improve time strings

Joystream Stats преди 4 години
родител
ревизия
b2df3568be

+ 3 - 2
community-contributions/joystreamtelegrambot/src/bot.ts

@@ -18,14 +18,15 @@ const log = (msg: string): void | number => opts.verbose && console.log(msg);
 process.env.NTBA_FIX_319 ||
   log("TL;DR: Set NTBA_FIX_319 to hide this warning.");
 
-const bot = new TelegramBot(token, { polling: true });
+const bot = token ? new TelegramBot(token, { polling: true }) : null;
 
 let startTime: number = moment().valueOf();
 
 const sendMessage = (msg: string) => {
   if (msg === "") return;
   try {
-    bot.sendMessage(chatid, msg, { parse_mode: "HTML" });
+    if (bot) bot.sendMessage(chatid, msg, { parse_mode: "HTML" });
+    else console.log(msg);
   } catch (e) {
     console.log(`Failed to send message: ${e}`);
   }

+ 2 - 2
community-contributions/joystreamtelegrambot/src/lib/announcements.ts

@@ -186,7 +186,7 @@ export const proposals = async (
       let label: string = result;
       if (result === "Approved") {
         const executed = parameters.gracePeriod.toNumber() > 0 ? false : true;
-        label = executed ? "Executed" : "Finalized";
+        label = executed ? "executed" : "finalized";
         if (!executed) executing.push(id);
       }
       const msg = `Proposal ${id} <b>${label}</b> at block ${finalizedAt}.\r\n${message}`;
@@ -247,7 +247,7 @@ export const heartbeat = (
   if (executing) props += `{executing} ${p(executing)} to be executed.`;
 
   sendMessage(
-    `  ${blocks.length} blocks produced in ${timePassed}h
+    `  ${blocks.length} blocks produced in ${timePassed}
   Blocktime: ${blocktime.toFixed(3)}s
   Stake: ${avgStake.toFixed(1)} / ${avgIssued.toFixed()} M tJOY (${percent}%)
   Validators: ${avgVals.toFixed()} (${reward} tJOY/h)

+ 13 - 5
community-contributions/joystreamtelegrambot/src/lib/util.ts

@@ -43,11 +43,19 @@ export const printStatus = (
 };
 
 // time
-export const formatTime = (time?: any): string =>
-  moment(time).format("H:mm:ss");
-
-export const passedTime = (start: number, now: number): string =>
-  formatTime(moment.utc(moment(now).diff(moment(start))));
+export const formatTime = (time?: any, format = "H:mm:ss"): string =>
+  moment(time).format(format);
+
+export const passedTime = (start: number, now: number): string => {
+  const passed = moment.utc(moment(now).diff(start)).valueOf();
+  const format =
+    passed > 86400000
+      ? "d:HH:mm:ss[d]"
+      : passed > 3600000
+      ? "H:mm:ss[h]"
+      : "mm:ss[m]";
+  return formatTime(passed, format);
+};
 
 export const exit = (log: (s: string) => void) => {
   log("\nNo connection, exiting.\n");