1
0
Fork 0

seed.js should now upload the L1 & L2 emojis

This commit is contained in:
Cadence Ember 2023-10-07 23:39:49 +13:00
parent 9e5a3f5f32
commit 3ef569d855
8 changed files with 79 additions and 2 deletions

View file

@ -0,0 +1,10 @@
BEGIN TRANSACTION;
CREATE TABLE auto_emoji (
name TEXT NOT NULL,
emoji_id TEXT NOT NULL,
guild_id TEXT NOT NULL,
PRIMARY KEY (name)
) WITHOUT ROWID;
COMMIT;

6
db/orm-defs.d.ts vendored
View file

@ -75,6 +75,12 @@ export type Models = {
message_id: string message_id: string
encoded_emoji: string encoded_emoji: string
} }
auto_emoji: {
name: string
emoji_id: string
guild_id: string
}
} }
export type Prepared<Row> = { export type Prepared<Row> = {

BIN
docs/img/L1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
docs/img/L2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 514 B

3
package-lock.json generated
View file

@ -7,7 +7,7 @@
"": { "": {
"name": "out-of-your-element", "name": "out-of-your-element",
"version": "1.0.0", "version": "1.0.0",
"license": "MIT", "license": "AGPL-3.0-or-later",
"dependencies": { "dependencies": {
"@chriscdn/promise-semaphore": "^2.0.1", "@chriscdn/promise-semaphore": "^2.0.1",
"better-sqlite3": "^8.3.0", "better-sqlite3": "^8.3.0",
@ -18,6 +18,7 @@
"heatsync": "^2.4.1", "heatsync": "^2.4.1",
"js-yaml": "^4.1.0", "js-yaml": "^4.1.0",
"matrix-appservice": "^2.0.0", "matrix-appservice": "^2.0.0",
"minimist": "^1.2.8",
"mixin-deep": "github:cloudrac3r/mixin-deep#v3.0.0", "mixin-deep": "github:cloudrac3r/mixin-deep#v3.0.0",
"node-fetch": "^2.6.7", "node-fetch": "^2.6.7",
"pngjs": "^7.0.0", "pngjs": "^7.0.0",

View file

@ -24,6 +24,7 @@
"heatsync": "^2.4.1", "heatsync": "^2.4.1",
"js-yaml": "^4.1.0", "js-yaml": "^4.1.0",
"matrix-appservice": "^2.0.0", "matrix-appservice": "^2.0.0",
"minimist": "^1.2.8",
"mixin-deep": "github:cloudrac3r/mixin-deep#v3.0.0", "mixin-deep": "github:cloudrac3r/mixin-deep#v3.0.0",
"node-fetch": "^2.6.7", "node-fetch": "^2.6.7",
"pngjs": "^7.0.0", "pngjs": "^7.0.0",

View file

@ -59,7 +59,6 @@ You'll need:
* Administrative access to a homeserver * Administrative access to a homeserver
* Discord bot * Discord bot
* Custom emojis named `L1` and `L2` for replies sent to Discord (TODO: provide)
Follow these steps: Follow these steps:
@ -148,6 +147,7 @@ Follow these steps:
* (1) heatsync: Module hot-reloader that I trust. * (1) heatsync: Module hot-reloader that I trust.
* (1) js-yaml: It seems to do what I want, and it's already pulled in by matrix-appservice. * (1) js-yaml: It seems to do what I want, and it's already pulled in by matrix-appservice.
* (70) matrix-appservice: I wish it didn't pull in express :( * (70) matrix-appservice: I wish it didn't pull in express :(
* (0) minimist: It's already pulled in by better-sqlite3->prebuild-install
* (0) mixin-deep: This is my fork! It fixes a bug in regular mixin-deep. * (0) mixin-deep: This is my fork! It fixes a bug in regular mixin-deep.
* (3) node-fetch@2: I like it and it does what I want. * (3) node-fetch@2: I like it and it does what I want.
* (0) pngjs: Lottie stickers are converted to bitmaps with the vendored Rlottie WASM build, then the bitmaps are converted to PNG with pngjs. * (0) pngjs: Lottie stickers are converted to bitmaps with the vendored Rlottie WASM build, then the bitmaps are converted to PNG with pngjs.

View file

@ -1,10 +1,14 @@
// @ts-check // @ts-check
console.log("This could take up to 30 seconds. Please be patient.")
const assert = require("assert").strict const assert = require("assert").strict
const fs = require("fs") const fs = require("fs")
const sqlite = require("better-sqlite3") const sqlite = require("better-sqlite3")
const HeatSync = require("heatsync") const HeatSync = require("heatsync")
const args = require("minimist")(process.argv.slice(2), {string: ["emoji-guild"]})
const config = require("../config") const config = require("../config")
const passthrough = require("../passthrough") const passthrough = require("../passthrough")
const db = new sqlite("db/ooye.db") const db = new sqlite("db/ooye.db")
@ -27,6 +31,24 @@ const file = require("../matrix/file")
const reg = require("../matrix/read-registration") const reg = require("../matrix/read-registration")
const utils = require("../m2d/converters/utils") const utils = require("../m2d/converters/utils")
function die(message) {
console.error(message)
process.exit(1)
}
async function uploadAutoEmoji(guild, name, filename) {
let emoji = guild.emojis.find(e => e.name === name)
if (!emoji) {
console.log(` Uploading ${name}...`)
const data = fs.readFileSync(filename, null)
emoji = await discord.snow.guildAssets.createEmoji(guild.id, {name, image: "data:image/png;base64," + data.toString("base64")})
} else {
console.log(` Reusing ${name}...`)
}
db.prepare("REPLACE INTO auto_emoji (name, emoji_id, guild_id) VALUES (?, ?, ?)").run(emoji.name, emoji.id, guild.id)
return emoji
}
;(async () => { ;(async () => {
const mxid = `@${reg.sender_localpart}:${reg.ooye.server_name}` const mxid = `@${reg.sender_localpart}:${reg.ooye.server_name}`
@ -45,6 +67,43 @@ const utils = require("../m2d/converters/utils")
console.log("✅ Database is ready...") console.log("✅ Database is ready...")
// upload the L1 L2 emojis to some guild
const emojis = db.prepare("SELECT name FROM auto_emoji WHERE name = 'L1' OR name = 'L2'").pluck().all()
if (emojis.length !== 2) {
// If an argument was supplied, always use that one
let guild = null
if (args["emoji-guild"]) {
if (typeof args["emoji-guild"] === "string") {
guild = await discord.snow.guild.getGuild(args["emoji-guild"])
}
if (!guild) return die(`Error: You asked emojis to be uploaded to guild ID ${args["emoji-guild"]}, but the bot isn't in that guild.`)
}
// Otherwise, check if we have already registered an auto emoji guild
if (!guild) {
const guildID = passthrough.select("auto_emoji", "guild_id", {name: "_"}).pluck().get()
if (guildID) {
guild = await discord.snow.guild.getGuild(guildID, false)
}
}
// Otherwise, check if we should create a new guild
if (!guild) {
const guilds = await discord.snow.user.getGuilds({limit: 11, with_counts: false})
if (guilds.length < 10) {
console.log(" Creating a guild for emojis...")
guild = await discord.snow.guild.createGuild({name: "OOYE Emojis"})
}
}
// Otherwise, it's the user's problem
if (!guild) {
return die(`Error: The bot needs to upload some emojis. Please say where to upload them to. Run seed.js again with --emoji-guild=GUILD_ID`)
}
// Upload those emojis to the chosen location
db.prepare("REPLACE INTO auto_emoji (name, emoji_id, guild_id) VALUES ('_', '_', ?)").run(guild.id)
await uploadAutoEmoji(guild, "L1", "docs/img/L1.png")
await uploadAutoEmoji(guild, "L2", "docs/img/L2.png")
}
console.log("✅ Emojis are ready...")
// ensure homeserver well-known is valid and returns reg.ooye.server_name... // ensure homeserver well-known is valid and returns reg.ooye.server_name...
// upload initial images... // upload initial images...