From 7ff9ac2bccaaadef9bee57daafffe9ef89bb982f Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Sun, 11 Sep 2016 10:55:41 -0400 Subject: [PATCH] Add TextBasedChannel.sendCode and make User/GuildMember partial implementations --- src/structures/DMChannel.js | 1 + src/structures/GroupDMChannel.js | 1 + src/structures/GuildMember.js | 3 ++- src/structures/TextChannel.js | 1 + src/structures/User.js | 3 ++- src/structures/interface/TextBasedChannel.js | 19 ++++++++++++++++++- 6 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/structures/DMChannel.js b/src/structures/DMChannel.js index 169555c23..f7f53c322 100644 --- a/src/structures/DMChannel.js +++ b/src/structures/DMChannel.js @@ -40,6 +40,7 @@ class DMChannel extends Channel { sendMessage() { return; } sendTTSMessage() { return; } sendFile() { return; } + sendCode() { return; } fetchMessage() { return; } fetchMessages() { return; } fetchPinnedMessages() { return; } diff --git a/src/structures/GroupDMChannel.js b/src/structures/GroupDMChannel.js index c14ee34a8..e51d5d1dd 100644 --- a/src/structures/GroupDMChannel.js +++ b/src/structures/GroupDMChannel.js @@ -118,6 +118,7 @@ class GroupDMChannel extends Channel { sendMessage() { return; } sendTTSMessage() { return; } sendFile() { return; } + sendCode() { return; } fetchMessage() { return; } fetchMessages() { return; } fetchPinnedMessages() { return; } diff --git a/src/structures/GuildMember.js b/src/structures/GuildMember.js index 41c260f44..be94ce6bc 100644 --- a/src/structures/GuildMember.js +++ b/src/structures/GuildMember.js @@ -354,8 +354,9 @@ class GuildMember { sendMessage() { return; } sendTTSMessage() { return; } sendFile() { return; } + sendCode() { return; } } -TextBasedChannel.applyToClass(GuildMember); +TextBasedChannel.applyToClass(GuildMember, false); module.exports = GuildMember; diff --git a/src/structures/TextChannel.js b/src/structures/TextChannel.js index d8604144c..d14b2e28a 100644 --- a/src/structures/TextChannel.js +++ b/src/structures/TextChannel.js @@ -46,6 +46,7 @@ class TextChannel extends GuildChannel { sendMessage() { return; } sendTTSMessage() { return; } sendFile() { return; } + sendCode() { return; } fetchMessage() { return; } fetchMessages() { return; } fetchPinnedMessages() { return; } diff --git a/src/structures/User.js b/src/structures/User.js index 700ecd6a6..27dc12e78 100644 --- a/src/structures/User.js +++ b/src/structures/User.js @@ -161,8 +161,9 @@ class User { sendMessage() { return; } sendTTSMessage() { return; } sendFile() { return; } + sendCode() { return; } } -TextBasedChannel.applyToClass(User); +TextBasedChannel.applyToClass(User, false); module.exports = User; diff --git a/src/structures/interface/TextBasedChannel.js b/src/structures/interface/TextBasedChannel.js index ce6ce00d4..a9071c4ff 100644 --- a/src/structures/interface/TextBasedChannel.js +++ b/src/structures/interface/TextBasedChannel.js @@ -109,6 +109,23 @@ class TextBasedChannel { }); } + /** + * Send a code block to this channel + * @param {string} lang Language for the code block + * @param {StringResolvable} content Content of the code block + * @param {MessageOptions} options The options to provide + * @returns {Promise} + */ + sendCode(lang, content, options = {}) { + if (options.split) { + if (typeof options.split !== 'object') options.split = {}; + if (!options.split.prepend) options.split.prepend = `\`\`\`${lang}\n`; + if (!options.split.append) options.split.append = '\n```'; + } + content = this.client.resolver.resolveString(content); + return this.sendMessage(`\`\`\`${lang}\n${content}\n\`\`\``, options); + } + /** * Gets a single message from this channel, regardless of it being cached or not. * @param {string} messageID The ID of the message to get @@ -432,7 +449,7 @@ class MessageCollector extends EventEmitter { } exports.applyToClass = (structure, full = false) => { - const props = ['sendMessage', 'sendTTSMessage', 'sendFile']; + const props = ['sendMessage', 'sendTTSMessage', 'sendFile', 'sendCode']; if (full) { props.push('_cacheMessage'); props.push('fetchMessages');