@lambocreeper/mock-discord.js
Version:
Easily mock Discord.js for testing your bot's code.
126 lines (125 loc) • 4.38 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const discord_js_1 = __importDefault(require("discord.js"));
const guild_1 = __importDefault(require("./defaults/guild"));
const guildchannel_1 = __importDefault(require("./defaults/guildchannel"));
const textchannel_1 = __importDefault(require("./defaults/textchannel"));
const user_1 = __importDefault(require("./defaults/user"));
const guildmember_1 = __importDefault(require("./defaults/guildmember"));
const message_1 = __importDefault(require("./defaults/message"));
const messagerReaction_1 = __importDefault(require("./defaults/messagerReaction"));
class BaseMocks {
static client;
static guild;
static guildChannel;
static textChannel;
static user;
static guildMember;
static message;
static messageReaction;
/**
* Returns a generic and consistent mock of a Discord Client.
*
* @returns {Discord.Client}
*/
static getClient() {
if (!this.client) {
this.client = new discord_js_1.default.Client({
intents: 10
});
}
return this.client;
}
/**
* Returns a generic and consistent mock of a Discord Guild.
*
* @returns {Discord.Guild}
*/
static getGuild() {
if (!this.guild) {
this.guild = Reflect.construct(discord_js_1.default.Guild, [this.getClient(), guild_1.default]);
}
return this.guild;
}
/**
* Returns a generic and consistent mock of a Discord Guild Channel.
*
* @returns {Discord.GuildChannel}
*/
static getGuildChannel() {
if (!this.guildChannel) {
this.guildChannel = Reflect.construct(discord_js_1.default.GuildChannel, [this.getGuild(), guildchannel_1.default]);
}
return this.guildChannel;
}
/**
* Returns a generic and consistent mock of a Discord Text Channel.
*
* @returns {Discord.TextChannel}
*/
static getTextChannel() {
if (!this.textChannel) {
this.textChannel = Reflect.construct(discord_js_1.default.TextChannel, [this.getGuild(), textchannel_1.default]);
}
return this.textChannel;
}
/**
* Returns a generic and consistent mock of a Discord User.
*
* @returns {Discord.User}
*/
static getUser() {
if (!this.user) {
this.user = Reflect.construct(discord_js_1.default.User, [this.getClient(), user_1.default]);
}
return this.user;
}
/**
* Returns a generic and consistent mock of a Discord Guild Member.
*
* @returns {Discord.GuildMember}
*/
static getGuildMember() {
if (!this.guildMember) {
this.guildMember = Reflect.construct(discord_js_1.default.GuildMember, [this.getClient(), guildmember_1.default, this.getGuild()]);
}
return this.guildMember;
}
/**
* Returns a generic and consistent mock of a Discord Message.
*
* @returns {Discord.Message}
*/
static getMessage() {
if (!this.message) {
this.message = Reflect.construct(discord_js_1.default.Message, [this.getClient(), message_1.default]);
/**
* Both channel and member are "getter" methods that resolve to objects
* based on IDs which are stored on the client - since we're mocking
* the client there will be no IDs, so overwrite these getters
*/
Object.defineProperty(this.message, "channel", {
get: () => this.getTextChannel()
});
Object.defineProperty(this.message, "member", {
get: () => this.getGuildMember()
});
}
return this.message;
}
/**
* Returns a generic and consistent mock of a Discord Message Reaction.
*
* @returns {Discord.MessageReaction}
*/
static getMessageReaction() {
if (!this.messageReaction) {
this.messageReaction = Reflect.construct(discord_js_1.default.MessageReaction, [this.getClient(), messagerReaction_1.default, this.getMessage()]);
}
return this.messageReaction;
}
}
exports.default = BaseMocks;