standardize message object creation (#1986)

* standardize message object creation so i don't flip out again

* fix stuff

* Update Message.js

* Update index.js

* Update SendMessage.js

* Update Message.js
This commit is contained in:
Gus Caplan
2017-10-29 08:54:00 -05:00
committed by Crawl
parent 94a4a068b9
commit 29a81eab73
6 changed files with 135 additions and 244 deletions

View File

@@ -8,9 +8,9 @@ const Collection = require('../util/Collection');
const ReactionStore = require('../stores/ReactionStore');
const { MessageTypes } = require('../util/Constants');
const Permissions = require('../util/Permissions');
const GuildMember = require('./GuildMember');
const Base = require('./Base');
const { Error, TypeError } = require('../errors');
const { createMessage } = require('./shared');
/**
* Represents a message on Discord.
@@ -368,41 +368,22 @@ class Message extends Base {
* .then(msg => console.log(`Updated the content of a message from ${msg.author}`))
* .catch(console.error);
*/
edit(content, options) {
async edit(content, options) {
if (!options && typeof content === 'object' && !(content instanceof Array)) {
options = content;
content = '';
content = null;
} else if (!options) {
options = {};
}
if (options instanceof Embed) options = { embed: options };
if (!options.content) options.content = content;
if (typeof options.content !== 'undefined') content = options.content;
if (typeof content !== 'undefined') content = Util.resolveString(content);
let { embed, code, reply } = options;
if (embed) embed = new Embed(embed)._apiTransform();
// Wrap everything in a code block
if (typeof code !== 'undefined' && (typeof code !== 'boolean' || code === true)) {
content = Util.escapeMarkdown(Util.resolveString(content), true);
content = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n${content}\n\`\`\``;
}
// Add the reply prefix
if (reply && this.channel.type !== 'dm') {
const id = this.client.users.resolveID(reply);
const mention = `<@${reply instanceof GuildMember && reply.nickname ? '!' : ''}${id}>`;
content = `${mention}${content ? `, ${content}` : ''}`;
}
const { data, files } = await createMessage(this, options);
return this.client.api.channels[this.channel.id].messages[this.id]
.patch({ data: { content, embed } })
.then(data => {
.patch({ data, files })
.then(d => {
const clone = this._clone();
clone._patch(data);
clone._patch(d);
return clone;
});
}