From 0b40a5025498f1e84cddf2ceb14c51ec9b91bcd7 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sat, 7 Oct 2023 21:57:09 +1300 Subject: [PATCH] Set profile data in seed.js --- d2m/discord-packets.js | 1 - readme.md | 3 +-- scripts/seed.js | 29 +++++++++++++++++++++++++---- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/d2m/discord-packets.js b/d2m/discord-packets.js index 979f756..dfba8b0 100644 --- a/d2m/discord-packets.js +++ b/d2m/discord-packets.js @@ -25,7 +25,6 @@ const utils = { client.application = message.d.application console.log(`Discord logged in as ${client.user.username}#${client.user.discriminator} (${client.user.id})`) - } else if (message.t === "GUILD_CREATE") { client.guilds.set(message.d.id, message.d) const arr = [] diff --git a/readme.md b/readme.md index a086716..f1a35c7 100644 --- a/readme.md +++ b/readme.md @@ -73,8 +73,7 @@ Follow these steps: 1. Copy `registration.example.yaml` to `registration.yaml` and fill in bracketed values. You could generate each hex string with `dd if=/dev/urandom bs=32 count=1 2> /dev/null | basenc --base16 | dd conv=lcase 2> /dev/null`. Register the registration in Synapse's `homeserver.yaml` through the usual appservice installation process, then restart Synapse. -1. Run `node scripts/seed.js` to check your setup, create the database and server state (only need to run this once ever) - +1. Run `node scripts/seed.js` to check your setup and set the bot's initial state. You only need to run this once ever. 1. Make sure the tests work by running `npm t` 1. Start the bridge: `node start.js` diff --git a/scripts/seed.js b/scripts/seed.js index 3b24768..2a0c279 100644 --- a/scripts/seed.js +++ b/scripts/seed.js @@ -12,7 +12,15 @@ const migrate = require("../db/migrate") const sync = new HeatSync({watchFS: false}) -Object.assign(passthrough, { config, sync, db }) +Object.assign(passthrough, { sync, config, db }) + +const orm = sync.require("../db/orm") +passthrough.from = orm.from +passthrough.select = orm.select + +const DiscordClient = require("../d2m/discord-client") +const discord = new DiscordClient(config.discordToken, "no") +passthrough.discord = discord const api = require("../matrix/api") const file = require("../matrix/file") @@ -27,19 +35,32 @@ const utils = require("../m2d/converters/utils") 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 + 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) + + console.log("✅ Database is ready...") + // ensure homeserver well-known is valid and returns reg.ooye.server_name... // upload initial images... - const avatarUrl = await file.uploadDiscordFileToMxc("https://cadence.moe/friends/out_of_your_element_rev_2.jpg") + const avatarUrl = await file.uploadDiscordFileToMxc("https://cadence.moe/friends/out_of_your_element.png") + + // set profile data on discord... + const avatarImageBuffer = await fetch("https://cadence.moe/friends/out_of_your_element.png").then(res => res.arrayBuffer()) + await discord.snow.user.updateSelf({avatar: "data:image/png;base64," + Buffer.from(avatarImageBuffer).toString("base64")}) + await discord.snow.requestHandler.request(`/applications/@me`, {}, "patch", "json", {description: "Powered by **Out Of Your Element**\nhttps://gitdab.com/cadence/out-of-your-element"}) + console.log("✅ Discord profile updated...") // set profile data on homeserver... await api.profileSetDisplayname(mxid, "Out Of Your Element") await api.profileSetAvatarUrl(mxid, avatarUrl) + console.log("✅ Matrix profile updated...") - // add initial rows to database, like adding the bot to sim... - db.prepare("INSERT INTO sim (user_id, sim_name, localpart, mxid) VALUES (?, ?, ?, ?)").run("0", reg.sender_localpart.slice(reg.ooye.namespace_prefix.length), reg.sender_localpart, mxid) + console.log("Good to go. I hope you enjoy Out Of Your Element.") + process.exit() })()