From 81c307a11b2aacc020df95de258d1e21f7b28379 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Thu, 28 Jul 2022 22:04:08 +0300 Subject: [PATCH] guild only options --- src/localization/en_gb/observers/en_gb_commandHandler.lang | 3 +++ src/structure/components/observers/CommandHandler.js | 7 +++++-- src/structure/interfaces/CommandOption.js | 4 ++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/localization/en_gb/observers/en_gb_commandHandler.lang b/src/localization/en_gb/observers/en_gb_commandHandler.lang index c9a9990..781c11a 100644 --- a/src/localization/en_gb/observers/en_gb_commandHandler.lang +++ b/src/localization/en_gb/observers/en_gb_commandHandler.lang @@ -5,6 +5,9 @@ This is an issue that should be reported to a bot developer. [O_COMMANDHANDLER_GUILDONLY] This command can only be run in servers. +[O_COMMANDHANDLER_GUILDONLY_OPT] +The option `{option}` is only valid in servers. + [O_COMMANDHANDLER_TYPEINTEGER] The command option {option} requires an integer between `{min}` and `{max}`. diff --git a/src/structure/components/observers/CommandHandler.js b/src/structure/components/observers/CommandHandler.js index 62593e1..1dd53de 100644 --- a/src/structure/components/observers/CommandHandler.js +++ b/src/structure/components/observers/CommandHandler.js @@ -221,6 +221,7 @@ class CommandHandler extends Observer { continue; } + if (matched.guildOnly) return { error: true, params: { option: matched.name }, index: 'O_COMMANDHANDLER_GUILDONLY_OPT' }; const rawValue = matched.plural && typeof option.value === 'string' ? Util.parseQuotes(option.value).map(([x]) => x) : option.value; const newOption = matched.clone(rawValue, guild, true); const parsed = await newOption.parse(); @@ -320,6 +321,7 @@ class CommandHandler extends Observer { return f.name === _flag || aliased; }); if (!flag) return { error: true, index: 'O_COMMANDHANDLER_UNRECOGNISED_FLAG', params: { flag: _flag } }; + else if (flag.guildOnly) return { error: true, params: { option: flag.name }, index: 'O_COMMANDHANDLER_GUILDONLY_OPT' }; // console.log('aliased', aliased); params.splice(index, 1, null); @@ -349,7 +351,8 @@ class CommandHandler extends Observer { } // console.log('params', params); - const options = activeCommand.options.filter((opt) => !opt.flag && (opt.type !== 'STRING' || opt.choices.length)); + let options = activeCommand.options.filter((opt) => !opt.flag && (opt.type !== 'STRING' || opt.choices.length)); + if(!guild) options = options.filter((opt) => !opt.guildOnly); // const choiceOpts = activeCommand.options.filter((opt) => opt.choices.length); const stringOpts = activeCommand.options.filter((opt) => !opt.flag && !opt.choices.length && opt.type === 'STRING'); // console.log('non-flag options', options.map((opt) => opt.name)); @@ -590,7 +593,7 @@ class CommandHandler extends Observer { async _getCommand(message) { if (!this._mentionPattern) this._mentionPattern = new RegExp(`^(<@!?${this.client.user.id}>)`, 'iu'); - const [arg1, arg2, ...args] = message.content.split(' '); + const [arg1, arg2, ...args] = message.content.split(' ').filter((str) => str.length); if (message.guild) await message.guild.settings(); const userWrapper = await this.client.getUserWrapper(message.author.id); diff --git a/src/structure/interfaces/CommandOption.js b/src/structure/interfaces/CommandOption.js index 59f659d..97025ec 100644 --- a/src/structure/interfaces/CommandOption.js +++ b/src/structure/interfaces/CommandOption.js @@ -111,6 +111,10 @@ class CommandOption { } + get guildOnly() { + return ['ROLE', 'MEMBER', 'CHANNEL'].some((t) => this.type.includes(t)); + } + usage(guild, verbose = false) { let name = `》 ${this.name.toUpperCase()} [${this.type}]`; let flagProps = ['flag'];