From 670cf299ba86b14454287ca5143d0dddff7afe0b Mon Sep 17 00:00:00 2001 From: Domin-MND <69919939+domin-mnd@users.noreply.github.com> Date: Sat, 4 Nov 2023 20:09:46 +0300 Subject: [PATCH] init discord.js utility bridge --- packages/discord.js/.gitignore | 1 + packages/discord.js/README.md | 0 packages/discord.js/env.d.ts | 1 + packages/discord.js/library/main.ts | 2 + packages/discord.js/library/use-message.ts | 5 ++ packages/discord.js/library/use-reactions.ts | 50 ++++++++++++ packages/discord.js/package.json | 84 ++++++++++++++++++++ packages/discord.js/tsconfig.json | 7 ++ pnpm-lock.yaml | 58 ++++++++++++++ 9 files changed, 208 insertions(+) create mode 100644 packages/discord.js/.gitignore create mode 100644 packages/discord.js/README.md create mode 100644 packages/discord.js/env.d.ts create mode 100644 packages/discord.js/library/main.ts create mode 100644 packages/discord.js/library/use-message.ts create mode 100644 packages/discord.js/library/use-reactions.ts create mode 100644 packages/discord.js/package.json create mode 100644 packages/discord.js/tsconfig.json diff --git a/packages/discord.js/.gitignore b/packages/discord.js/.gitignore new file mode 100644 index 0000000..9dc7e3d --- /dev/null +++ b/packages/discord.js/.gitignore @@ -0,0 +1 @@ +/LICENSE diff --git a/packages/discord.js/README.md b/packages/discord.js/README.md new file mode 100644 index 0000000..e69de29 diff --git a/packages/discord.js/env.d.ts b/packages/discord.js/env.d.ts new file mode 100644 index 0000000..e2814d6 --- /dev/null +++ b/packages/discord.js/env.d.ts @@ -0,0 +1 @@ +/// diff --git a/packages/discord.js/library/main.ts b/packages/discord.js/library/main.ts new file mode 100644 index 0000000..0da32e3 --- /dev/null +++ b/packages/discord.js/library/main.ts @@ -0,0 +1,2 @@ +export * from "./use-message" +export * from "./use-reactions" diff --git a/packages/discord.js/library/use-message.ts b/packages/discord.js/library/use-message.ts new file mode 100644 index 0000000..8d1bd58 --- /dev/null +++ b/packages/discord.js/library/use-message.ts @@ -0,0 +1,5 @@ +import type { Message } from "discord.js"; + +export function useMessage(): Message { + return {} as Message; +} \ No newline at end of file diff --git a/packages/discord.js/library/use-reactions.ts b/packages/discord.js/library/use-reactions.ts new file mode 100644 index 0000000..48e250c --- /dev/null +++ b/packages/discord.js/library/use-reactions.ts @@ -0,0 +1,50 @@ +import { + Collection, + type Message, + type MessageReaction, + type ReactionCollector, +} from "discord.js" +import { useCallback, useEffect, useRef, useState } from "react" +import { useMessage } from "./use-message" + +interface Options { + message?: Message +} + +type Reactions = Collection + +export function useReactions({ message }: Options) { + // Hooks should not be called conditionally + const messageInstance = useMessage() + // Ref will persist the value across renders + const messageRef = useRef(message ?? messageInstance) + const [collector, setCollector] = useState(null) + const [alive, setAlive] = useState(true) + + // Reactions collection + const [reactions, setReactions] = useState( + collector?.collected ?? new Collection(), + ) + + const update = useCallback(() => { + if (collector) setReactions(() => collector.collected) + }, [collector]) + + useEffect(() => { + const collector = messageRef.current.createReactionCollector({ + dispose: true, + }) + + setCollector(collector) + collector.on("collect", update) + collector.on("remove", update) + collector.on("end", () => { + update() + setAlive(false) + }) + + return () => collector.stop() + }, [message, update]) + + return { reactions, alive, collector } +} diff --git a/packages/discord.js/package.json b/packages/discord.js/package.json new file mode 100644 index 0000000..a138f0b --- /dev/null +++ b/packages/discord.js/package.json @@ -0,0 +1,84 @@ +{ + "name": "@reacord/discord.js", + "type": "module", + "description": "Utility bridge between discord.js and react.", + "version": "0.0.1", + "homepage": "https://reacord.mapleleaf.dev", + "repository": "https://github.com/itsMapleLeaf/reacord.git", + "changelog": "https://github.com/itsMapleLeaf/reacord/releases", + "license": "MIT", + "keywords": [ + "discord", + "discord-js", + "react", + "react-js", + "react-renderer", + "interaction", + "message", + "embed", + "reacord", + "bridge" + ], + "files": [ + "dist", + "README.md", + "LICENSE" + ], + "types": "./dist/main.d.ts", + "exports": { + ".": { + "import": "./dist/main.js", + "require": "./dist/main.cjs", + "types": "./dist/main.d.ts" + }, + "./package.json": { + "import": "./package.json", + "require": "./package.json" + } + }, + "scripts": { + "build": "cpy ../../LICENSE . && tsup library/main.ts --target node18 --format cjs,esm --sourcemap --dts --dts-resolve", + "build-watch": "pnpm build -- --watch", + "test": "vitest --coverage --no-watch", + "test-dev": "vitest", + "typecheck": "tsc -b" + }, + "dependencies": { + "@types/node": "^20.8.4", + "@types/react": "^18.2.27", + "@types/react-reconciler": "^0.28.5", + "react-reconciler": "^0.29.0", + "rxjs": "^7.8.1" + }, + "peerDependencies": { + "discord.js": "^14", + "reacord": "workspace:*", + "react": ">=17" + }, + "devDependencies": { + "@reacord/helpers": "workspace:*", + "@types/lodash-es": "^4.17.9", + "c8": "^8.0.1", + "cpy-cli": "^5.0.0", + "discord.js": "^14.13.0", + "dotenv": "^16.3.1", + "lodash-es": "^4.17.21", + "nodemon": "^3.0.1", + "prettier": "^3.0.3", + "react": "^18.2.0", + "tsup": "^7.2.0", + "type-fest": "^4.4.0" + }, + "release-it": { + "git": { + "commitMessage": "release v${version}" + }, + "github": { + "release": true, + "web": true + } + }, + "publishConfig": { + "access": "public" + } +} diff --git a/packages/discord.js/tsconfig.json b/packages/discord.js/tsconfig.json new file mode 100644 index 0000000..2c51127 --- /dev/null +++ b/packages/discord.js/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "jsx": "react-jsx" + }, + "exclude": ["node_modules", "dist"] +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b23b34e..2eeb2a3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -36,6 +36,64 @@ importers: specifier: ^0.34.6 version: 0.34.6 + packages/discord.js: + dependencies: + '@types/node': + specifier: ^20.8.4 + version: 20.8.4 + '@types/react': + specifier: ^18.2.27 + version: 18.2.27 + '@types/react-reconciler': + specifier: ^0.28.5 + version: 0.28.5 + reacord: + specifier: workspace:* + version: link:../reacord + react-reconciler: + specifier: ^0.29.0 + version: 0.29.0(react@18.2.0) + rxjs: + specifier: ^7.8.1 + version: 7.8.1 + devDependencies: + '@reacord/helpers': + specifier: workspace:* + version: link:../helpers + '@types/lodash-es': + specifier: ^4.17.9 + version: 4.17.9 + c8: + specifier: ^8.0.1 + version: 8.0.1 + cpy-cli: + specifier: ^5.0.0 + version: 5.0.0 + discord.js: + specifier: ^14.13.0 + version: 14.13.0 + dotenv: + specifier: ^16.3.1 + version: 16.3.1 + lodash-es: + specifier: ^4.17.21 + version: 4.17.21 + nodemon: + specifier: ^3.0.1 + version: 3.0.1 + prettier: + specifier: ^3.0.3 + version: 3.0.3 + react: + specifier: ^18.2.0 + version: 18.2.0 + tsup: + specifier: ^7.2.0 + version: 7.2.0(postcss@8.4.31)(typescript@5.2.2) + type-fest: + specifier: ^4.4.0 + version: 4.4.0 + packages/helpers: dependencies: '@types/lodash-es':