errors enum instead of literal errors

This commit is contained in:
Skillz
2020-03-11 12:12:53 -04:00
parent b89afce574
commit 2efb79adea
6 changed files with 60 additions and 18 deletions

3
mod.ts
View File

@@ -9,7 +9,8 @@ const startup = async () => {
bot_id: "675412054529540107",
intents: [Intents.GUILDS, Intents.GUILD_MESSAGES],
event_handlers: {
guild_create: guild => logYellow(guild.roles().toString())
ready: () => logYellow("Bot ready emitted"),
channel_create: channel => console.log(channel.permission_overwrites() || "can;t find")
}
})
}

View File

@@ -12,6 +12,9 @@ import Client from "../module/client.ts"
import { endpoints } from "../constants/discord.ts"
import { create_message, Message } from "./message.ts"
import { Message_Create_Options } from "../types/message.ts"
import { calculate_permissions, bot_has_permission } from "../utils/permissions.ts"
import { Permissions } from "../types/permission.ts"
import { Errors } from "../types/errors.ts"
export const create_channel = (data: Channel_Create_Payload, client: Client) => {
const base_channel = {
@@ -23,9 +26,15 @@ export const create_channel = (data: Channel_Create_Payload, client: Client) =>
type: () => data.type,
/** The id of the guild where this channel exists */
guild_id: () => data.guild_id,
// TODO: fix this from being number on allow and deny to being array of strings
/** The permission overwrites for this channel */
permission_overwrites: () => data.permission_overwrites,
permission_overwrites: () =>
data.permission_overwrites
? data.permission_overwrites.map(perm => ({
...perm,
allow: calculate_permissions(perm.allow),
deny: calculate_permissions(perm.deny)
}))
: [],
/** Whether this channel is nsfw or not */
nsfw: () => false
}
@@ -38,13 +47,24 @@ export const create_channel = (data: Channel_Create_Payload, client: Client) =>
last_message_id: () => data.last_message_id,
/** Fetch a single message from the server. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */
get_message: async (id: string) => {
// TODO: check if the user has VIEW_CHANNEL and READ_MESSAGE_HISTORY
if (data.guild_id) {
if (!bot_has_permission(data.guild_id, client.bot_id, [Permissions.VIEW_CHANNEL]))
throw new Error(Errors.MISSING_VIEW_CHANNEL)
if (!bot_has_permission(data.guild_id, client.bot_id, [Permissions.READ_MESSAGE_HISTORY]))
throw new Error(Errors.MISSING_READ_MESSAGE_HISTORY)
}
const result = await client.discordRequestManager.get(endpoints.CHANNEL_MESSAGE(data.id, id))
return create_message(result, client)
},
/** Fetches between 2-100 messages. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */
get_messages: async (options?: Get_Messages_After | Get_Messages_Before | Get_Messages_Around | Get_Messages) => {
// TODO: check if the user has VIEW_CHANNEL and READ_MESSAGE_HISTORY
if (data.guild_id) {
if (!bot_has_permission(data.guild_id, client.bot_id, [Permissions.VIEW_CHANNEL]))
throw new Error(Errors.MISSING_VIEW_CHANNEL)
if (!bot_has_permission(data.guild_id, client.bot_id, [Permissions.READ_MESSAGE_HISTORY]))
throw new Error(Errors.MISSING_READ_MESSAGE_HISTORY)
}
if (options?.limit && options.limit > 100) return
const result = (await client.discordRequestManager.get(
@@ -62,8 +82,9 @@ export const create_channel = (data: Channel_Create_Payload, client: Client) =>
},
/** Send a message to the channel. Requires SEND_MESSAGES permission. */
send_message: async (content: string | MessageContent) => {
if (data.type !== Channel_Types.DM) {
// TODO: check if the bot has SEND_MESSAGES permission
if (data.guild_id) {
if (!bot_has_permission(data.guild_id, client.bot_id, [Permissions.SEND_MESSAGES]))
throw new Error(Errors.MISSING_SEND_MESSAGES)
}
if (typeof content === "string") content = { content }

View File

@@ -287,9 +287,9 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
return bits
}, 0)
channel.permission_overwrites()?.forEach(overwrite => {
permissionBits = (permissionBits & ~overwrite.deny) | overwrite.allow
})
// channel.permission_overwrites()?.forEach(overwrite => {
// permissionBits = (permissionBits & ~overwrite.deny) | overwrite.allow
// })
if (permissionBits & Permissions.ADMINISTRATOR) return true

View File

@@ -6,6 +6,7 @@ import { Image_Size, Image_Formats } from "../types/cdn.ts"
import { Permission, Permissions } from "../types/permission.ts"
import { Role_Data } from "../types/role.ts"
import { member_has_permission, bot_has_permission } from "../utils/permissions.ts"
import { Errors } from "../types/errors.ts"
export const create_member = (
data: Member_Create_Payload,
@@ -48,38 +49,38 @@ export const create_member = (
/** Add a role to the member */
add_role: (role_id: string, reason?: string) => {
// TODO: check if the bots highest role is above this one
if (!bot_has_permission(guild_id, client.bot_id, [Permissions.MANAGE_ROLES])) throw 'MISSING_MANAGE_ROLES'
if (!bot_has_permission(guild_id, client.bot_id, [Permissions.MANAGE_ROLES])) throw new Error(Errors.MISSING_MANAGE_ROLES)
return client.discordRequestManager.put(endpoints.GUILD_MEMBER_ROLE(guild_id, data.user.id, role_id), { reason })
},
/** Remove a role from the member */
remove_role: (role_id: string, reason?: string) => {
// TODO: check if the bots highest role is above this role
if (!bot_has_permission(guild_id, client.bot_id, [Permissions.MANAGE_ROLES])) throw 'MISSING_MANAGE_ROLES'
if (!bot_has_permission(guild_id, client.bot_id, [Permissions.MANAGE_ROLES])) throw new Error(Errors.MISSING_MANAGE_ROLES)
return client.discordRequestManager.delete(endpoints.GUILD_MEMBER_ROLE(guild_id, data.user.id, role_id), { reason })
},
/** Kick a member from the server */
kick: (reason?: string) => {
// TODO: Check if the bot is above the user so it is capable of kicking
if (!bot_has_permission(guild_id, client.bot_id, [Permissions.KICK_MEMBERS])) throw 'MISSING_KICK_MEMBERS'
if (!bot_has_permission(guild_id, client.bot_id, [Permissions.KICK_MEMBERS])) throw new Error(Errors.MISSING_KICK_MEMBERS)
return client.discordRequestManager.delete(endpoints.GUILD_MEMBER(guild_id, data.user.id), { reason })
},
/** Edit the member */
edit: (options: Edit_Member_Options) => {
if (options.nick) {
if (options.nick.length > 32) throw "NICKNAMES_MAX_LENGTH"
if (!bot_has_permission(guild_id, client.bot_id, [Permissions.MANAGE_NICKNAMES])) throw "MISSING_MANAGE_NICKNAME"
if (options.nick.length > 32) throw new Error(Errors.NICKNAMES_MAX_LENGTH)
if (!bot_has_permission(guild_id, client.bot_id, [Permissions.MANAGE_NICKNAMES])) throw new Error(Errors.MISSING_MANAGE_NICKNAMES)
}
if (options.roles && !bot_has_permission(guild_id, client.bot_id, [Permissions.MANAGE_ROLES]))
throw "MISSING_MANAGE_ROLES"
throw new Error(Errors.MISSING_MANAGE_ROLES)
if (options.mute) {
// TODO: This should check if the member is in a voice channel
if (!bot_has_permission(guild_id, client.bot_id, [Permissions.MUTE_MEMBERS])) throw "MISSING_MUTE_MEMBERS"
if (!bot_has_permission(guild_id, client.bot_id, [Permissions.MUTE_MEMBERS])) throw new Error(Errors.MISSING_MUTE_MEMBERS)
}
if (options.deaf && !bot_has_permission(guild_id, client.bot_id, [Permissions.DEAFEN_MEMBERS]))
throw "MISSING_DEAFEN_MEMBERS"
throw new Error(Errors.MISSING_DEAFEN_MEMBERS)
// TODO: if channel id is provided check if the bot has CONNECT and MOVE in channel and current channel

11
types/errors.ts Normal file
View File

@@ -0,0 +1,11 @@
export enum Errors {
MISSING_SEND_MESSAGES = "MISSING_SEND_MESSAGES",
MISSING_MANAGE_ROLES = "MISSING_MANAGE_ROLES",
MISSING_KICK_MEMBERS = "MISSING_KICK_MEMBERS",
MISSING_VIEW_CHANNEL = "MISSING_VIEW_CHANNEL",
MISSING_READ_MESSAGE_HISTORY = "MISSING_READ_MESSAGE_HISTORY",
MISSING_MANAGE_NICKNAMES = "MISSING_MANAGE_NICKNAMES",
MISSING_MUTE_MEMBERS = "MISSING_MUTE_MEMBERS",
MISSING_DEAFEN_MEMBERS = "MISSING_DEAFEN_MEMBERS",
NICKNAMES_MAX_LENGTH = "NICKNAMES_MAX_LENGTH"
}

View File

@@ -44,3 +44,11 @@ export const bot_has_permission = (guild_id: string, bot_id: string, permissions
return permissions.every(permission => permissionBits & permission)
}
export const calculate_permissions = (permission_bits: number) => {
console.log("calculating")
// console.log(Object.keys(Permissions))
return Object.keys(Permissions).filter(perm => {
return permission_bits & Permissions[perm as Permission]
})
}