From a43b20180fcb772e1c543df6a56c4b7a30184ae4 Mon Sep 17 00:00:00 2001 From: cirroskais <34550332+cirroskais@users.noreply.github.com> Date: Mon, 12 Feb 2024 02:43:32 -0500 Subject: [PATCH] Fuck --- .env.example | 1 + .gitignore | 177 +++++++++++++++++++++++++++ README.md | 15 +++ bin/delete.js | 19 +++ bin/list.js | 21 ++++ bin/update.js | 21 ++++ bun.lockb | Bin 0 -> 6804 bytes jsconfig.json | 22 ++++ package.json | 24 ++++ src/commands/changes.js | 24 ++++ src/commands/meta.js | 56 +++++++++ src/commands/mod.js | 77 ++++++++++++ src/commands/modify.js | 49 ++++++++ src/commands/ping.js | 21 ++++ src/commands/publish.js | 15 +++ src/components/addMod.js | 36 ++++++ src/discord.js | 33 +++++ src/events/interactionCreate.js | 19 +++ src/events/ready.js | 5 + src/index.js | 8 ++ src/lib/cache.js | 2 + src/lib/classes/BaseCommand.js | 8 ++ src/lib/classes/CommandResponse.js | 1 + src/lib/classes/Embed.js | 1 + src/lib/curseforge.js | 52 ++++++++ src/lib/database.js | 10 ++ src/lib/handlers/CommandHandler.js | 40 ++++++ src/lib/handlers/ComponentHandler.js | 42 +++++++ src/lib/handlers/EventHandler.js | 22 ++++ src/lib/handlers/RESTHandler.js | 7 ++ src/lib/modpack.js | 9 ++ src/lib/modrinth.js | 3 + src/routes/getAllMods.js | 1 + src/routes/getMod.js | 1 + src/routes/getUpdates.js | 1 + src/routes/index.js | 1 + src/server.js | 21 ++++ 37 files changed, 865 insertions(+) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 README.md create mode 100644 bin/delete.js create mode 100644 bin/list.js create mode 100644 bin/update.js create mode 100755 bun.lockb create mode 100644 jsconfig.json create mode 100644 package.json create mode 100644 src/commands/changes.js create mode 100644 src/commands/meta.js create mode 100644 src/commands/mod.js create mode 100644 src/commands/modify.js create mode 100644 src/commands/ping.js create mode 100644 src/commands/publish.js create mode 100644 src/components/addMod.js create mode 100644 src/discord.js create mode 100644 src/events/interactionCreate.js create mode 100644 src/events/ready.js create mode 100644 src/index.js create mode 100644 src/lib/cache.js create mode 100644 src/lib/classes/BaseCommand.js create mode 100644 src/lib/classes/CommandResponse.js create mode 100644 src/lib/classes/Embed.js create mode 100644 src/lib/curseforge.js create mode 100644 src/lib/database.js create mode 100644 src/lib/handlers/CommandHandler.js create mode 100644 src/lib/handlers/ComponentHandler.js create mode 100644 src/lib/handlers/EventHandler.js create mode 100644 src/lib/handlers/RESTHandler.js create mode 100644 src/lib/modpack.js create mode 100644 src/lib/modrinth.js create mode 100644 src/routes/getAllMods.js create mode 100644 src/routes/getMod.js create mode 100644 src/routes/getUpdates.js create mode 100644 src/routes/index.js create mode 100644 src/server.js diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..f74c661 --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +TOKEN= \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b8c6b16 --- /dev/null +++ b/.gitignore @@ -0,0 +1,177 @@ +# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore + +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Caches + +.cache + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +# IntelliJ based IDEs +.idea + +# Finder (MacOS) folder config +.DS_Store + +data \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6272b92 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# bun-terry + +To install dependencies: + +```bash +bun install +``` + +To run: + +```bash +bun run src/index.js +``` + +This project was created using `bun init` in bun v1.0.26. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. diff --git a/bin/delete.js b/bin/delete.js new file mode 100644 index 0000000..18aaab8 --- /dev/null +++ b/bin/delete.js @@ -0,0 +1,19 @@ +import { logger } from "@discordeno/utils" + +import REST from "../src/lib/handlers/RESTHandler" +import CommandHandler from "../src/lib/handlers/CommandHandler" + +const SlashCommandHandler = new CommandHandler() +const commands = await SlashCommandHandler.load() +const registered = await REST.getGlobalApplicationCommands() + +for (let { id, name, type } of registered) { + const cmd = commands.get(name) + if (cmd && cmd.type == type) continue + + await REST.deleteGlobalApplicationCommand(id) + + console.log(`Deleted ${name}(${id})`) +} + +process.exit() diff --git a/bin/list.js b/bin/list.js new file mode 100644 index 0000000..e602edc --- /dev/null +++ b/bin/list.js @@ -0,0 +1,21 @@ +import { logger } from "@discordeno/utils" + +import REST from "../src/lib/handlers/RESTHandler" +import CommandHandler from "../src/lib/handlers/CommandHandler" + +const TYPES = ["CHAT_INPUT", "USER", "MESSAGE"] + +const SlashCommandHandler = new CommandHandler() +const registered = await REST.getGlobalApplicationCommands() + +console.log("┌" + "-".repeat(58) + "┐") +console.log("| " + "ID".padEnd(24, " ") + " | " + "NAME".padEnd(16, " ") + " | " + "TYPE".padEnd(10, " ") + " |") +console.log("├" + "-".repeat(58) + "┤") + +for (let { id, name, type } of registered) { + console.log("| " + id.padEnd(24, " ") + " | " + name.padEnd(16, " ") + " | " + TYPES[type - 1].padEnd(10, " ") + " |") +} + +console.log("└" + "-".repeat(58) + "┘") + +process.exit() diff --git a/bin/update.js b/bin/update.js new file mode 100644 index 0000000..5a38c79 --- /dev/null +++ b/bin/update.js @@ -0,0 +1,21 @@ +import { logger } from "@discordeno/utils" + +import REST from "../src/lib/handlers/RESTHandler" +import CommandHandler from "../src/lib/handlers/CommandHandler" + +const SlashCommandHandler = new CommandHandler() +const commands = await SlashCommandHandler.load() + +for (let [key, value] of commands.entries()) { + const command = await REST.createGlobalApplicationCommand({ + name: key, + type: value.type, + description: value?.description || "", + options: value?.options, + nsfw: value?.nsfw, + }).catch((e) => console.log(e)) + + console.log(`Registered ${command.name}(${command.id})`) +} + +process.exit() diff --git a/bun.lockb b/bun.lockb new file mode 100755 index 0000000000000000000000000000000000000000..be5215f22e93200fa42632db1472bf0654c2fc47 GIT binary patch literal 6804 zcmeHL2~?9;7EWTUNGZ7Bg3AzGa7{u&0zuHC;=&?_SP&Pa0tpb71d=}>S}`gr)>bPh z=vWopj<|v&jMj>b!f*rxv~{B@&?#C4Tnd7t7UtfN2frIKbJwu)i&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ga4bL%cYXxu_PxPeJDynYQ+*6;i(8$Tp|@Ngfq<%`0rR$nKQKfeu>J>s{smVM BV;}$k literal 0 HcmV?d00001 diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000..dcd8fc5 --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "lib": ["ESNext"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "jsx": "react-jsx", + "allowJs": true, + + /* Bundler mode */ + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + /* Linting */ + "skipLibCheck": true, + "strict": true, + "noFallthroughCasesInSwitch": true, + "forceConsistentCasingInFileNames": true + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..30200a2 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "bun-terry", + "module": "src/index.js", + "devDependencies": { + "@types/bun": "latest", + "@discordeno/types": "^19.0.0-next.d81b28a" + }, + "peerDependencies": { + "typescript": "^5.0.0" + }, + "scripts": { + "dev": "NODE_ENV=development bun --watch ./src/index.js", + "start": "bun ./src/index.js", + "bot:delete": "bun ./bin/delete.js", + "bot:update": "bun ./bin/update.js", + "bot:list": "bun ./bin/list.js" + }, + "type": "module", + "dependencies": { + "@discordeno/bot": "^19.0.0-next.d81b28a", + "@discordeno/rest": "^19.0.0-next.d81b28a", + "@discordeno/utils": "^19.0.0-next.d81b28a" + } +} diff --git a/src/commands/changes.js b/src/commands/changes.js new file mode 100644 index 0000000..deb7048 --- /dev/null +++ b/src/commands/changes.js @@ -0,0 +1,24 @@ +import BaseCommand from "../lib/classes/BaseCommand" +import { staged } from "../lib/modpack" + +export default class Command extends BaseCommand { + static type = 1 + static name = "changes" + static description = "Get staged changes to the modpack" + + constructor(data) { + super(data) + } + + async run(interaction) { + let text = "" + + if (!staged.size) return interaction.respond("No changes to the modpack have been staged.") + + for (let [key, value] of staged.entries()) { + text += (value.action == "add" ? "+" : "-") + " " + key + " " + value.url + } + + interaction.respond("```diff\n" + text + "\n```") + } +} diff --git a/src/commands/meta.js b/src/commands/meta.js new file mode 100644 index 0000000..6e33058 --- /dev/null +++ b/src/commands/meta.js @@ -0,0 +1,56 @@ +import BaseCommand from "../lib/classes/BaseCommand" + +const META_CHOICES = [ + { name: "loader", value: "loader" }, + { name: "minecraft_version", value: "minecraft_version" }, +] + +export default class Command extends BaseCommand { + static type = 1 + static name = "meta" + static description = "Modify modpack metadata" + static options = [ + { + type: 1, + name: "set", + description: "Set an attribute", + options: [ + { + type: 3, + name: "name", + description: "Attribute name", + required: true, + choices: META_CHOICES, + }, + { + type: 3, + name: "value", + description: "Attribute value", + required: true, + }, + ], + }, + { + type: 1, + name: "get", + description: "Get an attribute", + options: [ + { + type: 3, + name: "name", + description: "Attribute name", + required: true, + choices: META_CHOICES, + }, + ], + }, + ] + + constructor(data) { + super(data) + } + + async run(interaction) { + console.log("meta.js") + } +} diff --git a/src/commands/mod.js b/src/commands/mod.js new file mode 100644 index 0000000..3565463 --- /dev/null +++ b/src/commands/mod.js @@ -0,0 +1,77 @@ +import { ButtonStyles, InteractionResponseTypes } from "@discordeno/types" +import BaseCommand from "../lib/classes/BaseCommand" +import * as curseforge from "../lib/curseforge" +import REST from "../lib/handlers/RESTHandler" + +export default class Command extends BaseCommand { + static type = 1 + static name = "mod" + static description = "Get information about a mod" + static options = [ + { + type: 3, + name: "url", + description: "A link to a Modrinth/Curseforge mod", + required: true, + }, + ] + + constructor(data) { + super(data) + } + + async run(interaction) { + const { data } = interaction + const url = data.options.find((_) => _.name == "url").value + + const parsedUrl = new URL(url) + if (parsedUrl.host.includes("curseforge.com")) { + const mod = await curseforge.getMod(url) + if (!mod) return interaction.respond("Mod not found.") + + const file = await curseforge.getModFiles(mod.id) + + const customId = new URLSearchParams() + customId.append("name", "addMod") + customId.append("modId", mod.id) + customId.append("fileId", file.id) + customId.append("provider", "curseforge") + + return await REST.sendInteractionResponse(interaction.id, interaction.token, { + type: InteractionResponseTypes.ChannelMessageWithSource, + data: { + embeds: [ + { + title: mod.name, + description: mod.summary, + url: mod.links.websiteUrl, + thumbnail: { url: mod.logo.url }, + footer: { text: `Curseforge | ModId ${mod.id} | FileId ${file.id}` }, + }, + ], + components: [ + { + type: 1, + components: [ + { + type: 2, + style: ButtonStyles.Primary, + label: "Add to Modpack", + customId: customId.toString(), + }, + { + type: 2, + style: ButtonStyles.Link, + label: "Download", + url: file.downloadUrl, + }, + ], + }, + ], + }, + }) + } else if (parsedUrl.host.includes("modrinth.com")) { + console.log("Modrinth mod detected") + } + } +} diff --git a/src/commands/modify.js b/src/commands/modify.js new file mode 100644 index 0000000..5b24c2f --- /dev/null +++ b/src/commands/modify.js @@ -0,0 +1,49 @@ +import BaseCommand from "../lib/classes/BaseCommand" + +export default class Command extends BaseCommand { + static type = 1 + static name = "modify" + static description = "Modify the modpack" + static options = [ + { + type: 1, + name: "add", + description: "Add a mod", + options: [ + { + type: 3, + name: "url", + description: "A link to a Modrinth/Curseforge mod", + required: true, + }, + ], + }, + { + type: 1, + name: "remove", + description: "Remove a mod", + options: [ + { + type: 3, + name: "name", + description: "The name of the mod", + autocomplete: true, + required: true, + }, + { + type: 5, + name: "from_modpack", + description: "Toggle removal from staging or the modpack", + }, + ], + }, + ] + + constructor(data) { + super(data) + } + + async run(interaction) { + console.log("modify.js") + } +} diff --git a/src/commands/ping.js b/src/commands/ping.js new file mode 100644 index 0000000..28d8e1f --- /dev/null +++ b/src/commands/ping.js @@ -0,0 +1,21 @@ +import BaseCommand from "../lib/classes/BaseCommand" + +export default class Command extends BaseCommand { + static type = 1 + static name = "ping" + static description = "A test command that responds with the gateway latency" + + constructor(data) { + super(data) + } + + async run(interaction) { + const { data } = interaction + const then = Date.now() + + await interaction.respond("Pinging...") + const ping = Date.now() - then + + return interaction.edit(`${ping}ms`) + } +} diff --git a/src/commands/publish.js b/src/commands/publish.js new file mode 100644 index 0000000..efbc71c --- /dev/null +++ b/src/commands/publish.js @@ -0,0 +1,15 @@ +import BaseCommand from "../lib/classes/BaseCommand" + +export default class Command extends BaseCommand { + static type = 1 + static name = "publish" + static description = "Publish the changes from staging" + + constructor(data) { + super(data) + } + + async run(interaction) { + console.log("publish.js") + } +} diff --git a/src/components/addMod.js b/src/components/addMod.js new file mode 100644 index 0000000..9dc2392 --- /dev/null +++ b/src/components/addMod.js @@ -0,0 +1,36 @@ +import { InteractionResponseTypes } from "@discordeno/types" +import * as curseforge from "../lib/curseforge" +import REST from "../lib/handlers/RESTHandler" +import { staged } from "../lib/modpack" +import { cache } from "../lib/cache" + +export default class Command { + static name = "addMod" + + constructor(data) {} + + async run(interaction) { + const params = new URLSearchParams(interaction.data.customId) + const modId = params.get("modId") + const fileId = params.get("fileId") + const provider = params.get("provider") + + if (provider == "curseforge") { + const mod = await curseforge.getModFromId(modId) + + if (staged.get(mod.data.name)) return interaction.respond(`**${mod.data.name}** is already in the modpack.`, { isPrivate: true }) + + staged.set(mod.data.name, { + modId, + fileId, + provider, + url: mod.data.links.websiteUrl, + action: "add", + }) + + interaction.respond(`Added **${mod.data.name}** to the modpack.`) + } else { + interaction.respond("Not implemented for provider " + provider) + } + } +} diff --git a/src/discord.js b/src/discord.js new file mode 100644 index 0000000..e0f4b2a --- /dev/null +++ b/src/discord.js @@ -0,0 +1,33 @@ +import { createBot, Intents, logger } from "@discordeno/bot" + +import EventHandler from "./lib/handlers/EventHandler" + +export default async function discord() { + const client = createBot({ + token: import.meta.env.DISCORD_TOKEN, + // intents: [], + }) + + client.events = await new EventHandler(client).load() + + client.transformers.desiredProperties.message.content = true + client.transformers.desiredProperties.message.author = true + + client.transformers.desiredProperties.user.id = true + client.transformers.desiredProperties.user.username = true + client.transformers.desiredProperties.user.avatar = true + + client.transformers.desiredProperties.member.user = true + client.transformers.desiredProperties.member.roles = true + client.transformers.desiredProperties.member.permissions = true + + client.transformers.desiredProperties.interaction.id = true + client.transformers.desiredProperties.interaction.type = true + client.transformers.desiredProperties.interaction.data = true + client.transformers.desiredProperties.interaction.token = true + client.transformers.desiredProperties.interaction.guildId = true + client.transformers.desiredProperties.interaction.channelId = true + client.transformers.desiredProperties.interaction.member = true + + client.start() +} diff --git a/src/events/interactionCreate.js b/src/events/interactionCreate.js new file mode 100644 index 0000000..6516dea --- /dev/null +++ b/src/events/interactionCreate.js @@ -0,0 +1,19 @@ +import { logger } from "@discordeno/utils" + +import CommandHandler from "../lib/handlers/CommandHandler" +import ComponentHandler from "../lib/handlers/ComponentHandler" + +const Commands = new CommandHandler() +await Commands.load((cmd) => { + console.log("Loaded command " + cmd.name) +}) + +const Components = new ComponentHandler() +await Components.load((comp) => { + console.log("Loaded component response " + comp.name) +}) + +export default async function (client, interaction) { + if (interaction.type == 2) Commands.check(client, interaction) + if (interaction.type == 3) Components.check(client, interaction) +} diff --git a/src/events/ready.js b/src/events/ready.js new file mode 100644 index 0000000..b0ed572 --- /dev/null +++ b/src/events/ready.js @@ -0,0 +1,5 @@ +import { logger } from "@discordeno/utils" + +export default async function (client, payload) { + console.log(`Logged into ${payload.user.username} on shard ${payload.shardId}`) +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..a82b7c0 --- /dev/null +++ b/src/index.js @@ -0,0 +1,8 @@ +import discord from "./discord" +import server from "./server" + +discord() +server() + +process.on("SIGINT", process.exit) +process.on("SIGTERM", process.exit) diff --git a/src/lib/cache.js b/src/lib/cache.js new file mode 100644 index 0000000..8f23074 --- /dev/null +++ b/src/lib/cache.js @@ -0,0 +1,2 @@ +//lol +export const cache = new Map() diff --git a/src/lib/classes/BaseCommand.js b/src/lib/classes/BaseCommand.js new file mode 100644 index 0000000..6af36d1 --- /dev/null +++ b/src/lib/classes/BaseCommand.js @@ -0,0 +1,8 @@ +import CommandResponse from "./CommandResponse" + +export default class BaseCommand extends CommandResponse { + constructor(data) { + super(data) + this.client = data?.client + } +} diff --git a/src/lib/classes/CommandResponse.js b/src/lib/classes/CommandResponse.js new file mode 100644 index 0000000..67821f7 --- /dev/null +++ b/src/lib/classes/CommandResponse.js @@ -0,0 +1 @@ +export default class CommandResponse {} diff --git a/src/lib/classes/Embed.js b/src/lib/classes/Embed.js new file mode 100644 index 0000000..5d95570 --- /dev/null +++ b/src/lib/classes/Embed.js @@ -0,0 +1 @@ +export default class Embed {} diff --git a/src/lib/curseforge.js b/src/lib/curseforge.js new file mode 100644 index 0000000..322ef27 --- /dev/null +++ b/src/lib/curseforge.js @@ -0,0 +1,52 @@ +const BASE_URL = "https://api.curseforge.com" +const SEARCH_MODS = "/v1/mods/search" +const GET_FILES = "/v1/mods/{modId}/files" +const GET_MOD = "/v1/mods/{modId}" + +const GAME_ID = 432 + +export async function getMod(url, gameVersion = "1.18.2", modLoaderType = 1) { + url = url.split("/") + const slug = url[url.length - 1] + + const query = new URLSearchParams() + query.append("gameId", GAME_ID) + query.append("slug", slug) + query.append("pageSize", 1) + if (gameVersion) query.append("gameVersion", gameVersion) + if (modLoaderType) query.append("modLoaderType", modLoaderType) + + const response = await fetch(BASE_URL + SEARCH_MODS + "?" + query.toString(), { + headers: { + "x-api-key": import.meta.env.CURSEFORGE_API, + }, + }).catch((e) => console.log(e)) + + return (await response.json()).data[0] +} + +export async function getModFromId(modId) { + const response = await fetch(BASE_URL + GET_MOD.replace("{modId}", modId), { + headers: { + "x-api-key": import.meta.env.CURSEFORGE_API, + }, + }).catch((e) => console.log(e)) + + return await response.json() +} + +export async function getModFiles(modId, gameVersion = "1.18.2", modLoaderType = 1) { + const query = new URLSearchParams() + if (gameVersion) query.append("gameVersion", gameVersion) + if (modLoaderType) query.append("modLoaderType", modLoaderType) + + const response = await fetch(BASE_URL + GET_FILES.replace("{modId}", modId) + "?" + query.toString(), { + headers: { + "x-api-key": import.meta.env.CURSEFORGE_API, + }, + }).catch((e) => console.log(e)) + + return (await response.json()).data[0] +} + +export async function downloadMod(id, fileId) {} diff --git a/src/lib/database.js b/src/lib/database.js new file mode 100644 index 0000000..b42ea66 --- /dev/null +++ b/src/lib/database.js @@ -0,0 +1,10 @@ +import { Database } from "bun:sqlite" +const database = new Database("data/database.sqlite") + +database.run(`CREATE TABLE IF NOT EXISTS attributes (key INTEGER PRIMARY KEY, value INTEGER NOT NULL);`) + +database.run(`CREATE TABLE IF NOT EXISTS mods (url INTEGER PRIMARY KEY);`) + +process.on("exit", () => { + database.close() +}) diff --git a/src/lib/handlers/CommandHandler.js b/src/lib/handlers/CommandHandler.js new file mode 100644 index 0000000..2206673 --- /dev/null +++ b/src/lib/handlers/CommandHandler.js @@ -0,0 +1,40 @@ +import { readdir } from "node:fs/promises" + +export default class CommandHandler { + constructor() { + this.cache = new Map() + } + + async load(postLoad = () => {}) { + const dir = await readdir("src/commands") + for (let file of dir) { + const cmd = (await this.loadCommand(file)).default + this.cache.set(cmd.name, cmd) + + postLoad(cmd) + } + + return this.cache + } + + async loadCommand(path) { + return await import(process.cwd() + "/src/commands/" + path) + } + + reloadCommand(name) {} + + unloadCommand(name) {} + + check(client, data) { + if (data.type !== 2) return false + this.run(client, data) + } + + run(client, interaction) { + const cmd = this.cache.get(interaction.data.name) + if (!cmd) return false + + const Command = new cmd(client) + return Command.run(interaction) + } +} diff --git a/src/lib/handlers/ComponentHandler.js b/src/lib/handlers/ComponentHandler.js new file mode 100644 index 0000000..1455bdb --- /dev/null +++ b/src/lib/handlers/ComponentHandler.js @@ -0,0 +1,42 @@ +import { readdir } from "node:fs/promises" + +export default class ComponentHandler { + constructor() { + this.cache = new Map() + } + + async load(postLoad = () => {}) { + const dir = await readdir("src/components") + for (let file of dir) { + const cmd = (await this.loadCommand(file)).default + this.cache.set(cmd.name, cmd) + + postLoad(cmd) + } + + return this.cache + } + + async loadCommand(path) { + return await import(process.cwd() + "/src/components/" + path) + } + + reloadCommand(name) {} + + unloadCommand(name) {} + + check(client, data) { + if (data.type !== 3) return false + this.run(client, data) + } + + run(client, interaction) { + const params = new URLSearchParams(interaction.data.customId) + + const cmd = this.cache.get(params.get("name")) + if (!cmd) return false + + const Command = new cmd(client) + return Command.run(interaction) + } +} diff --git a/src/lib/handlers/EventHandler.js b/src/lib/handlers/EventHandler.js new file mode 100644 index 0000000..40b1757 --- /dev/null +++ b/src/lib/handlers/EventHandler.js @@ -0,0 +1,22 @@ +import { readdir } from "node:fs/promises" + +export default class EventHandler { + constructor(client) { + this.client = client + this.cache = {} + } + + async load() { + const dir = await readdir("src/events") + for (let path of dir) { + const cmd = await this.loadEvent(path) + this.cache[path.replace(".js", "")] = cmd.default.bind(null, this.client) + } + + return this.cache + } + + async loadEvent(path) { + return await import(process.cwd() + "/src/events/" + path) + } +} diff --git a/src/lib/handlers/RESTHandler.js b/src/lib/handlers/RESTHandler.js new file mode 100644 index 0000000..ad2883f --- /dev/null +++ b/src/lib/handlers/RESTHandler.js @@ -0,0 +1,7 @@ +import { createRestManager } from "@discordeno/rest" + +const REST = createRestManager({ + token: import.meta.env.DISCORD_TOKEN, +}) + +export default REST diff --git a/src/lib/modpack.js b/src/lib/modpack.js new file mode 100644 index 0000000..324a0aa --- /dev/null +++ b/src/lib/modpack.js @@ -0,0 +1,9 @@ +import * as modrinth from "./modrinth" +import * as curseforge from "./curseforge" + +export let staged = new Map() +export let cache = new Map() + +export async function publishChanges() {} + +export async function getChanges(x, y) {} diff --git a/src/lib/modrinth.js b/src/lib/modrinth.js new file mode 100644 index 0000000..f4ddaf9 --- /dev/null +++ b/src/lib/modrinth.js @@ -0,0 +1,3 @@ +export async function getMod(id) {} + +export async function downloadMod(id) {} diff --git a/src/routes/getAllMods.js b/src/routes/getAllMods.js new file mode 100644 index 0000000..4b279aa --- /dev/null +++ b/src/routes/getAllMods.js @@ -0,0 +1 @@ +export default async function route(req) {} diff --git a/src/routes/getMod.js b/src/routes/getMod.js new file mode 100644 index 0000000..4b279aa --- /dev/null +++ b/src/routes/getMod.js @@ -0,0 +1 @@ +export default async function route(req) {} diff --git a/src/routes/getUpdates.js b/src/routes/getUpdates.js new file mode 100644 index 0000000..4b279aa --- /dev/null +++ b/src/routes/getUpdates.js @@ -0,0 +1 @@ +export default async function route(req) {} diff --git a/src/routes/index.js b/src/routes/index.js new file mode 100644 index 0000000..4b279aa --- /dev/null +++ b/src/routes/index.js @@ -0,0 +1 @@ +export default async function route(req) {} diff --git a/src/server.js b/src/server.js new file mode 100644 index 0000000..6a4ea7d --- /dev/null +++ b/src/server.js @@ -0,0 +1,21 @@ +import index from "./routes/index" + +const ROUTES = { + "/": index, +} + +export default async function server() { + const http = Bun.serve({ + fetch(req) { + const url = new URL(req.url) + const route = ROUTES[url.pathname] + if (route) return route() + throw new Error(404) + }, + error(error) { + return new Response(error) + }, + }) + + console.log(`Listening on ${http.hostname}:${http.port}`) +}