mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 11:28:15 +00:00
Members helpers
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
import type { DiscordImageFormat } from "../../types/misc/image_format.ts";
|
||||
import type { DiscordImageSize } from "../../types/misc/image_size.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { iconBigintToHash } from "../../util/hash.ts";
|
||||
import { formatImageURL } from "../../util/utils.ts";
|
||||
import type {Bot} from "../../bot.ts";
|
||||
|
||||
/** The users custom avatar or the default avatar if you don't have a member object. */
|
||||
export function avatarURL(
|
||||
bot: Bot,
|
||||
userId: bigint,
|
||||
discriminator: number,
|
||||
options: {
|
||||
@@ -16,15 +15,15 @@ export function avatarURL(
|
||||
}
|
||||
) {
|
||||
return options.avatar
|
||||
? formatImageURL(
|
||||
endpoints.USER_AVATAR(
|
||||
? bot.utils.formatImageURL(
|
||||
bot.constants.endpoints.USER_AVATAR(
|
||||
userId,
|
||||
typeof options.avatar === "string"
|
||||
? options.avatar
|
||||
: iconBigintToHash(options.avatar, options.animated ?? true)
|
||||
: bot.utils.iconBigintToHash(options.avatar, options.animated ?? true)
|
||||
),
|
||||
options.size || 128,
|
||||
options.format
|
||||
)
|
||||
: endpoints.USER_DEFAULT_AVATAR(Number(discriminator) % 5);
|
||||
: bot.constants.endpoints.USER_DEFAULT_AVATAR(Number(discriminator) % 5);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { rest } from "../../rest/rest.ts";
|
||||
import type { CreateGuildBan } from "../../types/guilds/create_guild_ban.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
||||
import { snakelize } from "../../util/utils.ts";
|
||||
import type {Bot} from "../../bot.ts";
|
||||
|
||||
/** Ban a user from the guild and optionally delete previous messages sent by the user. Requires the BAN_MEMBERS permission. */
|
||||
export async function ban(guildId: bigint, id: bigint, options?: CreateGuildBan) {
|
||||
await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]);
|
||||
export async function ban(bot: Bot, guildId: bigint, id: bigint, options?: CreateGuildBan) {
|
||||
await bot.utils.requireBotGuildPermissions(bot, guildId, ["BAN_MEMBERS"]);
|
||||
|
||||
return await rest.runMethod<undefined>("put", endpoints.GUILD_BAN(guildId, id), snakelize(options ?? {}));
|
||||
return await bot.rest.runMethod<undefined>(bot.rest,"put", bot.constants.endpoints.GUILD_BAN(guildId, id), (options ? {
|
||||
delete_message_days: options.deleteMessageDays,
|
||||
reason: options.reason
|
||||
} : {}));
|
||||
}
|
||||
|
||||
// aliases
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { editMember } from "./edit_member.ts";
|
||||
import type {Bot} from "../../bot.ts";
|
||||
|
||||
/** Kicks a member from a voice channel */
|
||||
export function disconnectMember(guildId: bigint, memberId: bigint) {
|
||||
return editMember(guildId, memberId, { channelId: null });
|
||||
export function disconnectMember(bot: Bot, guildId: bigint, memberId: bigint) {
|
||||
return bot.helpers.editMember(bot, guildId, memberId, { channelId: null });
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import { rest } from "../../rest/rest.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
||||
import type {Bot} from "../../bot.ts";
|
||||
|
||||
/** Edit the nickname of the bot in this guild */
|
||||
export async function editBotNickname(guildId: bigint, nickname: string | null) {
|
||||
await requireBotGuildPermissions(guildId, ["CHANGE_NICKNAME"]);
|
||||
export async function editBotNickname(bot: Bot, guildId: bigint, nickname: string | null) {
|
||||
await bot.utils.requireBotGuildPermissions(bot,guildId, ["CHANGE_NICKNAME"]);
|
||||
|
||||
const response = await rest.runMethod<{ nick: string }>("patch", endpoints.USER_NICK(guildId), {
|
||||
const response = await bot.rest.runMethod<{ nick: string }>(bot.rest,"patch", bot.constants.endpoints.USER_NICK(guildId), {
|
||||
nick: nickname,
|
||||
});
|
||||
|
||||
|
||||
@@ -1,22 +1,16 @@
|
||||
import { cacheHandlers } from "../../cache.ts";
|
||||
import { rest } from "../../rest/rest.ts";
|
||||
import { structures } from "../../structures/mod.ts";
|
||||
import { Errors } from "../../types/discordeno/errors.ts";
|
||||
import type { ModifyGuildMember } from "../../types/guilds/modify_guild_member.ts";
|
||||
import type { GuildMemberWithUser } from "../../types/members/guild_member.ts";
|
||||
import type { PermissionStrings } from "../../types/permissions/permission_strings.ts";
|
||||
import { bigintToSnowflake } from "../../util/bigint.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { requireBotChannelPermissions, requireBotGuildPermissions } from "../../util/permissions.ts";
|
||||
import { snakelize } from "../../util/utils.ts";
|
||||
import type {Bot} from "../../bot.ts";
|
||||
import type {SnakeCasedPropertiesDeep} from "../../types/util.ts";
|
||||
|
||||
/** Edit the member */
|
||||
export async function editMember(guildId: bigint, memberId: bigint, options: ModifyGuildMember) {
|
||||
export async function editMember(bot: Bot, guildId: bigint, memberId: bigint, options: ModifyGuildMember) {
|
||||
const requiredPerms: Set<PermissionStrings> = new Set();
|
||||
|
||||
if (options.nick) {
|
||||
if (options.nick.length > 32) {
|
||||
throw new Error(Errors.NICKNAMES_MAX_LENGTH);
|
||||
throw new Error(bot.constants.Errors.NICKNAMES_MAX_LENGTH);
|
||||
}
|
||||
requiredPerms.add("MANAGE_NICKNAMES");
|
||||
}
|
||||
@@ -24,10 +18,10 @@ export async function editMember(guildId: bigint, memberId: bigint, options: Mod
|
||||
if (options.roles) requiredPerms.add("MANAGE_ROLES");
|
||||
|
||||
if (options.mute !== undefined || options.deaf !== undefined || options.channelId !== undefined) {
|
||||
const memberVoiceState = (await cacheHandlers.get("guilds", guildId))?.voiceStates.get(memberId);
|
||||
const memberVoiceState = (await bot.cache.guilds.get(guildId))?.voiceStates.get(memberId);
|
||||
|
||||
if (!memberVoiceState?.channelId) {
|
||||
throw new Error(Errors.MEMBER_NOT_IN_VOICE_CHANNEL);
|
||||
throw new Error(bot.constants.Errors.MEMBER_NOT_IN_VOICE_CHANNEL);
|
||||
}
|
||||
|
||||
if (options.mute !== undefined) {
|
||||
@@ -41,21 +35,26 @@ export async function editMember(guildId: bigint, memberId: bigint, options: Mod
|
||||
if (options.channelId) {
|
||||
const requiredVoicePerms: Set<PermissionStrings> = new Set(["CONNECT", "MOVE_MEMBERS"]);
|
||||
if (memberVoiceState) {
|
||||
await requireBotChannelPermissions(memberVoiceState?.channelId, [...requiredVoicePerms]);
|
||||
await bot.utils.requireBotChannelPermissions(bot,memberVoiceState?.channelId, [...requiredVoicePerms]);
|
||||
}
|
||||
await requireBotChannelPermissions(options.channelId, [...requiredVoicePerms]);
|
||||
await bot.utils.requireBotChannelPermissions(bot,options.channelId, [...requiredVoicePerms]);
|
||||
}
|
||||
}
|
||||
|
||||
await requireBotGuildPermissions(guildId, [...requiredPerms]);
|
||||
await bot.utils.requireBotGuildPermissions(bot,guildId, [...requiredPerms]);
|
||||
|
||||
const result = await rest.runMethod<GuildMemberWithUser>(
|
||||
const result = await bot.rest.runMethod<SnakeCasedPropertiesDeep<GuildMemberWithUser>>(
|
||||
bot.rest,
|
||||
"patch",
|
||||
endpoints.GUILD_MEMBER(guildId, memberId),
|
||||
snakelize(options) as ModifyGuildMember
|
||||
bot.constants.endpoints.GUILD_MEMBER(guildId, memberId),
|
||||
{
|
||||
nick: options.nick,
|
||||
roles: options.roles,
|
||||
mute: options.mute,
|
||||
deaf: options.deaf,
|
||||
channel_id: options.channelId
|
||||
}
|
||||
);
|
||||
|
||||
const member = await structures.createDiscordenoMember(result, guildId);
|
||||
|
||||
return member;
|
||||
return bot.transformers.member(bot, result, guildId);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import { cache } from "../../cache.ts";
|
||||
import { DiscordenoMember } from "../../structures/member.ts";
|
||||
import { DiscordGatewayOpcodes } from "../../types/codes/gateway_opcodes.ts";
|
||||
import { Errors } from "../../types/discordeno/errors.ts";
|
||||
import { DiscordGatewayIntents } from "../../types/gateway/gateway_intents.ts";
|
||||
import type { RequestGuildMembers } from "../../types/members/request_guild_members.ts";
|
||||
import { Collection } from "../../util/collection.ts";
|
||||
import { ws } from "../../ws/ws.ts";
|
||||
import type {Bot} from "../../bot.ts";
|
||||
import {DiscordGatewayIntents} from "../../types/gateway/gateway_intents.ts";
|
||||
import {DiscordGatewayOpcodes} from "../../types/codes/gateway_opcodes.ts";
|
||||
import type {DiscordenoMember} from "../../transformers/member.ts";
|
||||
|
||||
/**
|
||||
* ⚠️ BEGINNER DEVS!! YOU SHOULD ALMOST NEVER NEED THIS AND YOU CAN GET FROM cache.members.get()
|
||||
@@ -15,15 +13,15 @@ import { ws } from "../../ws/ws.ts";
|
||||
* REST: 50/s global(across all shards) rate limit with ALL requests this included
|
||||
* GW(this function): 120/m(PER shard) rate limit. Meaning if you have 8 shards your limit is now 960/m.
|
||||
*/
|
||||
export function fetchMembers(guildId: bigint, shardId: number, options?: Omit<RequestGuildMembers, "guildId">) {
|
||||
export function fetchMembers(bot: Bot, guildId: bigint, shardId: number, options?: Omit<RequestGuildMembers, "guildId">) {
|
||||
// You can request 1 member without the intent
|
||||
// Check if intents is not 0 as proxy ws won't set intents in other instances
|
||||
if (
|
||||
ws.identifyPayload.intents &&
|
||||
bot.gateway.identifyPayload.intents &&
|
||||
(!options?.limit || options.limit > 1) &&
|
||||
!(ws.identifyPayload.intents & DiscordGatewayIntents.GuildMembers)
|
||||
!(bot.gateway.identifyPayload.intents & DiscordGatewayIntents.GuildMembers)
|
||||
) {
|
||||
throw new Error(Errors.MISSING_INTENT_GUILD_MEMBERS);
|
||||
throw new Error(bot.constants.Errors.MISSING_INTENT_GUILD_MEMBERS);
|
||||
}
|
||||
|
||||
if (options?.userIds?.length) {
|
||||
@@ -32,9 +30,10 @@ export function fetchMembers(guildId: bigint, shardId: number, options?: Omit<Re
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const nonce = `${guildId}-${Date.now()}`;
|
||||
// TODO: FIND A BETTER WAY TO DO THAT?
|
||||
cache.fetchAllMembersProcessingRequests.set(nonce, resolve);
|
||||
|
||||
ws.sendShardMessage(shardId, {
|
||||
bot.gateway.sendShardMessage(shardId, {
|
||||
op: DiscordGatewayOpcodes.RequestGuildMembers,
|
||||
d: {
|
||||
guild_id: guildId,
|
||||
|
||||
@@ -1,21 +1,19 @@
|
||||
import { cacheHandlers } from "../../cache.ts";
|
||||
import { rest } from "../../rest/rest.ts";
|
||||
import { structures } from "../../structures/mod.ts";
|
||||
import type { GuildMemberWithUser } from "../../types/members/guild_member.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import type {Bot} from "../../bot.ts";
|
||||
import type {SnakeCasedPropertiesDeep} from "../../types/util.ts";
|
||||
|
||||
/** Returns a guild member object for the specified user.
|
||||
*
|
||||
* ⚠️ **ADVANCED USE ONLY: Your members will be cached in your guild most likely. Only use this when you are absolutely sure the member is not cached.**
|
||||
*/
|
||||
export async function getMember(guildId: bigint, id: bigint, options?: { force?: boolean }) {
|
||||
const guild = await cacheHandlers.get("guilds", guildId);
|
||||
export async function getMember(bot: Bot, guildId: bigint, id: bigint, options?: { force?: boolean }) {
|
||||
const guild = await bot.cache.guilds.get(guildId);
|
||||
if (!guild && !options?.force) return;
|
||||
|
||||
const data = await rest.runMethod<GuildMemberWithUser>("get", endpoints.GUILD_MEMBER(guildId, id));
|
||||
const data = await bot.rest.runMethod<SnakeCasedPropertiesDeep<GuildMemberWithUser>>(bot.rest,"get", bot.constants.endpoints.GUILD_MEMBER(guildId, id));
|
||||
|
||||
const discordenoMember = await structures.createDiscordenoMember(data, guildId);
|
||||
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
|
||||
const discordenoMember = await bot.transformers.member(bot, data, guildId);
|
||||
await bot.cache.members.set(discordenoMember.id, discordenoMember);
|
||||
|
||||
return discordenoMember;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,10 @@
|
||||
import { eventHandlers } from "../../bot.ts";
|
||||
import { cacheHandlers } from "../../cache.ts";
|
||||
import { rest } from "../../rest/rest.ts";
|
||||
import { DiscordenoMember } from "../../structures/member.ts";
|
||||
import { structures } from "../../structures/mod.ts";
|
||||
import { Errors } from "../../types/discordeno/errors.ts";
|
||||
import { DiscordGatewayIntents } from "../../types/gateway/gateway_intents.ts";
|
||||
import type { GuildMemberWithUser } from "../../types/members/guild_member.ts";
|
||||
import type { ListGuildMembers } from "../../types/members/list_guild_members.ts";
|
||||
import { bigintToSnowflake } from "../../util/bigint.ts";
|
||||
import type {Bot} from "../../bot.ts";
|
||||
import {Collection} from "../../util/collection.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { ws } from "../../ws/ws.ts";
|
||||
import type {DiscordenoMember} from "../../transformers/member.ts";
|
||||
import type {SnakeCasedPropertiesDeep} from "../../types/util.ts";
|
||||
|
||||
/**
|
||||
* ⚠️ BEGINNER DEVS!! YOU SHOULD ALMOST NEVER NEED THIS AND YOU CAN GET FROM cache.members.get()
|
||||
@@ -20,14 +14,14 @@ import { ws } from "../../ws/ws.ts";
|
||||
* REST(this function): 50/s global(across all shards) rate limit with ALL requests this included
|
||||
* GW(fetchMembers): 120/m(PER shard) rate limit. Meaning if you have 8 shards your limit is 960/m.
|
||||
*/
|
||||
export async function getMembers(guildId: bigint, options?: ListGuildMembers & { addToCache?: boolean }) {
|
||||
export async function getMembers(bot: Bot, guildId: bigint, options?: ListGuildMembers & { addToCache?: boolean }) {
|
||||
// Check if intents is not 0 as proxy ws won't set intents in other instances
|
||||
if (ws.identifyPayload.intents && !(ws.identifyPayload.intents & DiscordGatewayIntents.GuildMembers)) {
|
||||
throw new Error(Errors.MISSING_INTENT_GUILD_MEMBERS);
|
||||
if (bot.gateway.identifyPayload.intents && !(bot.gateway.identifyPayload.intents & DiscordGatewayIntents.GuildMembers)) {
|
||||
throw new Error(bot.cache.Errors.MISSING_INTENT_GUILD_MEMBERS);
|
||||
}
|
||||
|
||||
const guild = await cacheHandlers.get("guilds", guildId);
|
||||
if (!guild) throw new Error(Errors.GUILD_NOT_FOUND);
|
||||
if (!guild) throw new Error(bot.constants.Errors.GUILD_NOT_FOUND);
|
||||
|
||||
const members = new Collection<bigint, DiscordenoMember>();
|
||||
|
||||
@@ -40,19 +34,20 @@ export async function getMembers(guildId: bigint, options?: ListGuildMembers & {
|
||||
console.log(`Paginating get members from REST. #${loops} / ${Math.ceil((options?.limit ?? 1) / 1000)}`);
|
||||
}
|
||||
|
||||
const result = await rest.runMethod<GuildMemberWithUser[]>(
|
||||
const result = await bot.rest.runMethod<SnakeCasedPropertiesDeep<GuildMemberWithUser>[]>(
|
||||
bot.rest,
|
||||
"get",
|
||||
`${endpoints.GUILD_MEMBERS(guildId)}?limit=${membersLeft > 1000 ? 1000 : membersLeft}${
|
||||
`${bot.constants.endpoints.GUILD_MEMBERS(guildId)}?limit=${membersLeft > 1000 ? 1000 : membersLeft}${
|
||||
options?.after ? `&after=${options.after}` : ""
|
||||
}`
|
||||
);
|
||||
|
||||
const discordenoMembers = await Promise.all(
|
||||
result.map(async (member) => {
|
||||
const discordenoMember = await structures.createDiscordenoMember(member, guildId);
|
||||
const discordenoMember = bot.transformers.member(member, guildId);
|
||||
|
||||
if (options?.addToCache !== false) {
|
||||
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
|
||||
await bot.cache.members.set(discordenoMember.id, discordenoMember);
|
||||
}
|
||||
|
||||
return discordenoMember;
|
||||
@@ -62,13 +57,13 @@ export async function getMembers(guildId: bigint, options?: ListGuildMembers & {
|
||||
if (!discordenoMembers.length) break;
|
||||
|
||||
discordenoMembers.forEach((member) => {
|
||||
eventHandlers.debug?.("loop", `Running forEach loop in get_members file.`);
|
||||
bot.events.debug("loop", `Running forEach loop in get_members file.`);
|
||||
members.set(member.id, member);
|
||||
});
|
||||
|
||||
options = {
|
||||
limit: options?.limit,
|
||||
after: bigintToSnowflake(discordenoMembers[discordenoMembers.length - 1].id),
|
||||
after: bot.transformers.snowflake(discordenoMembers[discordenoMembers.length - 1].id),
|
||||
};
|
||||
|
||||
membersLeft -= 1000;
|
||||
|
||||
@@ -1,20 +1,15 @@
|
||||
import { botId } from "../../bot.ts";
|
||||
import { rest } from "../../rest/rest.ts";
|
||||
import { Errors } from "../../types/discordeno/errors.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { highestRole, requireBotGuildPermissions } from "../../util/permissions.ts";
|
||||
|
||||
import {Bot} from "../../bot.ts";
|
||||
/** Kick a member from the server */
|
||||
export async function kick(guildId: bigint, memberId: bigint, reason?: string) {
|
||||
const botsHighestRole = await highestRole(guildId, botId);
|
||||
const membersHighestRole = await highestRole(guildId, memberId);
|
||||
export async function kick(bot: Bot, guildId: bigint, memberId: bigint, reason?: string) {
|
||||
const botsHighestRole = await bot.utils.highestRole(bot,guildId, botId);
|
||||
const membersHighestRole = await bot.utils.highestRole(bot,guildId, memberId);
|
||||
if (botsHighestRole && membersHighestRole && botsHighestRole.position <= membersHighestRole.position) {
|
||||
throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW);
|
||||
throw new Error(bot.constants.Errors.BOTS_HIGHEST_ROLE_TOO_LOW);
|
||||
}
|
||||
|
||||
await requireBotGuildPermissions(guildId, ["KICK_MEMBERS"]);
|
||||
await bot.utils.requireBotGuildPermissions(bot,guildId, ["KICK_MEMBERS"]);
|
||||
|
||||
return await rest.runMethod<undefined>("delete", endpoints.GUILD_MEMBER(guildId, memberId), { reason });
|
||||
return await bot.rest.runMethod<undefined>(bot.rest,"delete", bot.constants.endpoints.GUILD_MEMBER(guildId, memberId), { reason });
|
||||
}
|
||||
|
||||
// aliases
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { editMember } from "./edit_member.ts";
|
||||
import type {Bot} from "../../bot.ts";
|
||||
|
||||
/**
|
||||
* Move a member from a voice channel to another.
|
||||
* @param bot the bot
|
||||
* @param guildId the id of the guild which the channel exists in
|
||||
* @param memberId the id of the member to move.
|
||||
* @param channelId id of channel to move user to (if they are connected to voice)
|
||||
*/
|
||||
export function moveMember(guildId: bigint, memberId: bigint, channelId: bigint) {
|
||||
return editMember(guildId, memberId, { channelId });
|
||||
export function moveMember(bot: Bot, guildId: bigint, memberId: bigint, channelId: bigint) {
|
||||
return bot.helpers.editMember(bot, guildId, memberId, { channelId });
|
||||
}
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
import { rest } from "../../rest/rest.ts";
|
||||
import { Errors } from "../../types/discordeno/errors.ts";
|
||||
import type { BeginGuildPrune } from "../../types/guilds/begin_guild_prune.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
||||
import { snakelize } from "../../util/utils.ts";
|
||||
import type {Bot} from "../../bot.ts";
|
||||
|
||||
/**
|
||||
* Begin a prune operation. Requires the KICK_MEMBERS permission. Returns an object with one 'pruned' key indicating the number of members that were removed in the prune operation. For large guilds it's recommended to set the computePruneCount option to false, forcing 'pruned' to null. Fires multiple Guild Member Remove Gateway events.
|
||||
*
|
||||
* By default, prune will not remove users with roles. You can optionally include specific roles in your prune by providing the roles (resolved to include_roles internally) parameter. Any inactive user that has a subset of the provided role(s) will be included in the prune and users with additional roles will not.
|
||||
*/
|
||||
export async function pruneMembers(guildId: bigint, options: BeginGuildPrune) {
|
||||
if (options.days && options.days < 1) throw new Error(Errors.PRUNE_MIN_DAYS);
|
||||
if (options.days && options.days > 30) throw new Error(Errors.PRUNE_MAX_DAYS);
|
||||
export async function pruneMembers(bot: Bot, guildId: bigint, options: BeginGuildPrune) {
|
||||
if (options.days && options.days < 1) throw new Error(bot.constants.Errors.PRUNE_MIN_DAYS);
|
||||
if (options.days && options.days > 30) throw new Error(bot.constants.Errors.PRUNE_MAX_DAYS);
|
||||
|
||||
await requireBotGuildPermissions(guildId, ["KICK_MEMBERS"]);
|
||||
await bot.utils.requireBotGuildPermissions(bot,guildId, ["KICK_MEMBERS"]);
|
||||
|
||||
const result = await rest.runMethod<{ pruned: number }>("post", endpoints.GUILD_PRUNE(guildId), snakelize(options));
|
||||
const result = await bot.rest.runMethod<{ pruned: number }>(bot.rest,"post", bot.constants.endpoints.GUILD_PRUNE(guildId), {
|
||||
days: options.days,
|
||||
compute_prune_count: options.computePruneCount,
|
||||
include_roles: options.includeRoles
|
||||
});
|
||||
|
||||
return result.pruned;
|
||||
}
|
||||
|
||||
@@ -1,39 +1,40 @@
|
||||
import { cacheHandlers } from "../../cache.ts";
|
||||
import { rest } from "../../rest/rest.ts";
|
||||
import { DiscordenoMember } from "../../structures/member.ts";
|
||||
import { structures } from "../../structures/mod.ts";
|
||||
import { Errors } from "../../types/discordeno/errors.ts";
|
||||
import type { GuildMemberWithUser } from "../../types/members/guild_member.ts";
|
||||
import type { SearchGuildMembers } from "../../types/members/search_guild_members.ts";
|
||||
import { Collection } from "../../util/collection.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import {Bot} from "../../bot.ts";
|
||||
import {DiscordenoMember} from "../../transformers/member.ts";
|
||||
import type {SnakeCasedPropertiesDeep} from "../../types/util.ts";
|
||||
|
||||
/**
|
||||
* ⚠️ BEGINNER DEVS!! YOU SHOULD ALMOST NEVER NEED THIS AND YOU CAN GET FROM cache.members.filter()
|
||||
* @param bot
|
||||
* @param guildId
|
||||
* @param query Query string to match username(s) and nickname(s) against
|
||||
* @param options
|
||||
*/
|
||||
export async function searchMembers(
|
||||
bot: Bot,
|
||||
guildId: bigint,
|
||||
query: string,
|
||||
options?: Omit<SearchGuildMembers, "query"> & { cache?: boolean }
|
||||
) {
|
||||
if (options?.limit) {
|
||||
if (options.limit < 1) throw new Error(Errors.MEMBER_SEARCH_LIMIT_TOO_LOW);
|
||||
if (options.limit < 1) throw new Error(bot.constants.Errors.MEMBER_SEARCH_LIMIT_TOO_LOW);
|
||||
if (options.limit > 1000) {
|
||||
throw new Error(Errors.MEMBER_SEARCH_LIMIT_TOO_HIGH);
|
||||
throw new Error(bot.constants.Errors.MEMBER_SEARCH_LIMIT_TOO_HIGH);
|
||||
}
|
||||
}
|
||||
|
||||
const result = await rest.runMethod<GuildMemberWithUser[]>("get", endpoints.GUILD_MEMBERS_SEARCH(guildId), {
|
||||
const result = await bot.rest.runMethod<SnakeCasedPropertiesDeep<GuildMemberWithUser>[]>(bot.rest,"get", bot.constants.endpoints.GUILD_MEMBERS_SEARCH(guildId), {
|
||||
...options,
|
||||
query,
|
||||
});
|
||||
|
||||
const members = await Promise.all(
|
||||
result.map(async (member) => {
|
||||
const discordenoMember = await structures.createDiscordenoMember(member, guildId);
|
||||
const discordenoMember = bot.transformers.member(bot, member, guildId);
|
||||
if (options?.cache) {
|
||||
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
|
||||
await bot.cache.members.set(discordenoMember.id, discordenoMember);
|
||||
}
|
||||
|
||||
return discordenoMember;
|
||||
|
||||
@@ -1,29 +1,24 @@
|
||||
import { botId } from "../../bot.ts";
|
||||
import { cacheHandlers } from "../../cache.ts";
|
||||
import { rest } from "../../rest/rest.ts";
|
||||
import { structures } from "../../structures/mod.ts";
|
||||
import type { Channel } from "../../types/channels/channel.ts";
|
||||
import type { CreateMessage } from "../../types/messages/create_message.ts";
|
||||
import { Errors } from "../../types/mod.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { sendMessage } from "../messages/send_message.ts";
|
||||
import type {Bot} from "../../bot.ts";
|
||||
import type {SnakeCasedPropertiesDeep} from "../../types/util.ts";
|
||||
|
||||
/** Send a message to a users DM. Note: this takes 2 API calls. 1 is to fetch the users dm channel. 2 is to send a message to that channel. */
|
||||
export async function sendDirectMessage(memberId: bigint, content: string | CreateMessage) {
|
||||
if (memberId === botId) throw new Error(Errors.YOU_CAN_NOT_DM_THE_BOT_ITSELF);
|
||||
export async function sendDirectMessage(bot: Bot, memberId: bigint, content: string | CreateMessage) {
|
||||
if (memberId === bot.id) throw new Error(bot.constants.Errors.YOU_CAN_NOT_DM_THE_BOT_ITSELF);
|
||||
|
||||
let dmChannel = await cacheHandlers.get("channels", memberId);
|
||||
let dmChannel = await bot.cache.channels.get(memberId);
|
||||
if (!dmChannel) {
|
||||
// If not available in cache create a new one.
|
||||
const dmChannelData = await rest.runMethod<Channel>("post", endpoints.USER_DM, {
|
||||
const dmChannelData = await bot.rest.runMethod<SnakeCasedPropertiesDeep<Channel>>(bot.rest,"post", bot.constants.endpoints.USER_DM, {
|
||||
recipient_id: memberId,
|
||||
});
|
||||
const discordenoChannel = await structures.createDiscordenoChannel(dmChannelData);
|
||||
const discordenoChannel = await bot.transformers.channel(bot, dmChannelData);
|
||||
// Recreate the channel and add it under the users id
|
||||
await cacheHandlers.set("channels", memberId, discordenoChannel);
|
||||
await bot.cache.channels.set(memberId, discordenoChannel);
|
||||
dmChannel = discordenoChannel;
|
||||
}
|
||||
|
||||
// If it does exist try sending a message to this user
|
||||
return await sendMessage(dmChannel.id, content);
|
||||
return await bot.helpers.sendMessage(dmChannel.id, content);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import { rest } from "../../rest/rest.ts";
|
||||
import { endpoints } from "../../util/constants.ts";
|
||||
import { requireBotGuildPermissions } from "../../util/permissions.ts";
|
||||
import type {Bot} from "../../bot.ts";
|
||||
|
||||
/** Remove the ban for a user. Requires BAN_MEMBERS permission */
|
||||
export async function unban(guildId: bigint, id: bigint) {
|
||||
await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]);
|
||||
export async function unban(bot: Bot, guildId: bigint, id: bigint) {
|
||||
await bot.utils.requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]);
|
||||
|
||||
return await rest.runMethod<undefined>("delete", endpoints.GUILD_BAN(guildId, id));
|
||||
return await bot.rest.runMethod<undefined>(bot.rest,"delete", bot.constants.endpoints.GUILD_BAN(guildId, id));
|
||||
}
|
||||
|
||||
// aliases
|
||||
|
||||
@@ -5,7 +5,7 @@ import { SnakeCasedPropertiesDeep } from "../../types/util.ts";
|
||||
|
||||
/** Edit a guild role. Requires the MANAGE_ROLES permission. */
|
||||
export async function editRole(bot: Bot, guildId: bigint, id: bigint, options: CreateGuildRole) {
|
||||
await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]);
|
||||
await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_ROLES"]);
|
||||
|
||||
const result = await bot.rest.runMethod<SnakeCasedPropertiesDeep<Role>>(
|
||||
bot.rest,
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { Bot } from "../../bot.ts";
|
||||
* Requires the `MANAGE_GUILD` permission.
|
||||
*/
|
||||
export async function syncGuildTemplate(bot: Bot, guildId: bigint, templateCode: string) {
|
||||
await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
|
||||
await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]);
|
||||
|
||||
return await bot.rest.runMethod<Template>(
|
||||
bot.rest,
|
||||
|
||||
@@ -5,7 +5,7 @@ import type { SnakeCasedPropertiesDeep } from "../../types/util.ts";
|
||||
|
||||
/** Returns a list of guild webhooks objects. Requires the MANAGE_WEBHOOKs permission. */
|
||||
export async function getWebhooks(bot: Bot, guildId: bigint) {
|
||||
await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_WEBHOOKS"]);
|
||||
await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_WEBHOOKS"]);
|
||||
|
||||
const result = await bot.rest.runMethod<SnakeCasedPropertiesDeep<Webhook>[]>(
|
||||
bot.rest,
|
||||
|
||||
Reference in New Issue
Block a user