feat(plugins/helpers): fetchAndRetrieveMembers (#2035)

This commit is contained in:
Skillz4Killz
2022-02-11 17:24:47 +00:00
committed by GitHub
parent 23abefb291
commit 3f432caef6
2 changed files with 24 additions and 0 deletions
+8
View File
@@ -18,9 +18,14 @@ import { archiveThread, editThread, lockThread, unarchiveThread, unlockThread }
import { disconnectMember } from "./src/disconnectMember.ts";
import { getMembersPaginated } from "./src/getMembersPaginated.ts";
import { moveMember } from "./src/moveMember.ts";
import { fetchAndRetrieveMembers } from "./src/fetchAndRetrieveMembers.ts";
import { BotWithCache } from "../cache/src/addCacheCollections.ts";
export interface BotWithHelpersPlugin extends Bot {
helpers: FinalHelpers & {
fetchAndRetrieveMembers: (
guildId: bigint,
) => Promise<Collection<bigint, DiscordenoMember>>;
sendDirectMessage: (
userId: bigint,
content: string | CreateMessage,
@@ -70,6 +75,9 @@ export interface BotWithHelpersPlugin extends Bot {
export function enableHelpersPlugin(rawBot: Bot): BotWithHelpersPlugin {
const bot = rawBot as BotWithHelpersPlugin;
bot.helpers.fetchAndRetrieveMembers = (
guildId: bigint,
) => fetchAndRetrieveMembers(bot as unknown as BotWithCache, guildId);
bot.helpers.sendDirectMessage = (
userId: bigint,
content: string | CreateMessage,
@@ -0,0 +1,16 @@
import { BotWithCache } from "../../cache/src/addCacheCollections.ts";
/** Fetch members for an entire guild then return the entire guilds cached members. */
export async function fetchAndRetrieveMembers(bot: BotWithCache, guildId: bigint) {
if (!bot.enabledPlugins?.has("CACHE")) {
throw new Error("The fetchAndRetrieveMembers function requires the CACHE plugin first.");
}
const guild = bot.guilds.get(guildId);
if (!guild) {
throw new Error("The guild was not found in cache. Unable to fetch members for uncached guild.");
}
await bot.helpers.fetchMembers(guildId, guild.shardId);
return bot.members.filter((member) => member.guildId === guildId);
}