From f3e1760538887b4bb7c2c275961978b905eccb37 Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Thu, 21 Apr 2016 17:42:52 +0100 Subject: [PATCH] add channel.sendMessage and channel.sendTTSMessage --- src/client/rest/RESTMethods.js | 18 +++++++++++++++ .../packets/handlers/MessageCreate.js | 2 +- src/structures/DMChannel.js | 4 ++++ src/structures/Message.js | 23 +++++++++++-------- src/structures/TextChannel.js | 3 +++ src/structures/interface/TextBasedChannel.js | 20 ++++++++++++++++ test/random.js | 2 +- 7 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 src/structures/interface/TextBasedChannel.js diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index 228f6f7da..2f2c27f87 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -1,6 +1,9 @@ 'use strict'; const Constants = require('../../util/Constants'); +const Structure = name => require('../../structures/' + name); + +const Message = Structure('Message'); class RESTMethods{ constructor(restManager) { @@ -32,6 +35,21 @@ class RESTMethods{ .catch(reject); }); } + + SendMessage(channel, content, tts, nonce) { + return new Promise((resolve, reject) => { + this.rest.makeRequest('post', Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, { + content, tts, nonce, + }) + .then(data => { + let message = new Message(channel, data, this.rest.client); + channel._cacheMessage(message); + resolve(message); + this.rest.client.emit(Constants.Events.MESSAGE_CREATE, message); + }) + .catch(reject); + }); + } } module.exports = RESTMethods; diff --git a/src/client/websocket/packets/handlers/MessageCreate.js b/src/client/websocket/packets/handlers/MessageCreate.js index f6e2cf35e..d701d0a97 100644 --- a/src/client/websocket/packets/handlers/MessageCreate.js +++ b/src/client/websocket/packets/handlers/MessageCreate.js @@ -19,7 +19,7 @@ class MessageCreateHandler extends AbstractHandler { let channel = client.store.get('channels', data.channel_id); if (channel) { - let message = new Message(channel, data); + let message = new Message(channel, data, client); channel._cacheMessage(message); client.emit(Constants.Events.MESSAGE_CREATE, message); } diff --git a/src/structures/DMChannel.js b/src/structures/DMChannel.js index f755c3b87..f0b7523b2 100644 --- a/src/structures/DMChannel.js +++ b/src/structures/DMChannel.js @@ -1,6 +1,7 @@ 'use strict'; const Channel = require('./Channel'); +const TextBasedChannel = require('./interface/TextBasedChannel'); const User = require('./User'); const TextChannelDataStore = require('./datastore/TextChannelDataStore'); @@ -26,6 +27,7 @@ class DMChannel extends Channel{ } setup(data) { + super.setup(data); this.recipient = this.client.store.add('users', new User(this.client, data.recipient)); this.lastMessageID = data.last_message_id; } @@ -35,4 +37,6 @@ class DMChannel extends Channel{ } } +TextBasedChannel.applyToClass(DMChannel); + module.exports = DMChannel; diff --git a/src/structures/Message.js b/src/structures/Message.js index b0262e04a..b5a0b3e12 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -1,16 +1,21 @@ 'use strict'; class Message { - constructor(serverChannel, data) { - this.channel = serverChannel; - this.guild = serverChannel.guild; + constructor(channel, data, client) { + this.channel = channel; + + if (channel.guild) { + this.guild = channel.guild; + } + + this.client = client; if (data) { this.setup(data); } } setup(data) { - this.author = this.guild.client.store.NewUser(data.author); + this.author = this.client.store.NewUser(data.author); this.content = data.content; this.timestamp = new Date(data.timestamp); this.editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp) : null; @@ -22,11 +27,11 @@ class Message { this.mentions = []; this.id = data.id; for (let mention of data.mentions) { - let user = this.guild.client.store.get('users', mention.id); + let user = this.client.store.get('users', mention.id); if (user) { this.mentions.push(user); } else { - user = this.guild.client.store.NewUser(mention); + user = this.client.store.NewUser(mention); this.mentions.push(user); } } @@ -34,7 +39,7 @@ class Message { patch(data) { if (data.author) - this.author = this.guild.client.store.get('users', data.author.id); + this.author = this.client.store.get('users', data.author.id); if (data.content) this.content = data.content; if (data.timestamp) @@ -53,11 +58,11 @@ class Message { this.attachments = data.attachments; if (data.mentions) { for (let mention of data.mentions) { - let user = this.guild.client.store.get('users', mention.id); + let user = this.client.store.get('users', mention.id); if (user) { this.mentions.push(user); } else { - user = this.guild.client.store.NewUser(mention); + user = this.client.store.NewUser(mention); this.mentions.push(user); } } diff --git a/src/structures/TextChannel.js b/src/structures/TextChannel.js index eac0f45f6..a93c831be 100644 --- a/src/structures/TextChannel.js +++ b/src/structures/TextChannel.js @@ -2,6 +2,7 @@ const ServerChannel = require('./ServerChannel'); const TextChannelDataStore = require('./datastore/TextChannelDataStore'); +const TextBasedChannel = require('./interface/TextBasedChannel'); class TextChannel extends ServerChannel { @@ -26,4 +27,6 @@ class TextChannel extends ServerChannel { } } +TextBasedChannel.applyToClass(TextChannel); + module.exports = TextChannel; diff --git a/src/structures/interface/TextBasedChannel.js b/src/structures/interface/TextBasedChannel.js new file mode 100644 index 000000000..0656d03f7 --- /dev/null +++ b/src/structures/interface/TextBasedChannel.js @@ -0,0 +1,20 @@ +'use strict'; + +function sendMessage(content, options) { + options = options || {}; + return this.client.rest.methods.SendMessage(this, content, options.tts); +} + +function sendTTSMessage(content, options) { + options = options || {}; + return this.client.rest.methods.SendMessage(this, content, true); +} + +exports.applyToClass = structure => { + if (structure.name !== 'TextChannel' && structure.name !== 'DMChannel') { + throw new Error(structure + ' cannot implement TextBasedChannel'); + } + + structure.prototype.sendMessage = sendMessage; + +}; diff --git a/test/random.js b/test/random.js index 8e4b8e315..87633f281 100644 --- a/test/random.js +++ b/test/random.js @@ -70,7 +70,7 @@ client.on('typingStop.', (channel, user, data) => { client.on('message', message => { if (message.author.username === 'hydrabolt') - console.log(message.author.username, 'said', message.content, 'in', message.channel.name); + message.channel.sendMessage('hydrabolt said: ' + message.content).then(console.log).catch(console.log); }); client.on('messageDelete', message => {