diff --git a/src/handlers/MESSAGE_UPDATE.ts b/src/handlers/MESSAGE_UPDATE.ts new file mode 100644 index 0000000..7b363e7 --- /dev/null +++ b/src/handlers/MESSAGE_UPDATE.ts @@ -0,0 +1,15 @@ +import Client from '../client/Client.ts'; +import { Payload } from '../constants/Payloads.ts'; +import { buildMessage } from '../utils/resolvers.ts'; +import { TextChannel } from '../models/channels/TextChannel.ts'; + +export default async (client: Client, payload: Payload) => { + + // Need to serialize the payload object into an actual message. + const msg = payload.d; + console.log('Message Update'); + console.log(msg); + // const message = await buildMessage(client, msg); + // const channel = client.channels.get(message.channel.id); + // channel.messages.set(message.id, message); +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index f35d676..b445d84 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,5 @@ import { Client } from "../mod.ts"; import "https://deno.land/x/dotenv/load.ts"; -import Guild from "./models/Guild.ts"; import Message from "./models/Message.ts"; import { MessageEmbed } from './models/embeds/Embeds.ts'; @@ -13,6 +12,7 @@ client.on("ready", () => { }); const embed = new MessageEmbed() + .setTitle('adasdd') .setDescription('Hello') .setColor(3313064) .setFooter('hello') @@ -20,23 +20,24 @@ const embed = new MessageEmbed() client.on("message", async (message: Message) => { - console.log(message.channel.messages.size); if (message.content === "?hello") { const msg = await message.channel.send("hello"); - // msg.delete(); + msg.delete(); } else if (message.content === '?react') { const reaction = await message.react('😂'); console.log(reaction); } else if (message.content === '?edit') { - const msg = await message.channel.send('Hello World'); - msg?.edit(embed); + const msg = await message.channel.send(embed); + console.log(msg); + setTimeout(async () => { + console.log('Editing...'); + embed.setTitle('HELLLOOOOOOOOOOOOO'); + await msg.edit(embed); + }, 5500); } else if (message.content === '?embed') { } }); -client.on("debug", (data: any) => { - console.log(data); -}); // deno run --allow-read --allow-net --allow-env --allow-hrtime ./src/index.ts diff --git a/src/models/Message.ts b/src/models/Message.ts index abe8a07..a7e2599 100644 --- a/src/models/Message.ts +++ b/src/models/Message.ts @@ -3,13 +3,17 @@ import User from "./User.ts"; import { TextChannel } from "./channels/TextChannel.ts"; import GuildMember from './GuildMember.ts'; import { MessageDeleteOptions } from '../typedefs/MessageOptions.ts'; -import { StatusCode } from '../constants/Constants.ts'; import { MessageReaction } from './MessageReaction.ts'; import { validateEmoji, checkGuildEmoji } from '../utils/checks.ts'; -import Emoji from './Emoji.ts'; import { MessageEmbed } from './embeds/Embeds.ts'; +import Collection from './Collection.ts'; export default class Message { + + private _embeds: Array = []; + private _attachments: Array = []; + private _reactions: Collection = new Collection(); + constructor( private _id: string, private _channel: TextChannel, @@ -21,12 +25,9 @@ export default class Message { 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 _type: number ) { } @@ -41,13 +42,16 @@ export default class Message { 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 embeds(): Array { return this._embeds; } + public get reactions(): Collection { 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; } + public set embeds(embeds: Array) { + this._embeds = embeds; + } public delete(options?: MessageDeleteOptions): Promise { return new Promise((resolve, reject) => { @@ -92,12 +96,12 @@ export default class Message { } public async edit(payload: string | MessageEmbed) { - if (typeof payload === 'string') { + console.log('Going to edit...'); + console.log(payload); + if (typeof payload === 'string') return this.channel.client.rest.editMessage({ content: payload }, this.channel.id, this.id); - } - if (payload instanceof MessageEmbed) { + if (payload instanceof MessageEmbed) return this.channel.client.rest.editMessage({ embed: payload }, this.channel.id, this.id); - } } } diff --git a/src/models/channels/TextChannel.ts b/src/models/channels/TextChannel.ts index 3eb2abc..3ea1988 100644 --- a/src/models/channels/TextChannel.ts +++ b/src/models/channels/TextChannel.ts @@ -58,9 +58,9 @@ export class TextChannel extends GuildChannel implements TextBasedChannel { const options: MessageOptions = { embed: payload } - console.log(JSON.stringify(options)); const response = await this.client.rest.createMessage(options, this.id); - return; + 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; diff --git a/src/models/embeds/MessageEmbed.ts b/src/models/embeds/MessageEmbed.ts index 9b6ebc3..cc47da5 100644 --- a/src/models/embeds/MessageEmbed.ts +++ b/src/models/embeds/MessageEmbed.ts @@ -22,7 +22,7 @@ export class MessageEmbed { private video?: MessageEmbedVideo; private provider?: MessageEmbedProvider; private author?: MessageEmbedAuthor; - private fields: Array = []; + private fields?: Array = []; constructor( title?: string, @@ -37,6 +37,7 @@ export class MessageEmbed { video?: MessageEmbedVideo, provider?: MessageEmbedProvider, author?: MessageEmbedAuthor, + fields: Array = [] ) { this.title = title; this.type = type; @@ -50,6 +51,7 @@ export class MessageEmbed { this.video = video; this.provider = provider; this.author = author; + this.fields = fields; } getTitle(): string | undefined { return this.title; } @@ -126,7 +128,7 @@ export class MessageEmbed { } addField(text: string, value: string, inline?: boolean): MessageEmbed { - this.fields.push(new MessageEmbedField(text,value,inline)); + this.fields!.push(new MessageEmbedField(text,value,inline)); return this; } } diff --git a/src/utils/resolvers.ts b/src/utils/resolvers.ts index d5c2a0b..af3f622 100644 --- a/src/utils/resolvers.ts +++ b/src/utils/resolvers.ts @@ -12,6 +12,7 @@ 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'; +import { MessageEmbed, MessageEmbedFooter, MessageEmbedImage, MessageEmbedThumbnail, MessageEmbedVideo, MessageEmbedProvider, MessageEmbedAuthor, MessageEmbedField } from '../models/embeds/Embeds.ts'; export function resolveChannels( client: Client, @@ -265,20 +266,21 @@ export async function buildMessage(client: Client, message_payload: any) { 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; + const { embeds, reactions, attachments } = message_payload; + const message: Message = buildMessageInstance(message_payload, channel, guild, user, member); + const messageEmbeds: Array = buildMessageEmbeds(embeds); + message.embeds = messageEmbeds; + return message; +} + +export function buildMessageInstance( + message_payload: any, + channel: TextChannel, + guild: Guild, + user: User, + member: GuildMember + ): Message { + const { id, content, timestamp, edited_timestamp, tts, mention_everyone, nonce, pinned, type } = message_payload; return new Message( id, channel, @@ -290,15 +292,47 @@ export async function buildMessage(client: Client, message_payload: any) { edited_timestamp, tts, mention_everyone, - attachments, - embeds, - reactions, nonce, pinned, - type, + type ); } +export function buildMessageReactions(reactions: Array) { + +} + +export function buildMessageEmbeds(embeds: Array): Array { + const msgEmbeds: Array = []; + for (const embed of embeds) { + let footer: MessageEmbedFooter = new MessageEmbedFooter(); + let image: MessageEmbedImage = new MessageEmbedImage(); + let thumbnail: MessageEmbedThumbnail = new MessageEmbedThumbnail(); + let video: MessageEmbedVideo = new MessageEmbedVideo(); + let provider: MessageEmbedProvider = new MessageEmbedProvider(); + let author: MessageEmbedAuthor = new MessageEmbedAuthor(); + let fields: Array = []; + if (embed?.footer) + footer = new MessageEmbedFooter(embed?.footer?.text, embed?.footer?.icon_url, embed?.footer?.proxy_icon_url); + if (embed?.image) + image = new MessageEmbedImage(embed?.image?.url, embed?.image?.proxy_url, embed?.image?.height, embed?.image?.width); + if (embed?.thumbnail) + thumbnail = new MessageEmbedThumbnail(embed?.thumbnail?.url, embed?.thumbnail?.proxy_url, embed?.thumbnail?.height, embed?.thumbnail?.width); + if (embed?.video) + video = new MessageEmbedVideo(embed.video?.url, embed.video?.height, embed.video?.width); + if (embed?.provider) + provider = new MessageEmbedProvider(embed.provider?.name, embed.provider?.url); + if (embed?.author) + author = new MessageEmbedAuthor(embed.author?.name, embed.author?.url, embed.author?.icon_url, embed.author?.proxy_icon_url); + if (embed?.fields) { + for (const field of embed.fields) + fields.push(new MessageEmbedField(field?.name, field?.value, field?.inline)); + } + msgEmbeds.push( + new MessageEmbed(embed?.title, embed?.type, embed?.description, embed?.url, embed?.timestamp, embed?.color, footer, image, thumbnail, video, provider, author, fields) + ); + } return msgEmbeds; +} export function getChannelType(type: number): ChannelType { if (type === 0) return ChannelType.TEXT; if (type === 1) return ChannelType.DM;