commit 16664d7154d236646de86a215c6addfeb494c9c2 Author: Daniel Bulant Date: Sun Jul 18 19:44:37 2021 +0200 start working on proxy diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d909c6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +package-lock.json +pnpm-lock.yaml +node_modules +config.json \ No newline at end of file diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..65314cf --- /dev/null +++ b/index.mjs @@ -0,0 +1,31 @@ +import discord from "discord.js"; +import { defaultProxy } from "./proxies/index.mjs"; +import Module from "module"; + +/** The proxied version, in case you like named params */ +export const proxy = new Proxy(discord, defaultProxy); + +/** The original discord.js, unmodified. Useful when using discord.js-structures hooked */ +export const original = discord; + +/** + * Hooks discord.js-structures into discord.js require, which should fix errors in 3rd party libraries trying to use Structures. + * Overwrites global require function + * @see [StackOverflow source](https://stackoverflow.com/a/24602188/8404532) + */ +export function hook() { + var origRequire = Module.prototype.require; + var _require = function(context, path) { + return origRequire.call(context, path); + }; + + Module.prototype.require = function(path) { + if(path === "discord.js") { + return proxy; + } + + return _require(this, path); + }; +} + +export default proxy; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..6398a85 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "discord.js-structures", + "version": "1.0.0", + "description": "Adds discord.js structures back into discord.js", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "peerDependencies": { + "discord.js": ">=13.0.0-dev.0" + } +} diff --git a/proxies/index.mjs b/proxies/index.mjs new file mode 100644 index 0000000..76fb3a6 --- /dev/null +++ b/proxies/index.mjs @@ -0,0 +1,12 @@ + +const proxyHandlers = {}; + +/** + * @type {ProxyHandler} + */ +export const defaultProxy = { + get(target, property, receiver) { + if(property in proxyHandlers) return proxyHandlers[property]; + return Reflect.get(target, property, receiver); + } +} \ No newline at end of file diff --git a/proxies/structures.mjs b/proxies/structures.mjs new file mode 100644 index 0000000..e69de29 diff --git a/test/index.mjs b/test/index.mjs new file mode 100644 index 0000000..3ba0023 --- /dev/null +++ b/test/index.mjs @@ -0,0 +1,20 @@ +import discord from "../index.mjs"; +import config from "./config.json"; + +discord.Structures.extend('Message', Message => { + class BetterMessage extends Message { + constructor(client, data) { + super(client, data); + this.cool = true; + } + } + + return BetterMessage; +}); + +const client = new discord.Client(); +client.login(config.token); + +client.on("message", (msg) => { + console.log(msg); +}); \ No newline at end of file