mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 15:07:42 +02:00
Add examples
This commit is contained in:
@@ -15,6 +15,7 @@ module.exports = class Argument {
|
||||
this.default = typeof options.default === 'undefined' ? null : options.default;
|
||||
this.infinite = Boolean(options.infinite);
|
||||
this.avatarSize = typeof options.avatarSize === 'undefined' ? 2048 : options.avatarSize;
|
||||
this.examples = typeof options.examples === 'undefined' ? null : options.examples;
|
||||
this.validator = typeof options.validate === 'undefined' ? null : options.validate;
|
||||
this.parser = typeof options.parse === 'undefined' ? null : options.parse;
|
||||
this.emptyChecker = typeof options.isEmpty === 'undefined' ? null : options.isEmpty;
|
||||
@@ -40,6 +41,11 @@ module.exports = class Argument {
|
||||
return this.type.isEmpty(val, msg, arg);
|
||||
}
|
||||
|
||||
example(msg, arg) {
|
||||
if (this.examples) return this.examples[Math.floor(Math.random() * this.examples.length)];
|
||||
return this.type.example(msg, arg);
|
||||
}
|
||||
|
||||
get invalidText() {
|
||||
if (this.oneOf) {
|
||||
return `It must be one of the following: ${list(this.oneOf, 'or')}`;
|
||||
|
||||
@@ -16,4 +16,8 @@ module.exports = class ArgumentType {
|
||||
isEmpty(val) {
|
||||
return !val;
|
||||
}
|
||||
|
||||
example() {
|
||||
return 'moo';
|
||||
}
|
||||
};
|
||||
|
||||
@@ -49,6 +49,17 @@ module.exports = class Command {
|
||||
return `\`${this.client.commandPrefix}${this.name}${args}\` or \`@${this.client.user.tag} ${this.name}${args}\``;
|
||||
}
|
||||
|
||||
example(msg) {
|
||||
const args = this.args.map((arg, i) => {
|
||||
const example = arg.example(msg, arg);
|
||||
if (i !== args.length - 1 && example.includes(' ')) {
|
||||
return `"${example}"`;
|
||||
}
|
||||
return example;
|
||||
});
|
||||
return `${this.client.commandPrefix}${this.name} ${args.join(' ')}`;
|
||||
}
|
||||
|
||||
disable() {
|
||||
this._enabled = false;
|
||||
}
|
||||
|
||||
@@ -59,6 +59,7 @@ module.exports = class CommandDispatcher {
|
||||
error: stripIndents`
|
||||
The "${arg.label || arg.key}" argument is required.
|
||||
${arg.invalidText}
|
||||
Correct Usage Example: ${command.example(msg)}
|
||||
`
|
||||
};
|
||||
}
|
||||
@@ -75,6 +76,7 @@ module.exports = class CommandDispatcher {
|
||||
error: stripIndents`
|
||||
An invalid value was provided for one of the "${arg.label || arg.key}" arguments.
|
||||
${arg.invalidText}
|
||||
Correct Usage Example: ${command.example(msg)}
|
||||
`
|
||||
};
|
||||
}
|
||||
@@ -91,6 +93,7 @@ module.exports = class CommandDispatcher {
|
||||
error: stripIndents`
|
||||
The "${arg.label || arg.key}" argument is required.
|
||||
${arg.invalidText}
|
||||
Correct Usage Example: ${command.example(msg)}
|
||||
`
|
||||
};
|
||||
} else {
|
||||
@@ -107,6 +110,7 @@ module.exports = class CommandDispatcher {
|
||||
error: stripIndents`
|
||||
An invalid value was provided for the "${arg.label || arg.key}" argument.
|
||||
${arg.invalidText}
|
||||
Correct Usage Example: ${command.example(msg)}
|
||||
`
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
const ArgumentType = require('../ArgumentType');
|
||||
|
||||
module.exports = class BooleanArgumentType extends ArgumentType {
|
||||
constructor(client) {
|
||||
super(client, 'boolean');
|
||||
this.truthy = new Set(['true', 't', 'yes', 'y', 'on', 'enable', 'enabled', '1', '+']);
|
||||
this.falsy = new Set(['false', 'f', 'no', 'n', 'off', 'disable', 'disabled', '0', '-']);
|
||||
}
|
||||
|
||||
validate(val) {
|
||||
const lc = val.toLowerCase();
|
||||
return this.truthy.has(lc) || this.falsy.has(lc);
|
||||
}
|
||||
|
||||
parse(val) {
|
||||
const lc = val.toLowerCase();
|
||||
if (this.truthy.has(lc)) return true;
|
||||
if (this.falsy.has(lc)) return false;
|
||||
throw new RangeError('Unknown boolean value.');
|
||||
}
|
||||
};
|
||||
@@ -28,6 +28,10 @@ module.exports = class ChannelArgumentType extends ArgumentType {
|
||||
if (exactChannels.size === 1) return exactChannels.first();
|
||||
return null;
|
||||
}
|
||||
|
||||
example() {
|
||||
return '#general';
|
||||
}
|
||||
};
|
||||
|
||||
function nameFilterExact(search) {
|
||||
|
||||
@@ -14,4 +14,8 @@ module.exports = class CommandArgumentType extends ArgumentType {
|
||||
parse(val) {
|
||||
return this.client.registry.findCommands(val).first();
|
||||
}
|
||||
|
||||
example() {
|
||||
return this.client.registry.commands.random().name;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -29,6 +29,11 @@ module.exports = class CustomEmojiArgumentType extends ArgumentType {
|
||||
if (exactEmojis.size === 1) return exactEmojis.first();
|
||||
return null;
|
||||
}
|
||||
|
||||
example(msg) {
|
||||
if (msg.guild && msg.guild.emojis.cache.size) return msg.guild.emojis.cache.random().toString();
|
||||
return this.client.emojis.cache.random().toString();
|
||||
}
|
||||
};
|
||||
|
||||
function nameFilterExact(search) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const ArgumentType = require('../ArgumentType');
|
||||
const emojiRegex = require('emoji-regex')();
|
||||
const examples = ['😄', '🏳️🌈', '🦑', '🦆'];
|
||||
|
||||
module.exports = class DefaultEmojiArgumentType extends ArgumentType {
|
||||
constructor(client) {
|
||||
@@ -14,4 +15,8 @@ module.exports = class DefaultEmojiArgumentType extends ArgumentType {
|
||||
parse(value) {
|
||||
return value.match(emojiRegex)[0];
|
||||
}
|
||||
|
||||
example() {
|
||||
return examples[Math.floor(Math.random() * examples.length)];
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const ArgumentType = require('../ArgumentType');
|
||||
const { randomRange } = require('../../util/Util');
|
||||
|
||||
module.exports = class FloatArgumentType extends ArgumentType {
|
||||
constructor(client) {
|
||||
@@ -17,4 +18,11 @@ module.exports = class FloatArgumentType extends ArgumentType {
|
||||
parse(val) {
|
||||
return Number.parseFloat(val);
|
||||
}
|
||||
|
||||
example(msg, arg) {
|
||||
if (arg.oneOf) return arg.oneOf[Math.floor(Math.random() * arg.oneOf.length)];
|
||||
const min = arg.min || 0;
|
||||
const max = arg.max || 1000;
|
||||
return randomRange(min, max);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -14,4 +14,8 @@ module.exports = class GroupArgumentType extends ArgumentType {
|
||||
parse(val) {
|
||||
return this.client.registry.findGroups(val).first();
|
||||
}
|
||||
|
||||
example() {
|
||||
return this.client.registry.groups.random().id;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const ArgumentType = require('../ArgumentType');
|
||||
const { randomRange } = require('../../util/Util');
|
||||
|
||||
module.exports = class IntegerArgumentType extends ArgumentType {
|
||||
constructor(client) {
|
||||
@@ -17,4 +18,11 @@ module.exports = class IntegerArgumentType extends ArgumentType {
|
||||
parse(val) {
|
||||
return Number.parseInt(val, 10);
|
||||
}
|
||||
|
||||
example(msg, arg) {
|
||||
if (arg.oneOf) return arg.oneOf[Math.floor(Math.random() * arg.oneOf.length)];
|
||||
const min = arg.min || 0;
|
||||
const max = arg.max || 1000;
|
||||
return randomRange(min, max);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -36,6 +36,12 @@ module.exports = class MemberArgumentType extends ArgumentType {
|
||||
if (exactMembers.size === 1) return exactMembers.first();
|
||||
return null;
|
||||
}
|
||||
|
||||
example(msg) {
|
||||
if (msg.guild) return msg.guild.members.cache.random().tag;
|
||||
const members = [this.client.user, msg.channel.recipient];
|
||||
return members[Math.floor(Math.random() * members.length)].tag;
|
||||
}
|
||||
};
|
||||
|
||||
function memberFilterExact(search) {
|
||||
|
||||
@@ -13,4 +13,8 @@ module.exports = class MessageArgumentType extends ArgumentType {
|
||||
parse(val, msg) {
|
||||
return msg.channel.messages.cache.get(val);
|
||||
}
|
||||
|
||||
example(msg) {
|
||||
return msg.id;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -28,6 +28,11 @@ module.exports = class RoleArgumentType extends ArgumentType {
|
||||
if (exactRoles.size === 1) return exactRoles.first();
|
||||
return null;
|
||||
}
|
||||
|
||||
example(msg) {
|
||||
if (!msg.guild) return 'Administrator';
|
||||
return msg.guild.roles.cache.random().name;
|
||||
}
|
||||
};
|
||||
|
||||
function nameFilterExact(search) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const ArgumentType = require('../ArgumentType');
|
||||
const words = require('../../assets/json/word-list');
|
||||
|
||||
module.exports = class StringArgumentType extends ArgumentType {
|
||||
constructor(client) {
|
||||
@@ -15,4 +16,15 @@ module.exports = class StringArgumentType extends ArgumentType {
|
||||
parse(val) {
|
||||
return val;
|
||||
}
|
||||
|
||||
example(msg, arg) {
|
||||
if (arg.oneOf) return arg.oneOf[Math.floor(Math.random() * arg.oneOf.length)];
|
||||
let sentence = '';
|
||||
while (sentence.length <= (arg.min || 50) || sentence.length <= (arg.max || 100)) {
|
||||
const valid = words.filter(word => sentence.length + word.length + 1 <= (arg.max || 100));
|
||||
if (!valid.length) break;
|
||||
sentence += ` ${valid[Math.floor(Math.random() * valid.length)]}`;
|
||||
}
|
||||
return sentence.trim();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -38,6 +38,12 @@ module.exports = class UserArgumentType extends ArgumentType {
|
||||
if (exactMembers.size === 1) return exactMembers.first().user;
|
||||
return null;
|
||||
}
|
||||
|
||||
example(msg) {
|
||||
if (msg.guild) return msg.guild.members.cache.random().tag;
|
||||
const members = [this.client.user, msg.channel.recipient];
|
||||
return members[Math.floor(Math.random() * members.length)].tag;
|
||||
}
|
||||
};
|
||||
|
||||
function memberFilterExact(search) {
|
||||
|
||||
Reference in New Issue
Block a user