discord.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.setStage(1);
  10. this.client = new Discord.Client();
  11. this.adminAlertChannelId = config.get("apis.discord").loggingChannel;
  12. this.client.on("ready", () => {
  13. this.logger.info("DISCORD_MODULE", `Logged in as ${this.client.user.tag}!`);
  14. if (this.state === "INITIALIZING") resolve();
  15. else {
  16. this.logger.info("DISCORD_MODULE", `Discord client reconnected.`);
  17. this.setState("INITIALIZED");
  18. }
  19. });
  20. this.client.on("disconnect", () => {
  21. this.logger.info("DISCORD_MODULE", `Discord client disconnected.`);
  22. if (this.state === "INITIALIZING") reject();
  23. else {
  24. this.failed = true;
  25. this._lockdown;
  26. }
  27. });
  28. this.client.on("reconnecting", () => {
  29. this.logger.info("DISCORD_MODULE", `Discord client reconnecting.`);
  30. this.setState("RECONNECTING");
  31. });
  32. this.client.on("error", err => {
  33. this.logger.info("DISCORD_MODULE", `Discord client encountered an error: ${err.message}.`);
  34. });
  35. this.client.login(config.get("apis.discord").token);
  36. });
  37. }
  38. async sendAdminAlertMessage(message, color, type, critical, extraFields) {
  39. try { await this._validateHook(); } catch { return; }
  40. const channel = this.client.channels.find(channel => channel.id === this.adminAlertChannelId);
  41. if (channel !== null) {
  42. let richEmbed = new Discord.RichEmbed();
  43. richEmbed.setAuthor(
  44. "Musare Logger",
  45. `${config.get("domain")}/favicon-194x194.png`,
  46. config.get("domain")
  47. );
  48. richEmbed.setColor(color);
  49. richEmbed.setDescription(message);
  50. //richEmbed.setFooter("Footer", "https://musare.com/favicon-194x194.png");
  51. //richEmbed.setImage("https://musare.com/favicon-194x194.png");
  52. //richEmbed.setThumbnail("https://musare.com/favicon-194x194.png");
  53. richEmbed.setTimestamp(new Date());
  54. richEmbed.setTitle("MUSARE ALERT");
  55. richEmbed.setURL(config.get("domain"));
  56. richEmbed.addField("Type:", type, true);
  57. richEmbed.addField("Critical:", critical ? "True" : "False", true);
  58. extraFields.forEach(extraField => {
  59. richEmbed.addField(
  60. extraField.name,
  61. extraField.value,
  62. extraField.inline
  63. );
  64. });
  65. channel
  66. .send(message, { embed: richEmbed })
  67. .then(message =>
  68. this.logger.success("SEND_ADMIN_ALERT_MESSAGE", `Sent admin alert message: ${message}`)
  69. )
  70. .catch(() =>
  71. this.logger.error("SEND_ADMIN_ALERT_MESSAGE", "Couldn't send admin alert message")
  72. );
  73. } else {
  74. this.logger.error("SEND_ADMIN_ALERT_MESSAGE", "Couldn't send admin alert message, channel was not found.");
  75. }
  76. }
  77. }