- 1 :
const Resolver = require("./Resolver");
- 2 :
- 3 :
/* eslint-disable no-throw-literal, class-methods-use-this */
- 4 :
/**
- 5 :
* The resolver that is used for arguments.
- 6 :
* @extends Resolver
- 7 :
*/
- 8 :
class ArgResolver extends Resolver {
- 9 :
- 10 :
/**
- 11 :
* Resolves a message
- 12 :
* @param {string} arg This arg
- 13 :
* @param {Object} currentUsage This current usage
- 14 :
* @param {number} possible This possible usage id
- 15 :
* @param {boolean} repeat If it is a looping/repeating arg
- 16 :
* @param {Message} msg The message that triggered the command
- 17 :
* @returns {external:Message}
- 18 :
*/
- 19 :
message(...args) {
- 20 :
return this.msg(...args);
- 21 :
}
- 22 :
- 23 :
/**
- 24 :
* Resolves a message
- 25 :
* @param {string} arg This arg
- 26 :
* @param {Object} currentUsage This current usage
- 27 :
* @param {number} possible This possible usage id
- 28 :
* @param {boolean} repeat If it is a looping/repeating arg
- 29 :
* @param {Message} msg The message that triggered the command
- 30 :
* @returns {external:Message}
- 31 :
*/
- 32 :
async msg(arg, currentUsage, possible, repeat, msg) {
- 33 :
const message = await super.msg(arg, msg.channel);
- 34 :
if (message) return message;
- 35 :
if (currentUsage.type === "optional" && !repeat) return null;
- 36 :
throw `${currentUsage.possibles[possible].name} must be a valid message id.`;
- 37 :
}
- 38 :
- 39 :
messages(...args) {
- 40 :
return this.msgs(...args);
- 41 :
}
- 42 :
- 43 :
async msgs(arg, currentUsage, possible, repeat, msg) {
- 44 :
const messages = await super.messages(arg, msg.channel, currentUsage.possibles[possible].min);
- 45 :
if (messages.size > 0) return messages;
- 46 :
if (currentUsage.type === "optional" && !repeat) return null;
- 47 :
throw `${currentUsage.possibles[possible].name} must be a valid message id.`;
- 48 :
}
- 49 :
- 50 :
/**
- 51 :
* Resolves a user
- 52 :
* @param {string} arg This arg
- 53 :
* @param {Object} currentUsage This current usage
- 54 :
* @param {number} possible This possible usage id
- 55 :
* @param {boolean} repeat If it is a looping/repeating arg
- 56 :
* @returns {external:User}
- 57 :
*/
- 58 :
mention(...args) {
- 59 :
return this.user(...args);
- 60 :
}
- 61 :
- 62 :
/**
- 63 :
* Resolves a user
- 64 :
* @param {string} arg This arg
- 65 :
* @param {Object} currentUsage This current usage
- 66 :
* @param {number} possible This possible usage id
- 67 :
* @param {boolean} repeat If it is a looping/repeating arg
- 68 :
* @returns {external:User}
- 69 :
*/
- 70 :
async user(arg, currentUsage, possible, repeat) {
- 71 :
const user = await super.user(arg);
- 72 :
if (user) return user;
- 73 :
if (currentUsage.type === "optional" && !repeat) return null;
- 74 :
throw `${currentUsage.possibles[possible].name} must be a mention or valid user id.`;
- 75 :
}
- 76 :
- 77 :
/**
- 78 :
* Resolves a member
- 79 :
* @param {string} arg This arg
- 80 :
* @param {Object} currentUsage This current usage
- 81 :
* @param {number} possible This possible usage id
- 82 :
* @param {boolean} repeat If it is a looping/repeating arg
- 83 :
* @param {Message} msg The message that triggered the command
- 84 :
* @returns {external:GuildMember}
- 85 :
*/
- 86 :
async member(arg, currentUsage, possible, repeat, msg) {
- 87 :
const member = await super.member(arg, msg.guild);
- 88 :
if (member) return member;
- 89 :
if (currentUsage.type === "optional" && !repeat) return null;
- 90 :
throw `${currentUsage.possibles[possible].name} must be a mention or valid user id.`;
- 91 :
}
- 92 :
- 93 :
/**
- 94 :
* Resolves a channel
- 95 :
* @param {string} arg This arg
- 96 :
* @param {Object} currentUsage This current usage
- 97 :
* @param {number} possible This possible usage id
- 98 :
* @param {boolean} repeat If it is a looping/repeating arg
- 99 :
* @returns {external:Channel}
- 100 :
*/
- 101 :
async channel(arg, currentUsage, possible, repeat) {
- 102 :
const channel = await super.channel(arg);
- 103 :
if (channel) return channel;
- 104 :
if (currentUsage.type === "optional" && !repeat) return null;
- 105 :
throw `${currentUsage.possibles[possible].name} must be a channel tag or valid channel id.`;
- 106 :
}
- 107 :
- 108 :
/**
- 109 :
* Resolves a guild
- 110 :
* @param {string} arg This arg
- 111 :
* @param {Object} currentUsage This current usage
- 112 :
* @param {number} possible This possible usage id
- 113 :
* @param {boolean} repeat If it is a looping/repeating arg
- 114 :
* @returns {external:Guild}
- 115 :
*/
- 116 :
async guild(arg, currentUsage, possible, repeat) {
- 117 :
const guild = await super.guild(arg);
- 118 :
if (guild) return guild;
- 119 :
if (currentUsage.type === "optional" && !repeat) return null;
- 120 :
throw `${currentUsage.possibles[possible].name} must be a valid guild id.`;
- 121 :
}
- 122 :
- 123 :
/**
- 124 :
* Resolves a role
- 125 :
* @param {string} arg This arg
- 126 :
* @param {Object} currentUsage This current usage
- 127 :
* @param {number} possible This possible usage id
- 128 :
* @param {boolean} repeat If it is a looping/repeating arg
- 129 :
* @param {Message} msg The message that triggered the command
- 130 :
* @returns {external:Role}
- 131 :
*/
- 132 :
async role(arg, currentUsage, possible, repeat, msg) {
- 133 :
const role = await super.role(arg, msg.guild);
- 134 :
if (role) return role;
- 135 :
if (currentUsage.type === "optional" && !repeat) return null;
- 136 :
throw `${currentUsage.possibles[possible].name} must be a role mention or role id.`;
- 137 :
}
- 138 :
- 139 :
/**
- 140 :
* Resolves a literal
- 141 :
* @param {string} arg This arg
- 142 :
* @param {Object} currentUsage This current usage
- 143 :
* @param {number} possible This possible usage id
- 144 :
* @param {boolean} repeat If it is a looping/repeating arg
- 145 :
* @returns {string}
- 146 :
*/
- 147 :
async literal(arg, currentUsage, possible, repeat) {
- 148 :
if (arg.toLowerCase() === currentUsage.possibles[possible].name.toLowerCase()) return arg.toLowerCase();
- 149 :
if (currentUsage.type === "optional" && !repeat) return null;
- 150 :
throw [
- 151 :
`Your option did not literally match the only possibility: (${currentUsage.possibles.map(poss => poss.name).join(", ")})`,
- 152 :
"This is likely caused by a mistake in the usage string.",
- 153 :
].join("\n");
- 154 :
}
- 155 :
- 156 :
/**
- 157 :
* Resolves a boolean
- 158 :
* @param {string} arg This arg
- 159 :
* @param {Object} currentUsage This current usage
- 160 :
* @param {number} possible This possible usage id
- 161 :
* @param {boolean} repeat If it is a looping/repeating arg
- 162 :
* @returns {boolean}
- 163 :
*/
- 164 :
bool(...args) {
- 165 :
return this.boolean(...args);
- 166 :
}
- 167 :
- 168 :
/**
- 169 :
* Resolves a boolean
- 170 :
* @param {string} arg This arg
- 171 :
* @param {Object} currentUsage This current usage
- 172 :
* @param {number} possible This possible usage id
- 173 :
* @param {boolean} repeat If it is a looping/repeating arg
- 174 :
* @returns {boolean}
- 175 :
*/
- 176 :
async boolean(arg, currentUsage, possible, repeat) {
- 177 :
const boolean = await super.boolean(arg);
- 178 :
if (boolean !== null) return boolean;
- 179 :
if (currentUsage.type === "optional" && !repeat) return null;
- 180 :
throw `${currentUsage.possibles[possible].name} must be true or false.`;
- 181 :
}
- 182 :
- 183 :
/**
- 184 :
* Resolves a string
- 185 :
* @param {string} arg This arg
- 186 :
* @param {Object} currentUsage This current usage
- 187 :
* @param {number} possible This possible usage id
- 188 :
* @param {boolean} repeat If it is a looping/repeating arg
- 189 :
* @returns {string}
- 190 :
*/
- 191 :
str(...args) {
- 192 :
return this.string(...args);
- 193 :
}
- 194 :
- 195 :
/**
- 196 :
* Resolves a string
- 197 :
* @param {string} arg This arg
- 198 :
* @param {Object} currentUsage This current usage
- 199 :
* @param {number} possible This possible usage id
- 200 :
* @param {boolean} repeat If it is a looping/repeating arg
- 201 :
* @returns {string}
- 202 :
*/
- 203 :
async string(arg, currentUsage, possible, repeat) {
- 204 :
const { min, max } = currentUsage.possibles[possible];
- 205 :
if (min && max) {
- 206 :
if (arg.length >= min && arg.length <= max) return arg;
- 207 :
if (currentUsage.type === "optional" && !repeat) return null;
- 208 :
if (min === max) throw `${currentUsage.possibles[possible].name} must be exactly ${min} characters.`;
- 209 :
throw `${currentUsage.possibles[possible].name} must be between ${min} and ${max} characters.`;
- 210 :
} else if (min) {
- 211 :
if (arg.length >= min) return arg;
- 212 :
if (currentUsage.type === "optional" && !repeat) return null;
- 213 :
throw `${currentUsage.possibles[possible].name} must be longer than ${min} characters.`;
- 214 :
} else if (max) {
- 215 :
if (arg.length <= max) return arg;
- 216 :
if (currentUsage.type === "optional" && !repeat) return null;
- 217 :
throw `${currentUsage.possibles[possible].name} must be shorter than ${max} characters.`;
- 218 :
}
- 219 :
return arg;
- 220 :
}
- 221 :
- 222 :
/**
- 223 :
* Resolves a integer
- 224 :
* @param {string} arg This arg
- 225 :
* @param {Object} currentUsage This current usage
- 226 :
* @param {number} possible This possible usage id
- 227 :
* @param {boolean} repeat If it is a looping/repeating arg
- 228 :
* @returns {number}
- 229 :
*/
- 230 :
int(...args) {
- 231 :
return this.integer(...args);
- 232 :
}
- 233 :
- 234 :
/**
- 235 :
* Resolves a integer
- 236 :
* @param {string} arg This arg
- 237 :
* @param {Object} currentUsage This current usage
- 238 :
* @param {number} possible This possible usage id
- 239 :
* @param {boolean} repeat If it is a looping/repeating arg
- 240 :
* @returns {number}
- 241 :
*/
- 242 :
async integer(arg, currentUsage, possible, repeat) {
- 243 :
const { min, max } = currentUsage.possibles[possible];
- 244 :
arg = await super.integer(arg);
- 245 :
if (arg === null) {
- 246 :
if (currentUsage.type === "optional" && !repeat) return null;
- 247 :
throw `${currentUsage.possibles[possible].name} must be an integer.`;
- 248 :
} else if (min && max) {
- 249 :
if (arg >= min && arg <= max) return arg;
- 250 :
if (currentUsage.type === "optional" && !repeat) return null;
- 251 :
if (min === max) throw `${currentUsage.possibles[possible].name} must be exactly ${min}\nSo why didn't the dev use a literal?`;
- 252 :
throw `${currentUsage.possibles[possible].name} must be between ${min} and ${max}.`;
- 253 :
} else if (min) {
- 254 :
if (arg >= min) return arg;
- 255 :
if (currentUsage.type === "optional" && !repeat) return null;
- 256 :
throw `${currentUsage.possibles[possible].name} must be greater than ${min}.`;
- 257 :
} else if (max) {
- 258 :
if (arg <= max) return arg;
- 259 :
if (currentUsage.type === "optional" && !repeat) return null;
- 260 :
throw `${currentUsage.possibles[possible].name} must be less than ${max}.`;
- 261 :
}
- 262 :
return arg;
- 263 :
}
- 264 :
- 265 :
/**
- 266 :
* Resolves a number
- 267 :
* @param {string} arg This arg
- 268 :
* @param {Object} currentUsage This current usage
- 269 :
* @param {number} possible This possible usage id
- 270 :
* @param {boolean} repeat If it is a looping/repeating arg
- 271 :
* @returns {number}
- 272 :
*/
- 273 :
num(...args) {
- 274 :
return this.float(...args);
- 275 :
}
- 276 :
- 277 :
/**
- 278 :
* Resolves a number
- 279 :
* @param {string} arg This arg
- 280 :
* @param {Object} currentUsage This current usage
- 281 :
* @param {number} possible This possible usage id
- 282 :
* @param {boolean} repeat If it is a looping/repeating arg
- 283 :
* @returns {number}
- 284 :
*/
- 285 :
number(...args) {
- 286 :
return this.float(...args);
- 287 :
}
- 288 :
- 289 :
/**
- 290 :
* Resolves a number
- 291 :
* @param {string} arg This arg
- 292 :
* @param {Object} currentUsage This current usage
- 293 :
* @param {number} possible This possible usage id
- 294 :
* @param {boolean} repeat If it is a looping/repeating arg
- 295 :
* @returns {number}
- 296 :
*/
- 297 :
async float(arg, currentUsage, possible, repeat) {
- 298 :
const { min, max } = currentUsage.possibles[possible];
- 299 :
arg = await super.float(arg);
- 300 :
if (arg === null) {
- 301 :
if (currentUsage.type === "optional" && !repeat) return null;
- 302 :
throw `${currentUsage.possibles[possible].name} must be a valid number.`;
- 303 :
} else if (min && max) {
- 304 :
if (arg >= min && arg <= max) return arg;
- 305 :
if (currentUsage.type === "optional" && !repeat) return null;
- 306 :
if (min === max) throw `${currentUsage.possibles[possible].name} must be exactly ${min}\nSo why didn't the dev use a literal?`;
- 307 :
throw `${currentUsage.possibles[possible].name} must be between ${min} and ${max}.`;
- 308 :
} else if (min) {
- 309 :
if (arg >= min) return arg;
- 310 :
if (currentUsage.type === "optional" && !repeat) return null;
- 311 :
throw `${currentUsage.possibles[possible].name} must be greater than ${min}.`;
- 312 :
} else if (max) {
- 313 :
if (arg <= max) return arg;
- 314 :
if (currentUsage.type === "optional" && !repeat) return null;
- 315 :
throw `${currentUsage.possibles[possible].name} must be less than ${max}.`;
- 316 :
}
- 317 :
return arg;
- 318 :
}
- 319 :
- 320 :
/**
- 321 :
* Resolves a hyperlink
- 322 :
* @param {string} arg This arg
- 323 :
* @param {Object} currentUsage This current usage
- 324 :
* @param {number} possible This possible usage id
- 325 :
* @param {boolean} repeat If it is a looping/repeating arg
- 326 :
* @returns {string}
- 327 :
*/
- 328 :
async url(arg, currentUsage, possible, repeat) {
- 329 :
const hyperlink = await super.url(arg);
- 330 :
if (hyperlink !== null) return hyperlink;
- 331 :
if (currentUsage.type === "optional" && !repeat) return null;
- 332 :
throw `${currentUsage.possibles[possible].name} must be a valid url.`;
- 333 :
}
- 334 :
- 335 :
}
- 336 :
- 337 :
module.exports = ArgResolver;