discord.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. let lockdown = false;
  2. const Discord = require("discord.js");
  3. const logger = require("./logger");
  4. const config = require("config");
  5. const client = new Discord.Client();
  6. let messagesToSend = [];
  7. let connected = false;
  8. // TODO Maybe we need to only finish init when ready is called, or maybe we don't wait for it
  9. module.exports = {
  10. adminAlertChannelId: "",
  11. init: function(discordToken, adminAlertChannelId, errorCb, cb) {
  12. this.adminAlertChannelId = adminAlertChannelId;
  13. client.on("ready", () => {
  14. logger.info("DISCORD_READY", `Logged in as ${client.user.tag}!`);
  15. connected = true;
  16. messagesToSend.forEach(message => {
  17. this.sendAdminAlertMessage(message.message, message.color, message.type, message.critical, message.extraFields);
  18. });
  19. messagesToSend = [];
  20. });
  21. client.on("invalidated", () => {
  22. logger.info("DISCORD_INVALIDATED", `Discord client was invalidated.`);
  23. connected = false;
  24. });
  25. client.on("disconnected", () => {
  26. logger.info("DISCORD_DISCONNECTED", `Discord client was disconnected.`);
  27. connected = false;
  28. });
  29. client.on("error", err => {
  30. logger.info(
  31. "DISCORD_ERROR",
  32. `Discord client encountered an error: ${err.message}.`
  33. );
  34. });
  35. client.login(discordToken);
  36. if (lockdown) return this._lockdown();
  37. cb();
  38. },
  39. sendAdminAlertMessage: function(message, color, type, critical, extraFields) {
  40. if (!connected) return messagesToSend.push({message, color, type, critical, extraFields});
  41. else {
  42. let channel = client.channels.find("id", this.adminAlertChannelId);
  43. if (channel !== null) {
  44. let richEmbed = new Discord.RichEmbed();
  45. richEmbed.setAuthor(
  46. "Musare Logger",
  47. config.get("domain") + "/favicon-194x194.png",
  48. config.get("domain")
  49. );
  50. richEmbed.setColor(color);
  51. richEmbed.setDescription(message);
  52. //richEmbed.setFooter("Footer", "https://musare.com/favicon-194x194.png");
  53. //richEmbed.setImage("https://musare.com/favicon-194x194.png");
  54. //richEmbed.setThumbnail("https://musare.com/favicon-194x194.png");
  55. richEmbed.setTimestamp(new Date());
  56. richEmbed.setTitle("MUSARE ALERT");
  57. richEmbed.setURL(config.get("domain"));
  58. richEmbed.addField("Type:", type, true);
  59. richEmbed.addField("Critical:", critical ? "True" : "False", true);
  60. extraFields.forEach(extraField => {
  61. richEmbed.addField(
  62. extraField.name,
  63. extraField.value,
  64. extraField.inline
  65. );
  66. });
  67. channel
  68. .send(message, { embed: richEmbed })
  69. .then(message =>
  70. logger.success(
  71. "SEND_ADMIN_ALERT_MESSAGE",
  72. `Sent admin alert message: ${message}`
  73. )
  74. )
  75. .catch(() =>
  76. logger.error(
  77. "SEND_ADMIN_ALERT_MESSAGE",
  78. "Couldn't send admin alert message"
  79. )
  80. );
  81. } else {
  82. logger.error(
  83. "SEND_ADMIN_ALERT_MESSAGE",
  84. "Couldn't send admin alert message, channel was not found."
  85. );
  86. }
  87. }
  88. },
  89. _lockdown: () => {
  90. lockdown = true;
  91. }
  92. };