From bd9ae6cfddf4782ea95958cca3a6bae3f302a5be Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Tue, 23 Nov 2021 11:59:02 +0000 Subject: [PATCH] fix: thread transfomer is uselss --- src/bot.ts | 3 -- .../threads/startThreadWithMessage.ts | 4 +- src/transformers/channel.ts | 47 +++++++++---------- src/transformers/message.ts | 6 +-- src/transformers/thread.ts | 45 ------------------ 5 files changed, 28 insertions(+), 77 deletions(-) delete mode 100644 src/transformers/thread.ts diff --git a/src/bot.ts b/src/bot.ts index 0b3809bd8..da873c02a 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -86,7 +86,6 @@ import { transformAttachment } from "./transformers/attachment.ts"; import { transformEmbed } from "./transformers/embed.ts"; import { transformComponent } from "./transformers/component.ts"; import { AsyncCache, AsyncCacheHandler, Cache, CacheHandler, createCache, TableNames } from "./cache.ts"; -import { transformThread } from "./transformers/thread.ts"; import { transformWebhook } from "./transformers/webhook.ts"; import { transformAuditlogEntry } from "./transformers/auditlogEntry.ts"; import { transformApplicationCommandPermission } from "./transformers/applicationCommandPermission.ts"; @@ -813,7 +812,6 @@ export interface Transformers { attachment: typeof transformAttachment; embed: typeof transformEmbed; component: typeof transformComponent; - thread: typeof transformThread; webhook: typeof transformWebhook; auditlogEntry: typeof transformAuditlogEntry; applicationCommandPermission: typeof transformApplicationCommandPermission; @@ -839,7 +837,6 @@ export function createTransformers(options: Partial) { role: options.role || transformRole, user: options.user || transformUser, team: options.team || transformTeam, - thread: options.thread || transformThread, voiceState: options.voiceState || transformVoiceState, snowflake: options.snowflake || snowflakeToBigint, webhook: options.webhook || transformWebhook, diff --git a/src/helpers/channels/threads/startThreadWithMessage.ts b/src/helpers/channels/threads/startThreadWithMessage.ts index 15ebe9f51..f62ba20cf 100644 --- a/src/helpers/channels/threads/startThreadWithMessage.ts +++ b/src/helpers/channels/threads/startThreadWithMessage.ts @@ -9,7 +9,7 @@ export async function startThreadWithMessage( messageId: bigint, options: StartThreadWithMessage ) { - return await bot.rest.runMethod( + const result = await bot.rest.runMethod( bot.rest, "post", bot.constants.endpoints.THREAD_START_PUBLIC(channelId, messageId), @@ -18,4 +18,6 @@ export async function startThreadWithMessage( auto_archive_duration: options.autoArchiveDuration, } ); + + return bot.transformers.channel(bot, { channel: result, guildId: bot.transformers.snowflake(result.guild_id!) }); } diff --git a/src/transformers/channel.ts b/src/transformers/channel.ts index a0046d603..a74edcb88 100644 --- a/src/transformers/channel.ts +++ b/src/transformers/channel.ts @@ -1,24 +1,12 @@ import { Channel } from "../types/channels/channel.ts"; import { Bot } from "../bot.ts"; import { SnakeCasedPropertiesDeep } from "../types/util.ts"; -import { DiscordOverwrite } from "../types/channels/overwrite.ts"; import { ChannelTypes } from "../types/channels/channelTypes.ts"; import type { DiscordenoVoiceState } from "./voiceState.ts"; import { Collection } from "../util/collection.ts"; import { DiscordenoUser } from "./member.ts"; import { VideoQualityModes } from "../types/channels/videoQualityModes.ts"; -// function merge(allow: string, deny: string, id: string, type: number) { -// return BigInt(`0x${type}g${BigInt(id)}g${BigInt(allow).toString(16)}g${BigInt(deny).toString(16)}`); -// } - -// export function separateOverwrites(thing: bigint) { -// return thing -// .toString(16) -// .split("g") -// .map((x, index) => index ? BigInt(`0x${x}`) : Number(x)) as [number, bigint, bigint, bigint]; -// } - const Mask = (1n << 64n) - 1n; export function packOverwrites(allow: string, deny: string, id: string, type: number) { @@ -75,6 +63,16 @@ export function transformChannel( parentId: payload.channel.parent_id ? bot.transformers.snowflake(payload.channel.parent_id) : undefined, // TODO: stage channels? voiceStates: payload.channel.type === ChannelTypes.GuildVoice ? new Collection() : undefined, + + memberCount: payload.channel.member_count, + messageCount: payload.channel.message_count, + archiveTimestamp: payload.channel.thread_metadata?.archive_timestamp + ? Date.parse(payload.channel.thread_metadata.archive_timestamp) + : undefined, + autoArchiveDuration: payload.channel.thread_metadata?.auto_archive_duration, + botIsMember: Boolean(payload.channel.member), + archived: payload.channel.thread_metadata?.archived, + locked: payload.channel.thread_metadata?.locked, }; } @@ -111,23 +109,22 @@ export interface DiscordenoChannel { /** An approximate count of users in a thread, stops counting at 50 */ memberCount?: number; /** Thread-specifig fields not needed by other channels */ - threadMetadata?: { - /** Whether the thread is archived */ - archived: boolean; - /** Duration in minutes to automatically archive the thread after recent activity */ - autoArchiveDuration: 60 | 1440 | 4320 | 10080; - // TODO(threads): channel struct should convert this to a unixx - /** Timestamp when the thread's archive status was last changed, used for calculating recent activity */ - archiveTimestamp: string; - /** When a thread is locked, only users with `MANAGE_THREADS` can unarchive it */ - locked?: boolean; - /** whether non-moderators can add other non-moderators to a thread; only available on private threads */ - invitable?: boolean; - }; + /** Whether the thread is archived */ + archived?: boolean; + /** Duration in minutes to automatically archive the thread after recent activity */ + autoArchiveDuration?: 60 | 1440 | 4320 | 10080; + /** Timestamp when the thread's archive status was last changed, used for calculating recent activity */ + archiveTimestamp?: number; + /** When a thread is locked, only users with `MANAGE_THREADS` can unarchive it */ + locked?: boolean; + /** whether non-moderators can add other non-moderators to a thread; only available on private threads */ + invitable?: boolean; /** The time the current user last joined the thread */ threadJoinTimestamp?: number; /** Default duration for newly created threads, in minutes, to automatically archive the thread after recent activity, can be set to: 60, 1440, 4320, 10080 */ defaultAutoArchiveDuration?: number; + /** Whether or not the bot is part of this channel thread. */ + botIsMember: boolean; /** computed permissions for the invoking user in the channel, including overwrites, only included when part of the resolved data received on a slash command interaction */ permissions?: bigint; diff --git a/src/transformers/message.ts b/src/transformers/message.ts index 66023f831..3b22d8b9d 100644 --- a/src/transformers/message.ts +++ b/src/transformers/message.ts @@ -11,7 +11,7 @@ import { MessageActivityTypes } from "../types/messages/messageActivityTypes.ts" import { InteractionTypes } from "../types/interactions/interactionTypes.ts"; import { DiscordenoComponent } from "./component.ts"; import { Application } from "../types/applications/application.ts"; -import { DiscordenoThread } from "./thread.ts"; +import { DiscordenoChannel } from "./channel.ts"; export function transformMessage(bot: Bot, payload: SnakeCasedPropertiesDeep): DiscordenoMessage { const guildId = payload.guild_id ? bot.transformers.snowflake(payload.guild_id) : undefined; @@ -53,7 +53,7 @@ export function transformMessage(bot: Bot, payload: SnakeCasedPropertiesDeep bot.transformers.component(bot, component)), stickerItems: payload.sticker_items?.map((sticker) => ({ id: bot.transformers.snowflake(sticker.id), @@ -195,7 +195,7 @@ export interface DiscordenoMessage { user: DiscordenoUser; }; /** The thread that was started from this message, includes thread member object */ - thread?: DiscordenoThread; + thread?: DiscordenoChannel; /** The components related to this message */ components?: DiscordenoComponent[]; } diff --git a/src/transformers/thread.ts b/src/transformers/thread.ts deleted file mode 100644 index 761c1ab72..000000000 --- a/src/transformers/thread.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { Bot } from "../bot.ts"; -import { Channel } from "../types/channels/channel.ts"; -import { ChannelTypes } from "../types/channels/channelTypes.ts"; -import { SnakeCasedPropertiesDeep } from "../types/util.ts"; - -export function transformThread(bot: Bot, channel: SnakeCasedPropertiesDeep): DiscordenoThread { - if ( - channel.type !== ChannelTypes.GuildNewsThread && - channel.type !== ChannelTypes.GuildPublicThread && - channel.type !== ChannelTypes.GuildPrivateThread - ) - throw new Error("Cannot convert non-thread channel to a thread."); - - return { - name: channel.name || "", - id: bot.transformers.snowflake(channel.id), - type: channel.type, - parentId: bot.transformers.snowflake(channel.parent_id!), - memberCount: channel.member_count || 1, - messageCount: channel.message_count || 1, - archiveTimestamp: channel.thread_metadata?.archive_timestamp - ? Date.parse(channel.thread_metadata.archive_timestamp) - : undefined, - autoArchiveDuration: channel.thread_metadata?.auto_archive_duration || 0, - ownerId: bot.transformers.snowflake(channel.owner_id!), - botIsMember: Boolean(channel.member), - archived: channel.thread_metadata?.archived, - locked: channel.thread_metadata?.locked, - }; -} - -export interface DiscordenoThread { - id: bigint; - name: string; - type: ChannelTypes.GuildNewsThread | ChannelTypes.GuildPublicThread | ChannelTypes.GuildPrivateThread; - parentId: bigint; - memberCount: number; - messageCount: number; - archiveTimestamp?: number; - autoArchiveDuration: number; - archived?: boolean; - locked?: boolean; - ownerId: bigint; - botIsMember: boolean; -}