added commandoption dependencies

This commit is contained in:
Erik 2022-01-28 20:32:30 +02:00
parent 6427a33dba
commit 617e63d5fb
No known key found for this signature in database
GPG Key ID: FEFF4B220DDF5589
3 changed files with 26 additions and 9 deletions

View File

@ -58,10 +58,13 @@ class CommandHandler extends Observer {
const response = await this._parseInteraction(interaction);
if (response.error) {
return interaction.reply({
content: interaction.format(`O_COMMANDHANDLER_TYPE${response.option.type}`, {
let content = interaction.format(`O_COMMANDHANDLER_TYPE${response.option.type}`, {
option: response.option.name, min: response.option.minimum, max: response.option.maximum
}),
});
if (response.dependency) content = interaction.format(`O_COMMANDHANDLER_DEPEDENCY`,
{ option: response.option.name, dependency: response.dependency });
return interaction.reply({
content,
emoji: 'failure',
ephemeral: true
});
@ -114,7 +117,7 @@ class CommandHandler extends Observer {
const newOption = new CommandOption({
name: matched.name, type: matched.type,
minimum: matched.minimum, maximum: matched.maximum,
_rawValue: option.value
_rawValue: option.value, dependsOn: matched.dependsOn
});
const parsed = await this._parseOption(interaction, newOption);
@ -130,6 +133,13 @@ class CommandHandler extends Observer {
options[matched.name] = newOption;
}
// Ensure option dependencies
for (const opt of Object.values(options)) {
for (const dep of opt.dependsOn) if (!options[dep]) {
return { option: opt, error: true, dependency: dep };
}
}
if(error) return error;
return {
error: false,

View File

@ -38,6 +38,8 @@ class CommandOption {
this.choices = options.choices || []; //Used for STRING/INTEGER/NUMBER types.
this.options = options.options || []; //Used for SUB_COMMAND/SUB_COMMAND_GROUP types.
this.dependsOn = options.dependsOn || []; // If an option has to be paired with another
this.minimum = typeof options.minimum === 'number' ? options.minimum : undefined; //Used for INTEGER/NUMBER/FLOAT types.
this.maximum = typeof options.maximum === 'number' ? options.maximum : undefined;

View File

@ -93,9 +93,14 @@ class Setting extends Component {
fields.push({
name: `${guild.format(`GENERAL_OPTIONS`)}`,
value: this.commandOptions.map(
(opt) => `**${opt.name} [${opt.type}]:** ${opt.description} ${opt.choices.length
? `\n__${guild.format('GENERAL_CHOICES')}__: ${opt.choices.map((choice) => choice.name).join(', ')}`
: ''}`
(opt) => {
let msg = `**${opt.name} [${opt.type}]:** ${opt.description}`;
if (opt.choices.length)
msg += `\n__${guild.format('GENERAL_CHOICES')}__: ${opt.choices.map((choice) => choice.name).join(', ')}`;
if (opt.dependsOn.length)
msg += `\n${guild.format('GENERAL_DEPENDSON', { dependencies: opt.dependsOn.join('`, `') })}`;
return msg;
}
).join('\n\n')
});
}
@ -159,7 +164,7 @@ class Setting extends Component {
if (!message.length && !index && !embeds.length) throw new Error('Must declare either message, index or embeds');
const response = await interaction.promptMessage(
index ? interaction.format(index, params) : message,
{ time: time * 1000, editInteraction: true, embeds }
{ time, editInteraction: true, embeds }
);
if (!response) return { error: true, message: interaction.format('ERR_TIMEOUT') };