How to use the matrix-bot-sdk.MatrixClient function in matrix-bot-sdk

To help you get started, we’ve selected a few matrix-bot-sdk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bnjbvr / botzilla / src / index.js View on Github external
if (!fs.existsSync("./data")) {
    fs.mkdirSync("./data");
  }
  if (!fs.existsSync(storageDir)) {
    fs.mkdirSync(storageDir);
  }

  // We'll want to make sure the bot doesn't have to do an initial sync every
  // time it restarts, so we need to prepare a storage provider. Here we use
  // a simple JSON database.
  const storage = new SimpleFsStorageProvider(
    path.join(storageDir, "matrix.json")
  );

  // Now we can create the client and set it up to automatically join rooms.
  const client = new MatrixClient(
    config.homeserverUrl,
    config.accessToken,
    storage
  );
  AutojoinRoomsMixin.setupOnClient(client);

  // We also want to make sure we can receive events - this is where we will
  // handle our command.
  client.on("room.message", makeHandleCommand(client, config));

  // Now that the client is all set up and the event handler is registered, start the
  // client up. This will start it syncing.
  await client.start();
  console.log("Client started!");
}
github matrix-org / mjolnir / src / index.ts View on Github external
(async function () {
    const storage = new SimpleFsStorageProvider(path.join(config.dataPath, "bot.json"));

    let client: MatrixClient;
    if (config.pantalaimon.use) {
        const pantalaimon = new PantalaimonClient(config.homeserverUrl, storage);
        client = await pantalaimon.createClientWithCredentials(config.pantalaimon.username, config.pantalaimon.password);
    } else {
        client = new MatrixClient(config.homeserverUrl, config.accessToken, storage);
    }

    if (config.autojoin) {
        AutojoinRoomsMixin.setupOnClient(client);
    }

    const banLists: BanList[] = [];
    const protectedRooms: { [roomId: string]: string } = {};

    const joinedRooms = await client.getJoinedRooms();

    // Ensure we're also joined to the rooms we're protecting
    for (const roomRef of config.protectedRooms) {
        const permalink = Permalinks.parseUrl(roomRef);
        if (!permalink.roomIdOrAlias) continue;
github turt2live / matrix-dimension / src / matrix / MatrixStickerBot.ts View on Github external
constructor() {
        this.client = new MatrixClient(
            config.homeserver.clientServerUrl,
            config.homeserver.accessToken,
            new SimpleFsStorageProvider(config.database.botData));

        this.client.setJoinStrategy(new SimpleRetryJoinStrategy());
        this.client.on("room.event", this.onEvent.bind(this));
        AutojoinUpgradedRoomsMixin.setupOnClient(this.client);
    }
github turt2live / matrix-voyager-bot / src / room_handler / RoomStateCalculator.ts View on Github external
constructor(private roomId: string) {
        this.client = new MatrixClient(VoyagerConfig.matrix.homeserverUrl, VoyagerConfig.appservice.asToken);
    }
github turt2live / matrix-voyager-bot / src / AvatarCache.ts View on Github external
public static async getMxcForItem(name: string): Promise {
        const hex = COLOR_HASH.hex(name).substring(1);
        const avatarUrl = url.resolve(VoyagerConfig.misc.uiAvatarsUrl, `/api?color=fff&size=512&background=${hex}&name=${name.startsWith('#') ? name.substring(1)[0] : name[0]}`);

        const existingAvatar = AvatarCache.getMxcForUrl(avatarUrl);
        if (existingAvatar) return existingAvatar;

        const client = new MatrixClient(VoyagerConfig.matrix.homeserverUrl, VoyagerConfig.appservice.asToken);
        const buf = await downloadFromUrl(avatarUrl);
        const mxc = await client.uploadContent(buf, "image/png");
        AvatarCache.setMxcForUrl(avatarUrl, mxc);

        return mxc;
    }