discord.js 3.5 KB

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