From 11ca42fd8f97d0dfeca6621491c4d9116eb31ae0 Mon Sep 17 00:00:00 2001 From: Anson Foong Date: Thu, 21 May 2020 17:46:20 -0400 Subject: [PATCH] Build message instances and cache them whenever MESSAGE_CREATE event is fired --- src/handlers/MESSAGE_CREATE.ts | 71 ++---------------------------- src/index.ts | 24 +++------- src/models/Message.ts | 53 +++++++++++++--------- src/models/channels/TextChannel.ts | 22 ++++----- src/utils/resolvers.ts | 67 ++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 116 deletions(-) diff --git a/src/handlers/MESSAGE_CREATE.ts b/src/handlers/MESSAGE_CREATE.ts index 1ba3700..9186f3f 100644 --- a/src/handlers/MESSAGE_CREATE.ts +++ b/src/handlers/MESSAGE_CREATE.ts @@ -2,75 +2,12 @@ import Client from "../client/Client.ts"; import { Payload } from "../constants/Payloads.ts"; import { Events } from "../constants/Events.ts"; import { MessageType } from "../typedefs/MessageType.ts"; -import Message from "../models/Message.ts"; -import { BaseChannel } from "../models/channels/BaseChannel.ts"; -import Guild from "../models/Guild.ts"; -import User from "../models/User.ts"; -import { TextChannel } from "../models/channels/TextChannel.ts"; +import { buildMessage } from '../utils/resolvers.ts'; export default async function (client: Client, payload: Payload) { const { d: message_payload } = payload; - const { - channel_id, - guild_id, - id, - author, - content, - timestamp, - edited_timestamp, - tts, - mention_everyone, - attachments, - embeds, - reactions, - nonce, - pinned, - type, - } = message_payload; - - let channel = client.channels.get(channel_id); - let guild: Guild = client.guilds.get(guild_id); - let user: User = client.users.get(author.id); - if (!channel) { - const now = performance.now(); - channel = await client.rest.fetchChannel(channel_id); - const end = performance.now(); - console.log(`Took ${Math.round(end - now)}ms to fetch channel.`); - } - - if (!guild) { - const now = performance.now(); - guild = await client.rest.fetchGuild(guild_id); - const end = performance.now(); - console.log(`Took ${Math.round(end - now)}ms to fetch guild.`); - } - - if (!user) { - const now = performance.now(); - user = await client.rest.fetchUser(author.id); - const end = performance.now(); - console.log(`Took ${Math.round(end - now)}ms to fetch guild.`); - } - - const member = guild.members.get(author.id); - const message = new Message( - id, - channel, - guild, - user, - member, - content, - timestamp, - edited_timestamp, - tts, - mention_everyone, - attachments, - embeds, - reactions, - nonce, - pinned, - type, - ); - + const message = await buildMessage(client, message_payload); + message.channel.messages.set(message.id, message); + console.log('Cached Message ' + message.id); client.emit(Events.MESSAGE_CREATE, message); } diff --git a/src/index.ts b/src/index.ts index b9b0549..0abe010 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import "https://deno.land/x/dotenv/load.ts"; import Guild from "./models/Guild.ts"; import Message from "./models/Message.ts"; + const client = new Client(); client.login(Deno.env.get("BOT_TOKEN")!.toString()); @@ -10,25 +11,12 @@ client.on("ready", () => { console.log("Bot has logged in."); }); -client.on("message", (message: Message) => { +client.on("message", async (message: Message) => { + + console.log(message.channel.messages.size); if (message.content === "?hello") { - message.channel.send("hello"); - } else if (message.content === "?embed") { - message.channel.send({ - content: "Hello", - embed: { - title: "Hi", - description: "Yoooo", - }, - }); - } else if (message.content.toLowerCase() === 'helloworld') { - console.log('hello'); - message.channel.send({ - embed: { - title: 'This is an embed', - description: 'This is an embed', - } - }); + const msg = await message.channel.send("hello"); + console.log(msg); } }); diff --git a/src/models/Message.ts b/src/models/Message.ts index 0bcccb4..c1a6f05 100644 --- a/src/models/Message.ts +++ b/src/models/Message.ts @@ -1,33 +1,44 @@ -import { GuildChannel } from "./channels/GuildChannel.ts"; import Guild from "./Guild.ts"; import User from "./User.ts"; import { TextChannel } from "./channels/TextChannel.ts"; +import GuildMember from './GuildMember.ts'; export default class Message { constructor( - private id: string, + private _id: string, private _channel: TextChannel, - private guild: Guild, - private author: User, - private member: any, + private _guild: Guild, + private _author: User, + private _member: any, private _content: string, - private timestamp: Date, - private editedAt: Date, - private tts: boolean, - private mentionedEveryone: boolean, - private attachments: Array, - private embeds: Array, - private reactions: Array, - private nonce: number | string, - private pinned: boolean, - private type: number, + private _timestamp: Date, + private _editedAt: Date, + private _tts: boolean, + private _mentionedEveryone: boolean, + private _attachments: Array, + private _embeds: Array, + private _reactions: Array, + private _nonce: number | string, + private _pinned: boolean, + private _type: number, ) { - } - public get content(): string { - return this._content; - } - public get channel(): TextChannel { - return this._channel; } + + public get id(): string { return this._id; } + public get channel(): TextChannel { return this._channel; } + public get user(): User { return this._author; } + public get guild(): Guild { return this._guild; } + public get member(): GuildMember { return this._member; } + public get timestamp(): Date { return this._timestamp; } + public get editedAt(): Date { return this._editedAt; } + public get tts(): boolean { return this._tts; } + public get mentionedEveryone(): boolean { return this._mentionedEveryone; } + public get attachments(): Array { return this._attachments; } + public get embeds(): Array { return this._embeds; } + public get reactions(): Array { return this._reactions; } + public get nonce(): number | string { return this._nonce; } + public get pinned(): boolean { return this._pinned; } + public get type(): number { return this._type; } + public get content(): string { return this._content; } } diff --git a/src/models/channels/TextChannel.ts b/src/models/channels/TextChannel.ts index b33a9c8..61741b6 100644 --- a/src/models/channels/TextChannel.ts +++ b/src/models/channels/TextChannel.ts @@ -6,10 +6,12 @@ import TextBasedChannel from "../interfaces/ITextChannel.ts"; import { MessageOptions } from "../../typedefs/MessageOptions.ts"; import Collection from "../Collection.ts"; import Message from "../Message.ts"; +import { buildMessage } from '../../utils/resolvers.ts'; export class TextChannel extends GuildChannel implements TextBasedChannel { - private _messages: Collection = new Mes + private _messages: Collection = new Collection(); + constructor( _id: string, _client: Client, @@ -42,17 +44,17 @@ export class TextChannel extends GuildChannel implements TextBasedChannel { ); } - send(payload: string | MessageOptions): any { + get messages(): Collection { return this._messages; } + + async send(payload: string | MessageOptions) { if (typeof payload === "string") { const body: MessageOptions = { content: payload }; - const response = this.client.rest.createMessage(body, this.id); - return; - } - - if (payload) { - console.log(payload); - const response = this.client.rest.createMessage(payload, this.id); - return; + const response = await this.client.rest.createMessage(body, this.id); + response.guild_id = this.guild.id; + return await buildMessage(this.client, response); } + const response = await this.client.rest.createMessage(payload, this.id); + response.guild_id = this.guild.id; + return await buildMessage(this.client, response); } } diff --git a/src/utils/resolvers.ts b/src/utils/resolvers.ts index df15fcd..ca44e24 100644 --- a/src/utils/resolvers.ts +++ b/src/utils/resolvers.ts @@ -11,6 +11,7 @@ import { TextChannel } from "../models/channels/TextChannel.ts"; import { CategoryChannel } from "../models/channels/CategoryChannel.ts"; import { VoiceChannel } from "../models/channels/VoiceChannel.ts"; import { BaseChannel } from "../models/channels/BaseChannel.ts"; +import Message from '../models/Message.ts'; export function resolveChannels( client: Client, @@ -249,6 +250,72 @@ export function buildTextChannel(client: Client, guild: Guild, c: any) { export function buildGroupDMChannel(client: Client, guild: Guild, c: any) { } +export async function buildMessage(client: Client, message_payload: any) { + + const { + channel_id, + guild_id, + author, + } = message_payload; + + let channel = client.channels.get(channel_id); + let guild: Guild = client.guilds.get(guild_id); + let user: User = client.users.get(author.id); + if (!channel) { + const now = performance.now(); + channel = await client.rest.fetchChannel(channel_id); + const end = performance.now(); + console.log(`Took ${Math.round(end - now)}ms to fetch channel.`); + } + + if (!guild) { + const now = performance.now(); + guild = await client.rest.fetchGuild(guild_id); + const end = performance.now(); + console.log(`Took ${Math.round(end - now)}ms to fetch guild.`); + } + + if (!user) { + const now = performance.now(); + user = await client.rest.fetchUser(author.id); + const end = performance.now(); + console.log(`Took ${Math.round(end - now)}ms to fetch guild.`); + } + const member = guild.members.get(author.id); + const { + id, + content, + timestamp, + edited_timestamp, + tts, + mention_everyone, + attachments, + embeds, + reactions, + nonce, + pinned, + type, + } = message_payload; + return new Message( + id, + channel, + guild, + user, + member, + content, + timestamp, + edited_timestamp, + tts, + mention_everyone, + attachments, + embeds, + reactions, + nonce, + pinned, + type, + ); +} + export function getChannelType(type: number): ChannelType { if (type === 0) return ChannelType.TEXT; if (type === 1) return ChannelType.DM;