Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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!");
}
(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;
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);
}
constructor(private roomId: string) {
this.client = new MatrixClient(VoyagerConfig.matrix.homeserverUrl, VoyagerConfig.appservice.asToken);
}
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;
}