-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New discord.js-react utility bridge library #44
base: main
Are you sure you want to change the base?
Changes from all commits
dcc0a0e
670cf29
1027743
fe07e63
6ebadb1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/LICENSE |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/// <reference types="@total-typescript/ts-reset" /> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./use-message" | ||
export * from "./use-reactions" |
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; | ||
} | ||
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 { useEffect, useState } from "react" | ||
import { useMessage } from "./use-message" | ||
|
||
export interface UseReactionsOptions { | ||
message?: Message | ||
} | ||
|
||
type Reactions = Collection<string, MessageReaction> | ||
|
||
export function useReactions(options: UseReactionsOptions) { | ||
// Hooks should not be called conditionally | ||
domin-mnd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const messageInstance = useMessage() | ||
// Ref will persist the value across renders | ||
const message = options.message ?? messageInstance | ||
const [collector, setCollector] = useState<ReactionCollector | null>(null) | ||
const [alive, setAlive] = useState<boolean>(true) | ||
|
||
// Reactions collection | ||
const [reactions, setReactions] = useState<Reactions>( | ||
() => new Collection(), | ||
) | ||
|
||
useEffect(() => { | ||
const collector = message.createReactionCollector({ | ||
dispose: true, | ||
}) | ||
|
||
const update = () => { | ||
setReactions(() => collector.collected) | ||
} | ||
|
||
setCollector(collector) | ||
collector.on("collect", update) | ||
collector.on("remove", update) | ||
collector.on("end", () => { | ||
update() | ||
setAlive(false) | ||
}) | ||
|
||
return () => collector.stop() | ||
}, [message]) | ||
|
||
return { reactions, alive, collector } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is exposing the I also don't see a use case for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} |
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": ">=0.6.0", | ||
"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" | ||
} | ||
} |
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"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make this work, the DJS adapter could render its own context provider with whatever content the user passes:
Although 🤔 since this is a separate package, that means
MessageContext
has to become a public export of the library... not great.I guess we could just mark it as
@private
in JSDoc, but the "real" solution would be to moveReacordDiscordJs
to this new package, so it can use that context without having to export it publiclyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I was thinking about a similar solution, this is why I left it empty. I will investigate the problem further. Thank you for an example