From 566b2a9d9e3ddfec02725c7713c74829a5827ecf Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sat, 23 Mar 2024 18:39:37 +1300 Subject: [PATCH] Move bridge bot to its real ID in the database --- .../0011-move-bridge-bot-to-real-id.up.js | 16 ++++++++++++++++ scripts/seed.js | 12 +++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 db/migrations/0011-move-bridge-bot-to-real-id.up.js diff --git a/db/migrations/0011-move-bridge-bot-to-real-id.up.js b/db/migrations/0011-move-bridge-bot-to-real-id.up.js new file mode 100644 index 0000000..1808fcd --- /dev/null +++ b/db/migrations/0011-move-bridge-bot-to-real-id.up.js @@ -0,0 +1,16 @@ +/* + a. If the bridge bot sim already has the correct ID: + - No rows updated. + + b. If the bridge bot sim has the wrong ID but there's no duplicate: + - One row updated. + + c. If the bridge bot sim has the wrong ID and there's a duplicate: + - One row updated (replaces an existing row). +*/ + +module.exports = async function(db) { + const config = require("../../config") + const id = Buffer.from(config.discordToken.split(".")[0], "base64").toString() + db.prepare("UPDATE OR REPLACE sim SET user_id = ? WHERE user_id = '0'").run(id) +} diff --git a/scripts/seed.js b/scripts/seed.js index 1a21490..2ab3e02 100644 --- a/scripts/seed.js +++ b/scripts/seed.js @@ -53,17 +53,19 @@ async function uploadAutoEmoji(guild, name, filename) { const mxid = `@${reg.sender_localpart}:${reg.ooye.server_name}` // ensure registration is correctly set... - assert(reg.sender_localpart.startsWith(reg.ooye.namespace_prefix)) // appservice's localpart must be in the namespace it controls - assert(utils.eventSenderIsFromDiscord(mxid)) // appservice's mxid must be in the namespace it controls - assert(reg.ooye.server_origin.match(/^https?:\/\//)) // must start with http or https - assert.notEqual(reg.ooye.server_origin.slice(-1), "/") // must not end in slash + assert(reg.sender_localpart.startsWith(reg.ooye.namespace_prefix), "appservice's localpart must be in the namespace it controls") + assert(utils.eventSenderIsFromDiscord(mxid), "appservice's mxid must be in the namespace it controls") + assert(reg.ooye.server_origin.match(/^https?:\/\//), "server origin must start with http or https") + assert.notEqual(reg.ooye.server_origin.slice(-1), "/", "server origin must not end in slash") + const botID = Buffer.from(config.discordToken.split(".")[0], "base64").toString() + assert(botID.match(/^[0-9]{10,}$/), "discord token must follow the correct format") console.log("✅ Configuration looks good...") // database ddl... await migrate.migrate(db) // add initial rows to database, like adding the bot to sim... - db.prepare("INSERT OR IGNORE INTO sim (user_id, sim_name, localpart, mxid) VALUES (?, ?, ?, ?)").run("0", reg.sender_localpart.slice(reg.ooye.namespace_prefix.length), reg.sender_localpart, mxid) + db.prepare("INSERT OR IGNORE INTO sim (user_id, sim_name, localpart, mxid) VALUES (?, ?, ?, ?)").run(botID, reg.sender_localpart.slice(reg.ooye.namespace_prefix.length), reg.sender_localpart, mxid) console.log("✅ Database is ready...")