From d08adff0dd643b5206779304e73f421f11976f38 Mon Sep 17 00:00:00 2001 From: ayntee Date: Sat, 19 Dec 2020 13:52:48 +0400 Subject: [PATCH] feat: add getters for Template --- .vscode/settings.json | 6 +++- src/structures/channel.ts | 1 - src/structures/guild.ts | 8 ++--- src/structures/member.ts | 26 ++++++++++---- src/structures/message.ts | 73 +++++++++++++++++++++++++++++--------- src/structures/mod.ts | 5 +-- src/structures/template.ts | 67 +++++++++++++++++++++++++++------- src/types/misc.ts | 3 -- src/utils/utils.ts | 2 +- 9 files changed, 141 insertions(+), 50 deletions(-) delete mode 100644 src/types/misc.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index 24b5136ca..8b3b2fa17 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,8 @@ { "deno.enable": true, - + "editor.defaultFormatter": "denoland.vscode-deno", + "editor.formatOnSave": true, + "editor.codeActionsOnSave": { + "source.organizeImports": true + } } \ No newline at end of file diff --git a/src/structures/channel.ts b/src/structures/channel.ts index 4d4823a9f..545a341b7 100644 --- a/src/structures/channel.ts +++ b/src/structures/channel.ts @@ -4,7 +4,6 @@ import { ChannelCreatePayload, ChannelType, RawOverwrite, - Unpromise, } from "../types/types.ts"; import { cache } from "../utils/cache.ts"; import { Message } from "./message.ts"; diff --git a/src/structures/guild.ts b/src/structures/guild.ts index f0c9ab4aa..ced66d717 100644 --- a/src/structures/guild.ts +++ b/src/structures/guild.ts @@ -1,8 +1,4 @@ -import { - CreateGuildPayload, - MemberCreatePayload, - Unpromise, -} from "../types/types.ts"; +import { CreateGuildPayload, MemberCreatePayload } from "../types/types.ts"; import { Collection } from "../utils/collection.ts"; import { structures } from "./mod.ts"; @@ -99,4 +95,4 @@ export async function createGuild(data: CreateGuildPayload, shardID: number) { return guild; } -export interface Guild extends Unpromise> {} +export interface Guild {} diff --git a/src/structures/member.ts b/src/structures/member.ts index d29a2a61a..c4dc53cbc 100644 --- a/src/structures/member.ts +++ b/src/structures/member.ts @@ -1,7 +1,19 @@ import { cacheHandlers } from "../controllers/cache.ts"; import { ban } from "../handlers/guild.ts"; -import { addRole, editMember, kick, removeRole, sendDirectMessage } from "../handlers/member.ts"; -import { BanOptions, EditMemberOptions, GuildMember, MemberCreatePayload, MessageContent, Unpromise } from "../types/types.ts"; +import { + addRole, + editMember, + kick, + removeRole, + sendDirectMessage, +} from "../handlers/member.ts"; +import { + BanOptions, + EditMemberOptions, + GuildMember, + MemberCreatePayload, + MessageContent, +} from "../types/types.ts"; import { cache } from "../utils/cache.ts"; import { Collection } from "../utils/collection.ts"; import { createNewProp } from "../utils/utils.ts"; @@ -17,16 +29,16 @@ const baseMember: Partial = { return this.guildMember!(guildID)?.nick || this.username!; }, guildMember(guildID) { - return this.guilds?.get(guildID) + return this.guilds?.get(guildID); }, sendDM(content) { return sendDirectMessage(this.id!, content); }, kick(guildID, reason) { - return kick(guildID, this.id!, reason) + return kick(guildID, this.id!, reason); }, edit(guildID, options) { - return editMember(guildID, this.id!, options) + return editMember(guildID, this.id!, options); }, ban(guildID, options) { return ban(guildID, this.id!, options); @@ -37,7 +49,7 @@ const baseMember: Partial = { removeRole(guildID, roleID, reason) { return removeRole(guildID, this.id!, roleID, reason); }, -} +}; export async function createMember(data: MemberCreatePayload, guildID: string) { const { @@ -128,7 +140,7 @@ export interface Member { guilds: Collection; // METHODS - + /** Returns the guild for this guildID */ guild(guildID: string): Guild | undefined; /** Get the nickname or the username if no nickname */ diff --git a/src/structures/message.ts b/src/structures/message.ts index 75ccbc7d8..c502dc683 100644 --- a/src/structures/message.ts +++ b/src/structures/message.ts @@ -1,7 +1,28 @@ import { Channel } from "../../mod.ts"; import { sendMessage } from "../handlers/channel.ts"; -import { addReaction, addReactions, deleteMessageByID, editMessage, pin, removeAllReactions, removeReaction, removeReactionEmoji } from "../handlers/message.ts"; -import { Activity, Application, Attachment, Embed, GuildMember, MessageContent, MessageCreateOptions, MessageSticker, Reaction, Reference, Unpromise, UserPayload } from "../types/types.ts"; +import { + addReaction, + addReactions, + deleteMessageByID, + editMessage, + pin, + removeAllReactions, + removeReaction, + removeReactionEmoji, +} from "../handlers/message.ts"; +import { + Activity, + Application, + Attachment, + Embed, + GuildMember, + MessageContent, + MessageCreateOptions, + MessageSticker, + Reaction, + Reference, + UserPayload, +} from "../types/types.ts"; import { cache } from "../utils/cache.ts"; import { createNewProp } from "../utils/utils.ts"; import { Guild } from "./guild.ts"; @@ -26,24 +47,30 @@ const baseMessage: Partial = { "@me"}/${this.channelID}/${this.id}`; }, get mentionedRoles() { - return this.mentionRoleIDs?.map(id => this.guild?.roles.get(id)) || []; + // TODO: add getters for Guild structure, that will fix this error + return this.mentionRoleIDs?.map((id) => this.guild?.roles.get(id)) || []; }, get mentionedChannels() { - return this.mentionChannelIDs?.map(id => cache.channels.get(id)) || []; + return this.mentionChannelIDs?.map((id) => cache.channels.get(id)) || []; }, get mentionedMembers() { - return this.mentions?.map(id => cache.members.get(id)) || [] + return this.mentions?.map((id) => cache.members.get(id)) || []; }, // METHODS delete(reason, delayMilliseconds) { - return deleteMessageByID(this.channelID!, this.id!, reason, delayMilliseconds); + return deleteMessageByID( + this.channelID!, + this.id!, + reason, + delayMilliseconds, + ); }, edit(content) { return editMessage(this as Message, content); }, pin() { - return pin(this.channelID!, this.id!) + return pin(this.channelID!, this.id!); }, addReaction(reaction) { return addReaction(this.channelID!, this.id!, reaction); @@ -59,17 +86,21 @@ const baseMessage: Partial = { mentions: { ...(content.mentions || {}), repliedUser: true }, replyMessageID: this.id, }; - + return sendMessage(this.channelID!, contentWithMention); }, reply(content) { return sendMessage(this.channelID!, content); }, alert(content, timeout = 10, reason = "") { - return sendMessage(this.channelID!, content).then(response => response.delete(reason, timeout * 1000).catch(console.error)) + return sendMessage(this.channelID!, content).then((response) => + response.delete(reason, timeout * 1000).catch(console.error) + ); }, alertResponse(content, timeout = 10, reason = "") { - return this.sendResponse!(content).then(response => response.delete(reason, timeout * 1000).catch(console.error)) + return this.sendResponse!(content).then((response) => + response.delete(reason, timeout * 1000).catch(console.error) + ); }, removeAllReactions() { return removeAllReactions(this.channelID!, this.id!); @@ -111,11 +142,13 @@ export async function createMessage(data: MessageCreateOptions) { mentions: createNewProp(data.mentions.map((m) => m.id)), mentionsEveryone: createNewProp(mentionsEveryone), mentionRoleIDs: createNewProp(mentionRoleIDs), - mentionChannelIDs: createNewProp(mentionChannelIDs?.map(m => m.id) || []), + mentionChannelIDs: createNewProp(mentionChannelIDs?.map((m) => m.id) || []), webhookID: createNewProp(webhookID), messageReference: createNewProp(messageReference), timestamp: createNewProp(Date.parse(data.timestamp)), - editedTimestamp: createNewProp(editedTimestamp ? Date.parse(editedTimestamp) : undefined), + editedTimestamp: createNewProp( + editedTimestamp ? Date.parse(editedTimestamp) : undefined, + ), }); return message; @@ -172,7 +205,7 @@ export interface Message { stickers?: MessageSticker[]; /** The message id of the original message if this message was sent as a reply. If null, the original message was deleted. */ referencedMessageID?: MessageCreateOptions | null; - + // GETTERS /** The guild of this message. Can be undefined if not in cache or in DM */ @@ -207,13 +240,21 @@ export interface Message { /** Send a message to this channel where this message is */ reply(content: string | MessageContent): Promise; /** Send a message to this channel and then delete it after a bit. By default it will delete after 10 seconds with no reason provided. */ - alert(content: string | MessageContent, timeout?: number, reason?: string): Promise; + alert( + content: string | MessageContent, + timeout?: number, + reason?: string, + ): Promise; /** Send a inline reply to this message but then delete it after a bit. By default it will delete after 10 seconds with no reason provided. */ - alertResponse(content: string | MessageContent, timeout?: number, reason?: string): Promise; + alertResponse( + content: string | MessageContent, + timeout?: number, + reason?: string, + ): Promise; /** Remove all reactions */ removeAllReactions(): Promise; /** Remove all reactions */ removeReactionEmoji(reaction: string): Promise; /** Remove all reactions */ removeReaction(reaction: string): Promise; -} \ No newline at end of file +} diff --git a/src/structures/mod.ts b/src/structures/mod.ts index abbdea3b7..1a44569c4 100644 --- a/src/structures/mod.ts +++ b/src/structures/mod.ts @@ -17,9 +17,10 @@ export let structures = { export type Structures = typeof structures; -/** This function is used to update/reload/customize the internal structure of Discordeno. +/** This function is used to update/reload/customize the internal structures of Discordeno. * - * ⚠️ **ADVANCED USE ONLY: If you customize this in a wrong way, you could potentially create many new errors/bugs. Please take caution when using this. + * ⚠️ **ADVANCED USE ONLY: If you customize this incorrectly, you could potentially create many new errors/bugs. + * Please take caution when using this.** */ export function updateStructures(newStructures: Structures) { structures = { diff --git a/src/structures/template.ts b/src/structures/template.ts index bb199a20b..5aa6a2650 100644 --- a/src/structures/template.ts +++ b/src/structures/template.ts @@ -1,4 +1,13 @@ -import { GuildTemplate } from "../types/types.ts"; +import { GuildTemplate, UserPayload } from "../types/types.ts"; +import { cache } from "../utils/cache.ts"; +import { createNewProp } from "../utils/utils.ts"; +import { Guild } from "./guild.ts"; + +const baseTemplate: any = { + get sourceGuild() { + return cache.guilds.get(this.sourceGuildID); + }, +}; export function createTemplate( data: GuildTemplate, @@ -12,20 +21,52 @@ export function createTemplate( serialized_source_guild: serializedSourceGuild, is_dirty: isDirty, ...rest - } = data; + }: { [key: string]: any } = data; - const template = { - ...rest, - usageCount, - creatorID, - createdAt, - updatedAt, - sourceGuildID, - serializedSourceGuild, - isDirty, - }; + const restProps: Record> = {}; + for (const key of Object.keys(rest)) { + restProps[key] = createNewProp(rest[key]); + } + + const template = Object.create(baseTemplate, { + ...restProps, + usageCount: createNewProp(sourceGuildID), + creatorID: createNewProp(creatorID), + createdAt: createNewProp(createdAt), + updatedAt: createNewProp(updatedAt), + sourceGuildID: createNewProp(sourceGuildID), + serializedSourceGuild: createNewProp(serializedSourceGuild), + isDirty: createNewProp(isDirty), + }); return template; } -export interface Template extends ReturnType {} +export interface Template { + /** the template code (unique ID) */ + code: string; + /** template name */ + name: string; + /** the description for the template */ + description: string | null; + /** number of times this template has been used */ + usageCount: number; + /** the ID of the user who created the template */ + createdID: string; + /** the user who created the template */ + creator: UserPayload; + /** when this template was created */ + createdAt: string; + /** when this template was last synced to the source guild */ + updatedAt: string; + /** the ID of the guild this template is based on */ + sourceGuildID: string; + /** the guild snapshot this template contains */ + serializedSourceGuild: Partial; + /** whether the template has unsynced changes */ + isDirty: boolean | null; + + // GETTERS + + sourceGuild: Guild | undefined; +} diff --git a/src/types/misc.ts b/src/types/misc.ts deleted file mode 100644 index 84bb25051..000000000 --- a/src/types/misc.ts +++ /dev/null @@ -1,3 +0,0 @@ -export type Unpromise> = T extends Promise - ? K - : never; diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 1c70309d8..7842637ff 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -34,6 +34,6 @@ export async function urlToBase64(url: string) { } /** Allows easy way to add a prop to a base object when needing to use complicated getters solution. */ -export function createNewProp(value: any) { +export function createNewProp(value: any): Partial { return { configurable: true, enumerable: true, writable: true, value }; }