Merge pull request #203 from Skillz4Killz/permission-consistency

[BREAKING] Permission consistency
This commit is contained in:
Skillz4Killz
2020-11-18 12:50:23 -05:00
committed by GitHub
7 changed files with 84 additions and 91 deletions
+21 -24
View File
@@ -13,29 +13,26 @@ import {
MessageContent, MessageContent,
} from "../types/channel.ts"; } from "../types/channel.ts";
import { Errors } from "../types/errors.ts"; import { Errors } from "../types/errors.ts";
import { PermissionOverwrite } from "../types/guild.ts"; import { RawOverwrite } from "../types/guild.ts";
import { MessageCreateOptions } from "../types/message.ts"; import { MessageCreateOptions } from "../types/message.ts";
import { Permissions } from "../types/permission.ts"; import { Permissions, Permission } from "../types/permission.ts";
import { endpoints } from "../utils/constants.ts"; import { endpoints } from "../utils/constants.ts";
import { import { botHasChannelPermissions, } from "../utils/permissions.ts";
botHasChannelPermissions,
calculateBits,
} from "../utils/permissions.ts";
/** Checks if a channel overwrite for a user id or a role id has permission in this channel */ /** Checks if a channel overwrite for a user id or a role id has permission in this channel */
export function channelOverwriteHasPermission( export function channelOverwriteHasPermission(
guildID: string, guildID: string,
id: string, id: string,
overwrites: PermissionOverwrite[], overwrites: RawOverwrite[],
permissions: Permissions[], permissions: Permission[],
) { ) {
const overwrite = overwrites.find((perm) => perm.id === id) || const overwrite = overwrites.find((perm) => perm.id === id) ||
overwrites.find((perm) => perm.id === guildID); overwrites.find((perm) => perm.id === guildID);
return permissions.every((perm) => { return permissions.every((perm) => {
if (overwrite) { if (overwrite) {
const allowBits = calculateBits(overwrite.allow); const allowBits = overwrite.allow;
const denyBits = calculateBits(overwrite.deny); const denyBits = overwrite.deny;
if (BigInt(denyBits) & BigInt(perm)) return false; if (BigInt(denyBits) & BigInt(perm)) return false;
if (BigInt(allowBits) & BigInt(perm)) return true; if (BigInt(allowBits) & BigInt(perm)) return true;
} }
@@ -50,7 +47,7 @@ export async function getMessage(
) { ) {
const hasViewChannelPerm = await botHasChannelPermissions( const hasViewChannelPerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.VIEW_CHANNEL], ["VIEW_CHANNEL"],
); );
if ( if (
!hasViewChannelPerm !hasViewChannelPerm
@@ -60,7 +57,7 @@ export async function getMessage(
const hasReadMessageHistoryPerm = await botHasChannelPermissions( const hasReadMessageHistoryPerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.READ_MESSAGE_HISTORY], ["READ_MESSAGE_HISTORY"],
); );
if ( if (
!hasReadMessageHistoryPerm !hasReadMessageHistoryPerm
@@ -85,7 +82,7 @@ export async function getMessages(
) { ) {
const hasViewChannelPerm = await botHasChannelPermissions( const hasViewChannelPerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.VIEW_CHANNEL], ["VIEW_CHANNEL"],
); );
if ( if (
!hasViewChannelPerm !hasViewChannelPerm
@@ -95,7 +92,7 @@ export async function getMessages(
const hasReadMessageHistoryPerm = await botHasChannelPermissions( const hasReadMessageHistoryPerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.READ_MESSAGE_HISTORY], ["READ_MESSAGE_HISTORY"],
); );
if ( if (
!hasReadMessageHistoryPerm !hasReadMessageHistoryPerm
@@ -128,7 +125,7 @@ export async function sendMessage(
if (typeof content === "string") content = { content }; if (typeof content === "string") content = { content };
const hasSendMessagesPerm = await botHasChannelPermissions( const hasSendMessagesPerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.SEND_MESSAGES], ["SEND_MESSAGES"],
); );
if ( if (
!hasSendMessagesPerm !hasSendMessagesPerm
@@ -138,7 +135,7 @@ export async function sendMessage(
const hasSendTtsMessagesPerm = await botHasChannelPermissions( const hasSendTtsMessagesPerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.SEND_TTS_MESSAGES], ["SEND_TTS_MESSAGES"],
); );
if ( if (
content.tts && content.tts &&
@@ -149,7 +146,7 @@ export async function sendMessage(
const hasEmbedLinksPerm = await botHasChannelPermissions( const hasEmbedLinksPerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.EMBED_LINKS], ["EMBED_LINKS"],
); );
if ( if (
content.embed && content.embed &&
@@ -192,7 +189,7 @@ export async function sendMessage(
if ( if (
!(await botHasChannelPermissions( !(await botHasChannelPermissions(
channelID, channelID,
[Permissions.READ_MESSAGE_HISTORY], ["READ_MESSAGE_HISTORY"],
)) ))
) { ) {
throw new Error(Errors.MISSING_SEND_MESSAGES); throw new Error(Errors.MISSING_SEND_MESSAGES);
@@ -236,7 +233,7 @@ export async function deleteMessages(
) { ) {
const hasManageMessages = await botHasChannelPermissions( const hasManageMessages = await botHasChannelPermissions(
channelID, channelID,
[Permissions.MANAGE_MESSAGES], ["MANAGE_MESSAGES"],
); );
if ( if (
!hasManageMessages !hasManageMessages
@@ -263,7 +260,7 @@ export async function deleteMessages(
export async function getChannelInvites(channelID: string) { export async function getChannelInvites(channelID: string) {
const hasManagaChannels = await botHasChannelPermissions( const hasManagaChannels = await botHasChannelPermissions(
channelID, channelID,
[Permissions.MANAGE_CHANNELS], ["MANAGE_CHANNELS"],
); );
if ( if (
!hasManagaChannels !hasManagaChannels
@@ -280,7 +277,7 @@ export async function createInvite(
) { ) {
const hasCreateInstantInvitePerm = await botHasChannelPermissions( const hasCreateInstantInvitePerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.CREATE_INSTANT_INVITE], ["CREATE_INSTANT_INVITE"],
); );
if ( if (
!hasCreateInstantInvitePerm !hasCreateInstantInvitePerm
@@ -294,7 +291,7 @@ export async function createInvite(
export async function getChannelWebhooks(channelID: string) { export async function getChannelWebhooks(channelID: string) {
const hasManageWebhooksPerm = await botHasChannelPermissions( const hasManageWebhooksPerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.MANAGE_WEBHOOKS], ["MANAGE_WEBHOOKS"],
); );
if ( if (
!hasManageWebhooksPerm !hasManageWebhooksPerm
@@ -357,7 +354,7 @@ export async function editChannel(
) { ) {
const hasManageChannelsPerm = await botHasChannelPermissions( const hasManageChannelsPerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.MANAGE_CHANNELS], ["MANAGE_CHANNELS"],
); );
if ( if (
!hasManageChannelsPerm !hasManageChannelsPerm
@@ -428,7 +425,7 @@ export async function followChannel(
) { ) {
const hasManageWebhooksPerm = await botHasChannelPermissions( const hasManageWebhooksPerm = await botHasChannelPermissions(
targetChannelID, targetChannelID,
[Permissions.MANAGE_WEBHOOKS], ["MANAGE_WEBHOOKS"],
); );
if ( if (
!hasManageWebhooksPerm !hasManageWebhooksPerm
+31 -31
View File
@@ -109,7 +109,7 @@ export async function createGuildChannel(
) { ) {
const hasPerm = await botHasPermission( const hasPerm = await botHasPermission(
guild.id, guild.id,
[Permissions.MANAGE_CHANNELS], ["MANAGE_CHANNELS"],
); );
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_CHANNELS); throw new Error(Errors.MISSING_MANAGE_CHANNELS);
@@ -146,7 +146,7 @@ export async function deleteChannel(
) { ) {
const hasPerm = await botHasPermission( const hasPerm = await botHasPermission(
guildID, guildID,
[Permissions.MANAGE_CHANNELS], ["MANAGE_CHANNELS"],
); );
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_CHANNELS); throw new Error(Errors.MISSING_MANAGE_CHANNELS);
@@ -243,7 +243,7 @@ export async function createEmoji(
image: string, image: string,
options: CreateEmojisOptions, options: CreateEmojisOptions,
) { ) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_EMOJIS]); const hasPerm = await botHasPermission(guildID, ["MANAGE_EMOJIS"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_EMOJIS); throw new Error(Errors.MISSING_MANAGE_EMOJIS);
} }
@@ -265,7 +265,7 @@ export async function editEmoji(
id: string, id: string,
options: EditEmojisOptions, options: EditEmojisOptions,
) { ) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_EMOJIS]); const hasPerm = await botHasPermission(guildID, ["MANAGE_EMOJIS"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_EMOJIS); throw new Error(Errors.MISSING_MANAGE_EMOJIS);
} }
@@ -282,7 +282,7 @@ export async function deleteEmoji(
id: string, id: string,
reason?: string, reason?: string,
) { ) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_EMOJIS]); const hasPerm = await botHasPermission(guildID, ["MANAGE_EMOJIS"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_EMOJIS); throw new Error(Errors.MISSING_MANAGE_EMOJIS);
} }
@@ -304,7 +304,7 @@ export async function createGuildRole(
options: CreateRoleOptions, options: CreateRoleOptions,
reason?: string, reason?: string,
) { ) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_ROLES]); const hasPerm = await botHasPermission(guildID, ["MANAGE_ROLES"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_ROLES); throw new Error(Errors.MISSING_MANAGE_ROLES);
} }
@@ -335,7 +335,7 @@ export async function editRole(
id: string, id: string,
options: CreateRoleOptions, options: CreateRoleOptions,
) { ) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_ROLES]); const hasPerm = await botHasPermission(guildID, ["MANAGE_ROLES"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_ROLES); throw new Error(Errors.MISSING_MANAGE_ROLES);
} }
@@ -350,7 +350,7 @@ export async function editRole(
/** Delete a guild role. Requires the MANAGE_ROLES permission. */ /** Delete a guild role. Requires the MANAGE_ROLES permission. */
export async function deleteRole(guildID: string, id: string) { export async function deleteRole(guildID: string, id: string) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_ROLES]); const hasPerm = await botHasPermission(guildID, ["MANAGE_ROLES"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_ROLES); throw new Error(Errors.MISSING_MANAGE_ROLES);
} }
@@ -363,7 +363,7 @@ export async function deleteRole(guildID: string, id: string) {
* ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your roles will be cached in your guild.** * ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your roles will be cached in your guild.**
*/ */
export async function getRoles(guildID: string) { export async function getRoles(guildID: string) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_ROLES]); const hasPerm = await botHasPermission(guildID, ["MANAGE_ROLES"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_ROLES); throw new Error(Errors.MISSING_MANAGE_ROLES);
} }
@@ -373,7 +373,7 @@ export async function getRoles(guildID: string) {
/** Modify the positions of a set of role objects for the guild. Requires the MANAGE_ROLES permission. */ /** Modify the positions of a set of role objects for the guild. Requires the MANAGE_ROLES permission. */
export async function swapRoles(guildID: string, rolePositons: PositionSwap) { export async function swapRoles(guildID: string, rolePositons: PositionSwap) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_ROLES]); const hasPerm = await botHasPermission(guildID, ["MANAGE_ROLES"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_ROLES); throw new Error(Errors.MISSING_MANAGE_ROLES);
} }
@@ -387,7 +387,7 @@ export async function getPruneCount(guildID: string, options: PruneOptions) {
throw new Error(Errors.PRUNE_MIN_DAYS); throw new Error(Errors.PRUNE_MIN_DAYS);
} }
const hasPerm = await botHasPermission(guildID, [Permissions.KICK_MEMBERS]); const hasPerm = await botHasPermission(guildID, ["KICK_MEMBERS"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_KICK_MEMBERS); throw new Error(Errors.MISSING_KICK_MEMBERS);
} }
@@ -406,7 +406,7 @@ export async function pruneMembers(guildID: string, options: PruneOptions) {
throw new Error(Errors.PRUNE_MIN_DAYS); throw new Error(Errors.PRUNE_MIN_DAYS);
} }
const hasPerm = await botHasPermission(guildID, [Permissions.KICK_MEMBERS]); const hasPerm = await botHasPermission(guildID, ["KICK_MEMBERS"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_KICK_MEMBERS); throw new Error(Errors.MISSING_KICK_MEMBERS);
} }
@@ -432,7 +432,7 @@ export async function getAuditLogs(
guildID: string, guildID: string,
options: GetAuditLogsOptions, options: GetAuditLogsOptions,
) { ) {
const hasPerm = await botHasPermission(guildID, [Permissions.VIEW_AUDIT_LOG]); const hasPerm = await botHasPermission(guildID, ["VIEW_AUDIT_LOG"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_VIEW_AUDIT_LOG); throw new Error(Errors.MISSING_VIEW_AUDIT_LOG);
} }
@@ -447,7 +447,7 @@ export async function getAuditLogs(
/** Returns the guild embed object. Requires the MANAGE_GUILD permission. */ /** Returns the guild embed object. Requires the MANAGE_GUILD permission. */
export async function getEmbed(guildID: string) { export async function getEmbed(guildID: string) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]); const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_GUILD); throw new Error(Errors.MISSING_MANAGE_GUILD);
} }
@@ -461,7 +461,7 @@ export async function editEmbed(
enabled: boolean, enabled: boolean,
channelID?: string | null, channelID?: string | null,
) { ) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]); const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_GUILD); throw new Error(Errors.MISSING_MANAGE_GUILD);
} }
@@ -479,7 +479,7 @@ export function getVanityURL(guildID: string) {
/** Returns a list of integrations for the guild. Requires the MANAGE_GUILD permission. */ /** Returns a list of integrations for the guild. Requires the MANAGE_GUILD permission. */
export async function getIntegrations(guildID: string) { export async function getIntegrations(guildID: string) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]); const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_GUILD); throw new Error(Errors.MISSING_MANAGE_GUILD);
} }
@@ -493,7 +493,7 @@ export async function editIntegration(
id: string, id: string,
options: EditIntegrationOptions, options: EditIntegrationOptions,
) { ) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]); const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_GUILD); throw new Error(Errors.MISSING_MANAGE_GUILD);
} }
@@ -506,7 +506,7 @@ export async function editIntegration(
/** Delete the attached integration object for the guild with this id. Requires MANAGE_GUILD permission. */ /** Delete the attached integration object for the guild with this id. Requires MANAGE_GUILD permission. */
export async function deleteIntegration(guildID: string, id: string) { export async function deleteIntegration(guildID: string, id: string) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]); const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_GUILD); throw new Error(Errors.MISSING_MANAGE_GUILD);
} }
@@ -516,7 +516,7 @@ export async function deleteIntegration(guildID: string, id: string) {
/** Sync an integration. Requires the MANAGE_GUILD permission. */ /** Sync an integration. Requires the MANAGE_GUILD permission. */
export async function syncIntegration(guildID: string, id: string) { export async function syncIntegration(guildID: string, id: string) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]); const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_GUILD); throw new Error(Errors.MISSING_MANAGE_GUILD);
} }
@@ -526,7 +526,7 @@ export async function syncIntegration(guildID: string, id: string) {
/** Returns a list of ban objects for the users banned from this guild. Requires the BAN_MEMBERS permission. */ /** Returns a list of ban objects for the users banned from this guild. Requires the BAN_MEMBERS permission. */
export async function getBans(guildID: string) { export async function getBans(guildID: string) {
const hasPerm = await botHasPermission(guildID, [Permissions.BAN_MEMBERS]); const hasPerm = await botHasPermission(guildID, ["BAN_MEMBERS"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_BAN_MEMBERS); throw new Error(Errors.MISSING_BAN_MEMBERS);
} }
@@ -542,7 +542,7 @@ export async function getBans(guildID: string) {
/** Returns a ban object for the given user or a 404 not found if the ban cannot be found. Requires the BAN_MEMBERS permission. */ /** Returns a ban object for the given user or a 404 not found if the ban cannot be found. Requires the BAN_MEMBERS permission. */
export async function getBan(guildID: string, memberID: string) { export async function getBan(guildID: string, memberID: string) {
const hasPerm = await botHasPermission(guildID, [Permissions.BAN_MEMBERS]); const hasPerm = await botHasPermission(guildID, ["BAN_MEMBERS"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_BAN_MEMBERS); throw new Error(Errors.MISSING_BAN_MEMBERS);
} }
@@ -554,7 +554,7 @@ export async function getBan(guildID: string, memberID: string) {
/** Ban a user from the guild and optionally delete previous messages sent by the user. Requires the BAN_MEMBERS permission. */ /** 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: string, id: string, options: BanOptions) { export async function ban(guildID: string, id: string, options: BanOptions) {
const hasPerm = await botHasPermission(guildID, [Permissions.BAN_MEMBERS]); const hasPerm = await botHasPermission(guildID, ["BAN_MEMBERS"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_BAN_MEMBERS); throw new Error(Errors.MISSING_BAN_MEMBERS);
} }
@@ -567,7 +567,7 @@ export async function ban(guildID: string, id: string, options: BanOptions) {
/** Remove the ban for a user. REquires BAN_MEMBERS permission */ /** Remove the ban for a user. REquires BAN_MEMBERS permission */
export async function unban(guildID: string, id: string) { export async function unban(guildID: string, id: string) {
const hasPerm = await botHasPermission(guildID, [Permissions.BAN_MEMBERS]); const hasPerm = await botHasPermission(guildID, ["BAN_MEMBERS"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_BAN_MEMBERS); throw new Error(Errors.MISSING_BAN_MEMBERS);
} }
@@ -576,7 +576,7 @@ export async function unban(guildID: string, id: string) {
/** Modify a guilds settings. Requires the MANAGE_GUILD permission. */ /** Modify a guilds settings. Requires the MANAGE_GUILD permission. */
export async function editGuild(guildID: string, options: GuildEditOptions) { export async function editGuild(guildID: string, options: GuildEditOptions) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]); const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_GUILD); throw new Error(Errors.MISSING_MANAGE_GUILD);
} }
@@ -598,7 +598,7 @@ export async function editGuild(guildID: string, options: GuildEditOptions) {
/** Get all the invites for this guild. Requires MANAGE_GUILD permission */ /** Get all the invites for this guild. Requires MANAGE_GUILD permission */
export async function getInvites(guildID: string) { export async function getInvites(guildID: string) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]); const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_GUILD); throw new Error(Errors.MISSING_MANAGE_GUILD);
} }
@@ -620,7 +620,7 @@ export function getVoiceRegions(guildID: string) {
export async function getWebhooks(guildID: string) { export async function getWebhooks(guildID: string) {
const hasPerm = await botHasPermission( const hasPerm = await botHasPermission(
guildID, guildID,
[Permissions.MANAGE_WEBHOOKS], ["MANAGE_WEBHOOKS"],
); );
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_WEBHOOKS); throw new Error(Errors.MISSING_MANAGE_WEBHOOKS);
@@ -688,7 +688,7 @@ export async function createGuildFromTemplate(
* Requires the `MANAGE_GUILD` permission. * Requires the `MANAGE_GUILD` permission.
*/ */
export async function getGuildTemplates(guildID: string) { export async function getGuildTemplates(guildID: string) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]); const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD); if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
const templates = await RequestManager.get( const templates = await RequestManager.get(
@@ -705,7 +705,7 @@ export async function deleteGuildTemplate(
guildID: string, guildID: string,
templateCode: string, templateCode: string,
) { ) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]); const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD); if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
const deletedTemplate = await RequestManager.delete( const deletedTemplate = await RequestManager.delete(
@@ -724,7 +724,7 @@ export async function createGuildTemplate(
guildID: string, guildID: string,
data: CreateGuildTemplate, data: CreateGuildTemplate,
) { ) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]); const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD); if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
if (data.name.length < 1 || data.name.length > 100) { if (data.name.length < 1 || data.name.length > 100) {
@@ -750,7 +750,7 @@ export async function createGuildTemplate(
* Requires the `MANAGE_GUILD` permission. * Requires the `MANAGE_GUILD` permission.
*/ */
export async function syncGuildTemplate(guildID: string, templateCode: string) { export async function syncGuildTemplate(guildID: string, templateCode: string) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]); const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD); if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
const template = await RequestManager.put( const template = await RequestManager.put(
@@ -768,7 +768,7 @@ export async function editGuildTemplate(
templateCode: string, templateCode: string,
data: EditGuildTemplate, data: EditGuildTemplate,
) { ) {
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_GUILD]); const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD); if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
if (data.name?.length && (data.name.length < 1 || data.name.length > 100)) { if (data.name?.length && (data.name.length < 1 || data.name.length > 100)) {
+7 -8
View File
@@ -7,7 +7,6 @@ import { ImageFormats, ImageSize } from "../types/cdn.ts";
import { DMChannelCreatePayload, MessageContent } from "../types/channel.ts"; import { DMChannelCreatePayload, MessageContent } from "../types/channel.ts";
import { Errors } from "../types/errors.ts"; import { Errors } from "../types/errors.ts";
import { EditMemberOptions } from "../types/member.ts"; import { EditMemberOptions } from "../types/member.ts";
import { Permissions } from "../types/permission.ts";
import { formatImageURL } from "../utils/cdn.ts"; import { formatImageURL } from "../utils/cdn.ts";
import { endpoints } from "../utils/constants.ts"; import { endpoints } from "../utils/constants.ts";
import { import {
@@ -65,7 +64,7 @@ export async function addRole(
} }
} }
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_ROLES]); const hasPerm = await botHasPermission(guildID, ["MANAGE_ROLES"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_ROLES); throw new Error(Errors.MISSING_MANAGE_ROLES);
} }
@@ -96,7 +95,7 @@ export async function removeRole(
} }
} }
const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_ROLES]); const hasPerm = await botHasPermission(guildID, ["MANAGE_ROLES"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_MANAGE_ROLES); throw new Error(Errors.MISSING_MANAGE_ROLES);
} }
@@ -142,7 +141,7 @@ export async function kick(guildID: string, memberID: string, reason?: string) {
throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW); throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW);
} }
const hasPerm = await botHasPermission(guildID, [Permissions.KICK_MEMBERS]); const hasPerm = await botHasPermission(guildID, ["KICK_MEMBERS"]);
if (!hasPerm) { if (!hasPerm) {
throw new Error(Errors.MISSING_KICK_MEMBERS); throw new Error(Errors.MISSING_KICK_MEMBERS);
} }
@@ -166,7 +165,7 @@ export async function editMember(
const hasManageNickPerm = await botHasPermission( const hasManageNickPerm = await botHasPermission(
guildID, guildID,
[Permissions.MANAGE_NICKNAMES], ["MANAGE_NICKNAMES"],
); );
if (!hasManageNickPerm) { if (!hasManageNickPerm) {
throw new Error(Errors.MISSING_MANAGE_NICKNAMES); throw new Error(Errors.MISSING_MANAGE_NICKNAMES);
@@ -175,7 +174,7 @@ export async function editMember(
const hasManageRolesPerm = await botHasPermission( const hasManageRolesPerm = await botHasPermission(
guildID, guildID,
[Permissions.MANAGE_ROLES], ["MANAGE_ROLES"],
); );
if ( if (
options.roles && options.roles &&
@@ -187,7 +186,7 @@ export async function editMember(
if (options.mute) { if (options.mute) {
const hasMuteMembersPerm = await botHasPermission( const hasMuteMembersPerm = await botHasPermission(
guildID, guildID,
[Permissions.MUTE_MEMBERS], ["MUTE_MEMBERS"],
); );
// TODO: This should check if the member is in a voice channel // TODO: This should check if the member is in a voice channel
if ( if (
@@ -199,7 +198,7 @@ export async function editMember(
const hasDeafenMembersPerm = await botHasPermission( const hasDeafenMembersPerm = await botHasPermission(
guildID, guildID,
[Permissions.DEAFEN_MEMBERS], ["DEAFEN_MEMBERS"],
); );
if ( if (
options.deaf && options.deaf &&
+10 -11
View File
@@ -8,7 +8,6 @@ import { MessageContent } from "../types/channel.ts";
import { Errors } from "../types/errors.ts"; import { Errors } from "../types/errors.ts";
import { UserPayload } from "../types/guild.ts"; import { UserPayload } from "../types/guild.ts";
import { MessageCreateOptions } from "../types/message.ts"; import { MessageCreateOptions } from "../types/message.ts";
import { Permissions } from "../types/permission.ts";
import { endpoints } from "../utils/constants.ts"; import { endpoints } from "../utils/constants.ts";
import { botHasChannelPermissions } from "../utils/permissions.ts"; import { botHasChannelPermissions } from "../utils/permissions.ts";
@@ -40,7 +39,7 @@ export async function deleteMessage(
// This needs to check the channels permission not the guild permission // This needs to check the channels permission not the guild permission
const hasManageMessages = await botHasChannelPermissions( const hasManageMessages = await botHasChannelPermissions(
message.channelID, message.channelID,
[Permissions.MANAGE_MESSAGES], ["MANAGE_MESSAGES"],
); );
if ( if (
!hasManageMessages !hasManageMessages
@@ -61,7 +60,7 @@ export async function deleteMessage(
export async function pin(channelID: string, messageID: string) { export async function pin(channelID: string, messageID: string) {
const hasManageMessagesPerm = await botHasChannelPermissions( const hasManageMessagesPerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.MANAGE_MESSAGES], ["MANAGE_MESSAGES"],
); );
if ( if (
!hasManageMessagesPerm !hasManageMessagesPerm
@@ -75,7 +74,7 @@ export async function pin(channelID: string, messageID: string) {
export async function unpin(channelID: string, messageID: string) { export async function unpin(channelID: string, messageID: string) {
const hasManageMessagesPerm = await botHasChannelPermissions( const hasManageMessagesPerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.MANAGE_MESSAGES], ["MANAGE_MESSAGES"],
); );
if ( if (
!hasManageMessagesPerm !hasManageMessagesPerm
@@ -95,7 +94,7 @@ export async function addReaction(
) { ) {
const hasAddReactionsPerm = await botHasChannelPermissions( const hasAddReactionsPerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.ADD_REACTIONS], ["ADD_REACTIONS"],
); );
if (!hasAddReactionsPerm) { if (!hasAddReactionsPerm) {
throw new Error(Errors.MISSING_ADD_REACTIONS); throw new Error(Errors.MISSING_ADD_REACTIONS);
@@ -103,7 +102,7 @@ export async function addReaction(
const hasReadMessageHistoryPerm = await botHasChannelPermissions( const hasReadMessageHistoryPerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.READ_MESSAGE_HISTORY], ["READ_MESSAGE_HISTORY"],
); );
if ( if (
!hasReadMessageHistoryPerm !hasReadMessageHistoryPerm
@@ -168,7 +167,7 @@ export async function removeUserReaction(
) { ) {
const hasManageMessagesPerm = await botHasChannelPermissions( const hasManageMessagesPerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.MANAGE_MESSAGES], ["MANAGE_MESSAGES"],
); );
if (!hasManageMessagesPerm) { if (!hasManageMessagesPerm) {
throw new Error(Errors.MISSING_MANAGE_MESSAGES); throw new Error(Errors.MISSING_MANAGE_MESSAGES);
@@ -188,7 +187,7 @@ export async function removeUserReaction(
export async function removeAllReactions(channelID: string, messageID: string) { export async function removeAllReactions(channelID: string, messageID: string) {
const hasManageMessagesPerm = await botHasChannelPermissions( const hasManageMessagesPerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.MANAGE_MESSAGES], ["MANAGE_MESSAGES"],
); );
if ( if (
!hasManageMessagesPerm !hasManageMessagesPerm
@@ -208,7 +207,7 @@ export async function removeReactionEmoji(
) { ) {
const hasManageMessagesPerm = await botHasChannelPermissions( const hasManageMessagesPerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.MANAGE_MESSAGES], ["MANAGE_MESSAGES"],
); );
if ( if (
!hasManageMessagesPerm !hasManageMessagesPerm
@@ -247,7 +246,7 @@ export async function editMessage(
const hasSendMessagesPerm = await botHasChannelPermissions( const hasSendMessagesPerm = await botHasChannelPermissions(
message.channelID, message.channelID,
[Permissions.SEND_MESSAGES], ["SEND_MESSAGES"],
); );
if ( if (
!hasSendMessagesPerm !hasSendMessagesPerm
@@ -257,7 +256,7 @@ export async function editMessage(
const hasSendTtsMessagesPerm = await botHasChannelPermissions( const hasSendTtsMessagesPerm = await botHasChannelPermissions(
message.channelID, message.channelID,
[Permissions.SEND_TTS_MESSAGES], ["SEND_TTS_MESSAGES"],
); );
if ( if (
content.tts && content.tts &&
+1 -2
View File
@@ -2,7 +2,6 @@ import { RequestManager } from "../module/requestManager.ts";
import { structures } from "../structures/mod.ts"; import { structures } from "../structures/mod.ts";
import { Errors } from "../types/errors.ts"; import { Errors } from "../types/errors.ts";
import { MessageCreateOptions } from "../types/message.ts"; import { MessageCreateOptions } from "../types/message.ts";
import { Permissions } from "../types/permission.ts";
import { import {
ExecuteWebhookOptions, ExecuteWebhookOptions,
WebhookCreateOptions, WebhookCreateOptions,
@@ -22,7 +21,7 @@ export async function createWebhook(
) { ) {
const hasManageWebhooksPerm = await botHasChannelPermissions( const hasManageWebhooksPerm = await botHasChannelPermissions(
channelID, channelID,
[Permissions.MANAGE_WEBHOOKS], ["MANAGE_WEBHOOKS"],
); );
if ( if (
!hasManageWebhooksPerm !hasManageWebhooksPerm
+12 -12
View File
@@ -50,7 +50,7 @@ export function memberHasPermission(
export async function botHasPermission( export async function botHasPermission(
guildID: string, guildID: string,
permissions: Permissions[], permissions: Permission[],
) { ) {
const guild = await cacheHandlers.get("guilds", guildID); const guild = await cacheHandlers.get("guilds", guildID);
if (!guild) return false; if (!guild) return false;
@@ -74,13 +74,13 @@ export async function botHasPermission(
if (permissionBits & BigInt(Permissions.ADMINISTRATOR)) return true; if (permissionBits & BigInt(Permissions.ADMINISTRATOR)) return true;
return permissions.every((permission) => permissionBits & BigInt(permission)); return permissions.every((permission) => permissionBits & BigInt(Permissions[permission]));
} }
/** Checks if the bot has the permissions in a channel */ /** Checks if the bot has the permissions in a channel */
export function botHasChannelPermissions( export function botHasChannelPermissions(
channelID: string, channelID: string,
permissions: Permissions[], permissions: Permission[],
) { ) {
return hasChannelPermissions(channelID, botID, permissions); return hasChannelPermissions(channelID, botID, permissions);
} }
@@ -89,7 +89,7 @@ export function botHasChannelPermissions(
export async function hasChannelPermissions( export async function hasChannelPermissions(
channelID: string, channelID: string,
memberID: string, memberID: string,
permissions: Permissions[], permissions: Permission[],
) { ) {
const channel = await cacheHandlers.get("channels", channelID); const channel = await cacheHandlers.get("channels", channelID);
if (!channel) return false; if (!channel) return false;
@@ -121,7 +121,7 @@ export async function hasChannelPermissions(
if (member.roles.includes(overwrite.id)) rolesOverwrites.push(overwrite); if (member.roles.includes(overwrite.id)) rolesOverwrites.push(overwrite);
} }
const allowedPermissions = new Set<Permissions>(); const allowedPermissions = new Set<Permission>();
// Member perms override everything so we must check them first // Member perms override everything so we must check them first
if (memberOverwrite) { if (memberOverwrite) {
@@ -129,12 +129,12 @@ export async function hasChannelPermissions(
const denyBits = memberOverwrite.deny; const denyBits = memberOverwrite.deny;
for (const perm of permissions) { for (const perm of permissions) {
// One of the necessary permissions is denied. Since this is main permission we can cancel if its denied. // One of the necessary permissions is denied. Since this is main permission we can cancel if its denied.
if (BigInt(denyBits) & BigInt(perm)) return false; if (BigInt(denyBits) & BigInt(Permissions[perm])) return false;
// Already allowed perm // Already allowed perm
if (allowedPermissions.has(perm)) continue; if (allowedPermissions.has(perm)) continue;
// This perm is allowed so we save it // This perm is allowed so we save it
if (BigInt(allowBits) & BigInt(perm)) { if (BigInt(allowBits) & BigInt(Permissions[perm])) {
allowedPermissions.add(perm); allowedPermissions.add(perm);
} }
} }
@@ -148,17 +148,17 @@ export async function hasChannelPermissions(
for (const overwrite of rolesOverwrites) { for (const overwrite of rolesOverwrites) {
const allowBits = overwrite.allow; const allowBits = overwrite.allow;
// This perm is allowed so we save it // This perm is allowed so we save it
if (BigInt(allowBits) & BigInt(perm)) { if (BigInt(allowBits) & BigInt(Permissions[perm])) {
allowedPermissions.add(perm); allowedPermissions.add(perm);
break; break;
} }
const denyBits = overwrite.deny; const denyBits = overwrite.deny;
// If this role denies it we need to save and check if another role allows it, allows > deny // If this role denies it we need to save and check if another role allows it, allows > deny
if (BigInt(denyBits) & BigInt(perm)) { if (BigInt(denyBits) & BigInt(Permissions[perm])) {
// This role denies his perm, but before denying we need to check all other roles if any allow as allow > deny // This role denies his perm, but before denying we need to check all other roles if any allow as allow > deny
const isAllowed = rolesOverwrites.some((o) => const isAllowed = rolesOverwrites.some((o) =>
BigInt(o.allow) & BigInt(perm) BigInt(o.allow) & BigInt(Permissions[perm])
); );
if (isAllowed) continue; if (isAllowed) continue;
// This permission is in fact denied. Since Roles overrule everything below here we can cancel ou here // This permission is in fact denied. Since Roles overrule everything below here we can cancel ou here
@@ -174,9 +174,9 @@ export async function hasChannelPermissions(
// Already allowed perm // Already allowed perm
if (allowedPermissions.has(perm)) continue; if (allowedPermissions.has(perm)) continue;
// One of the necessary permissions is denied. Since everyone overwrite overrides role perms we can cancel here // One of the necessary permissions is denied. Since everyone overwrite overrides role perms we can cancel here
if (BigInt(denyBits) & BigInt(perm)) return false; if (BigInt(denyBits) & BigInt(Permissions[perm])) return false;
// This perm is allowed so we save it // This perm is allowed so we save it
if (BigInt(allowBits) & BigInt(perm)) { if (BigInt(allowBits) & BigInt(Permissions[perm])) {
allowedPermissions.add(perm); allowedPermissions.add(perm);
} }
} }
+2 -3
View File
@@ -22,7 +22,6 @@ import {
editChannel, editChannel,
} from "../src/handlers/channel.ts"; } from "../src/handlers/channel.ts";
import { getChannel } from "../src/handlers/guild.ts"; import { getChannel } from "../src/handlers/guild.ts";
import { Permissions } from "../src/types/permission.ts";
const token = Deno.env.get("DISCORD_TOKEN"); const token = Deno.env.get("DISCORD_TOKEN");
if (!token) throw "Token is not provided"; if (!token) throw "Token is not provided";
@@ -179,13 +178,13 @@ Deno.test({
data.guildID, data.guildID,
data.roleID, data.roleID,
channel.permissionOverwrites, channel.permissionOverwrites,
[Permissions.VIEW_CHANNEL, Permissions.SEND_MESSAGES], ["VIEW_CHANNEL", "SEND_MESSAGES"],
); );
const missingPerm = channelOverwriteHasPermission( const missingPerm = channelOverwriteHasPermission(
data.guildID, data.guildID,
data.roleID, data.roleID,
channel.permissionOverwrites, channel.permissionOverwrites,
[Permissions.USE_EXTERNAL_EMOJIS], ["USE_EXTERNAL_EMOJIS"],
); );
assertEquals(hasPerm, true); assertEquals(hasPerm, true);