mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 00:53:31 +01:00
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:
111
src/structures/shared/CreateMessage.js
Normal file
111
src/structures/shared/CreateMessage.js
Normal file
@@ -0,0 +1,111 @@
|
||||
const Embed = require('../MessageEmbed');
|
||||
const DataResolver = require('../../util/DataResolver');
|
||||
const MessageEmbed = require('../MessageEmbed');
|
||||
const MessageAttachment = require('../MessageAttachment');
|
||||
const { browser } = require('../../util/Constants');
|
||||
const Util = require('../../util/Util');
|
||||
|
||||
// eslint-disable-next-line complexity
|
||||
module.exports = async function createMessage(channel, options) {
|
||||
const User = require('../User');
|
||||
const GuildMember = require('../GuildMember');
|
||||
const Webhook = require('../Webhook');
|
||||
|
||||
const webhook = channel instanceof Webhook;
|
||||
|
||||
if (typeof options.nonce !== 'undefined') {
|
||||
options.nonce = parseInt(options.nonce);
|
||||
if (isNaN(options.nonce) || options.nonce < 0) throw new RangeError('MESSAGE_NONCE_TYPE');
|
||||
}
|
||||
|
||||
if (options instanceof MessageEmbed) options = webhook ? { embeds: [options] } : { embed: options };
|
||||
if (options instanceof MessageAttachment) options = { files: [options.file] };
|
||||
|
||||
if (options.reply && !(channel instanceof User || channel instanceof GuildMember) && channel.type !== 'dm') {
|
||||
const id = channel.client.users.resolveID(options.reply);
|
||||
const mention = `<@${options.reply instanceof GuildMember && options.reply.nickname ? '!' : ''}${id}>`;
|
||||
if (options.split) options.split.prepend = `${mention}, ${options.split.prepend || ''}`;
|
||||
options.content = `${mention}${typeof options.content !== 'undefined' ? `, ${options.content}` : ''}`;
|
||||
}
|
||||
|
||||
if (options.content) {
|
||||
options.content = Util.resolveString(options.content);
|
||||
if (options.split && typeof options.split !== 'object') options.split = {};
|
||||
// Wrap everything in a code block
|
||||
if (typeof options.code !== 'undefined' && (typeof options.code !== 'boolean' || options.code === true)) {
|
||||
options.content = Util.escapeMarkdown(options.content, true);
|
||||
options.content =
|
||||
`\`\`\`${typeof options.code !== 'boolean' ? options.code || '' : ''}\n${options.content}\n\`\`\``;
|
||||
if (options.split) {
|
||||
options.split.prepend = `\`\`\`${typeof options.code !== 'boolean' ? options.code || '' : ''}\n`;
|
||||
options.split.append = '\n```';
|
||||
}
|
||||
}
|
||||
|
||||
// Add zero-width spaces to @everyone/@here
|
||||
if (options.disableEveryone ||
|
||||
(typeof options.disableEveryone === 'undefined' && channel.client.options.disableEveryone)) {
|
||||
options.content = options.content.replace(/@(everyone|here)/g, '@\u200b$1');
|
||||
}
|
||||
|
||||
if (options.split) options.content = Util.splitMessage(options.content, options.split);
|
||||
}
|
||||
|
||||
if (options.embed && options.embed.files) {
|
||||
if (options.files) options.files = options.files.concat(options.embed.files);
|
||||
else options.files = options.embed.files;
|
||||
}
|
||||
|
||||
if (options.embed && webhook) options.embeds = [new Embed(options.embed)._apiTransform()];
|
||||
else if (options.embed) options.embed = new Embed(options.embed)._apiTransform();
|
||||
else if (options.embeds) options.embeds = options.embeds.map(e => new Embed(e)._apiTransform());
|
||||
|
||||
let files;
|
||||
|
||||
if (options.files) {
|
||||
for (let i = 0; i < options.files.length; i++) {
|
||||
let file = options.files[i];
|
||||
if (typeof file === 'string' || (!browser && Buffer.isBuffer(file))) file = { attachment: file };
|
||||
if (!file.name) {
|
||||
if (typeof file.attachment === 'string') {
|
||||
file.name = Util.basename(file.attachment);
|
||||
} else if (file.attachment && file.attachment.path) {
|
||||
file.name = Util.basename(file.attachment.path);
|
||||
} else if (file instanceof MessageAttachment) {
|
||||
file = { attachment: file.file, name: Util.basename(file.file) || 'file.jpg' };
|
||||
} else {
|
||||
file.name = 'file.jpg';
|
||||
}
|
||||
} else if (file instanceof MessageAttachment) {
|
||||
file = file.file;
|
||||
}
|
||||
options.files[i] = file;
|
||||
}
|
||||
|
||||
files = await Promise.all(options.files.map(file =>
|
||||
DataResolver.resolveFile(file.attachment).then(resource => {
|
||||
file.file = resource;
|
||||
return file;
|
||||
})
|
||||
));
|
||||
delete options.files;
|
||||
}
|
||||
|
||||
if (webhook) {
|
||||
if (!options.username) options.username = this.name;
|
||||
if (options.avatarURL) {
|
||||
options.avatar_url = options.avatarURL;
|
||||
options.avatarURL = null;
|
||||
}
|
||||
}
|
||||
|
||||
return { data: {
|
||||
content: options.content,
|
||||
tts: options.tts,
|
||||
nonce: options.nonce,
|
||||
embed: options.embed,
|
||||
embeds: options.embeds,
|
||||
username: options.username,
|
||||
avatar_url: options.avatarURL,
|
||||
}, files };
|
||||
};
|
||||
@@ -1,65 +1,12 @@
|
||||
const Util = require('../../util/Util');
|
||||
const Embed = require('../MessageEmbed');
|
||||
const { RangeError } = require('../../errors');
|
||||
const createMessage = require('./CreateMessage');
|
||||
|
||||
module.exports = function sendMessage(channel, options) { // eslint-disable-line complexity
|
||||
module.exports = async function sendMessage(channel, options) { // eslint-disable-line complexity
|
||||
const User = require('../User');
|
||||
const GuildMember = require('../GuildMember');
|
||||
if (channel instanceof User || channel instanceof GuildMember) return channel.createDM().then(dm => dm.send(options));
|
||||
let { content, nonce, reply, code, disableEveryone, tts, embed, files, split } = options;
|
||||
|
||||
if (embed) embed = new Embed(embed)._apiTransform();
|
||||
const { data, files } = await createMessage(channel, options);
|
||||
|
||||
if (typeof nonce !== 'undefined') {
|
||||
nonce = parseInt(nonce);
|
||||
if (isNaN(nonce) || nonce < 0) throw new RangeError('MESSAGE_NONCE_TYPE');
|
||||
}
|
||||
|
||||
// Add the reply prefix
|
||||
if (reply && !(channel instanceof User || channel instanceof GuildMember) && channel.type !== 'dm') {
|
||||
const id = channel.client.users.resolveID(reply);
|
||||
const mention = `<@${reply instanceof GuildMember && reply.nickname ? '!' : ''}${id}>`;
|
||||
if (split) split.prepend = `${mention}, ${split.prepend || ''}`;
|
||||
content = `${mention}${typeof content !== 'undefined' ? `, ${content}` : ''}`;
|
||||
}
|
||||
|
||||
if (content) {
|
||||
content = Util.resolveString(content);
|
||||
if (split && typeof split !== 'object') split = {};
|
||||
// Wrap everything in a code block
|
||||
if (typeof code !== 'undefined' && (typeof code !== 'boolean' || code === true)) {
|
||||
content = Util.escapeMarkdown(content, true);
|
||||
content = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n${content}\n\`\`\``;
|
||||
if (split) {
|
||||
split.prepend = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n`;
|
||||
split.append = '\n```';
|
||||
}
|
||||
}
|
||||
|
||||
// Add zero-width spaces to @everyone/@here
|
||||
if (disableEveryone || (typeof disableEveryone === 'undefined' && channel.client.options.disableEveryone)) {
|
||||
content = content.replace(/@(everyone|here)/g, '@\u200b$1');
|
||||
}
|
||||
|
||||
if (split) content = Util.splitMessage(content, split);
|
||||
}
|
||||
|
||||
if (content instanceof Array) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const messages = [];
|
||||
(function sendChunk() {
|
||||
const opt = content.length ? { tts } : { tts, embed, files };
|
||||
channel.send(content.shift(), opt).then(message => {
|
||||
messages.push(message);
|
||||
if (content.length === 0) return resolve(messages);
|
||||
return sendChunk();
|
||||
}).catch(reject);
|
||||
}());
|
||||
});
|
||||
}
|
||||
|
||||
return channel.client.api.channels[channel.id].messages.post({
|
||||
data: { content, tts, nonce, embed },
|
||||
files,
|
||||
}).then(data => channel.client.actions.MessageCreate.handle(data).message);
|
||||
return channel.client.api.channels[channel.id].messages.post({ data, files })
|
||||
.then(d => channel.client.actions.MessageCreate.handle(d).message);
|
||||
};
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
module.exports = {
|
||||
search: require('./Search'),
|
||||
sendMessage: require('./SendMessage'),
|
||||
createMessage: require('./CreateMessage'),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user