Baphomet is the dedicated bot for nulloctet matrix
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.5 KiB

  1. let sdk = require('matrix-js-sdk');
  2. let message = require('./message');
  3. let utility = require('./utility');
  4. let { logger } = require('./logging');
  5. class Bot {
  6. constructor(config, buildInfo) {
  7. this.config = config;
  8. this.buildInfo = buildInfo;
  9. this.connected = false;
  10. }
  11. /**
  12. * Initialize the bot connection
  13. */
  14. init(messageCallback) {
  15. logger.info("Creating Matrix Client")
  16. this.client = sdk.createClient({
  17. baseUrl: this.config.baseUrl,
  18. accessToken: this.config.accessToken,
  19. userId: this.config.userId
  20. });
  21. this.client.on("sync", async (state, previousState, data) => {
  22. switch (state) {
  23. case "PREPARED":
  24. this.connected = true;
  25. await this.sendStatusStartup();
  26. this.client.getJoinedRooms()
  27. .done((rooms) => {
  28. logger.info("Connected to: %o", rooms)
  29. });
  30. break;
  31. case "SYNCING":
  32. logger.debug("Syncing")
  33. break;
  34. case "RECONNECTING":
  35. logger.debug("Reconnecting");
  36. break;
  37. default:
  38. logger.error("Unexpected sync state: %s", state);
  39. process.exit(1);
  40. }
  41. });
  42. this.client.on("RoomMember.membership", (event, member) => {
  43. if (member.membership === "invite"
  44. && this.config.admin.indexOf(ember.userId) >= 0) {
  45. this.client.joinRoom(member.roomId).done(() => {
  46. logger.info("Auto-joined %s", member.roomId);
  47. });
  48. }
  49. });
  50. this.client.on("Room.timeline", messageCallback);
  51. return this;
  52. }
  53. async connect() {
  54. // logger.info("Initializing Crypto");
  55. // await bot.client.initCrypto();
  56. logger.info("Starting Matrix SDK Client");
  57. await this.client.startClient();
  58. }
  59. sendStatusStartup() {
  60. let promises = [Promise.resolve(true)]
  61. if (this.connected) {
  62. this.config.statusRooms.forEach(roomId => {
  63. logger.debug("Notifying %s of startup", roomId);
  64. promises.push(this.client.sendMessage(
  65. roomId, message.createBasic("Started with version: " + this.buildInfo, message.types.NOTICE)
  66. ).then(() => {
  67. logger.debug("Notified %s of startup", roomId);
  68. }));
  69. });
  70. } else {
  71. logger.warn("Attempting to send startup message while disconnected");
  72. }
  73. return Promise.all(promises);
  74. }
  75. sendStatusShutdown() {
  76. let promises = [Promise.resolve(true)]
  77. if (this.connected) {
  78. this.config.statusRooms.forEach(roomId => {
  79. logger.debug("Notifying %s of shutdown", roomId);
  80. promises.push(this.client.sendMessage(
  81. roomId, message.createBasic("Shutting down", message.types.NOTICE)
  82. ).then(() => {
  83. logger.debug("Notified %s of shutdown", roomId);
  84. }));
  85. });
  86. } else {
  87. logger.warn("Attempting to send shutdown message while disconnected");
  88. }
  89. return Promise.all(promises);
  90. }
  91. }
  92. function create(config) {
  93. let buildInfo = utility.getBuildInfo("../build.info")
  94. logger.info("Running version: %s", buildInfo);
  95. return new Bot(config, buildInfo);
  96. }
  97. exports.create = create;