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.

61 lines
1.6 KiB

  1. let sdk = require("matrix-js-sdk");
  2. let message = require("./message.js");
  3. class Bot {
  4. constructor(config) {
  5. this.config = config
  6. }
  7. }
  8. function create(configFile) {
  9. let config = require(configFile);
  10. console.log("Running with config:");
  11. console.log(config);
  12. return new Bot(config);
  13. }
  14. function init(bot) {
  15. console.log("Creating Matrix Client")
  16. bot.client = sdk.createClient({
  17. baseUrl: bot.config.baseUrl,
  18. accessToken: bot.config.accessToken,
  19. userId: bot.config.userId
  20. });
  21. function sendClientStatusUpdate() {
  22. bot.config.statusRooms.forEach(roomId => {
  23. console.log("Notifying %s", roomId);
  24. bot.client.sendMessage(roomId, message.createBasic("Started!")).done(function () {
  25. console.log("Notified %s", roomId);
  26. })
  27. });
  28. }
  29. // Prep the bot
  30. bot.client.on("sync", function (state, previousState, data) {
  31. switch (state) {
  32. case "PREPARED":
  33. sendClientStatusUpdate();
  34. break;
  35. }
  36. });
  37. // auto join rooms that an admin user has invited the bot to
  38. bot.client.on("RoomMember.membership", function (event, member) {
  39. if (member.membership === "invite"
  40. && bot.config.admin.indexOf(ember.userId) >= 0) {
  41. bot.client.joinRoom(member.roomId).done(function () {
  42. console.log("Auto-joined %s", member.roomId);
  43. });
  44. }
  45. });
  46. return bot;
  47. }
  48. function run(bot) {
  49. bot.client.startClient()
  50. }
  51. exports.create = create;
  52. exports.init = init;
  53. exports.run = run;