discord.js 2.8 KB

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