diff --git a/src/bot.ts b/src/bot.ts index eee7eb5424b3a83060dfa8979b4a7eb08de4d4de..176d81c87f3e651836420f94ca524fafc4fcf300 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -71,7 +71,7 @@ export class DiscordBot { private discordMsgProcessor: DiscordMessageProcessor; private mxEventProcessor: MatrixEventProcessor; private presenceHandler: PresenceHandler; - private userSync: UserSyncroniser; + private userSync!: UserSyncroniser; private channelSync: ChannelSyncroniser; private roomHandler: MatrixRoomHandler; private provisioner: Provisioner; @@ -100,8 +100,6 @@ export class DiscordBot { new MatrixEventProcessorOpts(config, bridge, this), ); this.channelSync = new ChannelSyncroniser(bridge, config, this, store.roomStore); - this.userSync = new UserSyncroniser(bridge, config, this); - // init vars this.sentMessages = []; this.discordMessageQueue = {}; @@ -140,6 +138,8 @@ export class DiscordBot { public async init(): Promise<void> { await this.clientFactory.init(); + // This immediately pokes UserStore, so it must be created after the bridge has started. + this.userSync = new UserSyncroniser(this.bridge, this.config, this); } public async run(): Promise<void> { diff --git a/src/discordas.ts b/src/discordas.ts index 9115b3d6a5a5b11aa8958f5add2d4a6939372ca5..1a5c67dd1dc272eac15c9ceb5bd6b90a123d43ae 100644 --- a/src/discordas.ts +++ b/src/discordas.ts @@ -141,7 +141,19 @@ async function run(port: number, fileConfig: DiscordBridgeConfig) { registration, userStore: config.database.userStorePath, }); - // Warn and deprecate old config options. + + if (config.database.roomStorePath) { + log.warn("[DEPRECATED] The room store is now part of the SQL database." + + "The config option roomStorePath no longer has any use."); + } + + try { + await store.init(undefined, bridge.getRoomStore()); + } catch (ex) { + log.error("Failed to init database. Exiting.", ex); + process.exit(1); + } + const discordbot = new DiscordBot(botUserId, config, bridge, store); const roomhandler = discordbot.RoomHandler; @@ -162,8 +174,6 @@ async function run(port: number, fileConfig: DiscordBridgeConfig) { try { await bridge.run(port, config); log.info(`Started listening on port ${port}`); - await store.init(undefined, bridge.getRoomStore()); - log.info("Initing bot"); await discordbot.init(); await discordbot.run(); log.info("Discordbot started successfully"); diff --git a/src/store.ts b/src/store.ts index f17bf143a6c454755d93d389545d97afacaba0df..0b69ee82c92a1e60e54c04154dea9dd667b6c9c3 100644 --- a/src/store.ts +++ b/src/store.ts @@ -92,11 +92,15 @@ export class DiscordStore { await this.open_database(); let version = await this.getSchemaVersion(); const targetSchema = overrideSchema || CURRENT_SCHEMA; + log.info(`Database schema version is ${version}, latest version is ${targetSchema}`); while (version < targetSchema) { version++; const schemaClass = require(`./db/schema/v${version}.js`).Schema; let schema: IDbSchema; if (version === SCHEMA_ROOM_STORE_REQUIRED) { // 8 requires access to the roomstore. + if (!roomStore) { + throw Error("Schema 8 requires the room store, but it was not provided"); + } schema = (new schemaClass(roomStore) as IDbSchema); } else { schema = (new schemaClass() as IDbSchema);