djskage
Version:
A Discord.js extension for utility commands
78 lines (77 loc) • 2.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Args = void 0;
class Args {
static parse(content, client, guild) {
const split = content.split(" ").slice(2);
const args = [...split];
const result = {
args: [],
userIds: [],
roleIds: [],
channelIds: [],
other: args,
};
const userIdx = split.findIndex((a) => a === "?u");
if (userIdx !== -1 && split[userIdx + 1]) {
const match = this.extractId(split[userIdx + 1]);
if (match)
result.userId = match;
args.splice(userIdx, 2);
}
const chanIdx = split.findIndex((a) => a === "?c");
if (chanIdx !== -1 && split[chanIdx + 1]) {
const match = this.extractId(split[chanIdx + 1]);
if (match)
result.channelId = match;
args.splice(chanIdx, 2);
}
const roleIdx = split.findIndex((a) => a === "?r");
if (roleIdx !== -1 && split[roleIdx + 1]) {
const match = this.extractId(split[roleIdx + 1]);
if (match)
result.roleId = match;
args.splice(roleIdx, 2);
}
for (const arg of args) {
const id = this.extractId(arg);
if (!id)
continue;
result.other.filter((a) => a !== arg);
if (arg.startsWith("<@&"))
result.roleIds.push(id);
else if (arg.startsWith("<#"))
result.channelIds.push(id);
else if (arg.startsWith("<@"))
result.userIds.push(id);
const type = this.detectEntityType(id, guild, client);
if (type === "user")
result.userIds.push(id);
else if (type === "channel")
result.channelIds.push(id);
else if (type === "role")
result.roleIds.push(id);
else
result.other.push(arg);
}
result.args = args;
return result;
}
static detectEntityType(id, guild, client) {
if (client.users.cache.has(id)) {
return "user";
}
if (guild.channels.cache.has(id)) {
return "channel";
}
if (guild.roles.cache.has(id)) {
return "role";
}
return null;
}
static extractId(input) {
const match = input.match(/^<[@#&!]?[!&]?(\d+)>$/) || input.match(/^(\d{17,20})$/);
return match ? match[1] : null;
}
}
exports.Args = Args;