Skip to content

Commit

Permalink
feat(embeds): Serialize all embeds in Message instace
Browse files Browse the repository at this point in the history
  • Loading branch information
stuyy committed May 23, 2020
1 parent d3a5e54 commit 80ca5ca
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 42 deletions.
15 changes: 15 additions & 0 deletions src/handlers/MESSAGE_UPDATE.ts
Original file line number Diff line number Diff line change
@@ -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 = <TextChannel>client.channels.get(message.channel.id);
// channel.messages.set(message.id, message);
}
17 changes: 9 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -13,30 +12,32 @@ client.on("ready", () => {
});

const embed = new MessageEmbed()
.setTitle('adasdd')
.setDescription('Hello')
.setColor(3313064)
.setFooter('hello')
.addField('Hi', 'Hello');

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
28 changes: 16 additions & 12 deletions src/models/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MessageEmbed> = [];
private _attachments: Array<any> = [];
private _reactions: Collection<string, MessageReaction> = new Collection();

constructor(
private _id: string,
private _channel: TextChannel,
Expand All @@ -21,12 +25,9 @@ export default class Message {
private _editedAt: Date,
private _tts: boolean,
private _mentionedEveryone: boolean,
private _attachments: Array<any>,
private _embeds: Array<any>,
private _reactions: Array<any>,
private _nonce: number | string,
private _pinned: boolean,
private _type: number,
private _type: number
) {

}
Expand All @@ -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<any> { return this._attachments; }
public get embeds(): Array<any> { return this._embeds; }
public get reactions(): Array<any> { return this._reactions; }
public get embeds(): Array<MessageEmbed> { return this._embeds; }
public get reactions(): Collection<string, MessageReaction> { 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<MessageEmbed>) {
this._embeds = embeds;
}

public delete(options?: MessageDeleteOptions): Promise<Message> {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/models/channels/TextChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/models/embeds/MessageEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class MessageEmbed {
private video?: MessageEmbedVideo;
private provider?: MessageEmbedProvider;
private author?: MessageEmbedAuthor;
private fields: Array<MessageEmbedField> = [];
private fields?: Array<MessageEmbedField> = [];

constructor(
title?: string,
Expand All @@ -37,6 +37,7 @@ export class MessageEmbed {
video?: MessageEmbedVideo,
provider?: MessageEmbedProvider,
author?: MessageEmbedAuthor,
fields: Array<MessageEmbedField> = []
) {
this.title = title;
this.type = type;
Expand All @@ -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; }
Expand Down Expand Up @@ -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;
}
}
70 changes: 52 additions & 18 deletions src/utils/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<MessageEmbed> = 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,
Expand All @@ -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<any>) {

}

export function buildMessageEmbeds(embeds: Array<any>): Array<MessageEmbed> {
const msgEmbeds: Array<MessageEmbed> = [];
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<MessageEmbedField> = [];
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;
Expand Down

0 comments on commit 80ca5ca

Please sign in to comment.