feat: add getters for Template

This commit is contained in:
ayntee
2020-12-19 13:52:48 +04:00
parent e082a5d693
commit d08adff0dd
9 changed files with 141 additions and 50 deletions

View File

@@ -1,4 +1,8 @@
{
"deno.enable": true,
"editor.defaultFormatter": "denoland.vscode-deno",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
}

View File

@@ -4,7 +4,6 @@ import {
ChannelCreatePayload,
ChannelType,
RawOverwrite,
Unpromise,
} from "../types/types.ts";
import { cache } from "../utils/cache.ts";
import { Message } from "./message.ts";

View File

@@ -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<ReturnType<typeof createGuild>> {}
export interface Guild {}

View File

@@ -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<Member> = {
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<Member> = {
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<string, GuildMember>;
// METHODS
/** Returns the guild for this guildID */
guild(guildID: string): Guild | undefined;
/** Get the nickname or the username if no nickname */

View File

@@ -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<Message> = {
"@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<Message> = {
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<Message>;
/** 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<void>;
alert(
content: string | MessageContent,
timeout?: number,
reason?: string,
): Promise<void>;
/** 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<unknown>;
alertResponse(
content: string | MessageContent,
timeout?: number,
reason?: string,
): Promise<unknown>;
/** Remove all reactions */
removeAllReactions(): Promise<unknown>;
/** Remove all reactions */
removeReactionEmoji(reaction: string): Promise<any>;
/** Remove all reactions */
removeReaction(reaction: string): Promise<any>;
}
}

View File

@@ -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 = {

View File

@@ -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<string, Partial<PropertyDescriptor>> = {};
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<typeof createTemplate> {}
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<Guild>;
/** whether the template has unsynced changes */
isDirty: boolean | null;
// GETTERS
sourceGuild: Guild | undefined;
}

View File

@@ -1,3 +0,0 @@
export type Unpromise<T extends Promise<unknown>> = T extends Promise<infer K>
? K
: never;

View File

@@ -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<PropertyDescriptor> {
return { configurable: true, enumerable: true, writable: true, value };
}