start working on proxy

This commit is contained in:
Daniel Bulant 2021-07-18 19:44:37 +02:00
commit 16664d7154
6 changed files with 82 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
package-lock.json
pnpm-lock.yaml
node_modules
config.json

31
index.mjs Normal file
View file

@ -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;

15
package.json Normal file
View file

@ -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"
}
}

12
proxies/index.mjs Normal file
View file

@ -0,0 +1,12 @@
const proxyHandlers = {};
/**
* @type {ProxyHandler<import("discord.js")>}
*/
export const defaultProxy = {
get(target, property, receiver) {
if(property in proxyHandlers) return proxyHandlers[property];
return Reflect.get(target, property, receiver);
}
}

0
proxies/structures.mjs Normal file
View file

20
test/index.mjs Normal file
View file

@ -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);
});