client to functional instead of class

This commit is contained in:
Skillz
2020-03-23 19:26:19 -04:00
parent 33145771f9
commit ac3ea577a7
8 changed files with 474 additions and 492 deletions

View File

@@ -7,7 +7,7 @@ import {
MessageContent,
Create_Invite_Options
} from "../types/channel.ts"
import Client from "../module/client.ts"
import { bot_id } 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"
@@ -17,7 +17,7 @@ import { Errors } from "../types/errors.ts"
import { Request_Manager } from "../module/request_manager.ts"
import { cache } from "../utils/cache.ts"
export const create_channel = (data: Channel_Create_Payload, client: Client) => {
export const create_channel = (data: Channel_Create_Payload) => {
const channel = {
/** The raw channel data */
raw: () => data,
@@ -45,20 +45,20 @@ export const create_channel = (data: Channel_Create_Payload, client: Client) =>
/** Fetch a single message from the server. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */
get_message: async (id: string) => {
if (data.guild_id) {
if (!bot_has_permission(data.guild_id, client.bot_id, [Permissions.VIEW_CHANNEL]))
if (!bot_has_permission(data.guild_id, 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]))
if (!bot_has_permission(data.guild_id, bot_id, [Permissions.READ_MESSAGE_HISTORY]))
throw new Error(Errors.MISSING_READ_MESSAGE_HISTORY)
}
const result = await Request_Manager.get(endpoints.CHANNEL_MESSAGE(data.id, id))
return create_message(result, client)
return create_message(result)
},
/** 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) => {
if (data.guild_id) {
if (!bot_has_permission(data.guild_id, client.bot_id, [Permissions.VIEW_CHANNEL]))
if (!bot_has_permission(data.guild_id, 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]))
if (!bot_has_permission(data.guild_id, bot_id, [Permissions.READ_MESSAGE_HISTORY]))
throw new Error(Errors.MISSING_READ_MESSAGE_HISTORY)
}
@@ -68,28 +68,28 @@ export const create_channel = (data: Channel_Create_Payload, client: Client) =>
endpoints.CHANNEL_MESSAGES(data.id),
options
)) as Message_Create_Options[]
return result.map(res => create_message(res, client))
return result.map(res => create_message(res))
},
/** Get pinned messages in this channel. */
get_pins: async () => {
const result = (await Request_Manager.get(endpoints.CHANNEL_PINS(data.id))) as Message_Create_Options[]
return result.map(res => create_message(res, client))
return result.map(res => create_message(res))
},
/** Send a message to the channel. Requires SEND_MESSAGES permission. */
send_message: async (content: string | MessageContent) => {
if (typeof content === "string") content = { content }
if (data.guild_id) {
if (!bot_has_permission(data.guild_id, client.bot_id, [Permissions.SEND_MESSAGES]))
if (!bot_has_permission(data.guild_id, bot_id, [Permissions.SEND_MESSAGES]))
throw new Error(Errors.MISSING_SEND_MESSAGES)
if (content.tts && !bot_has_permission(data.guild_id, client.bot_id, [Permissions.SEND_TTS_MESSAGES]))
if (content.tts && !bot_has_permission(data.guild_id, bot_id, [Permissions.SEND_TTS_MESSAGES]))
throw new Error(Errors.MISSING_SEND_TTS_MESSAGE)
}
if (content.content && content.content.length > 2000) throw new Error(Errors.MESSAGE_MAX_LENGTH)
const result = await Request_Manager.post(endpoints.CHANNEL_MESSAGES(data.id), content)
return create_message(result, client)
return create_message(result)
},
/** The position of the channel in the server. If this channel does not have a position for example DM channels, it will be -1 */
position: () => {
@@ -103,7 +103,7 @@ export const create_channel = (data: Channel_Create_Payload, client: Client) =>
mention: () => `<#${data.id}>`,
/** Delete messages from the channel. 2-100. Requires the MANAGE_MESSAGES permission */
delete_messages: (ids: string[], reason?: string) => {
if (data.guild_id && !bot_has_permission(data.guild_id, client.bot_id, [Permissions.MANAGE_MESSAGES]))
if (data.guild_id && !bot_has_permission(data.guild_id, bot_id, [Permissions.MANAGE_MESSAGES]))
throw new Error(Errors.MISSING_MANAGE_MESSAGES)
if (ids.length < 2) throw new Error(Errors.DELETE_MESSAGES_MIN)
@@ -119,19 +119,19 @@ export const create_channel = (data: Channel_Create_Payload, client: Client) =>
},
/** Gets the invites for this channel. Requires MANAGE_CHANNEL */
get_invites: () => {
if (data.guild_id && !bot_has_permission(data.guild_id, client.bot_id, [Permissions.MANAGE_CHANNELS]))
if (data.guild_id && !bot_has_permission(data.guild_id, bot_id, [Permissions.MANAGE_CHANNELS]))
throw new Error(Errors.MISSING_MANAGE_CHANNELS)
return Request_Manager.get(endpoints.CHANNEL_INVITES(data.id))
},
/** Creates a new invite for this channel. Requires CREATE_INSTANT_INVITE */
create_invite: (options: Create_Invite_Options) => {
if (data.guild_id && !bot_has_permission(data.guild_id, client.bot_id, [Permissions.CREATE_INSTANT_INVITE]))
if (data.guild_id && !bot_has_permission(data.guild_id, bot_id, [Permissions.CREATE_INSTANT_INVITE]))
throw new Error(Errors.MISSING_CREATE_INSTANT_INVITE)
return Request_Manager.post(endpoints.CHANNEL_INVITES(data.id), options)
},
/** Gets the webhooks for this channel. Requires MANAGE_WEBHOOKS */
get_webhooks: () => {
if (data.guild_id && !bot_has_permission(data.guild_id, client.bot_id, [Permissions.MANAGE_WEBHOOKS]))
if (data.guild_id && !bot_has_permission(data.guild_id, bot_id, [Permissions.MANAGE_WEBHOOKS]))
throw new Error(Errors.MISSING_MANAGE_WEBHOOKS)
return Request_Manager.get(endpoints.CHANNEL_WEBHOOKS(data.id))
}

View File

@@ -1,4 +1,4 @@
import Client from "../module/client.ts"
import { bot_id } from "../module/client.ts"
import { endpoints } from "../constants/discord.ts"
import { format_image_url } from "../utils/cdn.ts"
import {
@@ -22,8 +22,9 @@ import { Permissions, Permission } from "../types/permission.ts"
import { bot_has_permission } from "../utils/permissions.ts"
import { Errors } from "../types/errors.ts"
import { Request_Manager } from "../module/request_manager.ts"
import { Role_Data } from "../types/role.ts"
export const create_guild = (data: Create_Guild_Payload, client: Client) => {
export const create_guild = (data: Create_Guild_Payload) => {
const guild = {
/** The raw create guild payload data. */
raw: () => data,
@@ -66,9 +67,9 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
/** The current open voice states in the guild. */
voice_states: () => data.voice_states,
/** The users in this guild. */
members: new Map(data.members.map(m => [m.user.id, create_member(m, data.id, data.roles, data.owner_id, client)])),
members: new Map(data.members.map(m => [m.user.id, create_member(m, data.id, data.roles, data.owner_id)])),
/** The channels in the guild */
channels: new Map(data.channels.map(c => [c.id, create_channel(c, client)])),
channels: new Map(data.channels.map(c => [c.id, create_channel(c)])),
/** The presences of all the users in the guild. */
presences: new Map(data.presences.map(p => [p.user.id, p])),
/** The maximum amount of presences for the guild(the default value, currently 5000 is in effect when null is returned.) */
@@ -100,7 +101,7 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
data.banner ? format_image_url(endpoints.GUILD_BANNER(data.id, data.banner), size, format) : undefined,
/** Create a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */
create_channel: (name: string, options: Channel_Create_Options) => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_CHANNELS]))
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_CHANNELS]))
throw new Error(Errors.MISSING_MANAGE_CHANNELS)
return Request_Manager.post(endpoints.GUILD_CHANNELS(data.id), {
name,
@@ -138,7 +139,7 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
},
/** Create an emoji in the server. Emojis and animated emojis have a maximum file size of 256kb. Attempting to upload an emoji larger than this limit will fail and return 400 Bad Request and an error message, but not a JSON status code. */
create_emoji: (name: string, image: string, options: Create_Emojis_Options) => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_EMOJIS]))
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_EMOJIS]))
throw new Error(Errors.MISSING_MANAGE_EMOJIS)
return Request_Manager.post(endpoints.GUILD_EMOJIS(data.id), {
...options,
@@ -148,7 +149,7 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
},
/** Modify the given emoji. Requires the MANAGE_EMOJIS permission. */
edit_emoji: (id: string, options: Edit_Emojis_Options) => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_EMOJIS]))
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_EMOJIS]))
throw new Error(Errors.MISSING_MANAGE_EMOJIS)
return Request_Manager.patch(endpoints.GUILD_EMOJI(data.id, id), {
name: options.name,
@@ -157,30 +158,33 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
},
/** Delete the given emoji. Requires the MANAGE_EMOJIS permission. Returns 204 No Content on success. */
delete_emoji: (id: string, reason?: string) => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_EMOJIS]))
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_EMOJIS]))
throw new Error(Errors.MISSING_MANAGE_EMOJIS)
return Request_Manager.delete(endpoints.GUILD_EMOJI(data.id, id), { reason })
},
/** Create a new role for the guild. Requires the MANAGE_ROLES permission. */
create_role: async (options: Create_Role_Options) => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_ROLES]))
create_role: async (options: Create_Role_Options, reason?: string) => {
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_ROLES]))
throw new Error(Errors.MISSING_MANAGE_ROLES)
const role = await Request_Manager.post(endpoints.GUILD_ROLES(data.id), {
const role_data = await Request_Manager.post(endpoints.GUILD_ROLES(data.id), {
...options,
permissions: options.permissions?.map(perm => Permissions[perm])
permissions: options.permissions?.map(perm => Permissions[perm]),
reason
})
const role = create_role(role_data as Role_Data)
guild.roles().set(role_data.id, role)
return role
},
/** Edit a guild role. Requires the MANAGE_ROLES permission. */
edit_role: (id: string, options: Create_Role_Options) => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_ROLES]))
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_ROLES]))
throw new Error(Errors.MISSING_MANAGE_ROLES)
return Request_Manager.patch(endpoints.GUILD_ROLE(data.id, id), options)
},
/** Delete a guild role. Requires the MANAGE_ROLES permission. */
delete_role: (id: string) => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_ROLES]))
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_ROLES]))
throw new Error(Errors.MISSING_MANAGE_ROLES)
return Request_Manager.delete(endpoints.GUILD_ROLE(data.id, id))
},
@@ -189,20 +193,20 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
* ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your roles will be cached in your guild.**
*/
get_roles: () => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_ROLES]))
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_ROLES]))
throw new Error(Errors.MISSING_MANAGE_ROLES)
return Request_Manager.get(endpoints.GUILD_ROLES(data.id))
},
/** Modify the positions of a set of role objects for the guild. Requires the MANAGE_ROLES permission. */
swap_roles: (rolePositons: Position_Swap) => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_ROLES]))
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_ROLES]))
throw new Error(Errors.MISSING_MANAGE_ROLES)
return Request_Manager.patch(endpoints.GUILD_ROLES(data.id), rolePositons)
},
/** Check how many members would be removed from the server in a prune operation. Requires the KICK_MEMBERS permission */
get_prune_count: async (days: number) => {
if (days < 1) throw new Error(Errors.PRUNE_MIN_DAYS)
if (!bot_has_permission(data.id, client.bot_id, [Permissions.KICK_MEMBERS]))
if (!bot_has_permission(data.id, bot_id, [Permissions.KICK_MEMBERS]))
throw new Error(Errors.MISSING_KICK_MEMBERS)
const result = (await Request_Manager.get(endpoints.GUILD_PRUNE(data.id), { days })) as PrunePayload
return result.pruned
@@ -210,7 +214,7 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
/** Begin pruning all members in the given time period */
prune_members: (days: number) => {
if (days < 1) throw new Error(Errors.PRUNE_MIN_DAYS)
if (!bot_has_permission(data.id, client.bot_id, [Permissions.KICK_MEMBERS]))
if (!bot_has_permission(data.id, bot_id, [Permissions.KICK_MEMBERS]))
throw new Error(Errors.MISSING_KICK_MEMBERS)
return Request_Manager.post(endpoints.GUILD_PRUNE(data.id), { days })
},
@@ -219,7 +223,7 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
// },
/** Returns the audit logs for the guild. Requires VIEW AUDIT LOGS permission */
get_audit_logs: (options: Get_Audit_Logs_Options) => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.VIEW_AUDIT_LOG]))
if (!bot_has_permission(data.id, bot_id, [Permissions.VIEW_AUDIT_LOG]))
throw new Error(Errors.MISSING_VIEW_AUDIT_LOG)
return Request_Manager.get(endpoints.GUILD_AUDIT_LOGS(data.id), {
@@ -229,13 +233,13 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
},
/** Returns the guild embed object. Requires the MANAGE_GUILD permission. */
get_embed: () => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_GUILD]))
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_GUILD]))
throw new Error(Errors.MISSING_MANAGE_GUILD)
return Request_Manager.get(endpoints.GUILD_EMBED(data.id))
},
/** Modify a guild embed object for the guild. Requires the MANAGE_GUILD permission. */
edit_embed: (enabled: boolean, channel_id?: string | null) => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_GUILD]))
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_GUILD]))
throw new Error(Errors.MISSING_MANAGE_GUILD)
return Request_Manager.patch(endpoints.GUILD_EMBED(data.id), { enabled, channel_id })
},
@@ -245,43 +249,43 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
},
/** Returns a list of integrations for the guild. Requires the MANAGE_GUILD permission. */
get_integrations: () => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_GUILD]))
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_GUILD]))
throw new Error(Errors.MISSING_MANAGE_GUILD)
return Request_Manager.get(endpoints.GUILD_INTEGRATIONS(data.id))
},
/** Modify the behavior and settings of an integration object for the guild. Requires the MANAGE_GUILD permission. */
edit_integration: (id: string, options: Edit_Integration_Options) => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_GUILD]))
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_GUILD]))
throw new Error(Errors.MISSING_MANAGE_GUILD)
return Request_Manager.patch(endpoints.GUILD_INTEGRATION(data.id, id), options)
},
/** Delete the attached integration object for the guild with this id. Requires MANAGE_GUILD permission. */
delete_integration: (id: string) => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_GUILD]))
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_GUILD]))
throw new Error(Errors.MISSING_MANAGE_GUILD)
return Request_Manager.delete(endpoints.GUILD_INTEGRATION(data.id, id))
},
/** Sync an integration. Requires teh MANAGE_GUILD permission. */
sync_integration: (id: string) => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_GUILD]))
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_GUILD]))
throw new Error(Errors.MISSING_MANAGE_GUILD)
return Request_Manager.post(endpoints.GUILD_INTEGRATION_SYNC(data.id, id))
},
/** Returns a list of ban objects for the users banned from this guild. Requires the BAN_MEMBERS permission. */
get_bans: () => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.BAN_MEMBERS]))
if (!bot_has_permission(data.id, bot_id, [Permissions.BAN_MEMBERS]))
throw new Error(Errors.MISSING_BAN_MEMBERS)
return Request_Manager.get(endpoints.GUILD_BANS(data.id))
},
/** Ban a user from the guild and optionally delete previous messages sent by the user. Requires teh BAN_MEMBERS permission. */
ban: (id: string, options: BanOptions) => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.BAN_MEMBERS]))
if (!bot_has_permission(data.id, bot_id, [Permissions.BAN_MEMBERS]))
throw new Error(Errors.MISSING_BAN_MEMBERS)
return Request_Manager.put(endpoints.GUILD_BAN(data.id, id), options)
},
/** Remove the ban for a user. REquires BAN_MEMBERS permission */
unban: (id: string) => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.BAN_MEMBERS]))
if (!bot_has_permission(data.id, bot_id, [Permissions.BAN_MEMBERS]))
throw new Error(Errors.MISSING_BAN_MEMBERS)
return Request_Manager.delete(endpoints.GUILD_BAN(data.id, id))
},
@@ -318,13 +322,13 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
},
/** Modify a guilds settings. Requires the MANAGE_GUILD permission. */
edit: (options: Guild_Edit_Options) => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_GUILD]))
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_GUILD]))
throw new Error(Errors.MISSING_MANAGE_GUILD)
return Request_Manager.patch(endpoints.GUILD(data.id), options)
},
/** Get all the invites for this guild. Requires MANAGE_GUILD permission */
get_invites: () => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_GUILD]))
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_GUILD]))
throw new Error(Errors.MISSING_MANAGE_GUILD)
return Request_Manager.get(endpoints.GUILD_INVITES(data.id))
},
@@ -338,7 +342,7 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
},
/** Returns a list of guild webhooks objects. Requires the MANAGE_WEBHOOKs permission. */
get_webhooks: () => {
if (!bot_has_permission(data.id, client.bot_id, [Permissions.MANAGE_WEBHOOKS]))
if (!bot_has_permission(data.id, bot_id, [Permissions.MANAGE_WEBHOOKS]))
throw new Error(Errors.MISSING_MANAGE_WEBHOOKS)
return Request_Manager.get(endpoints.GUILD_WEBHOOKS(data.id))

View File

@@ -1,4 +1,4 @@
import Client from "../module/client.ts"
import { bot_id } from "../module/client.ts"
import { endpoints } from "../constants/discord.ts"
import { format_image_url } from "../utils/cdn.ts"
import { Member_Create_Payload, Edit_Member_Options } from "../types/member.ts"
@@ -13,8 +13,7 @@ export const create_member = (
data: Member_Create_Payload,
guild_id: string,
role_data: Role_Data[],
owner_id: string,
client: Client
owner_id: string
) => ({
/** The complete raw data from the member create payload */
raw: () => data,
@@ -50,21 +49,21 @@ 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]))
if (!bot_has_permission(guild_id, bot_id, [Permissions.MANAGE_ROLES]))
throw new Error(Errors.MISSING_MANAGE_ROLES)
return Request_Manager.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]))
if (!bot_has_permission(guild_id, bot_id, [Permissions.MANAGE_ROLES]))
throw new Error(Errors.MISSING_MANAGE_ROLES)
return Request_Manager.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]))
if (!bot_has_permission(guild_id, bot_id, [Permissions.KICK_MEMBERS]))
throw new Error(Errors.MISSING_KICK_MEMBERS)
return Request_Manager.delete(endpoints.GUILD_MEMBER(guild_id, data.user.id), { reason })
},
@@ -72,20 +71,20 @@ export const create_member = (
edit: (options: Edit_Member_Options) => {
if (options.nick) {
if (options.nick.length > 32) throw new Error(Errors.NICKNAMES_MAX_LENGTH)
if (!bot_has_permission(guild_id, client.bot_id, [Permissions.MANAGE_NICKNAMES]))
if (!bot_has_permission(guild_id, 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]))
if (options.roles && !bot_has_permission(guild_id, bot_id, [Permissions.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]))
if (!bot_has_permission(guild_id, 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]))
if (options.deaf && !bot_has_permission(guild_id, bot_id, [Permissions.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

View File

@@ -1,4 +1,3 @@
import Client from "../module/client.ts"
import { Message_Create_Options } from "../types/message.ts"
import { endpoints } from "../constants/discord.ts"
import { MessageContent } from "../types/channel.ts"
@@ -10,8 +9,9 @@ import { bot_has_permission } from "../utils/permissions.ts"
import { Errors } from "../types/errors.ts"
import { Permissions } from "../types/permission.ts"
import { Request_Manager } from "../module/request_manager.ts"
import { bot_id } from "../module/client.ts"
export const create_message = (data: Message_Create_Options, client: Client) => ({
export const create_message = (data: Message_Create_Options) => ({
raw: () => data,
author: () => create_user({ ...data.author, avatar: data.author.avatar || "" }),
id: () => data.id,
@@ -43,21 +43,21 @@ export const create_message = (data: Message_Create_Options, client: Client) =>
/** Delete a message */
delete: (reason?: string) => {
if (data.guild_id && !bot_has_permission(data.guild_id, client.bot_id, [Permissions.MANAGE_MESSAGES]))
if (data.guild_id && !bot_has_permission(data.guild_id, bot_id, [Permissions.MANAGE_MESSAGES]))
throw new Error(Errors.MISSING_MANAGE_MESSAGES)
if (data.author.id !== client.bot_id) {
if (data.author.id !== bot_id) {
}
Request_Manager.delete(endpoints.CHANNEL_MESSAGE(data.channel_id, data.id), { reason })
},
/** Pin a message in a channel. Requires MANAGE_MESSAGES. Max pins allowed in a channel = 50. */
pin: () => {
if (data.guild_id && !bot_has_permission(data.guild_id, client.bot_id, [Permissions.MANAGE_MESSAGES]))
if (data.guild_id && !bot_has_permission(data.guild_id, bot_id, [Permissions.MANAGE_MESSAGES]))
throw new Error(Errors.MISSING_MANAGE_MESSAGES)
Request_Manager.put(endpoints.CHANNEL_MESSAGE(data.channel_id, data.id))
},
unpin: () => {
if (data.guild_id && !bot_has_permission(data.guild_id, client.bot_id, [Permissions.MANAGE_MESSAGES]))
if (data.guild_id && !bot_has_permission(data.guild_id, bot_id, [Permissions.MANAGE_MESSAGES]))
throw new Error(Errors.MISSING_MANAGE_MESSAGES)
Request_Manager.delete(endpoints.CHANNEL_MESSAGE(data.channel_id, data.id))
},
@@ -71,13 +71,13 @@ export const create_message = (data: Message_Create_Options, client: Client) =>
},
/** Removes all reactions for all emojis on this message. */
remove_all_reactions: () => {
if (data.guild_id && !bot_has_permission(data.guild_id, client.bot_id, [Permissions.MANAGE_MESSAGES]))
if (data.guild_id && !bot_has_permission(data.guild_id, bot_id, [Permissions.MANAGE_MESSAGES]))
throw new Error(Errors.MISSING_MANAGE_MESSAGES)
Request_Manager.delete(endpoints.CHANNEL_MESSAGE_REACTIONS(data.channel_id, data.id))
},
/** Removes all reactions for a single emoji on this message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
remove_reaction_emoji: (reaction: string) => {
if (data.guild_id && !bot_has_permission(data.guild_id, client.bot_id, [Permissions.MANAGE_MESSAGES]))
if (data.guild_id && !bot_has_permission(data.guild_id, bot_id, [Permissions.MANAGE_MESSAGES]))
throw new Error(Errors.MISSING_MANAGE_MESSAGES)
Request_Manager.delete(endpoints.CHANNEL_MESSAGE_REACTION(data.channel_id, data.id, reaction))
},
@@ -90,22 +90,22 @@ export const create_message = (data: Message_Create_Options, client: Client) =>
},
/** Edit the message. */
edit: async (content: string | MessageContent) => {
if (data.author.id !== client.bot_id) throw "You can only edit a message that was sent by the bot."
if (data.author.id !== bot_id) throw "You can only edit a message that was sent by the bot."
if (typeof content === "string") content = { content }
if (data.guild_id) {
if (!bot_has_permission(data.guild_id, client.bot_id, [Permissions.SEND_MESSAGES]))
if (!bot_has_permission(data.guild_id, bot_id, [Permissions.SEND_MESSAGES]))
throw new Error(Errors.MISSING_SEND_MESSAGES)
if (content.tts && !bot_has_permission(data.guild_id, client.bot_id, [Permissions.SEND_TTS_MESSAGES]))
if (content.tts && !bot_has_permission(data.guild_id, bot_id, [Permissions.SEND_TTS_MESSAGES]))
throw new Error(Errors.MISSING_SEND_TTS_MESSAGE)
}
if (content.content && content.content.length > 2000) throw new Error(Errors.MESSAGE_MAX_LENGTH)
const result = await Request_Manager.patch(endpoints.CHANNEL_MESSAGE(data.channel_id, data.id), content)
return create_message(result, client)
return create_message(result)
}
})