@averagehelper/corde
Version:
A simple library for Discord bot tests. (Republished fork to demonstrate a bugfix)
86 lines (85 loc) • 2.93 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = __importDefault(require("assert"));
const utils_1 = __importDefault(require("../../../utils/utils"));
class MessageUtilsManager {
messagesMatches(returnedMessage, expectation) {
let msg = "";
if (utils_1.default.isValuePrimitive(expectation)) {
const formattedMsg = this.getMessageByType(returnedMessage, "text");
msg = formattedMsg.content;
return msg === expectation;
}
const jsonMessage = this.getMessageByType(returnedMessage, "embed");
msg = JSON.stringify(jsonMessage);
let result = true;
try {
assert_1.default.deepStrictEqual(expectation.toJSON(), jsonMessage);
}
catch (error) {
result = false;
}
return result;
}
/**
* Format Discord responses
*
* @param answer Discord response for a message sent
*
* @param type Type expected of that message
*
* @description Discord adds some attributes that are not present in embed message before it is sent
*
* This is data **before** send to Discord
*
* ```javascript
* "image": {
* "url": "https://i.imgur.com/wSTFkRM.png"
* },
* "thumbnail": {
* "url": "https://i.imgur.com/wSTFkRM.png"
* }
* ```
*
* And this is part of embed message **after** get from Discord
*
* ```javascript
* "image": {
* "height": 0,
* "proxyURL": "https://images-ext-2.discordapp.net/external/DoAGN014Q46B7iDBr2VJyHUL59QLSWdEAZ5wOoWe8CY/https/i.imgur.com/wSTFkRM.png",
* "url": "https://i.imgur.com/wSTFkRM.png",
* "width": 0
* },
* "thumbnail": {
* "height": 0,
* "proxyURL": "https://images-ext-2.discordapp.net/external/DoAGN014Q46B7iDBr2VJyHUL59QLSWdEAZ5wOoWe8CY/https/i.imgur.com/wSTFkRM.png",
* "url": "https://i.imgur.com/wSTFkRM.png",
* "width": 0
* }
* ```
*/
getMessageByType(answer, type) {
if (type === "embed") {
const embed = answer.embeds[0];
if (!embed) {
return null;
}
const tempObject = embed.toJSON();
if (tempObject.image) {
tempObject.image = utils_1.default.pick(tempObject.image, "url");
}
if (tempObject.thumbnail) {
tempObject.thumbnail = utils_1.default.pick(tempObject.thumbnail, "url");
}
return tempObject;
}
else {
return answer;
}
}
}
const MessageUtils = new MessageUtilsManager();
exports.default = MessageUtils;