From 486fcbe2896a4dc530a554833126f005d1e89788 Mon Sep 17 00:00:00 2001 From: Yuzu Date: Fri, 1 Jul 2022 13:28:59 -0500 Subject: [PATCH] feat: Message.pin/unpin TextChannel.pinMessage/unpinMessage --- structures/Guild.ts | 28 ++++++++++++++++++---------- structures/Member.ts | 6 +++++- structures/Message.ts | 16 ++++++++++++++++ structures/TextChannel.ts | 8 ++++++++ structures/VoiceChannel.ts | 12 ++++++------ util/Routes.ts | 14 +++++++++----- 6 files changed, 62 insertions(+), 22 deletions(-) diff --git a/structures/Guild.ts b/structures/Guild.ts index 2f12982..1fde9c6 100644 --- a/structures/Guild.ts +++ b/structures/Guild.ts @@ -1,7 +1,13 @@ import type { Model } from "./Base.ts"; import type { Snowflake } from "../util/Snowflake.ts"; import type { Session } from "../session/Session.ts"; -import type { DiscordEmoji, DiscordGuild, DiscordMemberWithUser, DiscordInviteMetadata, DiscordRole } from "../vendor/external.ts"; +import type { + DiscordEmoji, + DiscordGuild, + DiscordInviteMetadata, + DiscordMemberWithUser, + DiscordRole, +} from "../vendor/external.ts"; import type { GetInvite } from "../util/Routes.ts"; import { DefaultMessageNotificationLevels, @@ -56,7 +62,7 @@ export interface CreateGuildBan { /** * @link https://discord.com/developers/docs/resources/guild#modify-guild-member - * */ + */ export interface ModifyGuildMember { nick?: string; roles?: Snowflake[]; @@ -68,11 +74,11 @@ export interface ModifyGuildMember { /** * @link https://discord.com/developers/docs/resources/guild#begin-guild-prune - * */ + */ export interface BeginGuildPrune { - days?: number; - computePruneCount?: boolean; - includeRoles?: Snowflake[]; + days?: number; + computePruneCount?: boolean; + includeRoles?: Snowflake[]; } /** @@ -110,8 +116,8 @@ export class Guild extends BaseGuild implements Model { emojis: GuildEmoji[]; /** - * 'null' would reset the nickname - * */ + * 'null' would reset the nickname + */ async editBotNickname(options: { nick: string | null; reason?: string }) { const result = await this.session.rest.runMethod<{ nick?: string } | undefined>( this.session.rest, @@ -286,8 +292,10 @@ export class Guild extends BaseGuild implements Model { mute: options.mute, deaf: options.deaf, channel_id: options.channelId, - communication_disabled_until: options.communicationDisabledUntil ? new Date(options.communicationDisabledUntil).toISOString() : undefined, - } + communication_disabled_until: options.communicationDisabledUntil + ? new Date(options.communicationDisabledUntil).toISOString() + : undefined, + }, ); return new Member(this.session, member, this.id); diff --git a/structures/Member.ts b/structures/Member.ts index 4603599..352331a 100644 --- a/structures/Member.ts +++ b/structures/Member.ts @@ -73,7 +73,11 @@ export class Member implements Model { } async edit(options: ModifyGuildMember): Promise { - const member = await Guild.prototype.editMember.call({ id: this.guildId, session: this.session }, this.user.id, options); + const member = await Guild.prototype.editMember.call( + { id: this.guildId, session: this.session }, + this.user.id, + options, + ); return member; } diff --git a/structures/Message.ts b/structures/Message.ts index 0505daf..98c9819 100644 --- a/structures/Message.ts +++ b/structures/Message.ts @@ -87,6 +87,22 @@ export class Message implements Model { return `https://discord.com/channels/${this.guildId ?? "@me"}/${this.channelId}/${this.id}`; } + async pin() { + await this.session.rest.runMethod( + this.session.rest, + "PUT", + Routes.CHANNEL_PIN(this.channelId, this.id), + ); + } + + async unpin() { + await this.session.rest.runMethod( + this.session.rest, + "DELETE", + Routes.CHANNEL_PIN(this.channelId, this.id), + ); + } + /** Edits the current message */ async edit({ content, allowedMentions, flags }: EditMessage): Promise { const message = await this.session.rest.runMethod( diff --git a/structures/TextChannel.ts b/structures/TextChannel.ts index a24dc29..aa090a2 100644 --- a/structures/TextChannel.ts +++ b/structures/TextChannel.ts @@ -107,6 +107,14 @@ export class TextChannel extends GuildChannel { Routes.CHANNEL_TYPING(this.id), ); } + + async pinMessage(messageId: Snowflake) { + await Message.prototype.pin.call({ id: messageId, channelId: this.id, session: this.session }); + } + + async unpinMessage(messageId: Snowflake) { + await Message.prototype.unpin.call({ id: messageId, channelId: this.id, session: this.session }); + } } export default TextChannel; diff --git a/structures/VoiceChannel.ts b/structures/VoiceChannel.ts index 2c65c9f..fa26b7e 100644 --- a/structures/VoiceChannel.ts +++ b/structures/VoiceChannel.ts @@ -7,12 +7,12 @@ import GuildChannel from "./GuildChannel.ts"; /** * @link https://discord.com/developers/docs/topics/gateway#update-voice-state - * */ + */ export interface UpdateVoiceState { - guildId: string; - channelId?: string; - selfMute: boolean; - selfDeaf: boolean; + guildId: string; + channelId?: string; + selfMute: boolean; + selfDeaf: boolean; } export class VoiceChannel extends GuildChannel { @@ -36,7 +36,7 @@ export class VoiceChannel extends GuildChannel { /** * This function was gathered from Discordeno it may not work - * */ + */ async connect(options?: UpdateVoiceState) { const shardId = calculateShardId(this.session.gateway, BigInt(super.guildId)); const shard = this.session.gateway.manager.shards.get(shardId); diff --git a/util/Routes.ts b/util/Routes.ts index 34638c7..7004e3f 100644 --- a/util/Routes.ts +++ b/util/Routes.ts @@ -46,10 +46,6 @@ export function MESSAGE_CREATE_THREAD(channelId: Snowflake, messageId: Snowflake return `/channels/${channelId}/messages/${messageId}/threads`; } -export function CHANNEL_PINS(channelId: Snowflake) { - return `/channels/${channelId}/pins`; -} - /** used to send messages */ export function CHANNEL_MESSAGES(channelId: Snowflake, options?: GetMessagesOptions) { let url = `/channels/${channelId}/messages?`; @@ -159,7 +155,7 @@ export function USER_NICK(guildId: Snowflake) { /** * @link https://discord.com/developers/docs/resources/guild#get-guild-prune-count - * */ + */ export interface GetGuildPruneCountQuery { days?: number; includeRoles?: Snowflake | Snowflake[]; @@ -173,3 +169,11 @@ export function GUILD_PRUNE(guildId: Snowflake, options?: GetGuildPruneCountQuer return url; } + +export function CHANNEL_PIN(channelId: Snowflake, messageId: Snowflake) { + return `/channels/${channelId}/pins/${messageId}`; +} + +export function CHANNEL_PINS(channelId: Snowflake) { + return `/channels/${channelId}/pins`; +}