Skip to content

Commit

Permalink
Build message instances and cache them whenever MESSAGE_CREATE event …
Browse files Browse the repository at this point in the history
…is fired
  • Loading branch information
stuyy committed May 21, 2020
1 parent f776bdd commit 11ca42f
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 116 deletions.
71 changes: 4 additions & 67 deletions src/handlers/MESSAGE_CREATE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
24 changes: 6 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,20 @@ 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());

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);
}
});

Expand Down
53 changes: 32 additions & 21 deletions src/models/Message.ts
Original file line number Diff line number Diff line change
@@ -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<any>,
private embeds: Array<any>,
private reactions: Array<any>,
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<any>,
private _embeds: Array<any>,
private _reactions: Array<any>,
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<any> { return this._attachments; }
public get embeds(): Array<any> { return this._embeds; }
public get reactions(): Array<any> { 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; }
}
22 changes: 12 additions & 10 deletions src/models/channels/TextChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Message> = new Mes
private _messages: Collection<string, Message> = new Collection();

constructor(
_id: string,
_client: Client,
Expand Down Expand Up @@ -42,17 +44,17 @@ export class TextChannel extends GuildChannel implements TextBasedChannel {
);
}

send(payload: string | MessageOptions): any {
get messages(): Collection<string, Message> { 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);
}
}
67 changes: 67 additions & 0 deletions src/utils/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 11ca42f

Please sign in to comment.