From 97396f243fb0a8facb2d2cef72b68b8ff69779c4 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Fri, 23 Oct 2020 22:53:36 -0600 Subject: [PATCH] Begin pagination, add listAppend mode to TagFilter --- .gitignore | 3 ++- commands/userinfo.js | 1 + test.js | 10 ++++++++++ util/tag.d.ts | 2 +- util/tagfilter.d.ts | 2 +- util/tagfilter.js | 32 +++++++++++++++++++++++++++++--- util/ts/tag.ts | 2 +- util/ts/tagfilter.ts | 25 +++++++++++++++++++++---- 8 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 test.js diff --git a/.gitignore b/.gitignore index df9906b..8ac6dfb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ config.json -auth.json \ No newline at end of file +auth.json +test.js \ No newline at end of file diff --git a/commands/userinfo.js b/commands/userinfo.js index 2dcf690..9ce5d9b 100644 --- a/commands/userinfo.js +++ b/commands/userinfo.js @@ -11,6 +11,7 @@ module.exports = { let person = message.guild ? mention ? message.guild.members.cache.get(mention.id) : args[0] ? message.guild.members.cache.has(args[0]) ? message.guild.members.cache.get(args[0]) : message.member : message.member : message.author; let name = message.guild ? person.displayName : person.username; let tu = await UserData.findOne({uid: person.id}); + if (!tu) {return message.channel.send("I don't have any data on that user yet.");} let infoembed= new Discord.MessageEmbed() .setTitle(`User Info for ${name}`) .setDescription(`Requested by ${message.guild ? message.member.displayName : message.author.username}`) diff --git a/test.js b/test.js new file mode 100644 index 0000000..f35786b --- /dev/null +++ b/test.js @@ -0,0 +1,10 @@ +const {TagFilter} = require("./util/tagfilter"); +const {Tag} = require("./util/tag"); + +console.log(new TagFilter([ + new Tag(['n', 'name'], 'name', 'append'), + new Tag(['desc', 'd'], 'description', 'append'), + new Tag(['f', 'force'], 'force', 'toggle'), + new Tag(['option', 'o'], 'options', 'listAppend'), + new Tag(['test', 't'], 'test', 'listAppend') +]).test('blah blah blah -n bonk -d stonks very stonks -f -t some stuff -o an option -test blah blah blah -o another optionl -o hecc -o hecc 2 -test such wow, very flex -t homks.exe has stopped working')); \ No newline at end of file diff --git a/util/tag.d.ts b/util/tag.d.ts index 4421210..3c579ea 100644 --- a/util/tag.d.ts +++ b/util/tag.d.ts @@ -5,5 +5,5 @@ export declare class Tag { constructor(triggers: string[], tagName: string, filterType: TagFilterType); addTrigger(trigger: string): Tag; } -declare type TagFilterType = "append" | "toggle"; +declare type TagFilterType = "append" | "toggle" | "listAppend"; export {}; diff --git a/util/tagfilter.d.ts b/util/tagfilter.d.ts index 0da5548..ed480c4 100644 --- a/util/tagfilter.d.ts +++ b/util/tagfilter.d.ts @@ -6,5 +6,5 @@ export declare class TagFilter { constructor(tags: Tag[]); test(text: string): object; } -declare type TagFilterType = "append" | "toggle"; +declare type TagFilterType = "append" | "toggle" | "listAppend"; export {}; diff --git a/util/tagfilter.js b/util/tagfilter.js index ed77f12..d55ec5f 100644 --- a/util/tagfilter.js +++ b/util/tagfilter.js @@ -20,20 +20,40 @@ class TagFilter { test(text) { var filtered = {}; var reading = null; + var filterType; + var ticks = {}; let words = text.trim().split(/\s+/g); let word; for (word of words) { if (word.startsWith('-') && word.length > 1 && this.triggers.has(word.trim())) { - reading = this.filterTypes.get(this.triggers.get(word.trim())) == "toggle" ? null : word.trim(); + filterType = this.filterTypes.get(this.triggers.get(word.trim())); + reading = !['append', 'listAppend'].includes(filterType) ? null : word.trim(); if (!reading) { filtered[`${this.triggers.get(word.trim())}`] = true; } else { - filtered[`${this.triggers.get(reading)}`] = ''; + filtered[`${this.triggers.get(reading)}`] = filterType == 'append' ? '' : Array.isArray(filtered[`${this.triggers.get(reading)}`]) ? filtered[`${this.triggers.get(reading)}`] : []; + } + if (filterType == "listAppend") { + if (ticks[`${this.triggers.get(word.trim())}`] && ticks[`${this.triggers.get(word.trim())}`].length) { + filtered[`${this.triggers.get(word.trim())}`].push(ticks[`${this.triggers.get(word.trim())}`]); + } + ticks[`${this.triggers.get(word.trim())}`] = ''; } } else if (reading) { - filtered[`${this.triggers.get(reading)}`] = `${filtered[`${this.triggers.get(reading)}`]} ${word}`; + if (filterType == "listAppend") { + ticks[`${this.triggers.get(reading)}`] += ` ${word}`; + } + else { + filtered[`${this.triggers.get(reading)}`] = `${filtered[`${this.triggers.get(reading)}`]} ${word}`; + } + } + } + let tick; + for (tick of Object.keys(ticks)) { + if (ticks[tick].length) { + filtered[tick].push(ticks[tick]); } } let key; @@ -41,6 +61,12 @@ class TagFilter { if (typeof filtered[key] == 'string') { filtered[key] = filtered[key].trim(); } + else if (Array.isArray(filtered[key])) { + let subkey; + for (subkey of filtered[key]) { + filtered[key][filtered[key].indexOf(subkey)] = subkey.trim(); + } + } } return filtered; } diff --git a/util/ts/tag.ts b/util/ts/tag.ts index 86c7ce2..380d46f 100644 --- a/util/ts/tag.ts +++ b/util/ts/tag.ts @@ -22,4 +22,4 @@ export class Tag { }; } -type TagFilterType = "append" | "toggle"; \ No newline at end of file +type TagFilterType = "append" | "toggle" | "listAppend"; \ No newline at end of file diff --git a/util/ts/tagfilter.ts b/util/ts/tagfilter.ts index fcf04f7..d0fcbdf 100644 --- a/util/ts/tagfilter.ts +++ b/util/ts/tagfilter.ts @@ -21,25 +21,42 @@ export class TagFilter { public test(text: string): object { var filtered: object = {}; var reading: string = null; + var filterType: TagFilterType; + var ticks = {}; let words = text.trim().split(/\s+/g); let word: string; for (word of words) { if (word.startsWith('-') && word.length > 1 && this.triggers.has(word.trim())) { - reading = this.filterTypes.get(this.triggers.get(word.trim())) == "toggle" ? null : word.trim(); + filterType = this.filterTypes.get(this.triggers.get(word.trim())); + reading = !['append', 'listAppend'].includes(filterType) ? null : word.trim(); if (!reading) {filtered[`${this.triggers.get(word.trim())}`] = true;} - else {filtered[`${this.triggers.get(reading)}`] = '';} + else {filtered[`${this.triggers.get(reading)}`] = filterType == 'append' ? '' : Array.isArray(filtered[`${this.triggers.get(reading)}`]) ? filtered[`${this.triggers.get(reading)}`] : [];} + if (filterType == "listAppend") { + if (ticks[`${this.triggers.get(word.trim())}`] && ticks[`${this.triggers.get(word.trim())}`].length) {filtered[`${this.triggers.get(word.trim())}`].push(ticks[`${this.triggers.get(word.trim())}`]);} + ticks[`${this.triggers.get(word.trim())}`] = ''; + } } else if (reading) { - filtered[`${this.triggers.get(reading)}`] = `${filtered[`${this.triggers.get(reading)}`]} ${word}`; + if (filterType == "listAppend") {ticks[`${this.triggers.get(reading)}`] += ` ${word}`;} + else {filtered[`${this.triggers.get(reading)}`] = `${filtered[`${this.triggers.get(reading)}`]} ${word}`;} } } + + let tick: string; for (tick of Object.keys(ticks)) { + if (ticks[tick].length) {filtered[tick].push(ticks[tick]);} + } let key: string; for (key of Object.keys(filtered)) { if (typeof filtered[key] == 'string') {filtered[key] = filtered[key].trim();} + else if (Array.isArray(filtered[key])) { + let subkey: string; for (subkey of filtered[key]) { + filtered[key][filtered[key].indexOf(subkey)] = subkey.trim(); + } + } } return filtered; }; } -type TagFilterType = "append" | "toggle"; \ No newline at end of file +type TagFilterType = "append" | "toggle" | "listAppend"; \ No newline at end of file