index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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(channel => channel.name === "storage-provider");
  28. const job = schedule.scheduleJob('0 */2 * * *', async function (fireDate) {
  29. const response = await generateSize();
  30. channel.send(`Current storage size: ${response}`);
  31. });
  32. }
  33. //UTILS
  34. const generateSize = async () => {
  35. let res;
  36. const isOld = isCacheOld();
  37. if (isOld) {
  38. res = await getSizeFromJoystream();
  39. }
  40. let size = isOld ? res : db.data.config[0].size;
  41. return Promise.resolve(size);
  42. }
  43. const generateMsg = async (msg = "", user = "") => {
  44. let size = await generateSize();
  45. msg && msg.edit(`<@${user.id}>, Current storage size: ${size}`);
  46. }
  47. const bytesToSize = (bytes) => {
  48. const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
  49. if (bytes === 0) return 'n/a'
  50. const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10)
  51. if (i === 0) return `${bytes} ${sizes[i]}`
  52. return `${(bytes / (1024 ** i)).toFixed(1)} ${sizes[i]}`
  53. }
  54. const getSizeFromJoystream = async () => {
  55. return axios.get('https://analytics.dapplooker.com/api/public/dashboard/c70b56bd-09a0-4472-a557-796afdc64d3b/card/155?parameters=%5B%5D')
  56. .then((response) => {
  57. // handle success
  58. // const res = bytesToSize(response.data.media.size);
  59. const res = Math.round(response.data.data.rows[0][0] / 1000) + "GB";
  60. db.data.config[0] = { timeStamp: moment.utc().format(), size: `${res}` };
  61. db.write();
  62. return res;
  63. })
  64. .catch((error) => {
  65. // handle error
  66. console.log(error);
  67. })
  68. }
  69. const isCacheOld = () => {
  70. let lastTime = db.data.config[0].timeStamp;
  71. console.log(JSON.stringify(lastTime));
  72. if (lastTime) {
  73. const today = moment();
  74. const diff = today.diff(lastTime, 'minutes')
  75. console.log(diff);
  76. return diff > 120 ? true : false;
  77. }
  78. return true
  79. }
  80. // COMMANDS
  81. client.on('message', async (msg) => {
  82. const user = msg.author;
  83. if (msg.content === '/storagesize') {
  84. msg.reply("Calculating... <a:check_no:717523612906684428>")
  85. .then(async (oldMessage) => {
  86. generateMsg(oldMessage, user);
  87. }).catch((error) => {
  88. console.log(error);
  89. });
  90. }
  91. });
  92. client.login(process.env.TOKEN);