From 0e03fcc14cdbf0cd53a03db2cd6dc96bb31c3ca5 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Tue, 14 Feb 2023 22:30:58 +0000 Subject: [PATCH] fix: remaining helpers --- packages/gateway/src/manager.ts | 35 +++++++++- .../guilds/voice/leaveVoiceChannel.ts | 1 + packages/rest/src/manager.ts | 70 ++++++++++++++++++- 3 files changed, 103 insertions(+), 3 deletions(-) diff --git a/packages/gateway/src/manager.ts b/packages/gateway/src/manager.ts index 4c2e3dde9..67373c39a 100644 --- a/packages/gateway/src/manager.ts +++ b/packages/gateway/src/manager.ts @@ -44,7 +44,7 @@ export function createGatewayManager(options: CreateGatewayManagerOptions): Gate requestMembers: { enabled: options.cache?.requestMembers?.enabled ?? false, pending: new Collection(), - } + }, }, calculateTotalShards() { @@ -261,7 +261,7 @@ export function createGatewayManager(options: CreateGatewayManagerOptions): Gate nonce, }, }) - return []; + return [] } return await new Promise((resolve) => { @@ -281,6 +281,24 @@ export function createGatewayManager(options: CreateGatewayManagerOptions): Gate }) }) }, + + async leaveVoiceChannel(guildId) { + const shardId = gateway.calculateShardId(guildId) + const shard = gateway.shards.get(shardId) + if (!shard) { + throw new Error(`Shard (id: ${shardId} not found`) + } + + return shard.send({ + op: GatewayOpcodes.VoiceStateUpdate, + d: { + guild_id: guildId.toString(), + channel_id: null, + self_mute: false, + self_deaf: false, + }, + }) + }, } return gateway @@ -465,6 +483,19 @@ export interface GatewayManager extends Required { * @see {@link https://discord.com/developers/docs/topics/gateway#request-guild-members} */ requestMembers: (guildId: BigString, options?: Omit) => Promise> + /** + * Leaves the voice channel the bot user is currently in. + * + * This function sends the _Update Voice State_ gateway command over the gateway behind the scenes. + * + * @param guildId - The ID of the guild the voice channel to leave is in. + * + * @remarks + * Fires a _Voice State Update_ gateway event. + * + * @see {@link https://discord.com/developers/docs/topics/gateway#update-voice-state} + */ + leaveVoiceChannel: (guildId: BigString) => Promise } export interface RequestMemberRequest { diff --git a/packages/old/gateway/src/noCheckHelpers/guilds/voice/leaveVoiceChannel.ts b/packages/old/gateway/src/noCheckHelpers/guilds/voice/leaveVoiceChannel.ts index 946f0eeb5..b9aeec835 100644 --- a/packages/old/gateway/src/noCheckHelpers/guilds/voice/leaveVoiceChannel.ts +++ b/packages/old/gateway/src/noCheckHelpers/guilds/voice/leaveVoiceChannel.ts @@ -5,6 +5,7 @@ import type { BigString } from '@discordeno/types' import { GatewayOpcodes } from '@discordeno/types' import { calculateShardId } from '@discordeno/utils' import type { RestManager } from '../../../../../rest/src/restManager.js' + /** * Leaves the voice channel the bot user is currently in. * diff --git a/packages/rest/src/manager.ts b/packages/rest/src/manager.ts index d51e6e21f..d4bbbf330 100644 --- a/packages/rest/src/manager.ts +++ b/packages/rest/src/manager.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/restrict-template-expressions */ /* eslint-disable no-const-assign */ -import { DiscordGuildWidget, InteractionResponseTypes } from '@discordeno/types' +import { InteractionResponseTypes } from '@discordeno/types' import { calculateBits, camelize, @@ -54,6 +54,7 @@ import type { DiscordGetGatewayBot, DiscordGuild, DiscordGuildPreview, + DiscordGuildWidget, DiscordGuildWidgetSettings, DiscordIntegration, DiscordInvite, @@ -180,6 +181,9 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage dm: () => { return '/users/@me/channels' }, + pin: (channelId, messageId) => { + return `/channels/${channelId}/pins/${messageId}` + }, pins: (channelId) => { return `/channels/${channelId}/pins` }, @@ -461,6 +465,9 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage invites: (guildId) => { return `/guilds/${guildId}/invites` }, + leave: (guildId) => { + return `/users/@me/guilds/${guildId}` + }, members: { ban: (guildId, userId) => { return `/guilds/${guildId}/bans/${userId}` @@ -1631,6 +1638,10 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage return await rest.put(rest.routes.channels.threads.me(channelId)) }, + async leaveGuild(guildId) { + return await rest.delete(rest.routes.guilds.leave(guildId)) + }, + async leaveThread(channelId) { return await rest.delete(rest.routes.channels.threads.me(channelId)) }, @@ -1723,6 +1734,10 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage }) }, + async pinMessage(channelId, messageId, reason) { + return await rest.put(rest.routes.channels.pin(channelId, messageId), reason ? { reason } : undefined) + }, + async pruneMembers(guildId, options) { return await rest.post<{ pruned: number | null }>(rest.routes.guilds.members.prune(guildId), options) }, @@ -1735,6 +1750,10 @@ export function createRestManager(options: CreateRestManagerOptions): RestManage return await rest.delete(rest.routes.guilds.members.ban(guildId, userId)) }, + async unpinMessage(channelId, messageId, reason) { + return await rest.delete(rest.routes.channels.pin(channelId, messageId), reason ? { reason } : undefined) + }, + async triggerTypingIndicator(channelId) { return await rest.post(rest.routes.channels.typing(channelId)) }, @@ -1830,6 +1849,8 @@ export interface RestManager { bulk: (channelId: BigString) => string /** Route for non-specific dm channel. */ dm: () => string + /** Route for handling a specific pin. */ + pin: (channelId: BigString, messageId: BigString) => string /** Route for handling a channels pins. */ pins: (channelId: BigString) => string /** Route for non-specific webhook in a channel. */ @@ -1931,6 +1952,8 @@ export interface RestManager { invite: (inviteCode: string, options?: GetInvite) => string /** Route for handling non-specific invites in a guild. */ invites: (guildId: BigString) => string + /** Route for handling a bot leaving a guild. */ + leave: (guildId: BigString) => string /** Route for handling a guild's preview. */ preview: (guildId: BigString) => string /** Route for handling pruning of a guild. */ @@ -3905,6 +3928,17 @@ export interface RestManager { * @see {@link https://discord.com/developers/docs/resources/channel#join-thread} */ joinThread: (channelId: BigString) => Promise + /** + * Leaves a guild. + * + * @param guildId - The ID of the guild to leave. + * + * @remarks + * Fires a _Guild Delete_ event. + * + * @see {@link https://discord.com/developers/docs/resources/user#leave-guild} + */ + leaveGuild: (guildId: BigString) => Promise /** * Removes the bot user from a thread. * @@ -4220,6 +4254,24 @@ export interface RestManager { * @see {@link https://discord.com/developers/docs/resources/guild#remove-guild-member} */ kickMember: (guildId: BigString, userId: BigString, reason?: string) => Promise + /** + * Pins a message in a channel. + * + * @param channelId - The ID of the channel where the message is to be pinned. + * @param messageId - The ID of the message to pin. + * + * @remarks + * Requires that the bot user be able to see the contents of the channel in which the messages were posted. + * + * Requires the `MANAGE_MESSAGES` permission. + * + * ⚠️ There can only be at max 50 messages pinned in a channel. + * + * Fires a _Channel Pins Update_ event. + * + * @see {@link https://discord.com/developers/docs/resources/channel#pin-message} + */ + pinMessage: (channelId: BigString, messageId: BigString, reason?: string) => Promise /** * Initiates the process of pruning inactive members. * @@ -4267,6 +4319,22 @@ export interface RestManager { * @see {@link https://discord.com/developers/docs/resources/guild#remove-guild-ban} */ unbanMember: (guildId: BigString, userId: BigString) => Promise + /** + * Unpins a pinned message in a channel. + * + * @param channelId - The ID of the channel where the message is pinned. + * @param messageId - The ID of the message to unpin. + * + * @remarks + * Requires that the bot user be able to see the contents of the channel in which the messages were posted. + * + * Requires the `MANAGE_MESSAGES` permission. + * + * Fires a _Channel Pins Update_ event. + * + * @see {@link https://discord.com/developers/docs/resources/channel#unpin-message} + */ + unpinMessage: (channelId: BigString, messageId: BigString, reason?: string) => Promise } export type RequestMethods = 'GET' | 'POST' | 'DELETE' | 'PATCH' | 'PUT'