discord.js 3.5 KB

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