index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // Initialize local DB
  18. if (db.data === null) {
  19. // Set default data
  20. db.data = { config: {} };
  21. db.write();
  22. }
  23. //INIT
  24. client.on("ready", () => {
  25. console.log(`Logged in as ${client.user.tag}!`);
  26. _init();
  27. });
  28. const _init = async () => {
  29. const storageProviderChannel = client.channels.cache.find(
  30. (channel) => channel.name === db?.data?.storageProviderChannelName
  31. );
  32. schedule.scheduleJob(
  33. `0 */${db?.data?.reRunCronTimingInHour} * * *`,
  34. async function () {
  35. const response = await generateSize();
  36. storageProviderChannel.send(`Current storage size: ${response}`);
  37. }
  38. );
  39. };
  40. //UTILS
  41. const generateSize = async () => {
  42. if (isCacheOld()) {
  43. return await getSizeFromAPI();
  44. }
  45. return Promise.resolve(db?.data?.config?.size);
  46. };
  47. const generateMsg = async (msg = "", user = "") => {
  48. let size = await generateSize();
  49. msg.edit(`<@${user.id}>, Current storage size: ${size}`);
  50. };
  51. const getSizeFromAPI = async () => {
  52. return axios
  53. .get(db.data?.dataURL)
  54. .then((response) => {
  55. const res = Math.round(response?.data?.data?.rows[0][0]) + "GB";
  56. db.data.config = { timeStamp: moment.utc().format(), size: `${res}` };
  57. db.write();
  58. return res;
  59. })
  60. .catch((error) => {
  61. console.log("API failed", error);
  62. });
  63. };
  64. const isCacheOld = () => {
  65. //returns true if the timestamp is older than 2 hours
  66. let lastTime = db.data?.config?.timeStamp;
  67. if (lastTime) {
  68. const today = moment();
  69. const diff = today.diff(lastTime, "minutes");
  70. console.log("Cache is old", diff, "minutes");
  71. return diff > 120;
  72. }
  73. return true;
  74. };
  75. // COMMANDS
  76. client.on("message", async (msg) => {
  77. const user = msg.author;
  78. if (msg.content === "/storagesize") {
  79. msg
  80. .reply("Calculating... ")
  81. .then(async (oldMessage) => {
  82. generateMsg(oldMessage, user);
  83. })
  84. .catch((error) => {
  85. console.log("Not able to send message", error);
  86. });
  87. }
  88. });
  89. client.login(process.env.TOKEN);