From 2bb650368fef59f5a4b41e026f12afcd4f4a3ccc Mon Sep 17 00:00:00 2001 From: Skillz Date: Thu, 21 May 2020 11:23:04 -0400 Subject: [PATCH] mem.guildID & mem.guild() & chnl.hasPermission() --- README.md | 3 +++ structures/channel.ts | 17 +++++++++++++++++ structures/member.ts | 5 +++++ 3 files changed, 25 insertions(+) diff --git a/README.md b/README.md index 151f82225..5da013ace 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,7 @@ This section will list out all the available methods and functionality in the li - mention - raw ```ts +- .hasPermission(id, permissions) - .getMessage(id) - .getMessages(options) - .getPins() @@ -222,7 +223,9 @@ This section will list out all the available methods and functionality in the li - premiumSince - tag - mention +- guildID ```ts +- .guild() - .avatarURL(size, format) - .addRole(roleID, reason) - .removeRole(roleID, reason) diff --git a/structures/channel.ts b/structures/channel.ts index cf90b746f..17b160f79 100644 --- a/structures/channel.ts +++ b/structures/channel.ts @@ -51,6 +51,23 @@ export function createChannel(data: ChannelCreatePayload) { /** The mention of the channel */ mention: `<#${data.id}>`, + /** Checks if a user id or a role id has permission in this channel */ + hasPermission: function (id: string, permissions: Permissions[]) { + const overwrite = data.permission_overwrites?.find((perm) => + perm.id === id + ) || + data.permission_overwrites?.find((perm) => perm.id === channel.guildID); + if (!overwrite) return false; + + return permissions.every((perm) => { + if (overwrite.deny & perm) return false; + if (overwrite.allow & perm) return true; + if (channel.guildID) { + return botHasPermission(channel.guildID, [perm]); + } + return false; + }); + }, /** Fetch a single message from the server. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */ getMessage: async (id: string) => { if (data.guild_id) { diff --git a/structures/member.ts b/structures/member.ts index 5b0702a94..fe8995454 100644 --- a/structures/member.ts +++ b/structures/member.ts @@ -13,6 +13,7 @@ import { Errors } from "../types/errors.ts"; import { RequestManager } from "../module/requestManager.ts"; import { botID } from "../module/client.ts"; import { Guild } from "./guild.ts"; +import { cache } from "../utils/cache.ts"; export const createMember = (data: MemberCreatePayload, guild: Guild) => ({ ...data, @@ -26,7 +27,11 @@ export const createMember = (data: MemberCreatePayload, guild: Guild) => ({ tag: `${data.user.username}#${data.user.discriminator}`, /** The user mention with nickname if possible */ mention: `<@!${data.user.id}>`, + /** The guild id where this member exists */ + guildID: guild.id, + /** Gets the guild object from cache for this member. This is a method instead of a prop to preserve memory. */ + guild: () => cache.guilds.get(guild.id)!, /** The users custom avatar or the default avatar */ avatarURL: (size: ImageSize = 128, format?: ImageFormats) => data.user.avatar