discord-bot-cli
Version:
An easy way to build a command-based discord bot with discord.js.
126 lines (125 loc) • 4.52 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseValue = void 0;
const discord_js_1 = require("discord.js");
const array_1 = require("../../utils/array");
/** @internal */
const ID_PATTERN = /^\d{17,21}$/;
/** @internal
* Return undefined if parse fail.
*/
function parseValue(parseData, message, argument) {
let value;
if (array_1.isArray(parseData.type)) {
for (const t of parseData.type) {
value = parse(t, argument, message);
if (value !== undefined)
break;
}
}
else {
value = parse(parseData.type, argument, message);
}
if (value === undefined)
return { value: undefined };
const validation = parseData.validator
? parseData.validator(value)
: undefined;
if (validation === true || validation === undefined)
return { value };
return {
value: undefined,
message: typeof validation === "string" ? validation : undefined,
};
}
exports.parseValue = parseValue;
/** @internal */
function parse(type, str, message) {
var _a, _b, _c, _d, _e;
function resolveChannel(type) {
const ch = message.client.channels.resolve(str);
if (ch && ch.type === type)
return ch;
return undefined;
}
switch (type) {
case "string":
return str;
case "integer": {
const i = parseInt(str);
return isNaN(i) ? undefined : i;
}
case "float": {
const f = parseFloat(str);
return isNaN(f) ? undefined : f;
}
case "boolean":
switch (str.toLocaleLowerCase()) {
case "true":
return true;
case "false":
return false;
}
break;
case "user": {
const user = discord_js_1.MessageMentions.USERS_PATTERN.exec(str);
discord_js_1.MessageMentions.USERS_PATTERN.lastIndex = 0;
if (user)
return message.mentions.users.get(user[1]);
else if (ID_PATTERN.test(str))
return (_a = message.client.users.resolve(str)) !== null && _a !== void 0 ? _a : undefined;
else
return undefined;
}
case "role": {
const role = discord_js_1.MessageMentions.ROLES_PATTERN.exec(str);
discord_js_1.MessageMentions.ROLES_PATTERN.lastIndex = 0;
if (role)
return message.mentions.roles.get(role[1]);
else if (ID_PATTERN.test(str))
return (_d = (_c = (_b = message.guild) === null || _b === void 0 ? void 0 : _b.roles) === null || _c === void 0 ? void 0 : _c.resolve(str)) !== null && _d !== void 0 ? _d : undefined;
else
return undefined;
}
case "channel":
if (ID_PATTERN.test(str))
return (_e = message.client.channels.resolve(str)) !== null && _e !== void 0 ? _e : undefined;
else
return undefined;
case "guild channel":
if (ID_PATTERN.test(str)) {
const ch = message.client.channels.resolve(str);
if (ch && ch.type !== "dm" && ch.type !== "unknown" && ch.type !== "group")
return ch;
}
return undefined;
case "dm channel":
if (ID_PATTERN.test(str))
return resolveChannel("dm");
return undefined;
case "voice channel":
if (ID_PATTERN.test(str))
return resolveChannel("voice");
return undefined;
case "category channel":
if (ID_PATTERN.test(str))
return resolveChannel("category");
return undefined;
case "news channel":
if (ID_PATTERN.test(str))
return resolveChannel("news");
return undefined;
case "store channel":
if (ID_PATTERN.test(str))
return resolveChannel("store");
return undefined;
case "text channel": {
const channel = discord_js_1.MessageMentions.CHANNELS_PATTERN.exec(str);
discord_js_1.MessageMentions.CHANNELS_PATTERN.lastIndex = 0;
if (channel)
return message.mentions.channels.get(channel[1]);
else if (ID_PATTERN.test(str))
return resolveChannel("text");
}
}
}