feat: voiceStateShorter (#286)

This commit is contained in:
MARCROCK22 2024-11-03 07:05:25 -04:00 committed by GitHub
parent 86e66c706b
commit db9e319545
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 67 additions and 70 deletions

View File

@ -42,6 +42,7 @@ import {
import { promises } from 'node:fs'; import { promises } from 'node:fs';
import { HandleCommand } from '../commands/handle'; import { HandleCommand } from '../commands/handle';
import { BanShorter } from '../common/shorters/bans'; import { BanShorter } from '../common/shorters/bans';
import { VoiceStateShorter } from '../common/shorters/voiceStates';
import type { Awaitable, DeepPartial, IntentStrings, OmitInsert, PermissionStrings, When } from '../common/types/util'; import type { Awaitable, DeepPartial, IntentStrings, OmitInsert, PermissionStrings, When } from '../common/types/util';
import type { ComponentCommand, ComponentContext, ModalCommand, ModalContext } from '../components'; import type { ComponentCommand, ComponentContext, ModalCommand, ModalContext } from '../components';
import { ComponentHandler } from '../components/handler'; import { ComponentHandler } from '../components/handler';
@ -75,6 +76,7 @@ export class BaseClient {
threads = new ThreadShorter(this); threads = new ThreadShorter(this);
bans = new BanShorter(this); bans = new BanShorter(this);
interactions = new InteractionShorter(this); interactions = new InteractionShorter(this);
voiceStates = new VoiceStateShorter(this);
debugger?: Logger; debugger?: Logger;

View File

@ -136,11 +136,10 @@ export class CommandContext<
} }
async fetchResponse(): Promise< async fetchResponse(): Promise<
If<InferWithPrefix, WebhookMessageStructure | MessageStructure | undefined, WebhookMessageStructure | undefined> If<InferWithPrefix, WebhookMessageStructure | MessageStructure, WebhookMessageStructure>
> { > {
if (this.interaction) return this.interaction.fetchResponse(); if (this.interaction) return this.interaction.fetchResponse();
this.messageResponse = await this.messageResponse?.fetch(); return (this.messageResponse = (await this.messageResponse!.fetch()) as never);
return this.messageResponse as undefined;
} }
channel(mode?: 'rest' | 'flow'): Promise<If<InferWithPrefix, AllChannels | undefined, AllChannels>>; channel(mode?: 'rest' | 'flow'): Promise<If<InferWithPrefix, AllChannels | undefined, AllChannels>>;

View File

@ -1,17 +1,11 @@
import type { ReturnCache } from '../..'; import type { ReturnCache } from '../..';
import type { import type { GuildMemberStructure, GuildStructure } from '../../client/transformers';
GuildMemberStructure,
GuildStructure,
MessageStructure,
WebhookMessageStructure,
} from '../../client/transformers';
import type { import type {
InteractionCreateBodyRequest, InteractionCreateBodyRequest,
InteractionMessageUpdateBodyRequest, InteractionMessageUpdateBodyRequest,
MakeRequired, MakeRequired,
ModalCreateBodyRequest, ModalCreateBodyRequest,
UnionToTuple, UnionToTuple,
When,
} from '../../common'; } from '../../common';
import type { AllChannels, EntryPointInteraction } from '../../structures'; import type { AllChannels, EntryPointInteraction } from '../../structures';
import { MessageFlags } from '../../types'; import { MessageFlags } from '../../types';
@ -43,22 +37,16 @@ export class EntryPointContext<M extends keyof RegisteredMiddlewares = never> ex
return this.command.name; return this.command.name;
} }
write<WR extends boolean = false>( write<WR extends boolean = false>(body: InteractionCreateBodyRequest, withResponse?: WR) {
body: InteractionCreateBodyRequest, return this.interaction.write<WR>(body, withResponse);
withResponse?: WR,
): Promise<When<WR, WebhookMessageStructure, void | WebhookMessageStructure>> {
return this.interaction.write(body, withResponse);
} }
modal(body: ModalCreateBodyRequest) { modal(body: ModalCreateBodyRequest) {
return this.interaction.modal(body); return this.interaction.modal(body);
} }
deferReply<WR extends boolean = false>( deferReply<WR extends boolean = false>(ephemeral = false, withResponse?: WR) {
ephemeral = false, return this.interaction.deferReply<WR>(ephemeral ? MessageFlags.Ephemeral : undefined, withResponse);
withResponse?: WR,
): Promise<When<WR, WebhookMessageStructure, undefined>> {
return this.interaction.deferReply(ephemeral ? MessageFlags.Ephemeral : undefined, withResponse);
} }
editResponse(body: InteractionMessageUpdateBodyRequest) { editResponse(body: InteractionMessageUpdateBodyRequest) {
@ -72,8 +60,8 @@ export class EntryPointContext<M extends keyof RegisteredMiddlewares = never> ex
editOrReply<WR extends boolean = false>( editOrReply<WR extends boolean = false>(
body: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest, body: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest,
withResponse?: WR, withResponse?: WR,
): Promise<When<WR, WebhookMessageStructure | MessageStructure, void | WebhookMessageStructure | MessageStructure>> { ) {
return this.interaction.editOrReply(body as InteractionCreateBodyRequest, withResponse); return this.interaction.editOrReply<WR>(body as InteractionCreateBodyRequest, withResponse);
} }
fetchResponse() { fetchResponse() {

View File

@ -5,7 +5,6 @@ import {
type MessageStructure, type MessageStructure,
Transformers, Transformers,
type UserStructure, type UserStructure,
type WebhookMessageStructure,
} from '../../client/transformers'; } from '../../client/transformers';
import { import {
type InteractionCreateBodyRequest, type InteractionCreateBodyRequest,
@ -13,7 +12,6 @@ import {
type MakeRequired, type MakeRequired,
type ModalCreateBodyRequest, type ModalCreateBodyRequest,
type UnionToTuple, type UnionToTuple,
type When,
toSnakeCase, toSnakeCase,
} from '../../common'; } from '../../common';
import type { AllChannels, MessageCommandInteraction, UserCommandInteraction } from '../../structures'; import type { AllChannels, MessageCommandInteraction, UserCommandInteraction } from '../../structures';
@ -68,22 +66,16 @@ export class MenuCommandContext<
return this.command.name; return this.command.name;
} }
write<WR extends boolean = false>( write<WR extends boolean = false>(body: InteractionCreateBodyRequest, withResponse?: WR) {
body: InteractionCreateBodyRequest, return this.interaction.write<WR>(body, withResponse);
withResponse?: WR,
): Promise<When<WR, WebhookMessageStructure, void | WebhookMessageStructure>> {
return this.interaction.write(body, withResponse);
} }
modal(body: ModalCreateBodyRequest) { modal(body: ModalCreateBodyRequest) {
return this.interaction.modal(body); return this.interaction.modal(body);
} }
deferReply<WR extends boolean = false>( deferReply<WR extends boolean = false>(ephemeral = false, withResponse?: WR) {
ephemeral = false, return this.interaction.deferReply<WR>(ephemeral ? MessageFlags.Ephemeral : undefined, withResponse);
withResponse?: WR,
): Promise<When<WR, WebhookMessageStructure, undefined>> {
return this.interaction.deferReply(ephemeral ? MessageFlags.Ephemeral : undefined, withResponse);
} }
editResponse(body: InteractionMessageUpdateBodyRequest) { editResponse(body: InteractionMessageUpdateBodyRequest) {
@ -97,8 +89,8 @@ export class MenuCommandContext<
editOrReply<WR extends boolean = false>( editOrReply<WR extends boolean = false>(
body: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest, body: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest,
withResponse?: WR, withResponse?: WR,
): Promise<When<WR, WebhookMessageStructure | MessageStructure, void | WebhookMessageStructure | MessageStructure>> { ) {
return this.interaction.editOrReply(body as InteractionCreateBodyRequest, withResponse); return this.interaction.editOrReply<WR>(body as InteractionCreateBodyRequest, withResponse);
} }
fetchResponse() { fetchResponse() {

View File

@ -0,0 +1,15 @@
import { BaseShorter } from './base';
export class VoiceStateShorter extends BaseShorter {
requestSpeak(guildId: string, date: string) {
return this.client.proxy.guilds(guildId)['voice-states']['@me'].patch({
body: { request_to_speak_timestamp: date },
});
}
setSuppress(guildId: string, suppress: boolean) {
return this.client.proxy.guilds(guildId)['voice-states']['@me'].patch({
body: { suppress },
});
}
}

View File

@ -140,14 +140,14 @@ export class WebhookShorter extends BaseShorter {
* @param token The token of the webhook. * @param token The token of the webhook.
* @param messageId The ID of the message to fetch. * @param messageId The ID of the message to fetch.
* @param threadId The ID of the thread the message belongs to. * @param threadId The ID of the thread the message belongs to.
* @returns A Promise that resolves to the fetched message, or undefined if not found. * @returns A Promise that resolves to the fetched message
*/ */
async fetchMessage(webhookId: string, token: string, messageId: string, threadId?: string) { async fetchMessage(webhookId: string, token: string, messageId: string, threadId?: string) {
const message = await this.client.proxy const message = await this.client.proxy
.webhooks(webhookId)(token) .webhooks(webhookId)(token)
.messages(messageId) .messages(messageId)
.get({ auth: false, query: threadId ? { thread_id: threadId } : undefined }); .get({ auth: false, query: threadId ? { thread_id: threadId } : undefined });
return message ? Transformers.WebhookMessage(this.client, message, webhookId, token) : undefined; return Transformers.WebhookMessage(this.client, message, webhookId, token);
} }
async listFromGuild(guildId: string) { async listFromGuild(guildId: string) {

View File

@ -9,12 +9,7 @@ import type {
StringSelectMenuInteraction, StringSelectMenuInteraction,
UserSelectMenuInteraction, UserSelectMenuInteraction,
} from '..'; } from '..';
import type { import type { GuildMemberStructure, GuildStructure } from '../client/transformers';
GuildMemberStructure,
GuildStructure,
MessageStructure,
WebhookMessageStructure,
} from '../client/transformers';
import type { CommandMetadata, ExtendContext, GlobalMetadata, RegisteredMiddlewares, UsingClient } from '../commands'; import type { CommandMetadata, ExtendContext, GlobalMetadata, RegisteredMiddlewares, UsingClient } from '../commands';
import { BaseContext } from '../commands/basecontext'; import { BaseContext } from '../commands/basecontext';
import type { import type {
@ -24,7 +19,6 @@ import type {
MakeRequired, MakeRequired,
ModalCreateBodyRequest, ModalCreateBodyRequest,
UnionToTuple, UnionToTuple,
When,
} from '../common'; } from '../common';
import { ComponentType, MessageFlags } from '../types'; import { ComponentType, MessageFlags } from '../types';
@ -77,15 +71,15 @@ export class ComponentContext<
* @param fetchReply - Whether to fetch the reply or not. * @param fetchReply - Whether to fetch the reply or not.
*/ */
write<FR extends boolean = false>(body: InteractionCreateBodyRequest, fetchReply?: FR) { write<FR extends boolean = false>(body: InteractionCreateBodyRequest, fetchReply?: FR) {
return this.interaction.write(body, fetchReply); return this.interaction.write<FR>(body, fetchReply);
} }
/** /**
* Defers the reply to the interaction. * Defers the reply to the interaction.
* @param ephemeral - Whether the reply should be ephemeral or not. * @param ephemeral - Whether the reply should be ephemeral or not.
*/ */
deferReply(ephemeral = false) { deferReply<FR extends boolean = false>(ephemeral = false, fetchReply?: FR) {
return this.interaction.deferReply(ephemeral ? MessageFlags.Ephemeral : undefined); return this.interaction.deferReply<FR>(ephemeral ? MessageFlags.Ephemeral : undefined, fetchReply);
} }
/** /**
@ -119,8 +113,15 @@ export class ComponentContext<
editOrReply<FR extends boolean = false>( editOrReply<FR extends boolean = false>(
body: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest, body: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest,
fetchReply?: FR, fetchReply?: FR,
): Promise<When<FR, WebhookMessageStructure | MessageStructure, void | WebhookMessageStructure | MessageStructure>> { ) {
return this.interaction.editOrReply(body as InteractionCreateBodyRequest, fetchReply); return this.interaction.editOrReply<FR>(body as InteractionCreateBodyRequest, fetchReply);
}
/**
* @returns A Promise that resolves to the fetched message
*/
fetchResponse() {
return this.interaction.fetchResponse();
} }
/** /**

View File

@ -1,10 +1,5 @@
import type { AllChannels, ModalCommand, ModalSubmitInteraction, ReturnCache } from '..'; import type { AllChannels, ModalCommand, ModalSubmitInteraction, ReturnCache } from '..';
import type { import type { GuildMemberStructure, GuildStructure } from '../client/transformers';
GuildMemberStructure,
GuildStructure,
MessageStructure,
WebhookMessageStructure,
} from '../client/transformers';
import type { CommandMetadata, ExtendContext, GlobalMetadata, RegisteredMiddlewares, UsingClient } from '../commands'; import type { CommandMetadata, ExtendContext, GlobalMetadata, RegisteredMiddlewares, UsingClient } from '../commands';
import { BaseContext } from '../commands/basecontext'; import { BaseContext } from '../commands/basecontext';
import type { import type {
@ -13,7 +8,6 @@ import type {
MakeRequired, MakeRequired,
ModalCreateBodyRequest, ModalCreateBodyRequest,
UnionToTuple, UnionToTuple,
When,
} from '../common'; } from '../common';
import { MessageFlags } from '../types'; import { MessageFlags } from '../types';
@ -61,15 +55,15 @@ export class ModalContext<M extends keyof RegisteredMiddlewares = never> extends
* @param fetchReply - Whether to fetch the reply or not. * @param fetchReply - Whether to fetch the reply or not.
*/ */
write<FR extends boolean = false>(body: InteractionCreateBodyRequest, fetchReply?: FR) { write<FR extends boolean = false>(body: InteractionCreateBodyRequest, fetchReply?: FR) {
return this.interaction.write(body, fetchReply); return this.interaction.write<FR>(body, fetchReply);
} }
/** /**
* Defers the reply to the interaction. * Defers the reply to the interaction.
* @param ephemeral - Whether the reply should be ephemeral or not. * @param ephemeral - Whether the reply should be ephemeral or not.
*/ */
deferReply(ephemeral = false) { deferReply<FR extends boolean = false>(ephemeral = false, fetchReply?: FR) {
return this.interaction.deferReply(ephemeral ? MessageFlags.Ephemeral : undefined); return this.interaction.deferReply<FR>(ephemeral ? MessageFlags.Ephemeral : undefined, fetchReply);
} }
/** /**
@ -88,8 +82,15 @@ export class ModalContext<M extends keyof RegisteredMiddlewares = never> extends
editOrReply<FR extends boolean = false>( editOrReply<FR extends boolean = false>(
body: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest, body: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest,
fetchReply?: FR, fetchReply?: FR,
): Promise<When<FR, WebhookMessageStructure | MessageStructure, void | WebhookMessageStructure | MessageStructure>> { ) {
return this.interaction.editOrReply(body as InteractionCreateBodyRequest, fetchReply); return this.interaction.editOrReply<FR>(body as InteractionCreateBodyRequest, fetchReply);
}
/**
* @returns A Promise that resolves to the fetched message
*/
fetchResponse() {
return this.interaction.fetchResponse();
} }
/** /**

View File

@ -173,8 +173,12 @@ export class WebhookMessage extends BaseMessage {
super(client, data); super(client, data);
} }
fetchWebhook() {
return this.client.webhooks.fetch(this.webhookId, this.webhookToken);
}
fetch() { fetch() {
return this.api.webhooks(this.webhookId)(this.webhookToken).get(); return this.client.webhooks.fetchMessage(this.webhookId, this.webhookToken, this.id, this.thread?.id);
} }
edit(body: EditMessageWebhook) { edit(body: EditMessageWebhook) {

View File

@ -48,9 +48,7 @@ export class VoiceState extends Base {
} }
async setSuppress(suppress = !this.suppress) { async setSuppress(suppress = !this.suppress) {
await this.client.proxy.guilds(this.guildId)['voice-states']['@me'].patch({ await this.client.voiceStates.setSuppress(this.guildId, suppress);
body: { suppress },
});
this.suppress = suppress; this.suppress = suppress;
} }
@ -58,10 +56,7 @@ export class VoiceState extends Base {
if (typeof date === 'string') date = new Date(date); if (typeof date === 'string') date = new Date(date);
if (Number.isNaN(date)) return Promise.reject('Invalid date'); if (Number.isNaN(date)) return Promise.reject('Invalid date');
date = date.toISOString(); date = date.toISOString();
await this.client.voiceStates.requestSpeak(this.guildId, date);
await this.client.proxy.guilds(this.guildId)['voice-states']['@me'].patch({
body: { request_to_speak_timestamp: date },
});
this.requestToSpeakTimestamp = date; this.requestToSpeakTimestamp = date;
} }