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,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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user