ton of fixes

This commit is contained in:
Skillz
2020-02-25 19:25:15 -05:00
parent 74a390041f
commit 4d1e7818b0
10 changed files with 140 additions and 209 deletions
+2 -9
View File
@@ -1,11 +1,4 @@
{ {
"singleQuote": true, "semi": false,
"tabWidth": 2, "printWidth": 120
"semi": false,
"printWidth": 120,
"trailingComma": "none",
"bracketSpacing": true,
"arrowParens": "avoid",
"endOfLine": "auto"
} }
-10
View File
@@ -1,10 +0,0 @@
{
"deno.enable": true,
"editor.formatOnSave": true,
"[typescript]": {
"editor.defaultFormatter": "axetroy.vscode-deno"
},
"[json]": {
"editor.defaultFormatter": "axetroy.vscode-deno"
}
}
+10
View File
@@ -0,0 +1,10 @@
{
"deno.enable": true,
"editor.formatOnSave": true,
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
+24 -14
View File
@@ -7,13 +7,13 @@ import {
Get_Messages_Before, Get_Messages_Before,
MessageContent, MessageContent,
Create_Invite_Options Create_Invite_Options
} from '../types/channel' } from '../types/channel.ts'
import Client from '../module/client' import Client from '../module/client.ts'
import { endpoints } from '../constants/discord.ts' import { endpoints } from '../constants/discord.ts'
import { create_message, Message } from './message' import { create_message, Message } from './message.ts'
import { Message_Create_Options } from '../types/message' import { Message_Create_Options } from '../types/message.ts'
import { Permission, Permissions } from '../types/permission' import { Permission, Permissions } from '../types/permission.ts'
import { Guild } from '../types/return-type' import { Guild } from '../types/return-type.ts'
export const create_channel = (data: Channel_Create_Options, guild: Guild, client: Client) => { export const create_channel = (data: Channel_Create_Options, guild: Guild, client: Client) => {
const base_channel = { const base_channel = {
@@ -49,7 +49,9 @@ export const create_channel = (data: Channel_Create_Options, guild: Guild, clien
}, },
/** Get pinned messages in this channel. */ /** Get pinned messages in this channel. */
get_pins: async () => { get_pins: async () => {
const result = (await client.discordRequestManager.get(endpoints.CHANNEL_PINS(data.id))) as Message_Create_Options[] const result = (await client.discordRequestManager.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, client))
}, },
/** Send a message to the channel. Requires SEND_MESSAGES permission. */ /** Send a message to the channel. Requires SEND_MESSAGES permission. */
@@ -71,11 +73,12 @@ export const create_channel = (data: Channel_Create_Options, guild: Guild, clien
} }
// If it is a dm channel // If it is a dm channel
if (data.type === Channel_Types.DM) if (data.type === Channel_Types.DM) {
return { return {
...base_channel, ...base_channel,
...base_text_channel ...base_text_channel
} }
}
// GUILD CHANNEL ONLY // GUILD CHANNEL ONLY
const base_guild_channel = { const base_guild_channel = {
@@ -94,8 +97,9 @@ export const create_channel = (data: Channel_Create_Options, guild: Guild, clien
if (id === guild.owner_id()) return true if (id === guild.owner_id()) return true
const member = guild.members.get(id) const member = guild.members.get(id)
if (!member) if (!member) {
throw 'Invalid member id provided. This member was not found in the cache. Please fetch them with getMember on guild.' throw 'Invalid member id provided. This member was not found in the cache. Please fetch them with getMember on guild.'
}
let permissionBits = member.roles().reduce((bits, role_id) => { let permissionBits = member.roles().reduce((bits, role_id) => {
const role = guild.roles.get(role_id) const role = guild.roles.get(role_id)
@@ -117,7 +121,7 @@ export const create_channel = (data: Channel_Create_Options, guild: Guild, clien
} }
// Guild Text Channel // Guild Text Channel
if ([Channel_Types.GUILD_TEXT, Channel_Types.GUILD_NEWS].includes(data.type)) if ([Channel_Types.GUILD_TEXT, Channel_Types.GUILD_NEWS].includes(data.type)) {
return { return {
...base_guild_channel, ...base_guild_channel,
...base_text_channel, ...base_text_channel,
@@ -128,11 +132,14 @@ export const create_channel = (data: Channel_Create_Options, guild: Guild, clien
/** Delete messages from the channel. 2-100. Requires the MANAGE_MESSAGES permission */ /** Delete messages from the channel. 2-100. Requires the MANAGE_MESSAGES permission */
delete_messages: (ids: string[], reason?: string) => { delete_messages: (ids: string[], reason?: string) => {
// TODO: Requires the MANAGE_MESSAGES permission. // TODO: Requires the MANAGE_MESSAGES permission.
if (ids.length < 2) throw 'This endpoint will only accept 2-100 message ids.' if (ids.length < 2) {
if (ids.length > 100) throw 'This endpoint will only accept 2-100 message ids.'
}
if (ids.length > 100) {
console.warn( console.warn(
`This endpoint only accepts a maximum of 100 messages. Deleting the first 100 message ids provided.` `This endpoint only accepts a maximum of 100 messages. Deleting the first 100 message ids provided.`
) )
}
return client.discordRequestManager.POST(endpoints.CHANNEL_BULK_DELETE(data.id), { return client.discordRequestManager.POST(endpoints.CHANNEL_BULK_DELETE(data.id), {
messages: ids.splice(0, 100), messages: ids.splice(0, 100),
reason reason
@@ -154,16 +161,18 @@ export const create_channel = (data: Channel_Create_Options, guild: Guild, clien
return client.discordRequestManager.get(endpoints.CHANNEL_WEBHOOKS(data.id)) return client.discordRequestManager.get(endpoints.CHANNEL_WEBHOOKS(data.id))
} }
} }
}
if (data.type === Channel_Types.GUILD_CATEGORY) if (data.type === Channel_Types.GUILD_CATEGORY) {
return { return {
...base_guild_channel, ...base_guild_channel,
/** Gets an array of all the channels ids that are the children of this category. */ /** Gets an array of all the channels ids that are the children of this category. */
children_ids: () => children_ids: () =>
Object.keys(guild.channels).filter(channel => guild.channels.get(channel).parent_id === data.id) Object.keys(guild.channels).filter(channel => guild.channels.get(channel).parent_id === data.id)
} }
}
if (data.type === Channel_Types.GUILD_VOICE) if (data.type === Channel_Types.GUILD_VOICE) {
return { return {
...base_guild_channel, ...base_guild_channel,
// TODO: after learning opus and stuff // TODO: after learning opus and stuff
@@ -172,6 +181,7 @@ export const create_channel = (data: Channel_Create_Options, guild: Guild, clien
/** Leave a voice channel */ /** Leave a voice channel */
leave: () => {} leave: () => {}
} }
}
return { return {
...data, ...data,
+83 -156
View File
@@ -1,6 +1,6 @@
import Client from "../module/client.ts"; import Client from '../module/client.ts'
import { endpoints } from "../constants/discord.ts"; import { endpoints } from '../constants/discord.ts'
import { format_image_url } from "../utils/cdn.ts"; import { format_image_url } from '../utils/cdn.ts'
import { import {
Create_Guild_Payload, Create_Guild_Payload,
ChannelTypes, ChannelTypes,
@@ -13,14 +13,13 @@ import {
Create_Emojis_Options, Create_Emojis_Options,
Edit_Emojis_Options, Edit_Emojis_Options,
Create_Role_Options Create_Role_Options
} from "../types/guild"; } from '../types/guild.ts'
import { create_role, Role } from "./role.ts"; import { create_role } from './role.ts'
import { create_member } from "./member.ts"; import { create_member } from './member.ts'
import { create_channel } from "./channel.ts"; import { create_channel } from './channel.ts'
import { Channel_Create_Options, Channel } from "../types/channel.ts"; import { Channel_Create_Options } from '../types/channel.ts'
import { Image_Size, Image_Formats } from "../types/cdn.ts"; import { Image_Size, Image_Formats } from '../types/cdn.ts'
import { Permissions, Permission } from "../types/permission.ts"; import { Permissions, Permission } from '../types/permission.ts'
import { Member } from "../types/member.ts";
export const create_guild = (data: Create_Guild_Payload, client: Client) => { export const create_guild = (data: Create_Guild_Payload, client: Client) => {
const guild = { const guild = {
@@ -45,7 +44,7 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
/** The verification level required for the guild */ /** The verification level required for the guild */
verification_level: () => data.verification_level, verification_level: () => data.verification_level,
/** The roles in the guild */ /** The roles in the guild */
roles: new Map<string, Role>(), roles: () => new Map(data.roles.map(r => [r.id, create_role(r)])),
/** The custom guild emojis */ /** The custom guild emojis */
emojis: () => data.emojis, emojis: () => data.emojis,
/** The enabled guild features. */ /** The enabled guild features. */
@@ -65,9 +64,9 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
/** The current open voice states in the guild. */ /** The current open voice states in the guild. */
voice_states: () => data.voice_states, voice_states: () => data.voice_states,
/** The users in this guild. */ /** The users in this guild. */
members: new Map<string, Member>(), members: new Map(data.members.map(m => [m.user.id, create_member(m, data.id, data.roles, data.owner_id, client)])),
/** The channels in the guild */ /** The channels in the guild */
channels: new Map<string, Channel>(), channels: new Map(data.channels.map(c => [c.id, create_channel(c, client)])),
/** The presences of all the users in the guild. */ /** The presences of all the users in the guild. */
presences: new Map(data.presences.map(p => [p.user.id, p])), 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.) */ /** The maximum amount of presences for the guild(the default value, currently 5000 is in effect when null is returned.) */
@@ -87,29 +86,14 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
/** The preferred locale of this guild only set if the guild has the DISCOVERABLE feature, defaults to en-US */ /** The preferred locale of this guild only set if the guild has the DISCOVERABLE feature, defaults to en-US */
preferred_locale: () => data.preferred_locale, preferred_locale: () => data.preferred_locale,
/** The full URL of the icon from Discords CDN. Undefined when no icon is set. */ /** The full URL of the icon from Discords CDN. Undefined when no icon is set. */
icon_url: (size: Image_Size = 128, format?: Image_Formats) => data.icon icon_url: (size: Image_Size = 128, format?: Image_Formats) =>
? format_image_url( data.icon ? format_image_url(endpoints.GUILD_ICON(data.id, data.icon), size, format) : undefined,
endpoints.GUILD_ICON(data.id, data.icon),
size,
format
)
: undefined,
/** The full URL of the splash from Discords CDN. Undefined if no splash is set. */ /** The full URL of the splash from Discords CDN. Undefined if no splash is set. */
splash_url: (size: Image_Size = 128, format?: Image_Formats) => data.splash splash_url: (size: Image_Size = 128, format?: Image_Formats) =>
? format_image_url( data.splash ? format_image_url(endpoints.GUILD_SPLASH(data.id, data.splash), size, format) : undefined,
endpoints.GUILD_SPLASH(data.id, data.splash),
size,
format
)
: undefined,
/** The full URL of the banner from Discords CDN. Undefined if no banner is set. */ /** The full URL of the banner from Discords CDN. Undefined if no banner is set. */
banner_url: (size: Image_Size = 128, format?: Image_Formats) => data.banner banner_url: (size: Image_Size = 128, format?: Image_Formats) =>
? format_image_url( data.banner ? format_image_url(endpoints.GUILD_BANNER(data.id, data.banner), size, format) : undefined,
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 a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */
create_channel: (name: string, options: Channel_Create_Options) => { create_channel: (name: string, options: Channel_Create_Options) => {
// TODO: Check if the bot has `MANAGE_CHANNELS` permission before making a channel // TODO: Check if the bot has `MANAGE_CHANNELS` permission before making a channel
@@ -118,52 +102,43 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
type: options?.type ? ChannelTypes[options.type] : undefined, type: options?.type ? ChannelTypes[options.type] : undefined,
permission_overwrites: options?.permission_overwrites permission_overwrites: options?.permission_overwrites
? options.permission_overwrites.map(perm => ({ ? options.permission_overwrites.map(perm => ({
...perm, ...perm,
allow: perm.allow.map(p => Permissions[p]), allow: perm.allow.map(p => Permissions[p]),
deny: perm.deny.map(p => Permissions[p]) deny: perm.deny.map(p => Permissions[p])
})) }))
: undefined, : undefined,
...options ...options
}); })
}, },
/** Returns a list of guild channel objects. /** Returns a list of guild channel objects.
* *
* ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your channels will be cached in your guild.** * ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your channels will be cached in your guild.**
*/ */
get_channels: () => { get_channels: () => {
return client.discordRequestManager.get(endpoints.GUILD_CHANNELS(data.id)); return client.discordRequestManager.get(endpoints.GUILD_CHANNELS(data.id))
}, },
/** Modify the positions of channels on the guild. Requires MANAGE_CHANNELS permisison. */ /** Modify the positions of channels on the guild. Requires MANAGE_CHANNELS permisison. */
swap_channels: (channel_positions: Position_Swap[]) => { swap_channels: (channel_positions: Position_Swap[]) => {
if (channel_positions.length < if (channel_positions.length < 2) {
2) throw 'You must provide atleast two channels to be swapped.'
{
throw "You must provide atleast two channels to be swapped.";
} }
return client.discordRequestManager.patch( return client.discordRequestManager.patch(endpoints.GUILD_CHANNELS(data.id), channel_positions)
endpoints.GUILD_CHANNELS(data.id),
channel_positions
);
}, },
/** Returns a guild member object for the specified user. /** Returns a guild member object for the specified user.
* *
* ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your members will be cached in your guild.** * ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your members will be cached in your guild.**
*/ */
get_member: (id: string) => { get_member: (id: string) => {
return client.discordRequestManager.get(endpoints.GUILD_MEMBER(data.id, id)); return client.discordRequestManager.get(endpoints.GUILD_MEMBER(data.id, id))
}, },
/** 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 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: ( create_emoji: (name: string, image: string, options: Create_Emojis_Options) => {
name: string,
image: string,
options: Create_Emojis_Options
) => {
// TODO: Check if the bot has `MANAGE_EMOJIS` permission // TODO: Check if the bot has `MANAGE_EMOJIS` permission
return client.discordRequestManager.post(endpoints.GUILD_EMOJIS(data.id), { return client.discordRequestManager.post(endpoints.GUILD_EMOJIS(data.id), {
...options, ...options,
name, name,
image image
}); })
}, },
/** Modify the given emoji. Requires the MANAGE_EMOJIS permission. */ /** Modify the given emoji. Requires the MANAGE_EMOJIS permission. */
edit_emoji: (id: string, options: Edit_Emojis_Options) => { edit_emoji: (id: string, options: Edit_Emojis_Options) => {
@@ -171,42 +146,33 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
return client.discordRequestManager.patch(endpoints.GUILD_EMOJI(data.id, id), { return client.discordRequestManager.patch(endpoints.GUILD_EMOJI(data.id, id), {
name: options.name, name: options.name,
roles: options.roles roles: options.roles
}); })
}, },
/** Delete the given emoji. Requires the MANAGE_EMOJIS permission. Returns 204 No Content on success. */ /** Delete the given emoji. Requires the MANAGE_EMOJIS permission. Returns 204 No Content on success. */
delete_emoji: (id: string, reason?: string) => { delete_emoji: (id: string, reason?: string) => {
// TODO: check if the bot has `MANAGE_EMOJIS` permission // TODO: check if the bot has `MANAGE_EMOJIS` permission
return client.discordRequestManager.delete( return client.discordRequestManager.delete(endpoints.GUILD_EMOJI(data.id, id), { reason })
endpoints.GUILD_EMOJI(data.id, id),
{ reason }
);
}, },
/** Create a new role for the guild. Requires the MANAGE_ROLES permission. */ /** Create a new role for the guild. Requires the MANAGE_ROLES permission. */
create_role: async (options: Create_Role_Options) => { create_role: async (options: Create_Role_Options) => {
// TODO: check if the bot has the `MANAGE_ROLES` permission. // TODO: check if the bot has the `MANAGE_ROLES` permission.
const role = await client.discordRequestManager.post( const role = await client.discordRequestManager.post(endpoints.GUILD_ROLES(data.id), {
endpoints.GUILD_ROLES(data.id), ...options,
{ permissions: options.permissions?.map(perm => Permissions[perm])
...options, })
permissions: options.permissions?.map(perm => Permissions[perm])
}
);
// TODO: cache this role // TODO: cache this role
return role; return role
}, },
/** Edit a guild role. Requires the MANAGE_ROLES permission. */ /** Edit a guild role. Requires the MANAGE_ROLES permission. */
edit_role: (id: string, options: Create_Role_Options) => { edit_role: (id: string, options: Create_Role_Options) => {
// TODO: check if the bot has the `MANAGE_ROLES` permission. // TODO: check if the bot has the `MANAGE_ROLES` permission.
return client.discordRequestManager.patch( return client.discordRequestManager.patch(endpoints.GUILD_ROLE(data.id, id), options)
endpoints.GUILD_ROLE(data.id, id),
options
);
}, },
/** Delete a guild role. Requires the MANAGE_ROLES permission. */ /** Delete a guild role. Requires the MANAGE_ROLES permission. */
delete_role: (id: string) => { delete_role: (id: string) => {
// TODO: check if the bot has the `MANAGE_ROLES` permission. // TODO: check if the bot has the `MANAGE_ROLES` permission.
return client.discordRequestManager.delete(endpoints.GUILD_ROLE(data.id, id)); return client.discordRequestManager.delete(endpoints.GUILD_ROLE(data.id, id))
}, },
/** Returns a list of role objects for the guild. /** Returns a list of role objects for the guild.
* *
@@ -214,38 +180,27 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
*/ */
get_roles: () => { get_roles: () => {
// TODO: check if the bot has the `MANAGE_ROLES` permission. // TODO: check if the bot has the `MANAGE_ROLES` permission.
return client.discordRequestManager.get(endpoints.GUILD_ROLES(data.id)); return client.discordRequestManager.get(endpoints.GUILD_ROLES(data.id))
}, },
/** 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. */
swap_roles: (rolePositons: Position_Swap) => { swap_roles: (rolePositons: Position_Swap) => {
// TODO: check if the bot has the `MANAGE_ROLES` permission. // TODO: check if the bot has the `MANAGE_ROLES` permission.
return client.discordRequestManager.patch( return client.discordRequestManager.patch(endpoints.GUILD_ROLES(data.id), rolePositons)
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 */ /** 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) => { get_prune_count: async (days: number) => {
if (days < if (days < 1) {
1) throw `The number of days to count prune for must be 1 or more.`
{
throw `The number of days to count prune for must be 1 or more.`;
} }
// TODO: check if the bot has `KICK_MEMBERS` permission // TODO: check if the bot has `KICK_MEMBERS` permission
const result = (await client.discordRequestManager.get( const result = (await client.discordRequestManager.get(endpoints.GUILD_PRUNE(data.id), { days })) as PrunePayload
endpoints.GUILD_PRUNE(data.id), return result.pruned
{ days }
)) as PrunePayload;
return result.pruned;
}, },
/** Begin pruning all members in the given time period */ /** Begin pruning all members in the given time period */
prune_members: (days: number) => { prune_members: (days: number) => {
if (days < 1) throw `The number of days must be 1 or more.`; if (days < 1) throw `The number of days must be 1 or more.`
// TODO: check if the bot has `KICK_MEMBERS` permission. // TODO: check if the bot has `KICK_MEMBERS` permission.
return client.discordRequestManager.post( return client.discordRequestManager.post(endpoints.GUILD_PRUNE(data.id), { days })
endpoints.GUILD_PRUNE(data.id),
{ days }
);
}, },
// TODO: REQUEST THIS OVER WEBSOCKET WITH GET_GUILD_MEMBERS ENDPOINT // TODO: REQUEST THIS OVER WEBSOCKET WITH GET_GUILD_MEMBERS ENDPOINT
// fetch_all_members: () => { // fetch_all_members: () => {
@@ -255,142 +210,114 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
// TODO: check if the bot has VIEW_AUDIT_LOGS permission // TODO: check if the bot has VIEW_AUDIT_LOGS permission
return client.discordRequestManager.get(endpoints.GUILD_AUDIT_LOGS(data.id), { return client.discordRequestManager.get(endpoints.GUILD_AUDIT_LOGS(data.id), {
...options, ...options,
limit: options.limit && options.limit >= 1 && options.limit <= 100 limit: options.limit && options.limit >= 1 && options.limit <= 100 ? options.limit : 50
? options.limit })
: 50
});
}, },
/** Returns the guild embed object. Requires the MANAGE_GUILD permission. */ /** Returns the guild embed object. Requires the MANAGE_GUILD permission. */
get_embed: () => { get_embed: () => {
// TODO: check if the bot has the MANAGE_GUILD permission // TODO: check if the bot has the MANAGE_GUILD permission
return client.discordRequestManager.get(endpoints.GUILD_EMBED(data.id)); return client.discordRequestManager.get(endpoints.GUILD_EMBED(data.id))
}, },
/** Modify a guild embed object for the guild. Requires the MANAGE_GUILD permission. */ /** Modify a guild embed object for the guild. Requires the MANAGE_GUILD permission. */
edit_embed: (enabled: boolean, channel_id?: string | null) => { edit_embed: (enabled: boolean, channel_id?: string | null) => {
// TODO: Requires the MANAGE_GUILD permission. // TODO: Requires the MANAGE_GUILD permission.
return client.discordRequestManager.patch( return client.discordRequestManager.patch(endpoints.GUILD_EMBED(data.id), { enabled, channel_id })
endpoints.GUILD_EMBED(data.id),
{ enabled, channel_id }
);
}, },
/** Returns the code and uses of the vanity url for this server if it is enabled. Requires the MANAGE_GUILD permission. */ /** Returns the code and uses of the vanity url for this server if it is enabled. Requires the MANAGE_GUILD permission. */
get_vanity_url: () => { get_vanity_url: () => {
return client.discordRequestManager.get(endpoints.GUILD_VANITY_URL(data.id)); return client.discordRequestManager.get(endpoints.GUILD_VANITY_URL(data.id))
}, },
/** 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. */
get_integrations: () => { get_integrations: () => {
// TODO: requires the MANAGE_GUILD permission // TODO: requires the MANAGE_GUILD permission
return client.discordRequestManager.get(endpoints.GUILD_INTEGRATIONS(data.id)); return client.discordRequestManager.get(endpoints.GUILD_INTEGRATIONS(data.id))
}, },
/** Modify the behavior and settings of an integration object for the guild. Requires the MANAGE_GUILD permission. */ /** 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) => { edit_integration: (id: string, options: Edit_Integration_Options) => {
// TODO: requires the MANAGE_GUILD permission // TODO: requires the MANAGE_GUILD permission
return client.discordRequestManager.patch( return client.discordRequestManager.patch(endpoints.GUILD_INTEGRATION(data.id, id), options)
endpoints.GUILD_INTEGRATION(data.id, id),
options
);
}, },
/** 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. */
delete_integration: (id: string) => { delete_integration: (id: string) => {
// TODO: requires the MANAGE_GUILD permission // TODO: requires the MANAGE_GUILD permission
return client.discordRequestManager.delete( return client.discordRequestManager.delete(endpoints.GUILD_INTEGRATION(data.id, id))
endpoints.GUILD_INTEGRATION(data.id, id)
);
}, },
/** Sync an integration. Requires teh MANAGE_GUILD permission. */ /** Sync an integration. Requires teh MANAGE_GUILD permission. */
sync_integration: (id: string) => { sync_integration: (id: string) => {
// TODO: requires MANAGE_GUILD // TODO: requires MANAGE_GUILD
return client.discordRequestManager.post( return client.discordRequestManager.post(endpoints.GUILD_INTEGRATION_SYNC(data.id, id))
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. */ /** Returns a list of ban objects for the users banned from this guild. Requires the BAN_MEMBERS permission. */
get_bans: () => { get_bans: () => {
// TODO: requires the BAN_MEMBERS permission // TODO: requires the BAN_MEMBERS permission
return client.discordRequestManager.get(endpoints.GUILD_BANS(data.id)); return client.discordRequestManager.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 a user from the guild and optionally delete previous messages sent by the user. Requires teh BAN_MEMBERS permission. */
ban: (id: string, options: BanOptions) => { ban: (id: string, options: BanOptions) => {
// TODO: requires the BAN_MEMBERS permission // TODO: requires the BAN_MEMBERS permission
return client.discordRequestManager.put( return client.discordRequestManager.put(endpoints.GUILD_BAN(data.id, id), options)
endpoints.GUILD_BAN(data.id, id),
options
);
}, },
/** Remove the ban for a user. REquires BAN_MEMBERS permission */ /** Remove the ban for a user. REquires BAN_MEMBERS permission */
unban: (id: string) => { unban: (id: string) => {
// TODO: requires the BAN_MEMBERS permission // TODO: requires the BAN_MEMBERS permission
return client.discordRequestManager.delete(endpoints.GUILD_BAN(data.id, id)); return client.discordRequestManager.delete(endpoints.GUILD_BAN(data.id, id))
}, },
/** Check whether a member has certain permissions in this channel. */ /** Check whether a member has certain permissions in this channel. */
channel_has_permissions: ( channel_has_permissions: (channel_id: string, member_id: string, permissions: Permission[]) => {
channel_id: string, if (member_id === data.owner_id) return true
member_id: string,
permissions: Permission[]
) => {
if (member_id === data.owner_id) return true;
const member = guild.members.get(member_id); const member = guild.members.get(member_id)
if (!member) { if (!member) {
throw "Invalid member id provided. This member was not found in the cache. Please fetch them with getMember on guild."; throw 'Invalid member id provided. This member was not found in the cache. Please fetch them with getMember on guild.'
} }
const channel = guild.channels.get(channel_id); const channel = guild.channels.get(channel_id)
if (!channel) { if (!channel) {
throw "Invalid channel id provided. This channel was not found in the cache."; throw 'Invalid channel id provided. This channel was not found in the cache.'
} }
let permissionBits = member.roles().reduce((bits, role_id) => { let permissionBits = member.roles().reduce((bits, role_id) => {
const role = guild.roles.get(role_id); const role = guild.roles.get(role_id)
if (!role) return bits; if (!role) return bits
bits |= role.permissions(); bits |= role.permissions()
return bits;
}, 0);
return bits
}, 0)
data.permission_overwrites?.forEach(overwrite => { data.permission_overwrites?.forEach(overwrite => {
permissionBits = (permissionBits & ~overwrite.deny) | overwrite.allow; permissionBits = (permissionBits & ~overwrite.deny) | overwrite.allow
}); })
if (permissionBits & Permissions.ADMINISTRATOR) return true if (permissionBits & Permissions.ADMINISTRATOR) return true
return permissions.every( return permissions.every(permission => permissionBits & Permissions[permission])
permission => permissionBits & Permissions[permission]
);
}, },
/** Modify a guilds settings. Requires the MANAGE_GUILD permission. */ /** Modify a guilds settings. Requires the MANAGE_GUILD permission. */
edit: (options: Guild_Edit_Options) => { edit: (options: Guild_Edit_Options) => {
// TODO: requires the MANAGE_GUILD permission // TODO: requires the MANAGE_GUILD permission
return client.discordRequestManager.patch(endpoints.GUILD(data.id), options); return client.discordRequestManager.patch(endpoints.GUILD(data.id), options)
}, },
/** Get all the invites for this guild. Requires MANAGE_GUILD permission */ /** Get all the invites for this guild. Requires MANAGE_GUILD permission */
get_invites: () => { get_invites: () => {
// TODO: requires MANAGE_GUILD permission // TODO: requires MANAGE_GUILD permission
return client.discordRequestManager.get(endpoints.GUILD_INVITES(data.id)); return client.discordRequestManager.get(endpoints.GUILD_INVITES(data.id))
}, },
/** Leave a guild */ /** Leave a guild */
leave: () => { leave: () => {
return client.discordRequestManager.delete(endpoints.GUILD_LEAVE(data.id)); return client.discordRequestManager.delete(endpoints.GUILD_LEAVE(data.id))
}, },
/** Returns a list of voice region objects for the guild. Unlike the similar /voice route, this returns VIP servers when the guild is VIP-enabled. */ /** Returns a list of voice region objects for the guild. Unlike the similar /voice route, this returns VIP servers when the guild is VIP-enabled. */
get_voice_regions: () => { get_voice_regions: () => {
return client.discordRequestManager.get(endpoints.GUILD_REGIONS(data.id)); return client.discordRequestManager.get(endpoints.GUILD_REGIONS(data.id))
}, },
/** Returns a list of guild webhooks objects. Requires the MANAGE_WEBHOOKs permission. */ /** Returns a list of guild webhooks objects. Requires the MANAGE_WEBHOOKs permission. */
get_webhooks: () => { get_webhooks: () => {
// TODO: requires MANAGE_WEBHOOKS // TODO: requires MANAGE_WEBHOOKS
return client.discordRequestManager.get(endpoints.GUILD_WEBHOOKS(data.id)); return client.discordRequestManager.get(endpoints.GUILD_WEBHOOKS(data.id))
} }
}; }
data.roles.forEach(r => guild.roles.set(r.id, create_role(r)));
data.members.forEach( return guild
m => guild.members.set(m.user.id, create_member(m, data.id, data.roles, data.owner_id, client)) }
);
data.channels.forEach(
c => guild.channels.set(c.id, create_channel(c, client))
);
return guild;
};
+4 -4
View File
@@ -1,10 +1,10 @@
import { UserPayload } from "./user.ts"; import { User_Payload } from "./user.ts";
import { ActivityPayload } from "./activity"; import { ActivityPayload } from "./activity.ts";
import { StatusType } from "../types/discord"; import { StatusType } from "../types/discord.ts";
export type PresencePayload = Partial<{ export type PresencePayload = Partial<{
/** The user presence is being updated for */ /** The user presence is being updated for */
user: UserPayload; user: User_Payload;
/** Roles this user is in */ /** Roles this user is in */
roles: string[]; roles: string[];
+3 -4
View File
@@ -1,5 +1,5 @@
import { Raw_Overwrite, Overwrite } from './guild' import { Raw_Overwrite, Overwrite } from './guild.ts'
import { create_channel } from '../structures/channel' import { Embed } from './message.ts'
export interface Base_Channel_Create { export interface Base_Channel_Create {
/** The id of this channel */ /** The id of this channel */
@@ -69,7 +69,7 @@ export interface MessageContent {
/** The contents of the file being sent */ /** The contents of the file being sent */
file?: File_Content file?: File_Content
/** Embed object */ /** Embed object */
embed?: Embed_Object embed?: Embed
/** JSON encoded body of any additional request fields. */ /** JSON encoded body of any additional request fields. */
payload_json?: string payload_json?: string
} }
@@ -105,4 +105,3 @@ export interface Create_Invite_Options {
unique: boolean unique: boolean
} }
export type Channel = ReturnType<typeof create_channel>
+8 -8
View File
@@ -1,11 +1,11 @@
import { Emoji, StatusType } from './discord' import { Emoji, StatusType } from './discord.ts'
import { User } from '../structures/user' import { User } from '../structures/user.ts'
import { Permission } from './permission' import { Permission } from './permission.ts'
import { Role_Data } from './role' import { Role_Data } from './role.ts'
import { Member_Create_Payload } from './member' import { Member_Create_Payload } from './member.ts'
import { Activity } from './message' import { Activity } from './message.ts'
import { ClientStatusPayload } from '../structures/presence' import { ClientStatusPayload } from '../structures/presence.ts'
import { Channel_Create_Options } from './channel' import { Channel_Create_Options } from './channel.ts'
export interface Create_Guild_Payload { export interface Create_Guild_Payload {
/** The guild id */ /** The guild id */
+3 -3
View File
@@ -1,6 +1,6 @@
import { Member } from '../structures/member' import { ChannelType, User_Data } from './guild.ts'
import { ChannelType, User_Data } from './guild' import { User } from '../structures/user.ts'
import { User } from '../structures/user' import { Member } from './member.ts'
export interface MentionedUser extends User { export interface MentionedUser extends User {
member: Member member: Member
+3 -1
View File
@@ -1,3 +1,5 @@
import { create_guild } from "../structures/guild"; import { create_guild } from "../structures/guild.ts";
import { create_channel } from "../structures/channel.ts";
export type Guild = ReturnType<typeof create_guild>; export type Guild = ReturnType<typeof create_guild>;
export type Channel = ReturnType<typeof create_channel>