Skip to content

Commit

Permalink
Support inline replies (#1069)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsian03 authored Nov 29, 2020
1 parent 984ffbb commit 5936086
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
13 changes: 10 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ declare namespace Eris {
content?: string;
embed?: EmbedOptions;
flags?: number;
messageReferenceID?: string;
tts?: boolean;
};
type ImageFormat = "jpg" | "jpeg" | "png" | "gif" | "webp";
Expand Down Expand Up @@ -684,6 +685,7 @@ declare namespace Eris {
everyone?: boolean;
roles?: boolean | string[];
users?: boolean | string[];
replied_user?: boolean;
}
interface Attachment {
filename: string;
Expand All @@ -707,10 +709,13 @@ declare namespace Eris {
file: Buffer | string;
name: string;
}
interface MessageReference {
interface MessageReference extends MessageReferenceBase {
channelID: string;
guildID: string;
messageID: string;
}
interface MessageReferenceBase {
channelID?: string;
guildID?: string;
messageID?: string;
}

// Presence
Expand Down Expand Up @@ -991,6 +996,7 @@ declare namespace Eris {
GUILD_DISCOVERY_REQUALIFIED: 15;
GUILD_DISCOVERY_GRACE_PERIOD_INITIAL_WARNING: 16;
GUILD_DISCOVERY_GRACE_PERIOD_FINAL_WARNING: 17;
REPLY: 19;
};
Permissions: {
createInstantInvite: 1;
Expand Down Expand Up @@ -1936,6 +1942,7 @@ declare namespace Eris {
pinned: boolean;
prefix?: string;
reactions: { [s: string]: { count: number; me: boolean } };
referencedMessage?: Message | null;
roleMentions: string[];
timestamp: number;
tts: boolean;
Expand Down
9 changes: 9 additions & 0 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Client extends EventEmitter {
* @arg {Boolean} [options.allowedMentions.everyone] Whether or not to allow @everyone/@here.
* @arg {Boolean | Array<String>} [options.allowedMentions.roles] Whether or not to allow all role mentions, or an array of specific role mentions to allow.
* @arg {Boolean | Array<String>} [options.allowedMentions.users] Whether or not to allow all user mentions, or an array of specific user mentions to allow.
* @arg {Boolean} [options.allowedMentions.repliedUser] Whether or not to mention the author of the message being replied to
* @arg {Boolean} [options.autoreconnect=true] Have Eris autoreconnect when connection is lost
* @arg {Boolean} [options.compress=false] Whether to request WebSocket data to be compressed or not
* @arg {Number} [options.connectionTimeout=30000] How long in milliseconds to wait for the connection to handshake with the server
Expand Down Expand Up @@ -524,8 +525,10 @@ class Client extends EventEmitter {
* @arg {Boolean} [content.allowedMentions.everyone] Whether or not to allow @everyone/@here.
* @arg {Boolean | Array<String>} [content.allowedMentions.roles] Whether or not to allow all role mentions, or an array of specific role mentions to allow.
* @arg {Boolean | Array<String>} [content.allowedMentions.users] Whether or not to allow all user mentions, or an array of specific user mentions to allow.
* @arg {Boolean} [content.allowedMentions.repliedUser] Whether or not to mention the author of the message being replied to.
* @arg {String} content.content A content string
* @arg {Object} [content.embed] An embed object. See [the official Discord API documentation entry](https://discord.com/developers/docs/resources/channel#embed-object) for object structure
* @arg {String} [content.messageReferenceID] The ID of the message should be replied to. The reference message cannot be a system message.
* @arg {Boolean} [content.tts] Set the message TTS flag
* @arg {Object | Array<Object>} [file] A file object (or an Array of them)
* @arg {Buffer} file.file A buffer containing file data
Expand All @@ -544,6 +547,9 @@ class Client extends EventEmitter {
return Promise.reject(new Error("No content, file, or embed"));
}
content.allowed_mentions = this._formatAllowedMentions(content.allowedMentions);
if(content.messageReferenceID) {
content.message_reference = {message_id: content.messageReferenceID};
}
} else if(!file) {
return Promise.reject(new Error("No content, file, or embed"));
}
Expand Down Expand Up @@ -2261,6 +2267,9 @@ class Client extends EventEmitter {
}
result.users = allowed.users;
}
if(allowed.repliedUser !== undefined) {
result.replied_user = allowed.repliedUser;
}
return result;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ module.exports.MessageTypes = {
GUILD_DISCOVERY_DISQUALIFIED: 14,
GUILD_DISCOVERY_REQUALIFIED: 15,
GUILD_DISCOVERY_GRACE_PERIOD_INITIAL_WARNING: 16,
GUILD_DISCOVERY_GRACE_PERIOD_FINAL_WARNING: 17
GUILD_DISCOVERY_GRACE_PERIOD_FINAL_WARNING: 17,
REPLY: 19
};

module.exports.ChannelTypes = {
Expand Down
22 changes: 19 additions & 3 deletions lib/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ const User = require("./User");
* @prop {Member?} member The message author with server-specific data
* @prop {Boolean} mentionEveryone Whether the message mentions everyone/here or not
* @prop {Array<User>} mentions Array of mentioned users
* @prop {Object?} messageReference An object containing the reference to the original message if it is a crossposted message
* @prop {String} messageReference.messageID The id of the original message this message was crossposted from
* @prop {Object?} messageReference An object containing the reference to the original message if it is a crossposted message or reply
* @prop {String?} messageReference.messageID The id of the original message this message was crossposted from
* @prop {String} messageReference.channelID The id of the channel this message was crossposted from
* @prop {String} messageReference.guildID The id of the guild this message was crossposted from
* @prop {String?} messageReference.guildID The id of the guild this message was crossposted from
* @prop {Boolean} pinned Whether the message is pinned or not
* @prop {String?} prefix The prefix used in the Message, if any (CommandClient only)
* @prop {Object} reactions An object containing the reactions on the message. Each key is a reaction emoji and each value is an object with properties `me` (Boolean) and `count` (Number) for that specific reaction emoji.
* @prop {Message?} referencedMessage The message that was replied to. If undefined, message data was not received. If null, the message was deleted.
* @prop {Array<String>} roleMentions Array of mentioned roles' ids
* @prop {Number} timestamp Timestamp of message creation
* @prop {Boolean} tts Whether to play the message using TTS or not
Expand Down Expand Up @@ -77,6 +78,18 @@ class Message extends Base {
} else {
this._client.emit("error", new Error("MESSAGE_CREATE but no message author:\n" + JSON.stringify(data, null, 2)));
}
if(data.referenced_message) {
const channel = data.referenced_message.guild_id
? this._client.guilds.get(data.referenced_message.guild_id).channels.get(data.referenced_message.channel_id)
: this._client.privateChannels.get(data.referenced_message.channel_id);
if(channel) {
this.referencedMessage = channel.messages.update(data.referenced_message);
} else {
this.referencedMessage = new Message(data.referenced_message, this._client);
}
} else {
this.referencedMessage = data.referenced_message;
}
if(this.channel.guild) {
if(data.member) {
data.member.id = this.author.id;
Expand Down Expand Up @@ -176,6 +189,9 @@ class Message extends Base {
data.content = "This server has failed Discovery activity requirements for 3 weeks in a row. If this server fails for 1 more week, it will be removed from Discovery.";
break;
}
case MessageTypes.REPLY: {
break;
}
default: {
client.emit("warn", `Unhandled MESSAGE_CREATE type: ${JSON.stringify(data, null, 2)}`);
break;
Expand Down
2 changes: 2 additions & 0 deletions lib/structures/PrivateChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ class PrivateChannel extends Channel {
* @arg {Boolean} [content.allowedMentions.everyone] Whether or not to allow @everyone/@here.
* @arg {Boolean | Array<String>} [content.allowedMentions.roles] Whether or not to allow all role mentions, or an array of specific role mentions to allow.
* @arg {Boolean | Array<String>} [content.allowedMentions.users] Whether or not to allow all user mentions, or an array of specific user mentions to allow.
* @arg {Boolean} [options.allowedMentions.repliedUser] Whether or not to mention the author of the message being replied to
* @arg {String} content.content A content string
* @arg {Object} [content.embed] An embed object. See [the official Discord API documentation entry](https://discord.com/developers/docs/resources/channel#embed-object) for object structure
* @arg {String} [content.messageReferenceID] The ID of the message should be replied to. The reference message cannot be a system message.
* @arg {Boolean} [content.tts] Set the message TTS flag
* @arg {Object} [file] A file object
* @arg {Buffer} file.file A buffer containing file data
Expand Down
2 changes: 2 additions & 0 deletions lib/structures/TextChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ class TextChannel extends GuildChannel {
* @arg {Boolean} [content.allowedMentions.everyone] Whether or not to allow @everyone/@here.
* @arg {Boolean | Array<String>} [content.allowedMentions.roles] Whether or not to allow all role mentions, or an array of specific role mentions to allow.
* @arg {Boolean | Array<String>} [content.allowedMentions.users] Whether or not to allow all user mentions, or an array of specific user mentions to allow.
* @arg {Boolean} [options.allowedMentions.repliedUser] Whether or not to mention the author of the message being replied to
* @arg {String} content.content A content string
* @arg {Object} [content.embed] An embed object. See [the official Discord API documentation entry](https://discord.com/developers/docs/resources/channel#embed-object) for object structure
* @arg {String} [content.messageReferenceID] The ID of the message should be replied to. The reference message cannot be a system message.
* @arg {Boolean} [content.tts] Set the message TTS flag
* @arg {Object} [file] A file object
* @arg {Buffer} file.file A buffer containing file data
Expand Down

0 comments on commit 5936086

Please sign in to comment.