From 08b8df41fa424c7ebbcfc5a3b057cd9daf87e265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Susa=C3=B1a?= Date: Thu, 21 Jul 2022 03:49:52 -0400 Subject: [PATCH] fix(types): Literal Types (#77) * fix(Message): delete() params * feat: fetches guilds items * fix(types): literal types * fix --- packages/api-types/shared.ts | 2 +- packages/biscuit/Routes.ts | 4 +- packages/biscuit/Session.ts | 20 +++++++--- packages/biscuit/Snowflake.ts | 2 +- packages/biscuit/Util.ts | 6 +-- packages/biscuit/structures/GuildEmoji.ts | 4 +- packages/biscuit/structures/Integration.ts | 4 +- packages/biscuit/structures/Member.ts | 21 +++++----- packages/biscuit/structures/Message.ts | 21 +++++++--- .../biscuit/structures/MessageReaction.ts | 10 ++--- packages/biscuit/structures/Role.ts | 8 ++-- packages/biscuit/structures/StageInstance.ts | 9 ++++- packages/biscuit/structures/ThreadMember.ts | 10 ++--- packages/biscuit/structures/User.ts | 9 ++++- packages/biscuit/structures/Webhook.ts | 16 +++++--- packages/biscuit/structures/Widget.ts | 17 ++++----- .../builders/slash/ApplicationCommand.ts | 4 +- .../components/ActionRowComponent.ts | 4 +- .../structures/components/ButtonComponent.ts | 8 ++-- .../structures/components/Component.ts | 6 ++- packages/biscuit/structures/guilds.ts | 38 ++++++++++++------- .../interactions/BaseInteraction.ts | 15 +++++--- .../interactions/CommandInteraction.ts | 16 ++++---- .../interactions/ModalSubmitInteraction.ts | 4 +- packages/cache/mod.ts | 1 - 25 files changed, 158 insertions(+), 101 deletions(-) diff --git a/packages/api-types/shared.ts b/packages/api-types/shared.ts index 4c619d8..d7929a6 100644 --- a/packages/api-types/shared.ts +++ b/packages/api-types/shared.ts @@ -1238,7 +1238,7 @@ export type CamelCase = S extends `${infer P1}_${infer P2}${in export type Camelize = { // deno-lint-ignore ban-types [K in keyof T as CamelCase]: T[K] extends Array ? U extends {} ? Array> - : T[K] + : T[K] : // deno-lint-ignore ban-types T[K] extends {} ? Camelize : never; diff --git a/packages/biscuit/Routes.ts b/packages/biscuit/Routes.ts index 439edc7..8fe6a2c 100644 --- a/packages/biscuit/Routes.ts +++ b/packages/biscuit/Routes.ts @@ -457,7 +457,7 @@ export function GUILD_VOICE_REGIONS(guildId: Snowflake): string { /** * @link https://discord.com/developers/docs/resources/guild#get-guild-vanity-url * @param guildId The guild - * @returns Get vanity URL + * @returns Get vanity URL */ export function GUILD_VANITY(guildId: Snowflake): string { return `/guilds/${guildId}/vanity-url`; @@ -470,4 +470,4 @@ export function GUILD_VANITY(guildId: Snowflake): string { */ export function GUILD_PREVIEW(guildId: Snowflake): string { return `/guilds/${guildId}/preview`; -} \ No newline at end of file +} diff --git a/packages/biscuit/Session.ts b/packages/biscuit/Session.ts index ea5343b..414aee6 100644 --- a/packages/biscuit/Session.ts +++ b/packages/biscuit/Session.ts @@ -37,6 +37,14 @@ export type DiscordRawEventHandler = (shard: Shard, data: DiscordGatewayPayload) // INTERACTIONS +export type CreateApplicationCommands = CreateApplicationCommand | CreateContextApplicationCommand; +export type UpsertDataApplicationCommands = + | AtLeastOne + | AtLeastOne; +export type LastCreateApplicationCommands = + | AtLeastOne + | AtLeastOne; + /** * @link https://discord.com/developers/docs/interactions/application-commands#endpoints-json-params */ @@ -133,7 +141,7 @@ export class Session extends EventEmitter { this.#applicationId = id; } - get applicationId() { + get applicationId(): Snowflake { return this.#applicationId!; } @@ -141,7 +149,7 @@ export class Session extends EventEmitter { this.#botId = id; } - get botId() { + get botId(): Snowflake { return this.#botId; } @@ -273,7 +281,7 @@ export class Session extends EventEmitter { } createApplicationCommand( - options: CreateApplicationCommand | CreateContextApplicationCommand, + options: CreateApplicationCommands, guildId?: Snowflake, ): Promise { return this.rest.runMethod( @@ -368,7 +376,7 @@ export class Session extends EventEmitter { upsertApplicationCommand( id: Snowflake, - options: AtLeastOne | AtLeastOne, + options: UpsertDataApplicationCommands, guildId?: Snowflake, ): Promise { return this.rest.runMethod( @@ -392,7 +400,7 @@ export class Session extends EventEmitter { } upsertApplicationCommands( - options: Array, + options: UpsertDataApplicationCommands[], guildId?: Snowflake, ): Promise { return this.rest.runMethod( @@ -428,7 +436,7 @@ export class Session extends EventEmitter { } // deno-fmt-ignore - isContextApplicationCommand(cmd: AtLeastOne | AtLeastOne): cmd is AtLeastOne { + isContextApplicationCommand(cmd: LastCreateApplicationCommands): cmd is AtLeastOne { return cmd.type === ApplicationCommandTypes.Message || cmd.type === ApplicationCommandTypes.User; } diff --git a/packages/biscuit/Snowflake.ts b/packages/biscuit/Snowflake.ts index cbf100a..bed3406 100644 --- a/packages/biscuit/Snowflake.ts +++ b/packages/biscuit/Snowflake.ts @@ -6,7 +6,7 @@ export const DiscordEpoch = 14200704e5; /** utilities for Snowflakes */ export const Snowflake = { - snowflakeToTimestamp(id: Snowflake) { + snowflakeToTimestamp(id: Snowflake): number { return (Number(id) >> 22) + DiscordEpoch; }, }; diff --git a/packages/biscuit/Util.ts b/packages/biscuit/Util.ts index bf83135..dbbd8b0 100644 --- a/packages/biscuit/Util.ts +++ b/packages/biscuit/Util.ts @@ -71,15 +71,15 @@ export type ImageSize = 16 | 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096; * Utility functions */ export class Util { - static formatImageURL(url: string, size: ImageSize = 128, format?: ImageFormat) { + static formatImageURL(url: string, size: ImageSize = 128, format?: ImageFormat): string { return `${url}.${format || (url.includes('/a_') ? 'gif' : 'jpg')}?size=${size}`; } - static iconHashToBigInt(hash: string) { + static iconHashToBigInt(hash: string): bigint { return BigInt('0x' + (hash.startsWith('a_') ? `a${hash.substring(2)}` : `b${hash}`)); } - static iconBigintToHash(icon: bigint) { + static iconBigintToHash(icon: bigint): string { const hash: string = icon.toString(16); return hash.startsWith('a') ? `a_${hash.substring(1)}` : hash.substring(1); diff --git a/packages/biscuit/structures/GuildEmoji.ts b/packages/biscuit/structures/GuildEmoji.ts index f9b66ba..f91fabd 100644 --- a/packages/biscuit/structures/GuildEmoji.ts +++ b/packages/biscuit/structures/GuildEmoji.ts @@ -35,8 +35,8 @@ export class GuildEmoji extends Emoji implements Model { return emoji; } - async delete({ reason }: { reason?: string } = {}): Promise { - await Guild.prototype.deleteEmoji.call({ id: this.guildId, session: this.session }, this.id, { reason }); + async delete(reason?: string): Promise { + await Guild.prototype.deleteEmoji.call({ id: this.guildId, session: this.session }, this.id, reason); return this; } diff --git a/packages/biscuit/structures/Integration.ts b/packages/biscuit/structures/Integration.ts index 183d008..5834d0c 100644 --- a/packages/biscuit/structures/Integration.ts +++ b/packages/biscuit/structures/Integration.ts @@ -4,6 +4,8 @@ import type { Session } from '../Session.ts'; import type { DiscordIntegration, IntegrationExpireBehaviors } from '../../discordeno/mod.ts'; import User from './User.ts'; +export type IntegrationTypes = 'twitch' | 'youtube' | 'discord'; + export interface IntegrationAccount { id: Snowflake; name: string; @@ -58,7 +60,7 @@ export class Integration implements Model { guildId?: Snowflake; name: string; - type: 'twitch' | 'youtube' | 'discord'; + type: IntegrationTypes; enabled?: boolean; syncing?: boolean; roleId?: string; diff --git a/packages/biscuit/structures/Member.ts b/packages/biscuit/structures/Member.ts index 4329241..f997ae9 100644 --- a/packages/biscuit/structures/Member.ts +++ b/packages/biscuit/structures/Member.ts @@ -2,11 +2,10 @@ import type { Model } from './Base.ts'; import type { Snowflake } from '../Snowflake.ts'; import type { Session } from '../Session.ts'; import type { DiscordMemberWithUser } from '../../discordeno/mod.ts'; -import type { ImageFormat, ImageSize } from '../Util.ts'; import type { CreateGuildBan, ModifyGuildMember } from './guilds.ts'; import { Guild } from './guilds.ts'; import Util from '../Util.ts'; -import User from './User.ts'; +import { avatarOptions, User } from './User.ts'; import * as Routes from '../Routes.ts'; /** @@ -91,7 +90,11 @@ export class Member implements Model { /** kicks a member from this guild */ async kick(options: { reason?: string }): Promise { - await Guild.prototype.kickMember.call({ id: this.guildId, session: this.session }, this.user.id, options); + await Guild.prototype.kickMember.call( + { id: this.guildId, session: this.session }, + this.user.id, + options.reason, + ); return this; } @@ -113,8 +116,8 @@ export class Member implements Model { } /** adds a role to this member */ - async addRole(roleId: Snowflake, options: { reason?: string } = {}): Promise { - await Guild.prototype.addRole.call({ id: this.guildId, session: this.session }, this.user.id, roleId, options); + async addRole(roleId: Snowflake, reason?: string): Promise { + await Guild.prototype.addRole.call({ id: this.guildId, session: this.session }, this.user.id, roleId, reason); } /** removes a role from this member */ @@ -123,16 +126,16 @@ export class Member implements Model { { id: this.guildId, session: this.session }, this.user.id, roleId, - options, + options.reason, ); } /** gets the members's guild avatar, not to be confused with Member.user.avatarURL() */ - avatarURL(options: { format?: ImageFormat; size?: ImageSize } = { size: 128 }): string { + avatarURL(options: avatarOptions): string { let url: string; if (this.user.bot) { - return this.user.avatarURL(); + return this.user.avatarURL(options); } if (!this.avatarHash) { @@ -141,7 +144,7 @@ export class Member implements Model { url = Routes.USER_AVATAR(this.user.id, Util.iconBigintToHash(this.avatarHash)); } - return Util.formatImageURL(url, options.size, options.format); + return Util.formatImageURL(url, options.size ?? 128, options.format); } toString(): string { diff --git a/packages/biscuit/structures/Message.ts b/packages/biscuit/structures/Message.ts index de6134a..6b33543 100644 --- a/packages/biscuit/structures/Message.ts +++ b/packages/biscuit/structures/Message.ts @@ -28,6 +28,18 @@ import InteractionFactory from './interactions/InteractionFactory.ts'; import * as Routes from '../Routes.ts'; import { StickerItem } from './Sticker.ts'; +export type GuildMessage = Message & { guildId: Snowflake }; +export type WebhookMessage = Message & { + author: Partial; + webhook: WebhookAuthor; + member: undefined; +}; + +export interface MessageActivity { + partyId?: Snowflake; + type: MessageActivityTypes; +} + /** * @link https://discord.com/developers/docs/resources/channel#allowed-mentions-object */ @@ -290,10 +302,7 @@ export class Message implements Model { application?: Partial; /** sent with Rich Presence-related chat embeds */ - activity?: { - partyId?: Snowflake; - type: MessageActivityTypes; - }; + activity?: MessageActivity; /** gets the timestamp of this message, this does not requires the timestamp field */ get createdTimestamp(): number { @@ -537,12 +546,12 @@ export class Message implements Model { } /** wheter the message comes from a guild **/ - inGuild(): this is Message & { guildId: Snowflake } { + inGuild(): this is GuildMessage { return !!this.guildId; } /** wheter the messages comes from a Webhook */ - isWebhookMessage(): this is Message & { author: Partial; webhook: WebhookAuthor; member: undefined } { + isWebhookMessage(): this is WebhookMessage { return !!this.webhook; } } diff --git a/packages/biscuit/structures/MessageReaction.ts b/packages/biscuit/structures/MessageReaction.ts index d0c2807..ecc5c77 100644 --- a/packages/biscuit/structures/MessageReaction.ts +++ b/packages/biscuit/structures/MessageReaction.ts @@ -3,10 +3,10 @@ import type { DiscordMemberWithUser, DiscordMessageReactionAdd, DiscordReaction import Emoji from './Emoji.ts'; import Member from './Member.ts'; -/** - * Represents when a new reaction was added to a message. +/** + * Represents when a new reaction was added to a message. * @link https://discord.com/developers/docs/topics/gateway#message-reaction-add - * */ + */ export interface MessageReactionAdd { userId: string; channelId: string; @@ -16,7 +16,7 @@ export interface MessageReactionAdd { emoji: Partial; } -/** +/** * Represents when a reaction was removed from a message. * Equal to MessageReactionAdd but without 'member' property. * @see {@link MessageReactionAdd} @@ -30,7 +30,7 @@ export type MessageReactionRemove = Omit; * @see {@link MessageReactionAdd} * @link https://discord.com/developers/docs/topics/gateway#message-reaction-remove-all */ -export type MessageReactionRemoveAll = Pick +export type MessageReactionRemoveAll = Pick; /** * Represents when a reaction-emoji was removed from a message. diff --git a/packages/biscuit/structures/Role.ts b/packages/biscuit/structures/Role.ts index 00d57f8..faaec46 100644 --- a/packages/biscuit/structures/Role.ts +++ b/packages/biscuit/structures/Role.ts @@ -56,12 +56,12 @@ export class Role implements Model { return role; } - async add(memberId: Snowflake, options: { reason?: string } = {}): Promise { - await Guild.prototype.addRole.call({ id: this.guildId, session: this.session }, memberId, this.id, options); + async add(memberId: Snowflake, reason?: string): Promise { + await Guild.prototype.addRole.call({ id: this.guildId, session: this.session }, memberId, this.id, reason); } - async remove(memberId: Snowflake, options: { reason?: string } = {}): Promise { - await Guild.prototype.removeRole.call({ id: this.guildId, session: this.session }, memberId, this.id, options); + async remove(memberId: Snowflake, reason?: string): Promise { + await Guild.prototype.removeRole.call({ id: this.guildId, session: this.session }, memberId, this.id, reason); } toString(): string { diff --git a/packages/biscuit/structures/StageInstance.ts b/packages/biscuit/structures/StageInstance.ts index 0dbd52e..f3069fc 100644 --- a/packages/biscuit/structures/StageInstance.ts +++ b/packages/biscuit/structures/StageInstance.ts @@ -15,6 +15,11 @@ export enum PrivacyLevels { GuildOnly = 2, } +export type stageEditOptions = { + topic?: string; + privacy?: PrivacyLevels; +}; + export class StageInstance implements Model { constructor(session: Session, data: DiscordStageInstanceB) { this.session = session; @@ -39,14 +44,14 @@ export class StageInstance implements Model { discoverableDisabled: boolean; guildScheduledEventId: Snowflake; - async edit(options: { topic?: string; privacyLevel?: PrivacyLevels }): Promise { + async edit(options: stageEditOptions): Promise { const stageInstance = await this.session.rest.runMethod( this.session.rest, 'PATCH', Routes.STAGE_INSTANCE(this.id), { topic: options.topic, - privacy_level: options.privacyLevel, + privacy_level: options.privacy, }, ); diff --git a/packages/biscuit/structures/ThreadMember.ts b/packages/biscuit/structures/ThreadMember.ts index 43e402a..7bffab2 100644 --- a/packages/biscuit/structures/ThreadMember.ts +++ b/packages/biscuit/structures/ThreadMember.ts @@ -7,7 +7,7 @@ import * as Routes from '../Routes.ts'; /** * A member that comes from a thread * @link https://discord.com/developers/docs/resources/channel#thread-member-object - * * */ + * **/ export class ThreadMember implements Model { constructor(session: Session, data: DiscordThreadMember) { this.session = session; @@ -25,19 +25,19 @@ export class ThreadMember implements Model { return this.id; } - async quitThread(memberId: Snowflake = this.session.botId): Promise { + async quitThread(memberId?: Snowflake): Promise { await this.session.rest.runMethod( this.session.rest, 'DELETE', - Routes.THREAD_USER(this.id, memberId), + Routes.THREAD_USER(this.id, memberId ?? this.session.botId), ); } - async fetchMember(memberId: Snowflake = this.session.botId): Promise { + async fetchMember(memberId?: Snowflake): Promise { const member = await this.session.rest.runMethod( this.session.rest, 'GET', - Routes.THREAD_USER(this.id, memberId), + Routes.THREAD_USER(this.id, memberId ?? this.session.botId), ); return new ThreadMember(this.session, member); diff --git a/packages/biscuit/structures/User.ts b/packages/biscuit/structures/User.ts index 090d491..03ce3bf 100644 --- a/packages/biscuit/structures/User.ts +++ b/packages/biscuit/structures/User.ts @@ -6,6 +6,11 @@ import type { ImageFormat, ImageSize } from '../Util.ts'; import Util from '../Util.ts'; import * as Routes from '../Routes.ts'; +export type avatarOptions = { + format?: ImageFormat; + size?: ImageSize; +}; + /** * @link https://discord.com/developers/docs/resources/user#user-object * Represents a user @@ -88,7 +93,7 @@ export class User implements Model { } /** gets the user's avatar */ - avatarURL(options: { format?: ImageFormat; size?: ImageSize } = { size: 128 }): string { + avatarURL(options: avatarOptions): string { let url: string; if (!this.avatarHash) { @@ -97,7 +102,7 @@ export class User implements Model { url = Routes.USER_AVATAR(this.id, Util.iconBigintToHash(this.avatarHash)); } - return Util.formatImageURL(url, options.size, options.format); + return Util.formatImageURL(url, options.size ?? 128, options.format); } toString(): string { diff --git a/packages/biscuit/structures/Webhook.ts b/packages/biscuit/structures/Webhook.ts index a56d651..9c87960 100644 --- a/packages/biscuit/structures/Webhook.ts +++ b/packages/biscuit/structures/Webhook.ts @@ -17,6 +17,10 @@ import User from './User.ts'; import Message from './Message.ts'; import * as Routes from '../Routes.ts'; +export type executeWebhookOptions = WebhookOptions & CreateMessage & { avatarUrl?: string; username?: string }; + +export type editMessageWithThread = EditWebhookMessage & { threadId?: Snowflake }; + /** * @link https://discord.com/developers/docs/resources/webhook#edit-webhook-message-jsonform-params */ @@ -68,7 +72,7 @@ export class Webhook implements Model { user?: User; async execute( - options?: WebhookOptions & CreateMessage & { avatarUrl?: string; username?: string }, + options?: executeWebhookOptions, ): Promise<(Message | undefined)> { if (!this.token) { return; @@ -110,7 +114,7 @@ export class Webhook implements Model { return new Webhook(this.session, message); } - async fetchMessage(messageId: Snowflake, options?: { threadId?: Snowflake }): Promise { + async fetchMessage(messageId: Snowflake, threadId?: Snowflake): Promise { if (!this.token) { return; } @@ -118,13 +122,13 @@ export class Webhook implements Model { const message = await this.session.rest.runMethod( this.session.rest, 'GET', - Routes.WEBHOOK_MESSAGE(this.id, this.token, messageId, options), + Routes.WEBHOOK_MESSAGE(this.id, this.token, messageId, { threadId }), ); return new Message(this.session, message); } - async deleteMessage(messageId: Snowflake, options?: { threadId?: Snowflake }): Promise { + async deleteMessage(messageId: Snowflake, threadId?: Snowflake): Promise { if (!this.token) { throw new Error('No token found'); } @@ -132,13 +136,13 @@ export class Webhook implements Model { await this.session.rest.runMethod( this.session.rest, 'DELETE', - Routes.WEBHOOK_MESSAGE(this.id, this.token, messageId, options), + Routes.WEBHOOK_MESSAGE(this.id, this.token, messageId, { threadId }), ); } async editMessage( messageId?: Snowflake, - options?: EditWebhookMessage & { threadId?: Snowflake }, + options?: editMessageWithThread, ): Promise { if (!this.token) { throw new Error('No token found'); diff --git a/packages/biscuit/structures/Widget.ts b/packages/biscuit/structures/Widget.ts index d5dda2d..b4bf739 100644 --- a/packages/biscuit/structures/Widget.ts +++ b/packages/biscuit/structures/Widget.ts @@ -1,7 +1,7 @@ -import { DiscordGuildWidget } from "../../api-types/discord.ts"; -import { Session } from "../Session.ts"; -import { Snowflake } from "../Snowflake.ts"; -import { Model } from "./Base.ts"; +import { DiscordGuildWidget } from '../../api-types/discord.ts'; +import { Session } from '../Session.ts'; +import { Snowflake } from '../Snowflake.ts'; +import { Model } from './Base.ts'; import { PartialChannel } from './channels.ts'; export interface WidgetMember { @@ -19,17 +19,16 @@ export class Widget implements Model { this.name = data.name; this.instantInvite = data.instant_invite; this.channels = data.channels; - this.members = data.members.map(x => { + this.members = data.members.map((x) => { return { id: x.id, username: x.username, avatar: x.avatar, status: x.status, - avatarURL: x.avatar_url - } + avatarURL: x.avatar_url, + }; }); this.presenceCount = data.presence_count; - } session: Session; id: Snowflake; @@ -38,4 +37,4 @@ export class Widget implements Model { channels: PartialChannel[]; members: WidgetMember[]; presenceCount: number; -} \ No newline at end of file +} diff --git a/packages/biscuit/structures/builders/slash/ApplicationCommand.ts b/packages/biscuit/structures/builders/slash/ApplicationCommand.ts index 204d1e8..29b94c3 100644 --- a/packages/biscuit/structures/builders/slash/ApplicationCommand.ts +++ b/packages/biscuit/structures/builders/slash/ApplicationCommand.ts @@ -58,6 +58,8 @@ export abstract class ApplicationCommandBuilder implements CreateApplicationComm } } +export type MessageApplicationCommandBuilderJSON = { name: string; type: ApplicationCommandTypes.Message }; + export class MessageApplicationCommandBuilder { type: ApplicationCommandTypes; name?: string; @@ -73,7 +75,7 @@ export class MessageApplicationCommandBuilder { return (this.name = name), this; } - toJSON(): { name: string; type: ApplicationCommandTypes.Message } { + toJSON(): MessageApplicationCommandBuilderJSON { if (!this.name) throw new TypeError('Propety \'name\' is required'); return { diff --git a/packages/biscuit/structures/components/ActionRowComponent.ts b/packages/biscuit/structures/components/ActionRowComponent.ts index faf76a3..ffe7115 100644 --- a/packages/biscuit/structures/components/ActionRowComponent.ts +++ b/packages/biscuit/structures/components/ActionRowComponent.ts @@ -1,6 +1,6 @@ import type { Session } from '../../Session.ts'; import type { DiscordComponent, DiscordInputTextComponent } from '../../../discordeno/mod.ts'; -import type { ActionRowComponent, Component } from './Component.ts'; +import type { ActionRowComponent, ComponentsWithoutRow } from './Component.ts'; import { ButtonStyles, MessageComponentTypes } from '../../../discordeno/mod.ts'; import BaseComponent from './Component.ts'; import Button from './ButtonComponent.ts'; @@ -33,7 +33,7 @@ export class ActionRow extends BaseComponent implements ActionRowComponent { readonly session: Session; override type: MessageComponentTypes.ActionRow; - components: Array>; + components: ComponentsWithoutRow[]; } export default ActionRow; diff --git a/packages/biscuit/structures/components/ButtonComponent.ts b/packages/biscuit/structures/components/ButtonComponent.ts index 0ec9c80..f0b445f 100644 --- a/packages/biscuit/structures/components/ButtonComponent.ts +++ b/packages/biscuit/structures/components/ButtonComponent.ts @@ -1,6 +1,6 @@ import type { Session } from '../../Session.ts'; -import type { ButtonStyles, DiscordComponent } from '../../../discordeno/mod.ts'; -import type { ButtonComponent } from './Component.ts'; +import type { DiscordComponent } from '../../../discordeno/mod.ts'; +import type { ButtonComponent, ClassicButton } from './Component.ts'; import { MessageComponentTypes } from '../../../discordeno/mod.ts'; import BaseComponent from './Component.ts'; import Emoji from '../Emoji.ts'; @@ -13,7 +13,7 @@ export class Button extends BaseComponent implements ButtonComponent { this.type = data.type as MessageComponentTypes.Button; this.customId = data.custom_id; this.label = data.label; - this.style = data.style as number; + this.style = data.style as ClassicButton; this.disabled = data.disabled; if (data.emoji) { @@ -25,7 +25,7 @@ export class Button extends BaseComponent implements ButtonComponent { override type: MessageComponentTypes.Button; customId?: string; label?: string; - style: ButtonStyles.Primary | ButtonStyles.Secondary | ButtonStyles.Success | ButtonStyles.Danger; + style: ClassicButton; disabled?: boolean; emoji?: Emoji; } diff --git a/packages/biscuit/structures/components/Component.ts b/packages/biscuit/structures/components/Component.ts index 66b199c..3bacd2d 100644 --- a/packages/biscuit/structures/components/Component.ts +++ b/packages/biscuit/structures/components/Component.ts @@ -40,9 +40,13 @@ export type Component = | TextInputComponent; /** Button Component */ +export type ClassicButton = Exclude; + +export type ComponentsWithoutRow = Exclude; + export interface ButtonComponent { type: MessageComponentTypes.Button; - style: ButtonStyles.Primary | ButtonStyles.Secondary | ButtonStyles.Success | ButtonStyles.Danger; + style: ClassicButton; label?: string; emoji?: Emoji; customId?: string; diff --git a/packages/biscuit/structures/guilds.ts b/packages/biscuit/structures/guilds.ts index e1be44e..03cd330 100644 --- a/packages/biscuit/structures/guilds.ts +++ b/packages/biscuit/structures/guilds.ts @@ -6,6 +6,7 @@ import type { DiscordBan, DiscordEmoji, DiscordGuild, + DiscordGuildPreview, DiscordGuildWidget, DiscordGuildWidgetSettings, DiscordInvite, @@ -21,7 +22,6 @@ import type { SystemChannelFlags, VerificationLevels, VideoQualityModes, - DiscordGuildPreview } from '../../discordeno/mod.ts'; import type { ImageFormat, ImageSize } from '../Util.ts'; import { GuildFeatures, PremiumTiers } from '../../discordeno/mod.ts'; @@ -228,11 +228,11 @@ export class GuildPreview implements Model { this.iconHash = data.icon ? Util.iconHashToBigInt(data.icon) : undefined; this.splashHash = data.splash ? Util.iconHashToBigInt(data.splash) : undefined; this.discoverySplashHash = data.discovery_splash ? Util.iconHashToBigInt(data.discovery_splash) : undefined; - this.emojis = data.emojis.map(x => new GuildEmoji(this.session, x, this.id)); + this.emojis = data.emojis.map((x) => new GuildEmoji(this.session, x, this.id)); this.features = data.features; this.approximateMemberCount = data.approximate_member_count; this.approximatePresenceCount = data.approximate_presence_count; - this.stickers = data.stickers.map(x => new Sticker(this.session, x)); + this.stickers = data.stickers.map((x) => new Sticker(this.session, x)); } session: Session; /** guild id */ @@ -254,9 +254,16 @@ export class GuildPreview implements Model { description?: string; /** custom guild stickers */ stickers: Sticker[]; -} +} + +// Guild +/** Maximun custom guild emojis per level */ +export type maximunEmojis = 50 | 100 | 150 | 250; +/** Maximun custom guild stickers per level */ +export type maximunStickers = 5 | 15 | 30 | 60; + +export type editBotNickOptions = { nick: string | null; reason?: string }; -/** Guild */ export interface CreateRole { name?: string; color?: number; @@ -541,7 +548,7 @@ export class Guild extends BaseGuild implements Model { /** * Returns the maximum number of emoji slots */ - get maxEmojis(): 50 | 100 | 150 | 250 { + get maxEmojis(): maximunEmojis { switch (this.premiumTier) { case 1: return 100; @@ -557,7 +564,7 @@ export class Guild extends BaseGuild implements Model { /** * Returns the maximum number of custom sticker slots */ - get maxStickers(): 5 | 15 | 30 | 60 { + get maxStickers(): maximunStickers { switch (this.premiumTier) { case 1: return 15; @@ -574,7 +581,7 @@ export class Guild extends BaseGuild implements Model { * edits the bot's nickname in the guild. * 'null' would reset the nickname. */ - async editBotNickname(options: { nick: string | null; reason?: string }): Promise<(string | undefined)> { + async editBotNickname(options: editBotNickOptions): Promise<(string | undefined)> { const result = await this.session.rest.runMethod<{ nick?: string } | undefined>( this.session.rest, 'PATCH', @@ -612,7 +619,7 @@ export class Guild extends BaseGuild implements Model { * @param id - The id of the emoji to delete. * @param reason - The reason for deleting the emoji. */ - async deleteEmoji(id: Snowflake, { reason }: { reason?: string } = {}): Promise { + async deleteEmoji(id: Snowflake, reason?: string): Promise { await this.session.rest.runMethod( this.session.rest, 'DELETE', @@ -711,7 +718,7 @@ export class Guild extends BaseGuild implements Model { * @param roleId - The id of the role to add. * @param reason - The reason for adding the role to the member. */ - async addRole(memberId: Snowflake, roleId: Snowflake, { reason }: { reason?: string } = {}): Promise { + async addRole(memberId: Snowflake, roleId: Snowflake, reason?: string): Promise { await this.session.rest.runMethod( this.session.rest, 'PUT', @@ -726,7 +733,7 @@ export class Guild extends BaseGuild implements Model { * @param roleId - The id of the role to remove. * @param reason - The reason for removing the role from the member. */ - async removeRole(memberId: Snowflake, roleId: Snowflake, { reason }: { reason?: string } = {}): Promise { + async removeRole(memberId: Snowflake, roleId: Snowflake, reason?: string): Promise { await this.session.rest.runMethod( this.session.rest, 'DELETE', @@ -823,7 +830,7 @@ export class Guild extends BaseGuild implements Model { * @param memberId - The id of the member to kick. * @param reason - The reason for kicking the member. */ - async kickMember(memberId: Snowflake, { reason }: { reason?: string }): Promise { + async kickMember(memberId: Snowflake, reason?: string): Promise { await this.session.rest.runMethod( this.session.rest, 'DELETE', @@ -1157,10 +1164,13 @@ export class Guild extends BaseGuild implements Model { * @returns Resolves a Guild Preview object */ async fetchGuildPreview(): Promise { - const preview = await this.session.rest.runMethod(this.session.rest, 'GET', Routes.GUILD_PREVIEW(this.id)); + const preview = await this.session.rest.runMethod( + this.session.rest, + 'GET', + Routes.GUILD_PREVIEW(this.id), + ); return new GuildPreview(this.session, preview); } - } export default Guild; diff --git a/packages/biscuit/structures/interactions/BaseInteraction.ts b/packages/biscuit/structures/interactions/BaseInteraction.ts index 579c9fc..23ef0f1 100644 --- a/packages/biscuit/structures/interactions/BaseInteraction.ts +++ b/packages/biscuit/structures/interactions/BaseInteraction.ts @@ -18,6 +18,9 @@ import Permsisions from '../Permissions.ts'; import Webhook from '../Webhook.ts'; import * as Routes from '../../Routes.ts'; +export type InteractionResponseWith = { with: InteractionApplicationCommandCallbackData }; +export type InteractionResponseWithData = InteractionResponse | InteractionResponseWith; + /** * @link https://discord.com/developers/docs/interactions/slash-commands#interaction-response */ @@ -185,25 +188,25 @@ export abstract class BaseInteraction implements Model { return message; } - async deleteFollowUp(messageId: Snowflake, options?: { threadId?: Snowflake }): Promise { + async deleteFollowUp(messageId: Snowflake, threadId?: Snowflake): Promise { await Webhook.prototype.deleteMessage.call( { id: this.session.applicationId, token: this.token, }, messageId, - options, + threadId, ); } - async fetchFollowUp(messageId: Snowflake, options?: { threadId?: Snowflake }): Promise { + async fetchFollowUp(messageId: Snowflake, threadId?: Snowflake): Promise { const message = await Webhook.prototype.fetchMessage.call( { id: this.session.applicationId, token: this.token, }, messageId, - options, + threadId, ); return message; @@ -213,9 +216,9 @@ export abstract class BaseInteraction implements Model { // deno-fmt-ignore async respond(resp: InteractionResponse): Promise; - async respond(resp: { with: InteractionApplicationCommandCallbackData }): Promise; + async respond(resp: InteractionResponseWith): Promise; async respond( - resp: InteractionResponse | { with: InteractionApplicationCommandCallbackData }, + resp: InteractionResponseWithData, ): Promise { const options = 'with' in resp ? resp.with : resp.data; const type = 'type' in resp ? resp.type : InteractionResponseTypes.ChannelMessageWithSource; diff --git a/packages/biscuit/structures/interactions/CommandInteraction.ts b/packages/biscuit/structures/interactions/CommandInteraction.ts index b6609ca..5ad0e7b 100644 --- a/packages/biscuit/structures/interactions/CommandInteraction.ts +++ b/packages/biscuit/structures/interactions/CommandInteraction.ts @@ -15,6 +15,14 @@ import Member from '../Member.ts'; import Message from '../Message.ts'; import Role from '../Role.ts'; +export interface CommandInteractionDataResolved { + users: Map; + members: Map; + roles: Map; + attachments: Map; + messages: Map; +} + export class CommandInteraction extends BaseInteraction implements Model { constructor(session: Session, data: DiscordInteraction) { super(session, data); @@ -69,13 +77,7 @@ export class CommandInteraction extends BaseInteraction implements Model { commandName: string; commandType: ApplicationCommandTypes; commandGuildId?: Snowflake; - resolved: { - users: Map; - members: Map; - roles: Map; - attachments: Map; - messages: Map; - }; + resolved: CommandInteractionDataResolved; options: CommandInteractionOptionResolver; } diff --git a/packages/biscuit/structures/interactions/ModalSubmitInteraction.ts b/packages/biscuit/structures/interactions/ModalSubmitInteraction.ts index 98a667a..bc7ec05 100644 --- a/packages/biscuit/structures/interactions/ModalSubmitInteraction.ts +++ b/packages/biscuit/structures/interactions/ModalSubmitInteraction.ts @@ -10,6 +10,8 @@ import type { import BaseInteraction from './BaseInteraction.ts'; import Message from '../Message.ts'; +export type ModalInMessage = ModalSubmitInteraction & { message: Message }; + export class ModalSubmitInteraction extends BaseInteraction implements Model { constructor(session: Session, data: DiscordInteraction) { super(session, data); @@ -46,7 +48,7 @@ export class ModalSubmitInteraction extends BaseInteraction implements Model { }; } - inMessage(): this is ModalSubmitInteraction & { message: Message } { + inMessage(): this is ModalInMessage { return !!this.message; } } diff --git a/packages/cache/mod.ts b/packages/cache/mod.ts index bc4d972..d8514e0 100644 --- a/packages/cache/mod.ts +++ b/packages/cache/mod.ts @@ -75,4 +75,3 @@ export function enableCache(session: Session): SessionCache { return cache; } -