discord.js 2.9 KB

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