2024-09-14 12:55:54 +00:00
|
|
|
#!/usr/bin/env node
|
2023-07-10 22:10:47 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const sqlite = require("better-sqlite3")
|
|
|
|
const HeatSync = require("heatsync")
|
|
|
|
|
2024-09-14 12:55:54 +00:00
|
|
|
const {reg} = require("../src/matrix/read-registration")
|
|
|
|
const passthrough = require("../src/passthrough")
|
2024-09-14 13:09:47 +00:00
|
|
|
const db = new sqlite("ooye.db")
|
2023-07-10 22:10:47 +00:00
|
|
|
|
|
|
|
const sync = new HeatSync({watchFS: false})
|
|
|
|
|
2024-09-14 12:55:54 +00:00
|
|
|
Object.assign(passthrough, {sync, db})
|
2023-07-10 22:10:47 +00:00
|
|
|
|
2024-09-14 12:55:54 +00:00
|
|
|
const DiscordClient = require("../src/d2m/discord-client")
|
2023-07-10 22:10:47 +00:00
|
|
|
|
2024-09-14 12:55:54 +00:00
|
|
|
const discord = new DiscordClient(reg.ooye.discord_token, "no")
|
2023-07-10 22:10:47 +00:00
|
|
|
passthrough.discord = discord
|
|
|
|
|
|
|
|
;(async () => {
|
|
|
|
await discord.cloud.connect()
|
2023-08-21 09:04:41 +00:00
|
|
|
console.log("Discord gateway started")
|
2023-07-10 22:10:47 +00:00
|
|
|
|
2023-08-21 09:04:41 +00:00
|
|
|
const f = event => onPacket(discord, event, () => discord.cloud.off("event", f))
|
|
|
|
discord.cloud.on("event", f)
|
2023-07-10 22:10:47 +00:00
|
|
|
})()
|
|
|
|
|
|
|
|
const expectedGuilds = new Set()
|
|
|
|
|
|
|
|
const prepared = db.prepare("UPDATE channel_room SET name = ? WHERE channel_id = ?")
|
|
|
|
|
|
|
|
/** @param {DiscordClient} discord */
|
|
|
|
function onPacket(discord, event, unsubscribe) {
|
2023-08-21 09:04:41 +00:00
|
|
|
if (event.t === "READY") {
|
|
|
|
for (const obj of event.d.guilds) {
|
|
|
|
expectedGuilds.add(obj.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (event.t === "GUILD_CREATE") {
|
|
|
|
expectedGuilds.delete(event.d.id)
|
|
|
|
|
|
|
|
// Store the channel.
|
|
|
|
for (const channel of event.d.channels || []) {
|
|
|
|
prepared.run(channel.name, channel.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checked them all?
|
|
|
|
if (expectedGuilds.size === 0) {
|
|
|
|
discord.cloud.disconnect()
|
|
|
|
unsubscribe()
|
|
|
|
|
|
|
|
// I don't know why node keeps running.
|
|
|
|
setTimeout(() => {
|
|
|
|
console.log("Stopping now.")
|
|
|
|
process.exit()
|
|
|
|
}, 1500).unref()
|
|
|
|
}
|
|
|
|
}
|
2023-07-10 22:10:47 +00:00
|
|
|
}
|