From 65b5c3ac406cf38b4e4cbe7e6dc5fcc4d5418047 Mon Sep 17 00:00:00 2001 From: Skillz Date: Wed, 5 Aug 2020 14:50:08 -0400 Subject: [PATCH] cleanup --- src/handlers/guild.ts | 9 +++------ src/types/channel.ts | 33 ++++----------------------------- src/types/discord.ts | 10 +++++----- src/types/guild.ts | 24 ++++++++++++------------ src/utils/permissions.ts | 12 +++--------- 5 files changed, 27 insertions(+), 61 deletions(-) diff --git a/src/handlers/guild.ts b/src/handlers/guild.ts index 6fcbb6db9..c16ba277a 100644 --- a/src/handlers/guild.ts +++ b/src/handlers/guild.ts @@ -11,7 +11,6 @@ import { endpoints } from "../constants/discord.ts"; import { Errors } from "../types/errors.ts"; import { Permissions, Permission } from "../types/permission.ts"; import { - CreateChannelOptions, ChannelCreatePayload, ChannelTypes, } from "../types/channel.ts"; @@ -28,6 +27,7 @@ import { GuildEditOptions, PruneOptions, PrunePayload, + ChannelCreateOptions, } from "../types/guild.ts"; import { RoleData } from "../types/role.ts"; import { createRole } from "../structures/role.ts"; @@ -93,7 +93,7 @@ export function guildBannerURL( export async function createGuildChannel( guild: Guild, name: string, - options?: CreateChannelOptions, + options?: ChannelCreateOptions, ) { if (!botHasPermission(guild.id, [Permissions.MANAGE_CHANNELS])) { throw new Error(Errors.MISSING_MANAGE_CHANNELS); @@ -510,10 +510,7 @@ export function channelHasPermissions( const role = guild.roles.get(roleID); if (!role) return bits; - bits |= role.permissions.reduce( - (bits, p) => bits & BigInt(Permissions[p]), - BigInt(0), - ); + bits |= BigInt(role.permissions_new) return bits; }, BigInt(0)); diff --git a/src/types/channel.ts b/src/types/channel.ts index 7f1236bf6..09da44403 100644 --- a/src/types/channel.ts +++ b/src/types/channel.ts @@ -22,7 +22,7 @@ export interface ChannelEditOptions { parent_id?: string; } -export interface Base_Channel_Create { +export interface BaseChannelCreate { /** The id of the guild */ guild_id?: string; /** Sorting position of the channel */ @@ -65,37 +65,16 @@ export interface DMChannelCreatePayload { }[]; } -export interface ChannelCreatePayload extends Base_Channel_Create { +export interface ChannelCreatePayload extends BaseChannelCreate { /** The id of this channel */ id: string; /** The type of the channel */ - type: Channel_Type; + type: ChannelType; /** Explicit permission overwrites for members and roles */ permission_overwrites?: RawOverwrite[]; } -export interface CreateChannelOptions { - /** The type of the channel */ - type?: ChannelTypes; - /** The channel topic (0-1024 characters) */ - topic?: string; - /** The bitrate (in bits) of the voice channel */ - bitrate?: number; - /** The user limit of the voice channel */ - user_limit?: number; - /** Amount of seconds a user has to wait before sending another message (0-21600) Bots and users with the permission MANAGE_MESSAGES or MANAGE_CHANNEL are unaffected. */ - rate_limit_per_user?: number; - /** Sorting position of the channel */ - position?: number; - /** Explicit permission overwrites for members and roles */ - permission_overwrites?: Overwrite[]; - /** The parent category id */ - parent_id?: string | null; - /** Whether the channel is nsfw */ - nsfw?: boolean; -} - -export type Channel_Type = 0 | 1 | 2 | 4 | 5 | 6; +export type ChannelType = 0 | 1 | 2 | 4 | 5 | 6; export enum ChannelTypes { /** A text channel within a server */ @@ -114,10 +93,6 @@ export enum ChannelTypes { GUILD_STORE, } -// export interface File_Content { - -// } - export interface MessageContent { mentions?: { /** An array of allowed mention types to parse from the content. */ diff --git a/src/types/discord.ts b/src/types/discord.ts index b814bd40d..20227b50a 100644 --- a/src/types/discord.ts +++ b/src/types/discord.ts @@ -1,6 +1,6 @@ import { Activity } from "./message.ts"; import { ClientStatusPayload } from "./presence.ts"; -import { Partial_User, UserPayload } from "./guild.ts"; +import { PartialUser, UserPayload } from "./guild.ts"; import { MemberCreatePayload } from "./member.ts"; export interface DiscordPayload { @@ -177,7 +177,7 @@ export interface Emoji { animated?: boolean; } -export enum StatusType { +export enum StatusTypes { Online = "online", DoNotDisturb = "dnd", Idle = "idle", @@ -185,7 +185,7 @@ export enum StatusType { Offline = "offline", } -export type Status_Type = "online" | "dnd" | "idle" | "invisible" | "offline"; +export type StatusType = "online" | "dnd" | "idle" | "invisible" | "offline"; export interface Status { afk: boolean; @@ -199,7 +199,7 @@ export interface WebhookUpdatePayload { export interface PresenceUpdatePayload { /** The user presence is being updated for. */ - user: Partial_User; + user: PartialUser; /** The roles this user is in */ roles: string[]; /** null, or the user's current activity */ @@ -207,7 +207,7 @@ export interface PresenceUpdatePayload { /** The id of the guild */ guild_id: string; /** Either idle, dnd, online, or offline */ - status: Status_Type; + status: StatusType; /** All user's current activity */ activities: Activity[]; /** The user's platform dependent status */ diff --git a/src/types/guild.ts b/src/types/guild.ts index 463e8e565..1742291aa 100644 --- a/src/types/guild.ts +++ b/src/types/guild.ts @@ -103,7 +103,7 @@ export interface UpdateGuildPayload { /** The custom guild emojis */ emojis: Emoji[]; /** Enabled guild features */ - features: Guild_Features[]; + features: GuildFeatures[]; /** Required MFA level for the guild */ mfa_level: number; /** True if the server widget is enabled */ @@ -151,7 +151,7 @@ export interface CreateGuildPayload extends UpdateGuildPayload { unavailable: boolean; /** Total number of members in this guild */ member_count?: number; - voice_states: Voice_State[]; + voice_states: VoiceState[]; /** Users in the guild */ members: MemberCreatePayload[]; /** Channels in the guild */ @@ -159,7 +159,7 @@ export interface CreateGuildPayload extends UpdateGuildPayload { presences: Presence[]; } -export type Guild_Features = +export type GuildFeatures = | "INVITE_SPLASH" | "VIP_REGIONS" | "VANITY_URL" @@ -173,7 +173,7 @@ export type Guild_Features = | "ANIMATED_ICON" | "BANNER"; -export interface Voice_Region { +export interface VoiceRegion { /** unique ID for the region */ id: string; /** name of the region */ @@ -245,7 +245,7 @@ export interface EditIntegrationOptions { enable_emoticons: boolean; } -export interface Guild_Integration { +export interface GuildIntegration { /** The integrations unique id */ id: string; /** the integrations name */ @@ -304,7 +304,7 @@ export interface UserPayload { premium_type?: number; } -export interface Partial_User { +export interface PartialUser { /** The user's id */ id: string; /** the user's username, not unique across the platform */ @@ -331,7 +331,7 @@ export interface Partial_User { premium_type?: number; } -export enum User_Flags { +export enum UserFlags { NONE, DISCORD_EMPLOYEE, DISCORD_PARTNER, @@ -345,17 +345,17 @@ export enum User_Flags { SYSTEM = 1 << 12, } -export enum Nitro_Types { +export enum NitroTypes { NITRO_CLASSIC = 1, NITRO, } -export interface Vanity_Invite { +export interface VanityInvite { code: string | null; uses: number; } -export interface Guild_Embed { +export interface GuildEmbed { /** Whether the embed is enbaled. */ enabled: boolean; } @@ -480,7 +480,7 @@ export interface RawOverwrite { deny_new: string; } -export interface ChannelCreate_Options { +export interface ChannelCreateOptions { /** The type of the channel */ type?: ChannelType; /** The channel topic. (0-1024 characters) */ @@ -536,7 +536,7 @@ export interface PruneOptions { roles: string[]; } -export interface Voice_State { +export interface VoiceState { /** the guild id this voice state is for */ guild_id?: string; /** the channel id this user is connected to */ diff --git a/src/utils/permissions.ts b/src/utils/permissions.ts index a34732e0e..7ce266be1 100644 --- a/src/utils/permissions.ts +++ b/src/utils/permissions.ts @@ -17,13 +17,10 @@ export function memberHasPermission( if (memberID === guild.ownerID) return true; const permissionBits = memberRoleIDs.map((id) => - guild.roles.get(id)?.permissions || [] + guild.roles.get(id)?.permissions_new ) .reduce((bits, permissions) => { - bits |= permissions.reduce( - (b, p) => b & BigInt(Permissions[p]), - BigInt(0), - ); + bits |= BigInt(permissions) return bits; }, BigInt(0)); @@ -44,10 +41,7 @@ export function botHasPermission(guildID: string, permissions: Permissions[]) { const permissionBits = member.roles .map((id) => guild.roles.get(id)!) .reduce((bits, data) => { - bits |= data.permissions.reduce( - (b, p) => b & BigInt(Permissions[p]), - BigInt(0), - ); + bits |= BigInt(data.permissions_new) return bits; }, BigInt(0));