discord.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const coreClass = require("../core");
  2. const EventEmitter = require('events');
  3. const Discord = require("discord.js");
  4. const config = require("config");
  5. const bus = new EventEmitter();
  6. module.exports = class extends coreClass {
  7. initialize() {
  8. return new Promise((resolve, reject) => {
  9. this.client = new Discord.Client();
  10. this.connected = false;
  11. this.adminAlertChannelId = config.get("apis.discord").loggingChannel;
  12. this.client.on("ready", () => {
  13. this.logger.info("DISCORD_READY", `Logged in as ${this.client.user.tag}!`);
  14. this.connected = true;
  15. //bus.emit("discordConnected");
  16. resolve();
  17. /*messagesToSend.forEach(message => {
  18. this.sendAdminAlertMessage(message.message, message.color, message.type, message.critical, message.extraFields);
  19. });
  20. messagesToSend = [];*/
  21. });
  22. this.client.on("disconnect", () => {
  23. this.logger.info("DISCORD_DISCONNECT", `Discord client was disconnected.`);
  24. this.connected = false;
  25. });
  26. this.client.on("reconnecting", () => {
  27. this.logger.info("DISCORD_RECONNECTING", `Discord client reconnecting.`);
  28. this.connected = false;
  29. });
  30. this.client.on("error", err => {
  31. this.logger.info("DISCORD_ERROR", `Discord client encountered an error: ${err.message}.`);
  32. reject();
  33. });
  34. this.client.login(config.get("apis.discord").token);
  35. });
  36. }
  37. async sendAdminAlertMessage(message, color, type, critical, extraFields) {
  38. try { await this._validateHook(); await this.connectedHook(); } catch { return; }
  39. const channel = this.client.channels.find("id", this.adminAlertChannelId);
  40. if (channel !== null) {
  41. let richEmbed = new Discord.RichEmbed();
  42. richEmbed.setAuthor(
  43. "Musare Logger",
  44. `${config.get("domain")}/favicon-194x194.png`,
  45. config.get("domain")
  46. );
  47. richEmbed.setColor(color);
  48. richEmbed.setDescription(message);
  49. //richEmbed.setFooter("Footer", "https://musare.com/favicon-194x194.png");
  50. //richEmbed.setImage("https://musare.com/favicon-194x194.png");
  51. //richEmbed.setThumbnail("https://musare.com/favicon-194x194.png");
  52. richEmbed.setTimestamp(new Date());
  53. richEmbed.setTitle("MUSARE ALERT");
  54. richEmbed.setURL(config.get("domain"));
  55. richEmbed.addField("Type:", type, true);
  56. richEmbed.addField("Critical:", critical ? "True" : "False", true);
  57. extraFields.forEach(extraField => {
  58. richEmbed.addField(
  59. extraField.name,
  60. extraField.value,
  61. extraField.inline
  62. );
  63. });
  64. channel
  65. .send(message, { embed: richEmbed })
  66. .then(message =>
  67. this.logger.success("SEND_ADMIN_ALERT_MESSAGE", `Sent admin alert message: ${message}`)
  68. )
  69. .catch(() =>
  70. this.logger.error("SEND_ADMIN_ALERT_MESSAGE", "Couldn't send admin alert message")
  71. );
  72. } else {
  73. this.logger.error("SEND_ADMIN_ALERT_MESSAGE", "Couldn't send admin alert message, channel was not found.");
  74. }
  75. }
  76. connectedHook() {
  77. return Promise.race([
  78. new Promise(resolve => bus.once("discordConnected", resolve)),
  79. new Promise(resolve => {
  80. if (this.connected) resolve();
  81. })
  82. ]);
  83. }
  84. }