Skip to content

Commit

Permalink
feat: hasInChannel permissions helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Smart committed Dec 11, 2022
1 parent 41c3aa8 commit f6c8913
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type CacheOp<T> =
| { op: "update"; resourceId: string; resource: T }
| { op: "delete"; resourceId: string }

export const makeWithParent = <RPMiss, EOps, EDriver, EMiss, EPMiss, A>({
export const makeWithParent = <EOps, EDriver, EMiss, EPMiss, A>({
driver,
ops = EffectSource.empty,
onMiss,
Expand All @@ -32,7 +32,7 @@ export const makeWithParent = <RPMiss, EOps, EDriver, EMiss, EPMiss, A>({
onMiss: (parentId: string, id: string) => Effect<never, EMiss, A>
onParentMiss: (
parentId: string,
) => Effect<RPMiss, EPMiss, [id: string, resource: A][]>
) => Effect<never, EPMiss, [id: string, resource: A][]>
}) => {
const sync = ops.tap((op): Effect<never, EDriver, void> => {
switch (op.op) {
Expand Down
22 changes: 22 additions & 0 deletions src/Helpers/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const forChannel =
)
} else {
const everyone = roles.find((role) => role.name === "@everyone")

basePermissions =
BigInt(everyone?.permissions || "0") | BigInt(memberOrRole.permissions)
filteredOverwrites = overwrites.filter(
Expand All @@ -87,6 +88,7 @@ export const forChannel =
if (hasAdmin(basePermissions)) {
return ALL
}

return applyOverwrites(basePermissions)(filteredOverwrites)
}

Expand All @@ -100,3 +102,23 @@ export const applyOverwrites =
(permissions & ~BigInt(overwrite.deny)) | BigInt(overwrite.allow),
permissions,
)

interface RolesCache<E> {
getForParent: (
parentId: string,
) => Effect<never, E, ReadonlyMap<string, Discord.Role>>
}

export const hasInChannel =
<E>(rolesCache: RolesCache<E>, permission: bigint) =>
(
channel: Discord.Channel,
memberOrRole: Discord.GuildMember | Discord.Role,
) =>
Do(($) => {
const roles = $(rolesCache.getForParent(channel.guild_id!))
const channelPerms = forChannel([...roles.values()])(channel)(
memberOrRole,
)
return has(permission)(channelPerms)
})

0 comments on commit f6c8913

Please sign in to comment.