UNPKG

deleo

Version:

Deleo, a TypeScript-based CLI tool, simplifies Discord content management by enabling efficient deletion of messages in open DMs or specific channels.

83 lines (82 loc) 3.11 kB
"use strict"; var e, t; Object.defineProperty(exports, "__esModule", { value: !0 }), function _export(e, t) { for(var n in t)Object.defineProperty(e, n, { enumerable: !0, get: t[n] }); }(exports, { PackageOpener: function() { return PackageOpener; }, PackageOpenerEvents: function() { return e; } }); const n = _interop_require_default(require("events")), i = require("node:fs/promises"), r = require("path"), s = require("@sapphire/result"), a = require("../shared/utils"); function _interop_require_default(e) { return e && e.__esModule ? e : { default: e }; } (t = e || (e = {})).Ready = "ready", t.Load = "load", t.FailedToLoad = "failedToLoad", t.Open = "open", t.FailedToOpen = "failedToOpen", t.Done = "done"; let PackageOpener = class PackageOpener extends n.default { client; options; loadedChannels; constructor(e, t){ super(), this.client = e, this.options = t, this.loadedChannels = []; } async readPackage(e) { return s.Result.fromAsync(async ()=>{ for (let t of (await (0, i.readdir)(e))){ if ("index.json" === t) continue; let n = await s.Result.fromAsync(async ()=>{ let n = await (0, i.readFile)((0, r.join)(e, t, "channel.json"), "utf-8"); return s.Result.ok(JSON.parse(n)); }); if (n.isErr()) { this.emit("failedToLoad", t); continue; } let a = n.unwrap(); a.recipients = a.recipients?.filter((e)=>e !== this.client.user.id) || [], a.recipients.length && (this.loadedChannels.push(a), this.emit("load", a)); } return this.emit("ready", this.loadedChannels), s.Result.ok(this.loadedChannels); }); } async openChannels(e) { return s.Result.fromAsync(async ()=>{ let t = [], n = this.client.channels.cache.filter((e)=>[ "DM", "GROUP_DM" ].includes(e.type)); for (let i of e.filter((e)=>!n.some((t)=>t.id === e.id))){ let e = await s.Result.fromAsync(async ()=>{ if (!i.recipients) return s.Result.err(); let e = await this.openRecipient(i.recipients); return s.Result.ok(e); }); if (e.isErr()) { this.emit("failedToOpen", i); continue; } t.push(e.unwrap()), this.emit("open", e.unwrap()), await (0, a.sleep)(this.options.openDelay); } return this.emit("done", t), s.Result.ok(t); }); } async openRecipient(e, { cache: t = !0 } = {}) { let n = await this.client.api.users("@me").channels.post({ data: { recipients: e }, DiscordContext: {} }), i = this.client.channels._add(n, null, { cache: t }); return await i.sync(), i; } };