diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-09-03 09:49:14 -0400 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-09-03 09:49:14 -0400 |
commit | 87d58eb925d0bde22626eb3a1e9ea03ab97d3a29 (patch) | |
tree | bd3229128608c90fd57d7ae152c1b42df5b94ba2 | |
parent | af845599600c3e3d72b3165bfbc9da7e4aa015bd (diff) | |
download | tanzanite-87d58eb925d0bde22626eb3a1e9ea03ab97d3a29.tar.gz tanzanite-87d58eb925d0bde22626eb3a1e9ea03ab97d3a29.tar.bz2 tanzanite-87d58eb925d0bde22626eb3a1e9ea03ab97d3a29.zip |
eval methods
-rw-r--r-- | src/commands/dev/eval.ts | 20 | ||||
-rw-r--r-- | src/lib/extensions/discord-akairo/BushClientUtil.ts | 56 |
2 files changed, 40 insertions, 36 deletions
diff --git a/src/commands/dev/eval.ts b/src/commands/dev/eval.ts index cbc6518..2f61822 100644 --- a/src/commands/dev/eval.ts +++ b/src/commands/dev/eval.ts @@ -22,11 +22,11 @@ export default class EvalCommand extends BushCommand { { id: 'typescript', match: 'flag', flag: '--ts' }, { id: 'hidden', match: 'flag', flag: '--hidden' }, { id: 'show_proto', match: 'flag', flag: '--proto' }, - // { - // id: 'show_methods', - // match: 'flag', - // flag: ['--func', '--function', '--functions', '--meth', '--method', '--methods'] - // }, + { + id: 'show_methods', + match: 'flag', + flag: ['--func', '--function', '--functions', '--meth', '--method', '--methods'] + }, { id: 'code', match: 'rest', @@ -42,8 +42,8 @@ export default class EvalCommand extends BushCommand { { name: 'silent', description: 'Whether or not to make the response silent', type: 'BOOLEAN', required: false }, { name: 'typescript', description: 'Whether or not the code is typescript.', type: 'BOOLEAN', required: false }, { name: 'hidden', description: 'Whether or not to show hidden items.', type: 'BOOLEAN', required: false }, - { name: 'show_proto', description: 'Show prototype.', type: 'BOOLEAN', required: false } - // { name: 'show_methods', description: 'Show class functions.', type: 'BOOLEAN', required: false } + { name: 'show_proto', description: 'Show prototype.', type: 'BOOLEAN', required: false }, + { name: 'show_methods', description: 'Show class functions.', type: 'BOOLEAN', required: false } ], ownerOnly: true }); @@ -60,7 +60,7 @@ export default class EvalCommand extends BushCommand { typescript: boolean; hidden: boolean; show_proto: boolean; - // show_methods: boolean; + show_methods: boolean; } ): Promise<unknown> { if (!message.author.isOwner()) @@ -130,7 +130,7 @@ export default class EvalCommand extends BushCommand { getters: true, showProxy: true }); - // const methods = args.show_methods ? await util.inspectCleanRedactCodeblock(util.getMethods(rawOutput), 'js') : undefined; + const methods = args.show_methods ? await util.inspectCleanRedactCodeblock(util.getMethods(rawOutput), 'js') : undefined; const proto = args.show_proto ? await util.inspectCleanRedactCodeblock(Object.getPrototypeOf(rawOutput), 'js', { depth: 1, @@ -143,7 +143,7 @@ export default class EvalCommand extends BushCommand { if (inputTS) embed.addField('📥 Input (typescript)', inputTS).addField('📥 Input (transpiled javascript)', inputJS); else embed.addField('📥 Input', inputJS); embed.addField('📤 Output', output); - // if (methods) embed.addField('🔧 Methods', methods); + if (methods) embed.addField('🔧 Methods', methods); if (proto) embed.addField('⚙️ Proto', proto); } catch (e) { embed.setTitle(`${emojis.errorFull} Unable to Evaluate Expression`).setColor(colors.error); diff --git a/src/lib/extensions/discord-akairo/BushClientUtil.ts b/src/lib/extensions/discord-akairo/BushClientUtil.ts index c80bfb2..56e2c13 100644 --- a/src/lib/extensions/discord-akairo/BushClientUtil.ts +++ b/src/lib/extensions/discord-akairo/BushClientUtil.ts @@ -1430,32 +1430,36 @@ export class BushClientUtil extends ClientUtil { return client.constants.pronounMapping[apiRes.pronouns]; } - //~ modified from https://stackoverflow.com/questions/31054910/get-functions-methods-of-a-class - //~ answer by Bruno Grieder - //~ public getMethods(obj: any): string { - //~ let props = []; - - //~ do { - //~ const l = Object.getOwnPropertyNames(obj) - //~ .concat(Object.getOwnPropertySymbols(obj).map((s) => s.toString())) - //~ .sort() - //~ .filter( - //~ (p, i, arr) => - //~ typeof obj[p] === 'function' && //only the methods - //~ p !== 'constructor' && //not the constructor - //~ (i == 0 || p !== arr[i - 1]) && //not overriding in this prototype - //~ props.indexOf(p) === -1 //not overridden in a child - //~ ); - //~ props = props.concat( - //~ l /* .map((p) => (obj[p] && obj[p][Symbol.toStringTag] === 'AsyncFunction' ? 'async ' : '' + p + '();')) */ - //~ ); - //~ } while ( - //~ (obj = Object.getPrototypeOf(obj)) && //walk-up the prototype chain - //~ Object.getPrototypeOf(obj) //not the the Object prototype methods (hasOwnProperty, etc...) - //~ ); - - //~ return props.join('\n'); - //~ } + // modified from https://stackoverflow.com/questions/31054910/get-functions-methods-of-a-class + // answer by Bruno Grieder + public getMethods(obj: any): string { + let props: string[] = []; + + do { + const l = Object.getOwnPropertyNames(obj) + .concat(Object.getOwnPropertySymbols(obj).map((s) => s.toString())) + .sort() + .filter( + (p, i, arr) => + typeof Object.getOwnPropertyDescriptor(obj, p)?.['get'] !== 'function' && // ignore getters + typeof Object.getOwnPropertyDescriptor(obj, p)?.['set'] !== 'function' && // ignore setters + typeof obj[p] === 'function' && //only the methods + p !== 'constructor' && //not the constructor + (i == 0 || p !== arr[i - 1]) && //not overriding in this prototype + props.indexOf(p) === -1 //not overridden in a child + ); + l.forEach((p) => console.debug(Object.getOwnPropertyDescriptor(obj, p))); + + props = props.concat( + l.map((p) => (obj[p] && obj[p][Symbol.toStringTag] === 'AsyncFunction' ? `async ${p}();` : `${p}();`)) + ); + } while ( + (obj = Object.getPrototypeOf(obj)) && //walk-up the prototype chain + Object.getPrototypeOf(obj) //not the the Object prototype methods (hasOwnProperty, etc...) + ); + + return props.join('\n'); + } /** * Discord.js's Util class |