waiting on vscode deno plugin to fix errors

This commit is contained in:
Skillz
2020-02-25 17:45:39 -05:00
parent 6bb2e2342a
commit 74a390041f
8 changed files with 315 additions and 189 deletions
+2
View File
@@ -1,6 +1,8 @@
# Discordeno
Discord API library wrapper in Deno
[Discord Server](https://discord.gg/rFJzqex)
## TODO
- [Review compression of payloads with GZIP](https://discordapp.com/developers/docs/topics/gateway#sending-payloads-example-gateway-dispatch)
+1 -1
View File
@@ -1,4 +1,4 @@
import Client from "./module/Client.ts"
import Client from "./module/client.ts"
import { configs } from "./configs.ts"
import { StatusType, GatewayOpcode } from "./types/discord.ts";
+2 -2
View File
@@ -1,4 +1,4 @@
import { endpoints } from '../constants/discord.ts'
import { endpoints } from '../constants/discord'
import DiscordRequestManager from '../managers/DiscordRequestManager.ts'
import { DiscordBotGatewayData, DiscordPayload, DiscordHeartbeatPayload, GatewayOpcode } from '../types/discord.ts'
import ShardingManager from '../managers/ShardingManager.ts'
@@ -130,7 +130,7 @@ class Client {
spawnShards(total: number, id = 1) {
// this.ShardingManager.spawnShard(id);
if (id < total) this.spawnShards(total, id + 1)
if (id < total) this.spawnShards(total, id + 1);
}
}
+12 -11
View File
@@ -9,12 +9,11 @@ import {
Create_Invite_Options
} from '../types/channel'
import Client from '../module/client'
import { endpoints } from '../constants/discord'
import { endpoints } from '../constants/discord.ts'
import { create_message, Message } from './message'
import { Message_Create_Options } from '../types/message'
import { Permission, Permissions } from '../types/permission'
import { Guild } from '../types/guild'
import { Guild } from '../types/return-type'
export const create_channel = (data: Channel_Create_Options, guild: Guild, client: Client) => {
const base_channel = {
@@ -34,7 +33,7 @@ export const create_channel = (data: Channel_Create_Options, guild: Guild, clien
/** 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
const result = await client.RequestManager.get(endpoints.CHANNEL_MESSAGE(data.id, id))
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 */
@@ -42,7 +41,7 @@ export const create_channel = (data: Channel_Create_Options, guild: Guild, clien
// TODO: check if the user has VIEW_CHANNEL and READ_MESSAGE_HISTORY
if (options?.limit && options.limit > 100) return
const result = (await client.RequestManager.get(
const result = (await client.discordRequestManager.get(
endpoints.CHANNEL_MESSAGES(data.id),
options
)) as Message_Create_Options[]
@@ -50,7 +49,7 @@ export const create_channel = (data: Channel_Create_Options, guild: Guild, clien
},
/** Get pinned messages in this channel. */
get_pins: async () => {
const result = (await client.RequestManager.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))
},
/** Send a message to the channel. Requires SEND_MESSAGES permission. */
@@ -66,7 +65,7 @@ export const create_channel = (data: Channel_Create_Options, guild: Guild, clien
// TODO: Check content length
const result = await client.RequestManager.post(endpoints.CHANNEL_MESSAGES(data.id), content)
const result = await client.discordRequestManager.post(endpoints.CHANNEL_MESSAGES(data.id), content)
return create_message(result, client)
}
}
@@ -111,6 +110,8 @@ export const create_channel = (data: Channel_Create_Options, guild: Guild, clien
permissionBits = (permissionBits & ~overwrite.deny) | overwrite.allow
})
if (permissionBits & Permissions.ADMINISTRATOR) return true
return permissions.every(permission => permissionBits & Permissions[permission])
}
}
@@ -132,7 +133,7 @@ export const create_channel = (data: Channel_Create_Options, guild: Guild, clien
console.warn(
`This endpoint only accepts a maximum of 100 messages. Deleting the first 100 message ids provided.`
)
return client.RequestManager.POST(endpoints.CHANNEL_BULK_DELETE(data.id), {
return client.discordRequestManager.POST(endpoints.CHANNEL_BULK_DELETE(data.id), {
messages: ids.splice(0, 100),
reason
})
@@ -140,17 +141,17 @@ export const create_channel = (data: Channel_Create_Options, guild: Guild, clien
/** Gets the invites for this channel. Requires MANAGE_CHANNEL */
get_invites: () => {
// TODO: Requires the MANAGE_CHANNELS permission
return client.RequestManager.get(endpoints.CHANNEL_INVITES(data.id))
return client.discordRequestManager.get(endpoints.CHANNEL_INVITES(data.id))
},
/** Creates a new invite for this channel. Requires CREATE_INSTANT_INVITE */
create_invite: (options: Create_Invite_Options) => {
// TODO: Requires CREATE_INSTANT_INVITE permissin.
return client.RequestManager.post(endpoints.CHANNEL_INVITES(data.id), options)
return client.discordRequestManager.post(endpoints.CHANNEL_INVITES(data.id), options)
},
/** Gets the webhooks for this channel. Requires MANAGE_WEBHOOKS */
get_webhooks: () => {
// TODO: Requires MANAGE_WEBHOOKS
return client.RequestManager.get(endpoints.CHANNEL_WEBHOOKS(data.id))
return client.discordRequestManager.get(endpoints.CHANNEL_WEBHOOKS(data.id))
}
}
+177 -74
View File
@@ -1,6 +1,6 @@
import Client from '../module/client.ts'
import { endpoints } from '../constants/discord.ts'
import { format_image_url } from '../utils/cdn.ts'
import Client from "../module/client.ts";
import { endpoints } from "../constants/discord.ts";
import { format_image_url } from "../utils/cdn.ts";
import {
Create_Guild_Payload,
ChannelTypes,
@@ -13,16 +13,14 @@ import {
Create_Emojis_Options,
Edit_Emojis_Options,
Create_Role_Options
} from '../types/guild'
import { create_role, Role } from './role.ts'
import { create_member } from './member'
import { create_channel } from './channel'
import { Channel_Create_Options, Channel } from '../types/channel'
import { Image_Size, Image_Formats } from '../types/cdn'
import { Permissions } from '../types/permission'
import { Member } from '../types/member'
export type Guild = ReturnType<typeof create_guild>;
} from "../types/guild";
import { create_role, Role } from "./role.ts";
import { create_member } from "./member.ts";
import { create_channel } from "./channel.ts";
import { Channel_Create_Options, Channel } from "../types/channel.ts";
import { Image_Size, Image_Formats } from "../types/cdn.ts";
import { Permissions, Permission } from "../types/permission.ts";
import { Member } from "../types/member.ts";
export const create_guild = (data: Create_Guild_Payload, client: Client) => {
const guild = {
@@ -89,91 +87,126 @@ 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 */
preferred_locale: () => data.preferred_locale,
/** 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 ? format_image_url(endpoints.GUILD_ICON(data.id, data.icon), size, format) : undefined,
icon_url: (size: Image_Size = 128, format?: Image_Formats) => data.icon
? format_image_url(
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. */
splash_url: (size: Image_Size = 128, format?: Image_Formats) =>
data.splash ? format_image_url(endpoints.GUILD_SPLASH(data.id, data.splash), size, format) : undefined,
splash_url: (size: Image_Size = 128, format?: Image_Formats) => data.splash
? format_image_url(
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. */
banner_url: (size: Image_Size = 128, format?: Image_Formats) =>
data.banner ? format_image_url(endpoints.GUILD_BANNER(data.id, data.banner), size, format) : undefined,
banner_url: (size: Image_Size = 128, format?: Image_Formats) => 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) => {
// TODO: Check if the bot has `MANAGE_CHANNELS` permission before making a channel
return client.RequestManager.post(endpoints.GUILD_CHANNELS(data.id), {
return client.discordRequestManager.post(endpoints.GUILD_CHANNELS(data.id), {
name,
type: options?.type ? ChannelTypes[options.type] : undefined,
permission_overwrites: options?.permission_overwrites
? options.permission_overwrites.map(perm => ({
...perm,
allow: perm.allow.map(p => Permissions[p]),
deny: perm.deny.map(p => Permissions[p])
}))
...perm,
allow: perm.allow.map(p => Permissions[p]),
deny: perm.deny.map(p => Permissions[p])
}))
: undefined,
...options
})
});
},
/** 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.**
*/
get_channels: () => {
return client.RequestManager.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. */
swap_channels: (channel_positions: Position_Swap[]) => {
if (channel_positions.length < 2) throw 'You must provide atleast two channels to be swapped.'
return client.RequestManager.patch(endpoints.GUILD_CHANNELS(data.id), channel_positions)
if (channel_positions.length <
2)
{
throw "You must provide atleast two channels to be swapped.";
}
return client.discordRequestManager.patch(
endpoints.GUILD_CHANNELS(data.id),
channel_positions
);
},
/** 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.**
*/
get_member: (id: string) => {
return client.RequestManager.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_emoji: (name: string, image: string, options: Create_Emojis_Options) => {
create_emoji: (
name: string,
image: string,
options: Create_Emojis_Options
) => {
// TODO: Check if the bot has `MANAGE_EMOJIS` permission
return client.RequestManager.post(endpoints.GUILD_EMOJIS(data.id), {
return client.discordRequestManager.post(endpoints.GUILD_EMOJIS(data.id), {
...options,
name,
image
})
});
},
/** Modify the given emoji. Requires the MANAGE_EMOJIS permission. */
edit_emoji: (id: string, options: Edit_Emojis_Options) => {
// TODO: check if the bot has `MANAGE_EMOJIS` permission
return client.RequestManager.patch(endpoints.GUILD_EMOJI(data.id, id), {
return client.discordRequestManager.patch(endpoints.GUILD_EMOJI(data.id, id), {
name: options.name,
roles: options.roles
})
});
},
/** Delete the given emoji. Requires the MANAGE_EMOJIS permission. Returns 204 No Content on success. */
delete_emoji: (id: string, reason?: string) => {
// TODO: check if the bot has `MANAGE_EMOJIS` permission
return client.RequestManager.delete(endpoints.GUILD_EMOJI(data.id, id), { reason })
return client.discordRequestManager.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) => {
// TODO: check if the bot has the `MANAGE_ROLES` permission.
const role = await client.RequestManager.post(endpoints.GUILD_ROLES(data.id), {
...options,
permissions: options.permissions?.map(perm => Permissions[perm])
})
const role = await client.discordRequestManager.post(
endpoints.GUILD_ROLES(data.id),
{
...options,
permissions: options.permissions?.map(perm => Permissions[perm])
}
);
// TODO: cache this role
return role
return role;
},
/** Edit a guild role. Requires the MANAGE_ROLES permission. */
edit_role: (id: string, options: Create_Role_Options) => {
// TODO: check if the bot has the `MANAGE_ROLES` permission.
return client.RequestManager.patch(endpoints.GUILD_ROLE(data.id, id), options)
return client.discordRequestManager.patch(
endpoints.GUILD_ROLE(data.id, id),
options
);
},
/** Delete a guild role. Requires the MANAGE_ROLES permission. */
delete_role: (id: string) => {
// TODO: check if the bot has the `MANAGE_ROLES` permission.
return client.RequestManager.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.
*
@@ -181,25 +214,38 @@ export const create_guild = (data: Create_Guild_Payload, client: Client) => {
*/
get_roles: () => {
// TODO: check if the bot has the `MANAGE_ROLES` permission.
return client.RequestManager.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. */
swap_roles: (rolePositons: Position_Swap) => {
// TODO: check if the bot has the `MANAGE_ROLES` permission.
return client.RequestManager.patch(endpoints.GUILD_ROLES(data.id), rolePositons)
return client.discordRequestManager.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 `The number of days to count prune for must be 1 or more.`
if (days <
1)
{
throw `The number of days to count prune for must be 1 or more.`;
}
// TODO: check if the bot has `KICK_MEMBERS` permission
const result = (await client.RequestManager.get(endpoints.GUILD_PRUNE(data.id), { days })) as PrunePayload
return result.pruned
const result = (await client.discordRequestManager.get(
endpoints.GUILD_PRUNE(data.id),
{ days }
)) as PrunePayload;
return result.pruned;
},
/** Begin pruning all members in the given time period */
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.
return client.RequestManager.post(endpoints.GUILD_PRUNE(data.id), { days })
return client.discordRequestManager.post(
endpoints.GUILD_PRUNE(data.id),
{ days }
);
},
// TODO: REQUEST THIS OVER WEBSOCKET WITH GET_GUILD_MEMBERS ENDPOINT
// fetch_all_members: () => {
@@ -207,87 +253,144 @@ 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) => {
// TODO: check if the bot has VIEW_AUDIT_LOGS permission
return client.RequestManager.get(endpoints.GUILD_AUDIT_LOGS(data.id), {
return client.discordRequestManager.get(endpoints.GUILD_AUDIT_LOGS(data.id), {
...options,
limit: options.limit && options.limit >= 1 && options.limit <= 100 ? options.limit : 50
})
limit: options.limit && options.limit >= 1 && options.limit <= 100
? options.limit
: 50
});
},
/** Returns the guild embed object. Requires the MANAGE_GUILD permission. */
get_embed: () => {
// TODO: check if the bot has the MANAGE_GUILD permission
return client.RequestManager.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. */
edit_embed: (enabled: boolean, channel_id?: string | null) => {
// TODO: Requires the MANAGE_GUILD permission.
return client.RequestManager.patch(endpoints.GUILD_EMBED(data.id), { enabled, channel_id })
return client.discordRequestManager.patch(
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. */
get_vanity_url: () => {
return client.RequestManager.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. */
get_integrations: () => {
// TODO: requires the MANAGE_GUILD permission
return client.RequestManager.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. */
edit_integration: (id: string, options: Edit_Integration_Options) => {
// TODO: requires the MANAGE_GUILD permission
return client.RequestManager.patch(endpoints.GUILD_INTEGRATION(data.id, id), options)
return client.discordRequestManager.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) => {
// TODO: requires the MANAGE_GUILD permission
return client.RequestManager.delete(endpoints.GUILD_INTEGRATION(data.id, id))
return client.discordRequestManager.delete(
endpoints.GUILD_INTEGRATION(data.id, id)
);
},
/** Sync an integration. Requires teh MANAGE_GUILD permission. */
sync_integration: (id: string) => {
// TODO: requires MANAGE_GUILD
return client.RequestManager.post(endpoints.GUILD_INTEGRATION_SYNC(data.id, id))
return client.discordRequestManager.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: () => {
// TODO: requires the BAN_MEMBERS permission
return client.RequestManager.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: (id: string, options: BanOptions) => {
// TODO: requires the BAN_MEMBERS permission
return client.RequestManager.put(endpoints.GUILD_BAN(data.id, id), options)
return client.discordRequestManager.put(
endpoints.GUILD_BAN(data.id, id),
options
);
},
/** Remove the ban for a user. REquires BAN_MEMBERS permission */
unban: (id: string) => {
// TODO: requires the BAN_MEMBERS permission
return client.RequestManager.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. */
channel_has_permissions: (
channel_id: string,
member_id: string,
permissions: Permission[]
) => {
if (member_id === data.owner_id) return true;
const member = guild.members.get(member_id);
if (!member) {
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);
if (!channel) {
throw "Invalid channel id provided. This channel was not found in the cache.";
}
let permissionBits = member.roles().reduce((bits, role_id) => {
const role = guild.roles.get(role_id);
if (!role) return bits;
bits |= role.permissions();
return bits;
}, 0);
data.permission_overwrites?.forEach(overwrite => {
permissionBits = (permissionBits & ~overwrite.deny) | overwrite.allow;
});
if (permissionBits & Permissions.ADMINISTRATOR) return true
return permissions.every(
permission => permissionBits & Permissions[permission]
);
},
/** Modify a guilds settings. Requires the MANAGE_GUILD permission. */
edit: (options: Guild_Edit_Options) => {
// TODO: requires the MANAGE_GUILD permission
return client.RequestManager.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 */
et_invites: () => {
get_invites: () => {
// TODO: requires MANAGE_GUILD permission
return client.RequestManager.get(endpoints.GUILD_INVITES(data.id))
return client.discordRequestManager.get(endpoints.GUILD_INVITES(data.id));
},
/** Leave a guild */
leave: () => {
return client.RequestManager.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. */
get_voice_regions: () => {
return client.RequestManager.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. */
get_webhooks: () => {
// TODO: requires MANAGE_WEBHOOKS
return client.RequestManager.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(m => guild.members.set(m.user.id, create_member(m, data.id, client)))
data.channels.forEach(c => guild.channels.set(c.id, create_channel(c, guild, client)))
return guild
}
data.roles.forEach(r => guild.roles.set(r.id, create_role(r)));
data.members.forEach(
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;
};
+109 -92
View File
@@ -1,100 +1,117 @@
import Client from '../module/client'
import { endpoints } from '../constants/discord'
import { format_image_url } from '../utils/cdn'
import { Member_Create_Payload, Edit_Member_Options } from '../types/member'
import { Image_Size, Image_Formats } from '../types/cdn'
import { Permission, Permissions } from '../types/permission'
import { cache } from '../utils/cache'
import Client 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";
import { Image_Size, Image_Formats } from "../types/cdn.ts";
import { Permission, Permissions } from "../types/permission.ts";
import { Role_Data } from "../types/role.ts";
export const create_member = (data: Member_Create_Payload, guild_id: string, client: Client) => {
const guild = cache.guilds.get(guild_id)
export const create_member = (
data: Member_Create_Payload,
guild_id: string,
role_data: Role_Data[],
owner_id: string,
client: Client
) => ({
/** The complete raw data from the member create payload */
raw: () => data,
/** The unique user id */
id: () => data.user.id,
/** The user's guild nickname if one is set. */
roles: () => data.roles,
/** Array of role ids that the member has */
nick: () => data.nick,
/** When the user joined the guild */
joined_at: () => Date.parse(data.joined_at),
/** When the user used their nitro boost on the server. */
premium_since:
() => (data.premium_since ? Date.parse(data.premium_since) : undefined),
/** Whether the user is deafened in voice channels */
deaf: () => data.deaf,
/** Whether the user is muted in voice channels */
mute: () => data.mute,
/** The username of the this member. */
username: () => data.user.username,
/** The 4 digit unique identifier */
discriminator: () => data.user.discriminator,
/** The full username#discriminator */
tag: () => `${data.user.username}#${data.user.discriminator}`,
/** The users custom avatar or the default avatar */
avatar_url: (
size: Image_Size = 128,
format?: Image_Formats
) => data.user.avatar
? format_image_url(
endpoints.USER_AVATAR(data.user.id, data.user.avatar),
size,
format
)
: endpoints.USER_DEFAULT_AVATAR(Number(data.user.discriminator) % 5),
/** The user mention with nickname if possible */
mention: () => `<@!${data.user.id}>`,
/** Whether the member is a bot */
bot: () => data.user.bot,
/** Add a role to the member */
add_role: (role_id: string, reason?: string) => {
// TODO: check if the bot has MANAGE_ROLE and its highest role is above this one
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 bot has MANAGE_ROLE permissions and its highest role is above this role
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 bot has KICK_MEMBER permissions
return client.discordRequestManager.delete(
endpoints.GUILD_MEMBER(guild_id, data.user.id),
{ reason }
);
},
/** Edit the member */
edit: (options: Edit_Member_Options) => {
// TODO: check if has MANAGE_NICKNAME Permission
// TODO: check if it is a valid nickname like 32 characters
options.nick = undefined;
return {
/** The complete raw data from the member create payload */
raw: () => data,
/** The unique user id */
id: () => data.user.id,
/** The user's guild nickname if one is set. */
roles: () => data.roles,
/** Array of role ids that the member has */
nick: () => data.nick,
/** When the user joined the guild. */
joined_at: () => Date.parse(data.joined_at),
/** When the user used their nitro boost on the server. */
premium_since: () => (data.premium_since ? Date.parse(data.premium_since) : undefined),
/** Whether the user is deafened in voice channels */
deaf: () => data.deaf,
/** Whether the user is muted in voice channels */
mute: () => data.mute,
/** The username of the this member. */
username: () => data.user.username,
/** The 4 digit unique identifier */
discriminator: () => data.user.discriminator,
/** The full username#discriminator */
tag: () => `${data.user.username}#${data.user.discriminator}`,
/** The users custom avatar or the default avatar */
avatar_url: (size: Image_Size = 128, format?: Image_Formats) =>
data.user.avatar
? format_image_url(endpoints.USER_AVATAR(data.user.id, data.user.avatar), size, format)
: endpoints.USER_DEFAULT_AVATAR(Number(data.user.discriminator) % 5),
/** The user mention with nickname if possible */
mention: () => `<@!${data.user.id}>`,
/** Whether the member is a bot */
bot: () => data.user.bot,
/** Add a role to the member */
add_role: (role_id: string, reason?: string) => {
// TODO: check if the bot has MANAGE_ROLE and its highest role is above this one
return client.RequestManager.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 bot has MANAGE_ROLE permissions and its highest role is above this role
return client.RequestManager.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 bot has KICK_MEMBER permissions
return client.RequestManager.delete(endpoints.GUILD_MEMBER(guild_id, data.user.id), { reason })
},
/** Ban a member from the server */
ban: (delete_message_days = 0, reason?: string) => {
return guild.ban(data.user.id, { delete_message_days, reason })
},
/** Checks if the member has this permission. If the member is an owner or has admin perms it will always be true. */
has_permissions: (permissions: Permission[]) => {
if (data.user.id === guild.owner_id) return true
// TODO: check if has MANAGE_ROLES permission
options.roles = undefined;
const permissionBits = data.roles.reduce((bits, role_id) => {
const role = guild.roles.get(role_id)
if (!role) return bits
// TODO: This should check if the member is in a voice channel
// TODO: CHeck if has MUTE_MEMBERS permission
options.mute = undefined;
// TODO: check if has DEAFEN_MEMBERS permission
options.deaf = undefined;
// TODO: if channel id is provided check if the bot has CONNECT and MOVE in channel and current channel
options.channel_id = undefined;
bits |= role.permissions
return client.discordRequestManager.patch(
endpoints.GUILD_MEMBER(guild_id, data.user.id),
options
);
},
/** Checks if the member has this permission. If the member is an owner or has admin perms it will always be true. */
has_permissions: (member_id: string, permissions: Permission[]) => {
if (member_id === owner_id) return true;
return bits
}, 0)
const permissionBits = role_data.filter(
role => data.roles.includes(role.id)
).reduce((bits, data) => {
bits |= data.permissions;
return permissions.every(permission => permissionBits & Permissions[permission])
},
/** Edit the member */
edit: (options: Edit_Member_Options) => {
// TODO: check if has MANAGE_NICKNAME Permission
// TODO: check if it is a valid nickname like 32 characters
options.nick = undefined
return bits;
}, 0);
// TODO: check if has MANAGE_ROLES permission
options.roles = undefined
// TODO: This should check if the member is in a voice channel
// TODO: CHeck if has MUTE_MEMBERS permission
options.mute = undefined
// TODO: check if has DEAFEN_MEMBERS permission
options.deaf = undefined
// TODO: if channel id is provided check if the bot has CONNECT and MOVE in channel and current channel
options.channel_id = undefined
return client.RequestManager.patch(endpoints.GUILD_MEMBER(guild_id, data.user.id), options)
}
return permissions.every(
permission => permissionBits & Permissions[permission]
);
}
}
});
+9 -9
View File
@@ -38,38 +38,38 @@ export const create_message = (data: Message_Create_Options, client: Client) =>
// TODO: Requires MANAGE_MESSAGES
if (data.author.id !== client.bot_id) {}
client.RequestManager.delete(endpoints.CHANNEL_MESSAGE(data.channel_id, data.id), { reason })
client.discordRequestManager.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: () => {
// TODO: Requires MANAGE_MESSAGES
client.RequestManager.put(endpoints.CHANNEL_MESSAGE(data.channel_id, data.id))
client.discordRequestManager.put(endpoints.CHANNEL_MESSAGE(data.channel_id, data.id))
},
unpin: () => {
// TODO: Requires MANAGE_MESSAGES
client.RequestManager.delete(endpoints.CHANNEL_MESSAGE(data.channel_id, data.id))
client.discordRequestManager.delete(endpoints.CHANNEL_MESSAGE(data.channel_id, data.id))
},
/** Create a reaction for the message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. Requires READ_MESSAGE_HISTORY and ADD_REACTIONS */
add_reaction: (reaction: string) => {
client.RequestManager.put(endpoints.CHANNEL_MESSAGE_REACTION_ME(data.channel_id, data.id, reaction))
client.discordRequestManager.put(endpoints.CHANNEL_MESSAGE_REACTION_ME(data.channel_id, data.id, reaction))
},
/** Removes a reaction from the bot on this message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
remove_reaction: (reaction: string) => {
client.RequestManager.delete(endpoints.CHANNEL_MESSAGE_REACTION_ME(data.channel_id, data.id, reaction))
client.discordRequestManager.delete(endpoints.CHANNEL_MESSAGE_REACTION_ME(data.channel_id, data.id, reaction))
},
/** Removes all reactions for all emojis on this message. */
remove_all_reactions: () => {
// TODO: Requires MANAGE_MESSAGES
client.RequestManager.delete(endpoints.CHANNEL_MESSAGE_REACTIONS(data.channel_id, data.id))
client.discordRequestManager.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) => {
// TODO: Requires MANAGE_MESSAGES
client.RequestManager.delete(endpoints.CHANNEL_MESSAGE_REACTION(data.channel_id, data.id, reaction))
client.discordRequestManager.delete(endpoints.CHANNEL_MESSAGE_REACTION(data.channel_id, data.id, reaction))
},
/** Get a list of users that reacted with this emoji. */
get_reactions: async (reaction: string) => {
const result = await client.RequestManager.get(endpoints.CHANNEL_MESSAGE_REACTION(data.channel_id, data.id, reaction)) as User_Payload[]
const result = await client.discordRequestManager.get(endpoints.CHANNEL_MESSAGE_REACTION(data.channel_id, data.id, reaction)) as User_Payload[]
return result.map(res => create_user(res))
},
/** Edit the message. */
@@ -86,7 +86,7 @@ export const create_message = (data: Message_Create_Options, client: Client) =>
// TODO: Check content length
const result = await client.RequestManager.patch(endpoints.CHANNEL_MESSAGES(data.id), content)
const result = await client.discordRequestManager.patch(endpoints.CHANNEL_MESSAGES(data.id), content)
return create_message(result, client)
}
}
+3
View File
@@ -0,0 +1,3 @@
import { create_guild } from "../structures/guild";
export type Guild = ReturnType<typeof create_guild>;