mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 09:33:32 +01:00
Add MessageCreate handler, set up Message class and add ability to Cache messages
This commit is contained in:
@@ -2,10 +2,26 @@
|
||||
|
||||
const Channel = require('./Channel');
|
||||
const User = require('./User');
|
||||
const TextChannelDataStore = require('./datastore/TextChannelDataStore');
|
||||
|
||||
class DMChannel extends Channel{
|
||||
constructor(client, data) {
|
||||
super(client, data);
|
||||
this.store = new TextChannelDataStore();
|
||||
}
|
||||
|
||||
_cacheMessage(message) {
|
||||
let maxSize = this.client.options.max_message_cache;
|
||||
if (maxSize === 0) {
|
||||
// saves on performance
|
||||
return;
|
||||
}
|
||||
|
||||
let storeKeys = Object.keys(this.store);
|
||||
if (storeKeys.length >= maxSize) {
|
||||
this.store.remove(storeKeys[0]);
|
||||
this.store.add('messages', message);
|
||||
}
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
|
||||
@@ -1,8 +1,31 @@
|
||||
'use strict';
|
||||
|
||||
class Message {
|
||||
constructor() {
|
||||
constructor(serverChannel, data) {
|
||||
this.channel = serverChannel;
|
||||
this.guild = serverChannel.guild;
|
||||
if (data) {
|
||||
this.setup(data);
|
||||
}
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
this.author = this.guild.client.store.get('users', data.author.id);
|
||||
this.content = data.content;
|
||||
this.timestamp = new Date(data.timestamp);
|
||||
this.editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp) : null;
|
||||
this.tts = data.tts;
|
||||
this.mentionEveryone = data.mention_everyone;
|
||||
this.nonce = data.nonce;
|
||||
this.embeds = data.embeds;
|
||||
this.attachments = data.attachments;
|
||||
this.mentions = [];
|
||||
for (let mention of data.mentions) {
|
||||
let user = this.guild.client.store.get('users', mention.id);
|
||||
if (user) {
|
||||
this.mentions.push(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,10 +4,25 @@ const ServerChannel = require('./ServerChannel');
|
||||
const TextChannelDataStore = require('./datastore/TextChannelDataStore');
|
||||
|
||||
class TextChannel extends ServerChannel {
|
||||
|
||||
constructor(guild, data) {
|
||||
super(guild, data);
|
||||
this.store = new TextChannelDataStore();
|
||||
}
|
||||
|
||||
_cacheMessage(message) {
|
||||
let maxSize = this.client.options.max_message_cache;
|
||||
if (maxSize === 0) {
|
||||
// saves on performance
|
||||
return;
|
||||
}
|
||||
|
||||
let storeKeys = Object.keys(this.store);
|
||||
if (storeKeys.length >= maxSize) {
|
||||
this.store.remove(storeKeys[0]);
|
||||
this.store.add('messages', message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TextChannel;
|
||||
|
||||
Reference in New Issue
Block a user