cli.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SkibidiHub Administration CLI //
  2. require("dotenv").config();
  3. const supabase = require("@supabase/supabase-js");
  4. const fs = require("node:fs");
  5. const path = require("node:path");
  6. const utils = require("./utils.js");
  7. const client = supabase.createClient(
  8. process.env.SUPABASE_URL,
  9. process.env.SUPABASE_KEY
  10. );
  11. // Get process arguments
  12. const args = process.argv;
  13. args.shift();
  14. args.shift();
  15. async function main() {
  16. let ips;
  17. switch(args[0]) {
  18. case "help":
  19. console.log("Valid commands are:\n\ndeleteVideo [id]\nban [ip]\nunban [ip]\ncleanDatabase\ncleanDrive\nupdateSocialScore [user] [-300]\nverifyUser [user]\ndeVerifyUser [user]");
  20. break;
  21. case "ban":
  22. if(!args[1]) return console.log("you need to provide an ip address to ban!");
  23. ips = JSON.parse(fs.readFileSync("./ipbans.json"));
  24. ips.push(args[1]);
  25. fs.writeFileSync("./ipbans.json", JSON.stringify(ips), {encoding:'utf8', flag:'w'})
  26. console.log("Successfully banned ip.");
  27. break;
  28. case "unban":
  29. if(!args[1]) return console.log("you need to provide an ip address to unban!");
  30. ips = JSON.parse(fs.readFileSync("./ipbans.json"));
  31. if(!ips.includes(args[1])) return console.log("the ip address provided isnt banned.");
  32. ips.splice(ips.indexOf(args[1]), 1)
  33. fs.writeFileSync("./ipbans.json", JSON.stringify(ips), {encoding:'utf8', flag:'w'})
  34. console.log("Successfully unbanned ip.");
  35. break;
  36. case "deleteVideo":
  37. if(!args[1]) return console.log("you need to provide a video id to delete!");
  38. if(!utils.videoExists(args[1])) return console.log("the video you are trying to delete doesnt exist!");
  39. await deleteVideo(args[1]);
  40. break;
  41. case "cleanDatabase":
  42. client.from("videos").select("id").then(data => {
  43. data.data.forEach(async (video) => {
  44. if(!utils.videoExists(video.id)) {
  45. console.log(`Video with ID ${video.id} doesn't exist on drive. Erasing it on the database...`)
  46. await client.from("videos").delete().eq("id", video.id);
  47. }
  48. });
  49. })
  50. break;
  51. case "cleanDrive":
  52. if(args[1] != "confirm") {
  53. console.log("This should only be run on a BROKEN videos folder from a DEVELOPMENT environment.")
  54. console.log("If you run this, you WILL destroy some videos which you can NOT undo.");
  55. console.log("You must also run cleanDatabase before running this.");
  56. console.log("to confirm, run node cli.js cleanDrive confirm.");
  57. return;
  58. }
  59. const videos = fs.readdirSync(path.join(__dirname, "videos"));
  60. videos.forEach(async (id) => {
  61. const videoPath = path.join(__dirname, path.join("videos", id));
  62. if(!fs.existsSync(path.join(videoPath, "video.mp4"))) return await deleteVideo(id);
  63. if(!fs.existsSync(path.join(videoPath, "thumbnail.jpg"))) return await deleteVideo(id);
  64. })
  65. break;
  66. case "updateSocialScore":
  67. if(!args[1]) return console.log("Please Provide a user to update the social credit score of.");
  68. if(!args[2]) return console.log("Please provide a value to update the social credit score.");
  69. if(!utils.userExists(args[1])) return console.log("User doesn't exist!");
  70. const socialScoreData = await client
  71. .from("users")
  72. .select("social_score")
  73. .eq("name", args[1]);
  74. const socialScore = parseInt(socialScoreData["data"][0]["social_score"]) + parseInt(args[2]);
  75. await client.from("users").update({"social_score": socialScore}).eq("name", args[1]).then(data => {
  76. if(data.error) {
  77. return console.error(data);
  78. } else if(data.status != 204) {
  79. return console.error(data);
  80. }
  81. return console.log("Updated successfully.");
  82. });
  83. break;
  84. case "verifyUser":
  85. if(!args[1]) return console.log("Please provide a user to verify.");
  86. if(!utils.userExists(args[1])) return console.log("User doesn't exist!");
  87. await client.from("users").update({"verified": "TRUE"}).eq("name", args[1]).then(data => {
  88. if(data.error) {
  89. return console.error(data);
  90. } else if(data.status != 204) {
  91. return console.error(data);
  92. }
  93. return console.log("Verified successfully.");
  94. });
  95. break;
  96. case "deVerifyUser":
  97. if(!args[1]) return console.log("Please provide a user to verify.");
  98. if(!utils.userExists(args[1])) return console.log("User doesn't exist!");
  99. await client.from("users").update({"verified": "FALSE"}).eq("name", args[1]).then(data => {
  100. if(data.error) {
  101. return console.error(data);
  102. } else if(data.status != 204) {
  103. return console.error(data);
  104. }
  105. return console.log("Deverified successfully.");
  106. });
  107. break;
  108. case "sunset":
  109. if(!args[1]) return console.log("please provide a unix timestamp for when you want skibidihub to explode");
  110. const sunset = path.join(__dirname, "sunset.json")
  111. const json = JSON.parse(fs.readFileSync(sunset))
  112. json.sunset = true
  113. json.timestamp = args[1]
  114. const text = JSON.stringify(json)
  115. fs.writeFileSync(sunset, text);
  116. break;
  117. default:
  118. console.log("SkibidiHub Administration CLI\n\nPlease enter a valid command.")
  119. break;
  120. }
  121. }
  122. async function deleteVideo(id) {
  123. console.log(`Deleting video with the id ${id}...`);
  124. // Delete video on hard drive
  125. if(fs.existsSync(path.join(__dirname, path.join("videos", path.join(id, "video.mp4"))))) fs.unlinkSync(path.join(__dirname, path.join("videos", path.join(id, "video.mp4"))));
  126. if(fs.existsSync(path.join(__dirname, path.join("videos", path.join(id, "thumbnail.jpg"))))) fs.unlinkSync(path.join(__dirname, path.join("videos", path.join(id, "thumbnail.jpg"))));
  127. fs.rmSync(path.join(__dirname, path.join("videos", id)), { recursive: true, force: true });
  128. // Delete video in the database
  129. await client.from("videos").delete().eq("id", id);
  130. }
  131. main();