Skip to content

Commit

Permalink
init discord.js utility bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
domin-mnd committed Nov 4, 2023
1 parent dcc0a0e commit 670cf29
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/discord.js/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/LICENSE
Empty file added packages/discord.js/README.md
Empty file.
1 change: 1 addition & 0 deletions packages/discord.js/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="@total-typescript/ts-reset" />
2 changes: 2 additions & 0 deletions packages/discord.js/library/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./use-message"
export * from "./use-reactions"
5 changes: 5 additions & 0 deletions packages/discord.js/library/use-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Message } from "discord.js";

export function useMessage(): Message {
return {} as Message;
}
50 changes: 50 additions & 0 deletions packages/discord.js/library/use-reactions.ts
Original file line number Diff line number Diff line change
@@ -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<string, MessageReaction>

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<ReactionCollector | null>(null)
const [alive, setAlive] = useState<boolean>(true)

// Reactions collection
const [reactions, setReactions] = useState<Reactions>(
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 }
}
84 changes: 84 additions & 0 deletions packages/discord.js/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
7 changes: 7 additions & 0 deletions packages/discord.js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"jsx": "react-jsx"
},
"exclude": ["node_modules", "dist"]
}
58 changes: 58 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 670cf29

Please sign in to comment.