feat(handlers): add getInvite() & deleteInvite() (#421)

* feat(handlers): add getInvite

* feat(handlers): add deleteInvite

* remove deving things

* add jsdoc

* move functions up

* import got deleted

* feat(handlers): invite better types

* throw enum error

* remove unnecessary checks

* Update mod.ts

* channel exists since botHasChannelPermissions did not throw any error
This commit is contained in:
ITOH
2021-01-23 20:33:12 +01:00
committed by GitHub
parent c281665961
commit 8d8c792747
4 changed files with 68 additions and 0 deletions
+35
View File
@@ -9,6 +9,7 @@ import {
GetMessagesAfter, GetMessagesAfter,
GetMessagesAround, GetMessagesAround,
GetMessagesBefore, GetMessagesBefore,
InvitePayload,
MessageContent, MessageContent,
MessageCreateOptions, MessageCreateOptions,
Permission, Permission,
@@ -19,6 +20,7 @@ import {
import { endpoints } from "../../util/constants.ts"; import { endpoints } from "../../util/constants.ts";
import { import {
botHasChannelPermissions, botHasChannelPermissions,
botHasPermission,
calculateBits, calculateBits,
} from "../../util/permissions.ts"; } from "../../util/permissions.ts";
import { cacheHandlers } from "../controllers/cache.ts"; import { cacheHandlers } from "../controllers/cache.ts";
@@ -301,6 +303,39 @@ export async function createInvite(
return RequestManager.post(endpoints.CHANNEL_INVITES(channelID), options); return RequestManager.post(endpoints.CHANNEL_INVITES(channelID), options);
} }
/** Returns an invite for the given code. */
export function getInvite(inviteCode: string) {
return RequestManager.get(endpoints.INVITE(inviteCode)) as Promise<
InvitePayload
>;
}
/** Deletes an invite for the given code. Requires `MANAGE_CHANNELS` or `MANAGE_GUILD` permission */
export async function deleteInvite(
channelID: string,
inviteCode: string,
) {
const hasPerm = await botHasChannelPermissions(channelID, [
"MANAGE_CHANNELS",
]);
if (!hasPerm) {
const channel = await cacheHandlers.get("channels", channelID);
const hasManageGuildPerm = await botHasPermission(channel!.guildID, [
"MANAGE_GUILD",
]);
if (!hasManageGuildPerm) {
throw new Error(Errors.MISSING_MANAGE_CHANNELS);
}
}
return RequestManager.delete(endpoints.INVITE(inviteCode)) as Promise<
InvitePayload
>;
}
/** Gets the webhooks for this channel. Requires MANAGE_WEBHOOKS */ /** Gets the webhooks for this channel. Requires MANAGE_WEBHOOKS */
export async function getChannelWebhooks(channelID: string) { export async function getChannelWebhooks(channelID: string) {
const hasManageWebhooksPerm = await botHasChannelPermissions( const hasManageWebhooksPerm = await botHasChannelPermissions(
+4
View File
@@ -1,11 +1,13 @@
import { import {
channelOverwriteHasPermission, channelOverwriteHasPermission,
createInvite, createInvite,
deleteInvite,
deleteMessages, deleteMessages,
editChannel, editChannel,
followChannel, followChannel,
getChannelInvites, getChannelInvites,
getChannelWebhooks, getChannelWebhooks,
getInvite,
getMessage, getMessage,
getMessages, getMessages,
getPins, getPins,
@@ -133,6 +135,8 @@ export let handlers = {
getPins, getPins,
isChannelSynced, isChannelSynced,
sendMessage, sendMessage,
getInvite,
deleteInvite,
startTyping, startTyping,
// Gateway handler // Gateway handler
+28
View File
@@ -0,0 +1,28 @@
import { Guild } from "../api/structures/mod.ts";
import { ChannelCreatePayload } from "./channel.ts";
import { UserPayload } from "./guild.ts";
/** https://discord.com/developers/docs/resources/invite#invite-object */
export interface InvitePayload {
/** the invite code (unique ID) */
code: string;
/** the guild this invite is for */
guild?: Partial<Guild>;
/** the channel this invite is for */
channel: Partial<ChannelCreatePayload>;
/** the user who created the invite */
inviter?: UserPayload;
/** the target user for this invite */
target_user?: Partial<UserPayload>;
/** the type of user target for this invite */
target_user_type?: InviteTargetUserTypes;
/** approximate count of online members (only present when target_user is set) */
approximate_presence_count?: number;
/** approximate count of total members */
approximate_member_count: number;
}
/** https://discord.com/developers/docs/resources/invite#invite-resource */
export enum InviteTargetUserTypes {
STREAM = 1,
}
+1
View File
@@ -6,6 +6,7 @@ export * from "./errors.ts";
export * from "./fetch.ts"; export * from "./fetch.ts";
export * from "./guild.ts"; export * from "./guild.ts";
export * from "./interactions.ts"; export * from "./interactions.ts";
export * from "./invite.ts";
export * from "./member.ts"; export * from "./member.ts";
export * from "./message.ts"; export * from "./message.ts";
export * from "./options.ts"; export * from "./options.ts";