discord.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. const CoreClass = require("../core.js");
  2. const Discord = require("discord.js");
  3. const config = require("config");
  4. class DiscordModule extends CoreClass {
  5. constructor() {
  6. super("discord");
  7. }
  8. initialize() {
  9. return new Promise((resolve, reject) => {
  10. this.log("INFO", "Discord initialize");
  11. this.client = new Discord.Client();
  12. this.adminAlertChannelId = config.get(
  13. "apis.discord"
  14. ).loggingChannel;
  15. this.client.on("ready", () => {
  16. this.log("INFO", `Logged in as ${this.client.user.tag}!`);
  17. if (this.getStatus() === "INITIALIZING") {
  18. resolve();
  19. } else if (this.getStatus() === "RECONNECTING") {
  20. this.log("INFO", `Discord client reconnected.`);
  21. this.setStatus("READY");
  22. }
  23. });
  24. this.client.on("disconnect", () => {
  25. this.log("INFO", `Discord client disconnected.`);
  26. if (this.getStatus() === "INITIALIZING") reject();
  27. else {
  28. this.setStatus("DISCONNECTED");
  29. }
  30. });
  31. this.client.on("reconnecting", () => {
  32. this.log("INFO", `Discord client reconnecting.`);
  33. this.setStatus("RECONNECTING");
  34. });
  35. this.client.on("error", (err) => {
  36. this.log(
  37. "INFO",
  38. `Discord client encountered an error: ${err.message}.`
  39. );
  40. });
  41. this.client.login(config.get("apis.discord").token);
  42. });
  43. }
  44. SEND_ADMIN_ALERT_MESSAGE(payload) {
  45. return new Promise((resolve, reject) => {
  46. const channel = this.client.channels.find(
  47. (channel) => channel.id === this.adminAlertChannelId
  48. );
  49. if (channel !== null) {
  50. let richEmbed = new Discord.RichEmbed();
  51. richEmbed.setAuthor(
  52. "Musare Logger",
  53. `${config.get("domain")}/favicon-194x194.png`,
  54. config.get("domain")
  55. );
  56. richEmbed.setColor(payload.color);
  57. richEmbed.setDescription(payload.message);
  58. //richEmbed.setFooter("Footer", "https://musare.com/favicon-194x194.png");
  59. //richEmbed.setImage("https://musare.com/favicon-194x194.png");
  60. //richEmbed.setThumbnail("https://musare.com/favicon-194x194.png");
  61. richEmbed.setTimestamp(new Date());
  62. richEmbed.setTitle("MUSARE ALERT");
  63. richEmbed.setURL(config.get("domain"));
  64. richEmbed.addField("Type:", payload.type, true);
  65. richEmbed.addField(
  66. "Critical:",
  67. payload.critical ? "True" : "False",
  68. true
  69. );
  70. payload.extraFields.forEach((extraField) => {
  71. richEmbed.addField(
  72. extraField.name,
  73. extraField.value,
  74. extraField.inline
  75. );
  76. });
  77. channel
  78. .send(payload.message, { embed: richEmbed })
  79. .then((message) =>
  80. resolve({
  81. status: "success",
  82. message: `Successfully sent admin alert message: ${message}`,
  83. })
  84. )
  85. .catch(() =>
  86. reject(new Error("Couldn't send admin alert message"))
  87. );
  88. } else {
  89. reject(new Error("Channel was not found"));
  90. }
  91. // if (true) {
  92. // resolve({});
  93. // } else {
  94. // reject(new Error("Nothing changed."));
  95. // }
  96. });
  97. }
  98. }
  99. module.exports = new DiscordModule();