Move client to files.ts

This commit is contained in:
split / May 2023-11-02 17:30:00 +00:00
parent 2e29d40a1b
commit 107709a98a
2 changed files with 16 additions and 18 deletions

View file

@ -1,4 +1,3 @@
import { IntentsBitField, Client } from "discord.js"
import { serve } from "@hono/node-server" import { serve } from "@hono/node-server"
import { serveStatic } from "@hono/node-server/serve-static" import { serveStatic } from "@hono/node-server/serve-static"
import { Hono } from "hono" import { Hono } from "hono"
@ -67,16 +66,7 @@ if (!fs.existsSync(__dirname + "/../.data/"))
fs.mkdirSync(__dirname + "/../.data/") fs.mkdirSync(__dirname + "/../.data/")
// discord // discord
let files = new Files(config)
let client = new Client({
intents: [
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
],
rest: { timeout: config.requestTimeout },
})
let files = new Files(client, config)
const apiRouter = new APIRouter(files) const apiRouter = new APIRouter(files)
apiRouter.loadAPIMethods().then(() => { apiRouter.loadAPIMethods().then(() => {
@ -117,6 +107,4 @@ serve(
} }
) )
client.login(process.env.TOKEN)
export = app export = app

View file

@ -1,5 +1,5 @@
import axios from "axios" import axios from "axios"
import Discord, { Client, Message, TextBasedChannel } from "discord.js" import Discord, { Client, Message, TextBasedChannel, IntentsBitField } from "discord.js"
import { readFile, writeFile } from "node:fs/promises" import { readFile, writeFile } from "node:fs/promises"
import { Readable } from "node:stream" import { Readable } from "node:stream"
import crypto from "node:crypto" import crypto from "node:crypto"
@ -74,14 +74,21 @@ export default class Files {
files: { [key: string]: FilePointer } = {} files: { [key: string]: FilePointer } = {}
uploadChannel?: TextBasedChannel uploadChannel?: TextBasedChannel
constructor(client: Client, config: Configuration) { constructor(config: Configuration) {
this.config = config this.config = config
this.client = client this.client = new Client({
intents: [
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
],
rest: { timeout: config.requestTimeout },
})
client.on("ready", () => { this.client.on("ready", () => {
console.log("Discord OK!") console.log("Discord OK!")
client.guilds.fetch(config.targetGuild).then((g) => { this.client.guilds.fetch(config.targetGuild).then((g) => {
g.channels.fetch(config.targetChannel).then((a) => { g.channels.fetch(config.targetChannel).then((a) => {
if (a?.isTextBased()) { if (a?.isTextBased()) {
this.uploadChannel = a this.uploadChannel = a
@ -90,6 +97,8 @@ export default class Files {
}) })
}) })
this.client.login(process.env.TOKEN)
readFile(process.cwd() + "/.data/files.json") readFile(process.cwd() + "/.data/files.json")
.then((buf) => { .then((buf) => {
this.files = JSON.parse(buf.toString() || "{}") this.files = JSON.parse(buf.toString() || "{}")
@ -333,6 +342,7 @@ export default class Files {
let attach = Array.from(msg.attachments.values()) let attach = Array.from(msg.attachments.values())
for ( for (
let i = let i =
useRanges && xi == scan_msg_begin useRanges && xi == scan_msg_begin
? scan_files_begin - xi * 10 ? scan_files_begin - xi * 10
: 0; : 0;