index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { Client } from "discord.js";
  2. import axios from "axios";
  3. import moment from "moment";
  4. import path, { join } from "path";
  5. import { Low, JSONFile } from "lowdb";
  6. import dotenv from "dotenv";
  7. import schedule from "node-schedule";
  8. dotenv.config();
  9. const __dirname = path.resolve();
  10. // Use JSON file for storage
  11. const file = join(__dirname, "db.json");
  12. const adapter = new JSONFile(file);
  13. const db = new Low(adapter);
  14. const client = new Client();
  15. // Read data from JSON file, this will set db.data content
  16. await db.read();
  17. // If file.json doesn't exist, db.data will be null
  18. // Set default data
  19. // db.data |= { config: [] };
  20. // db.write();
  21. //INIT
  22. client.on("ready", () => {
  23. console.log(`Logged in as ${client.user.tag}!`);
  24. _init();
  25. });
  26. const _init = async () => {
  27. const channel = client.channels.cache.find(
  28. (channel) => channel.name === "storage-provider"
  29. );
  30. const job = schedule.scheduleJob("0 */2 * * *", async function (fireDate) {
  31. const response = await generateSize();
  32. channel.send(`Current storage size: ${response}`);
  33. });
  34. };
  35. //UTILS
  36. const generateSize = async () => {
  37. let res;
  38. const isOld = isCacheOld();
  39. if (isOld) {
  40. res = await getSizeFromJoystream();
  41. }
  42. let size = isOld ? res : db.data.config[0].size;
  43. return Promise.resolve(size);
  44. };
  45. const generateMsg = async (msg = "", user = "") => {
  46. let size = await generateSize();
  47. msg && msg.edit(`<@${user.id}>, Current storage size: ${size}`);
  48. };
  49. const bytesToSize = (bytes) => {
  50. const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
  51. if (bytes === 0) return "n/a";
  52. const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10);
  53. if (i === 0) return `${bytes} ${sizes[i]}`;
  54. return `${(bytes / 1024 ** i).toFixed(1)} ${sizes[i]}`;
  55. };
  56. const getSizeFromJoystream = async () => {
  57. return axios
  58. .get(
  59. "https://analytics.dapplooker.com/api/public/dashboard/c70b56bd-09a0-4472-a557-796afdc64d3b/card/155?parameters=%5B%5D"
  60. )
  61. .then((response) => {
  62. // handle success
  63. // const res = bytesToSize(response.data.media.size);
  64. const res = Math.round(response.data.data.rows[0][0] / 1000) + "GB";
  65. db.data.config[0] = { timeStamp: moment.utc().format(), size: `${res}` };
  66. db.write();
  67. return res;
  68. })
  69. .catch((error) => {
  70. // handle error
  71. console.log(error);
  72. });
  73. };
  74. const isCacheOld = () => {
  75. let lastTime = db.data.config[0].timeStamp;
  76. console.log(JSON.stringify(lastTime));
  77. if (lastTime) {
  78. const today = moment();
  79. const diff = today.diff(lastTime, "minutes");
  80. console.log(diff);
  81. return diff > 120 ? true : false;
  82. }
  83. return true;
  84. };
  85. // COMMANDS
  86. client.on("message", async (msg) => {
  87. const user = msg.author;
  88. if (msg.content === "/storagesize") {
  89. msg
  90. .reply("Calculating... ")
  91. .then(async (oldMessage) => {
  92. generateMsg(oldMessage, user);
  93. })
  94. .catch((error) => {
  95. console.log(error);
  96. });
  97. }
  98. });
  99. client.login(process.env.TOKEN);