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.

100 lines
3.2 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", (state, previousState, data) => {
  22. switch (state) {
  23. case "PREPARED":
  24. this.connected = true;
  25. this.sendStatusStartup();
  26. break;
  27. case "SYNCING":
  28. logger.debug("Syncing")
  29. break;
  30. default:
  31. logger.error("Unexpected sync state: %s", state);
  32. process.exit(1);
  33. }
  34. });
  35. this.client.on("RoomMember.membership", (event, member) => {
  36. if (member.membership === "invite"
  37. && this.config.admin.indexOf(ember.userId) >= 0) {
  38. this.client.joinRoom(member.roomId).done(() => {
  39. logger.info("Auto-joined %s", member.roomId);
  40. });
  41. }
  42. });
  43. this.client.on("Room.timeline", messageCallback);
  44. return this;
  45. }
  46. connect() {
  47. // logger.info("Initializing Crypto");
  48. // await bot.client.initCrypto();
  49. logger.info("Starting Matrix SDK Client");
  50. this.client.startClient();
  51. }
  52. sendStatusStartup() {
  53. let promises = [Promise.resolve(true)]
  54. if (this.connected) {
  55. this.config.statusRooms.forEach(roomId => {
  56. logger.debug("Notifying %s of startup", roomId);
  57. promises.push(this.client.sendMessage(
  58. roomId, message.createBasic("Started with version: " + this.buildInfo, message.types.NOTICE)
  59. ).then(() => {
  60. logger.debug("Notified %s of startup", roomId);
  61. }));
  62. });
  63. } else {
  64. logger.warn("Attempting to send startup message while disconnected");
  65. }
  66. return Promise.all(promises);
  67. }
  68. sendStatusShutdown() {
  69. let promises = [Promise.resolve(true)]
  70. if (this.connected) {
  71. this.config.statusRooms.forEach(roomId => {
  72. logger.debug("Notifying %s of shutdown", roomId);
  73. promises.push(this.client.sendMessage(
  74. roomId, message.createBasic("Shutting down", message.types.NOTICE)
  75. ).then(() => {
  76. logger.debug("Notified %s of shutdown", roomId);
  77. }));
  78. });
  79. } else {
  80. logger.warn("Attempting to send shutdown message while disconnected");
  81. }
  82. return Promise.all(promises);
  83. }
  84. }
  85. function create(config) {
  86. let buildInfo = utility.getBuildInfo("../build.info")
  87. logger.info("Running version: %s", buildInfo);
  88. return new Bot(config, buildInfo);
  89. }
  90. exports.create = create;