JaBa/helpers/resolvers.js

100 lines
2.9 KiB
JavaScript
Raw Normal View History

/**
*
* @param {import("discord.js").Guild} guild
* @param {String} search
* @param {String} channelType
* @returns
*/
const resolveChannel = async ({ guild, search, channelType }) => {
if (!search || typeof search !== "string") return;
2022-01-04 02:18:28 +05:00
// Try by parsing the search
if (search.match(/^<#([0-9]{18})>/)) {
const [, channelId] = search.match(/^<#([0-9]{18})>/);
const channelFound = guild.channels.cache.get(channelId);
2022-01-04 02:18:28 +05:00
if (channelFound && channelType && channelFound.type === channelType) return channelFound;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
// Try with ID
if (guild.channels.cache.has(search)) {
const channelFound = guild.channels.cache.get(search);
2022-01-04 02:18:28 +05:00
if (channelFound && channelType && channelFound.type === channelType) return channelFound;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
// Try with name with #
if (guild.channels.cache.some(channel => `#${channel.name}` === search || channel.name === search)) {
const channelFound = guild.channels.cache.find(channel => `#${channel.name}` === search || channel.name === search);
2022-01-04 02:18:28 +05:00
if (channelFound && channelType && channelFound.type === channelType) return channelFound;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
return;
};
/**
*
* @param {import("discord.js").Guild} guild
* @param {String} search
* @returns
*/
const resolveMember = async ({ guild, search }) => {
if (!search || typeof search !== "string") return;
2022-01-04 02:18:28 +05:00
// Try by parsing the search
if (search.match(/^<@!?(\d+)>$/)) {
const [, userId] = search.match(/^<@!?(\d+)>$/);
const memberFound = await guild.members.fetch(userId).catch(() => {});
2022-01-04 02:18:28 +05:00
if (memberFound) return memberFound;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
// Try with ID
if (await guild.members.fetch(search).catch(() => {})) {
const memberFound = await guild.members.fetch(search);
2022-01-04 02:18:28 +05:00
if (memberFound) return memberFound;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
// Try with name with @
await guild.members.fetch({
2022-01-04 02:18:28 +05:00
query: search
});
if (guild.members.cache.some(member => member.user.tag === search || member.user.username === search)) {
const memberFound = guild.members.cache.find(member => member.user.tag === search || member.user.username === search);
2022-01-04 02:18:28 +05:00
if (memberFound) return memberFound;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
return;
};
/**
*
* @param {import("discord.js").Guild} guild
* @param {String} search
* @returns
*/
const resolveRole = async ({ guild, search }) => {
if (!search || typeof search !== "string") return;
2022-01-04 02:18:28 +05:00
// Try by parsing the search
if (search.match(/^<@&([0-9]{18})>/)) {
const [, roleId] = search.match(/^<@&([0-9]{18})>/);
const roleFound = guild.roles.cache.get(roleId);
2022-01-04 02:18:28 +05:00
if (roleFound)
return roleFound;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
// Try with ID
if (guild.roles.cache.has(search)) {
const roleFound = guild.roles.cache.get(search);
2022-01-04 02:18:28 +05:00
if (roleFound) return roleFound;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
// Try with name with @
if (guild.roles.cache.some(role => `@${role.name}` === search || role.name === search)) {
const roleFound = guild.roles.cache.find(role => `@${role.name}` === search || role.name === search);
2022-01-04 02:18:28 +05:00
if (roleFound) return roleFound;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
return;
};
module.exports = { resolveChannel, resolveMember, resolveRole };