out-of-your-element/d2m/discord-client.js

67 lines
2.2 KiB
JavaScript
Raw Normal View History

2023-04-25 20:06:08 +00:00
// @ts-check
2022-12-07 00:10:31 +00:00
const { SnowTransfer } = require("snowtransfer")
const { Client: CloudStorm } = require("cloudstorm")
2023-03-05 16:05:35 +00:00
const passthrough = require("../passthrough")
const { sync } = passthrough
2023-04-25 20:06:08 +00:00
/** @type {typeof import("./discord-packets")} */
const discordPackets = sync.require("./discord-packets")
2022-12-07 00:10:31 +00:00
class DiscordClient {
/**
* @param {string} discordToken
2023-09-03 13:38:30 +00:00
* @param {string} listen "full", "half", "no" - whether to set up the event listeners for OOYE to operate
2022-12-07 00:10:31 +00:00
*/
2023-09-03 13:38:30 +00:00
constructor(discordToken, listen = "full") {
2022-12-07 00:10:31 +00:00
this.discordToken = discordToken
this.snow = new SnowTransfer(discordToken)
this.cloud = new CloudStorm(discordToken, {
2023-04-25 20:06:08 +00:00
shards: [0],
2022-12-07 00:10:31 +00:00
reconnect: true,
snowtransferInstance: this.snow,
intents: [
"DIRECT_MESSAGES", "DIRECT_MESSAGE_REACTIONS", "DIRECT_MESSAGE_TYPING",
"GUILDS", "GUILD_EMOJIS_AND_STICKERS", "GUILD_MESSAGES", "GUILD_MESSAGE_REACTIONS", "GUILD_MESSAGE_TYPING", "GUILD_WEBHOOKS",
"MESSAGE_CONTENT"
],
ws: {
compress: false,
encoding: "json"
}
})
2023-03-05 16:05:35 +00:00
this.ready = false
/** @type {import("discord-api-types/v10").APIUser} */
2022-12-07 20:34:05 +00:00
// @ts-ignore avoid setting as or null because we know we need to wait for ready anyways
2022-12-07 00:10:31 +00:00
this.user = null
2023-03-05 16:05:35 +00:00
/** @type {Pick<import("discord-api-types/v10").APIApplication, "id" | "flags">} */
2022-12-07 20:34:05 +00:00
// @ts-ignore
2022-12-07 00:10:31 +00:00
this.application = null
2023-03-05 16:05:35 +00:00
/** @type {Map<string, import("discord-api-types/v10").APIChannel>} */
2022-12-07 00:10:31 +00:00
this.channels = new Map()
2023-03-05 16:05:35 +00:00
/** @type {Map<string, import("discord-api-types/v10").APIGuild>} */
2022-12-07 20:34:05 +00:00
this.guilds = new Map()
2023-03-05 16:05:35 +00:00
/** @type {Map<string, Array<string>>} */
2022-12-07 20:34:05 +00:00
this.guildChannelMap = new Map()
2023-09-03 13:38:30 +00:00
if (listen !== "no") {
this.cloud.on("event", message => discordPackets.onPacket(this, message, listen))
}
2024-01-19 12:01:34 +00:00
const addEventLogger = (eventName, logName) => {
this.cloud.on(eventName, (...args) => {
const d = new Date().toISOString().slice(0, 19)
console.error(`[${d} Client ${logName}]`, ...args)
})
}
addEventLogger("error", "Error")
addEventLogger("disconnected", "Disconnected")
addEventLogger("ready", "Ready")
this.snow.requestHandler.on("requestError", (requestID, error) => {
console.error("request error:", error)
})
2022-12-07 00:10:31 +00:00
}
}
module.exports = DiscordClient