v3.0 (#145)
* Change rules.txt to rules.json (#31) * Migrate to yarn * Add role configs to config template * Install packges and setup typescript * Migrate entry point * Migrate about command * Migrate ban command * Migrate clear command * Migrate kick command * Migrate mute command * Migrate poll command * Migrate bunny command * Update required roles checker * Migrate role command * Migrate unmute command * Migrate warn command * Migrate eval command * Migrate help command * Migrate rules command * Migrate events to typescript * Update about command to use the PublicEmbed class * Update ErrorMessage to ChannelNotFound * Update messageDelete event to ignore bots * Feature/74 merge vylbot core (#80) * Merge VylBot-Core * Update commands to new system * Fix issue where events would not load * Feature/12 create tests (#102) * Fix tests * Update coverage * Remove unrequired mock files * Add about command test * Update about tests * Ban command tests * eval command tests * Start help command tests * Add help command tests * Add kick command tests * Mute command tests * Poll command tests * Add role command tests Signed-off-by: Ethan Lane <ethan@vylpes.com> * Add rules command tests * Add unmute command tests * Add warn command tests * Add MemberEvents tests * Add GuildMemberUpdate tests Signed-off-by: Ethan Lane <ethan@vylpes.com> * Add MessageEvents tests * Add StringTools test Signed-off-by: Ethan Lane <ethan@vylpes.com> * Add embed tests Signed-off-by: Ethan Lane <ethan@vylpes.com> * Add GitHub Actions Signed-off-by: Ethan Lane <ethan@vylpes.com> * Move to tslint Signed-off-by: Ethan Lane <ethan@vylpes.com> * Remove tslint Signed-off-by: Ethan Lane <ethan@vylpes.com> * Remove linting script Signed-off-by: Ethan Lane <ethan@vylpes.com> * Update rules with blog website and event spoilers rule" (#106) Signed-off-by: Ethan Lane <ethan@vylpes.com> * Containerise bot (#107) * Add moderator names to audit reason (#108) * Feature/48 database (#114) * Add database and default values * Add ability to save a setting to the database * Get commands and events to use database * Setup and config command * Update commands to check roles per server * Different rules per server Signed-off-by: Ethan Lane <ethan@vylpes.com> * Different prefix per server Signed-off-by: Ethan Lane <ethan@vylpes.com> * Add verification system Signed-off-by: Ethan Lane <ethan@vylpes.com> * Disabled commands per server * Add devmode for default prefix * Update embeds * Fix broken tests * Feature/66 add different commands per server (#122) * Add ability for server exclusive commands * Add MankBot server-exclusive commands * Add lobby entity to database * Add documentation * Add setup command for lobby (#123) * Update bot to discord.js v13 (#125) * Update bot to discord.js v13 * Remove debug code * 110 commandshelp about command errors which causes command to not run (#126) * Change onMessage to onMessageCreate * Fix help command * Add override for bot owner and server owner (#135) * Change help command so exclusive commands can only be seen for the server they're assigned to (#136) * Change parsing to not crash if invalid (#142) * 137 role command cannot read properties of undefined (#141) * Fix issue with bot crashing * Fix server prefix not showing * Add easy way to configure role command * Move help text to its own directory * Make role config command to use role id * Get lobby command to use IDs instead of names (#144) Co-authored-by: Vylpes <getgravitysoftware@gmail.com>
This commit is contained in:
parent
1168898e57
commit
04a4a6204c
118 changed files with 13966 additions and 4027 deletions
11
tests/helpers/StringTools.test.ts
Normal file
11
tests/helpers/StringTools.test.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
import StringTools from "../../src/helpers/StringTools";
|
||||
|
||||
describe('Capitalise', () => {
|
||||
test('Expect sentence to be captilised', () => {
|
||||
const inputString = 'the big brown fox jumps over the lazy dog';
|
||||
|
||||
const result = StringTools.Capitalise(inputString);
|
||||
|
||||
expect(result).toBe('The Big Brown Fox Jumps Over The Lazy Dog');
|
||||
});
|
||||
});
|
57
tests/helpers/embeds/ErrorEmbed.test.ts
Normal file
57
tests/helpers/embeds/ErrorEmbed.test.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
import { Message } from "discord.js";
|
||||
import { ICommandContext } from "../../../src/contracts/ICommandContext";
|
||||
import ErrorEmbed from "../../../src/helpers/embeds/ErrorEmbed";
|
||||
|
||||
beforeEach(() => {
|
||||
process.env = {};
|
||||
});
|
||||
|
||||
describe('Constructor', () => {
|
||||
test('Expect properties to be set', () => {
|
||||
process.env = {
|
||||
EMBED_COLOUR_ERROR: '0xd52803'
|
||||
}
|
||||
|
||||
const message = {} as unknown as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'command',
|
||||
args: [],
|
||||
message: message
|
||||
};
|
||||
|
||||
const errorEmbed = new ErrorEmbed(context, 'Error Message');
|
||||
|
||||
expect(errorEmbed.color?.toString()).toBe('13969411'); // 0xd52803 in decimal
|
||||
expect(errorEmbed.description).toBe('Error Message');
|
||||
expect(errorEmbed.context).toBe(context);
|
||||
});
|
||||
});
|
||||
|
||||
describe('SendToCurrentChannel', () => {
|
||||
test('Expect embed to be sent to the current channel in context', () => {
|
||||
process.env = {
|
||||
EMBED_COLOUR_ERROR: '0xd52803'
|
||||
}
|
||||
|
||||
const messageChannelSend = jest.fn();
|
||||
|
||||
const message = {
|
||||
channel: {
|
||||
send: messageChannelSend
|
||||
}
|
||||
} as unknown as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'command',
|
||||
args: [],
|
||||
message: message
|
||||
};
|
||||
|
||||
const errorEmbed = new ErrorEmbed(context, 'Error Message');
|
||||
|
||||
errorEmbed.SendToCurrentChannel();
|
||||
|
||||
expect(messageChannelSend).toBeCalledWith(errorEmbed);
|
||||
});
|
||||
});
|
292
tests/helpers/embeds/EventEmbed.test.ts
Normal file
292
tests/helpers/embeds/EventEmbed.test.ts
Normal file
|
@ -0,0 +1,292 @@
|
|||
import { Guild, Message, TextChannel, User } from "discord.js";
|
||||
import { ICommandContext } from "../../../src/contracts/ICommandContext";
|
||||
import EventEmbed from "../../../src/helpers/embeds/EventEmbed";
|
||||
import SettingsHelper from "../../../src/helpers/SettingsHelper";
|
||||
|
||||
beforeEach(() => {
|
||||
process.env = {};
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
describe('Constructor', () => {
|
||||
test('Expect properties to be set', () => {
|
||||
const guild = {} as unknown as Guild;
|
||||
|
||||
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||
|
||||
expect(errorEmbed.color?.toString()).toBe('3166394'); // 0x3050ba in decimal
|
||||
expect(errorEmbed.title).toBe('Event Message');
|
||||
expect(errorEmbed.guild).toBe(guild);
|
||||
});
|
||||
});
|
||||
|
||||
describe('AddUser', () => {
|
||||
test('Given setThumbnail is false, add field WITHOUT user thumbnail', () => {
|
||||
process.env = {
|
||||
EMBED_COLOUR: '0xd52803',
|
||||
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||
}
|
||||
|
||||
const addField = jest.fn();
|
||||
const setThumbnail = jest.fn();
|
||||
|
||||
const guild = {} as unknown as Guild;
|
||||
|
||||
const user = {
|
||||
tag: 'USERTAG'
|
||||
} as unknown as User;
|
||||
|
||||
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||
|
||||
errorEmbed.addField = addField;
|
||||
errorEmbed.setThumbnail = setThumbnail;
|
||||
|
||||
errorEmbed.AddUser('User', user);
|
||||
|
||||
expect(addField).toBeCalledWith('User', '[object Object] `USERTAG`', true);
|
||||
expect(setThumbnail).not.toBeCalled();
|
||||
});
|
||||
|
||||
test('Given setThumbnail is true, add field WITH user thumbnail', () => {
|
||||
process.env = {
|
||||
EMBED_COLOUR: '0xd52803',
|
||||
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||
}
|
||||
|
||||
const addField = jest.fn();
|
||||
const setThumbnail = jest.fn();
|
||||
const displayAvatarURL = jest.fn()
|
||||
.mockReturnValue('image0.png');
|
||||
|
||||
const guild = {} as unknown as Guild;
|
||||
|
||||
const user = {
|
||||
tag: 'USERTAG',
|
||||
displayAvatarURL: displayAvatarURL
|
||||
} as unknown as User;
|
||||
|
||||
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||
|
||||
errorEmbed.addField = addField;
|
||||
errorEmbed.setThumbnail = setThumbnail;
|
||||
|
||||
errorEmbed.AddUser('User', user, true);
|
||||
|
||||
expect(addField).toBeCalledWith('User', '[object Object] `USERTAG`', true);
|
||||
expect(setThumbnail).toBeCalledWith('image0.png');
|
||||
expect(displayAvatarURL).toBeCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('AddReason', () => {
|
||||
test('Given a non-empty string is supplied, expect field with message', () => {
|
||||
const guild = {} as Guild;
|
||||
|
||||
const eventEmbed = new EventEmbed(guild, "Event Embed");
|
||||
|
||||
eventEmbed.addField = jest.fn();
|
||||
|
||||
eventEmbed.AddReason("Test reason");
|
||||
|
||||
expect(eventEmbed.addField).toBeCalledWith("Reason", "Test reason");
|
||||
});
|
||||
|
||||
test('Given an empty string is supplied, expect field with default message', () => {
|
||||
const guild = {} as Guild;
|
||||
|
||||
const eventEmbed = new EventEmbed(guild, "Event Embed");
|
||||
|
||||
eventEmbed.addField = jest.fn();
|
||||
|
||||
eventEmbed.AddReason("");
|
||||
|
||||
expect(eventEmbed.addField).toBeCalledWith("Reason", "*none*");
|
||||
});
|
||||
});
|
||||
|
||||
describe('SendToChannel', () => {
|
||||
test('Given channel can be found, expect embed to be sent to that channel', () => {
|
||||
const channelSend = jest.fn();
|
||||
|
||||
const channel = {
|
||||
send: channelSend
|
||||
} as unknown as TextChannel;
|
||||
|
||||
const guildChannelsCacheFind = jest.fn()
|
||||
.mockReturnValue(channel);
|
||||
|
||||
const guild = {
|
||||
channels: {
|
||||
cache: {
|
||||
find: guildChannelsCacheFind
|
||||
}
|
||||
}
|
||||
} as unknown as Guild;
|
||||
|
||||
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||
|
||||
errorEmbed.SendToChannel('channel-name');
|
||||
|
||||
expect(guildChannelsCacheFind).toBeCalledTimes(1);
|
||||
expect(channelSend).toBeCalledWith(errorEmbed);
|
||||
});
|
||||
|
||||
test('Given channel can NOT be found, expect error logged', () => {
|
||||
process.env = {
|
||||
EMBED_COLOUR: '0xd52803',
|
||||
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||
}
|
||||
|
||||
const guildChannelsCacheFind = jest.fn()
|
||||
.mockReturnValue(null);
|
||||
|
||||
const guild = {
|
||||
channels: {
|
||||
cache: {
|
||||
find: guildChannelsCacheFind
|
||||
}
|
||||
}
|
||||
} as unknown as Guild;
|
||||
|
||||
console.error = jest.fn();
|
||||
|
||||
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||
|
||||
errorEmbed.SendToChannel('channel-name');
|
||||
|
||||
expect(guildChannelsCacheFind).toBeCalledTimes(1);
|
||||
expect(console.error).toBeCalledWith('Unable to find channel channel-name');
|
||||
});
|
||||
});
|
||||
|
||||
describe('SendToMessageLogsChannel', () => {
|
||||
test('Given setting is set, expect SendToChannel to be called with value', async () => {
|
||||
const sendToChannel = jest.fn();
|
||||
const getSetting = jest.fn().mockResolvedValue("message-logs");
|
||||
|
||||
const guild = {
|
||||
id: "guildId"
|
||||
} as unknown as Guild;
|
||||
|
||||
SettingsHelper.GetSetting = getSetting;
|
||||
|
||||
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||
|
||||
errorEmbed.SendToChannel = sendToChannel;
|
||||
|
||||
await errorEmbed.SendToMessageLogsChannel();
|
||||
|
||||
expect(sendToChannel).toBeCalledWith('message-logs');
|
||||
expect(getSetting).toBeCalledWith("channels.logs.message", "guildId");
|
||||
});
|
||||
|
||||
test('Given setting is not set, expect function to return', async () => {
|
||||
const sendToChannel = jest.fn();
|
||||
const getSetting = jest.fn().mockResolvedValue(undefined);
|
||||
|
||||
const guild = {
|
||||
id: "guildId"
|
||||
} as unknown as Guild;
|
||||
|
||||
SettingsHelper.GetSetting = getSetting;
|
||||
|
||||
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||
|
||||
errorEmbed.SendToChannel = sendToChannel;
|
||||
|
||||
await errorEmbed.SendToMessageLogsChannel();
|
||||
|
||||
expect(sendToChannel).not.toBeCalled();
|
||||
expect(getSetting).toBeCalledWith("channels.logs.message", "guildId");
|
||||
});
|
||||
});
|
||||
|
||||
describe('SendToMemberLogsChannel', () => {
|
||||
test('Given setting is set, expect SendToChannel to be called with value', async () => {
|
||||
const sendToChannel = jest.fn();
|
||||
const getSetting = jest.fn().mockResolvedValue("member-logs");
|
||||
|
||||
const guild = {
|
||||
id: "guildId"
|
||||
} as unknown as Guild;
|
||||
|
||||
SettingsHelper.GetSetting = getSetting;
|
||||
|
||||
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||
|
||||
errorEmbed.SendToChannel = sendToChannel;
|
||||
|
||||
await errorEmbed.SendToMemberLogsChannel();
|
||||
|
||||
expect(sendToChannel).toBeCalledWith('member-logs');
|
||||
expect(getSetting).toBeCalledWith("channels.logs.member", "guildId");
|
||||
});
|
||||
|
||||
test('Given setting is not set, expect function to return', async () => {
|
||||
const sendToChannel = jest.fn();
|
||||
const getSetting = jest.fn().mockResolvedValue(undefined);
|
||||
|
||||
const guild = {
|
||||
id: "guildId"
|
||||
} as unknown as Guild;
|
||||
|
||||
SettingsHelper.GetSetting = getSetting;
|
||||
|
||||
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||
|
||||
errorEmbed.SendToChannel = sendToChannel;
|
||||
|
||||
await errorEmbed.SendToMemberLogsChannel();
|
||||
|
||||
expect(sendToChannel).not.toBeCalled();
|
||||
expect(getSetting).toBeCalledWith("channels.logs.member", "guildId");
|
||||
});
|
||||
});
|
||||
|
||||
describe('SendToModLogsChannel', () => {
|
||||
test('Given setting is set, expect SendToChannel to be called with value', async () => {
|
||||
const sendToChannel = jest.fn();
|
||||
const getSetting = jest.fn().mockResolvedValue("mod-logs");
|
||||
|
||||
const guild = {
|
||||
id: "guildId"
|
||||
} as unknown as Guild;
|
||||
|
||||
SettingsHelper.GetSetting = getSetting;
|
||||
|
||||
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||
|
||||
errorEmbed.SendToChannel = sendToChannel;
|
||||
|
||||
await errorEmbed.SendToModLogsChannel();
|
||||
|
||||
expect(sendToChannel).toBeCalledWith('mod-logs');
|
||||
expect(getSetting).toBeCalledWith("channels.logs.mod", "guildId");
|
||||
});
|
||||
|
||||
test('Given setting is not set, expect function to return', async () => {
|
||||
const sendToChannel = jest.fn();
|
||||
const getSetting = jest.fn().mockResolvedValue(undefined);
|
||||
|
||||
const guild = {
|
||||
id: "guildId"
|
||||
} as unknown as Guild;
|
||||
|
||||
SettingsHelper.GetSetting = getSetting;
|
||||
|
||||
const errorEmbed = new EventEmbed(guild, 'Event Message');
|
||||
|
||||
errorEmbed.SendToChannel = sendToChannel;
|
||||
|
||||
await errorEmbed.SendToModLogsChannel();
|
||||
|
||||
expect(sendToChannel).not.toBeCalled();
|
||||
expect(getSetting).toBeCalledWith("channels.logs.mod", "guildId");
|
||||
});
|
||||
});
|
407
tests/helpers/embeds/LogEmbed.test.ts
Normal file
407
tests/helpers/embeds/LogEmbed.test.ts
Normal file
|
@ -0,0 +1,407 @@
|
|||
import { Guild, Message, TextChannel, User } from "discord.js";
|
||||
import { ICommandContext } from "../../../src/contracts/ICommandContext";
|
||||
import LogEmbed from "../../../src/helpers/embeds/LogEmbed";
|
||||
import SettingsHelper from "../../../src/helpers/SettingsHelper";
|
||||
|
||||
beforeEach(() => {
|
||||
process.env = {};
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
describe('Constructor', () => {
|
||||
test('Expect properties to be set', () => {
|
||||
process.env = {
|
||||
EMBED_COLOUR: '0xd52803',
|
||||
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||
}
|
||||
|
||||
const messageChannelSend = jest.fn();
|
||||
|
||||
const message = {
|
||||
channel: {
|
||||
send: messageChannelSend
|
||||
}
|
||||
} as unknown as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'command',
|
||||
args: [],
|
||||
message: message
|
||||
};
|
||||
|
||||
const errorEmbed = new LogEmbed(context, 'Log Message');
|
||||
|
||||
expect(errorEmbed.color?.toString()).toBe('3166394'); // 0x3050ba in decimal
|
||||
expect(errorEmbed.title).toBe('Log Message');
|
||||
expect(errorEmbed.context).toBe(context);
|
||||
});
|
||||
});
|
||||
|
||||
describe('AddUser', () => {
|
||||
test('Given setThumbnail is false, add field WITHOUT user thumbnail', () => {
|
||||
process.env = {
|
||||
EMBED_COLOUR: '0xd52803',
|
||||
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||
}
|
||||
|
||||
const addField = jest.fn();
|
||||
const setThumbnail = jest.fn();
|
||||
|
||||
const user = {
|
||||
tag: 'USERTAG'
|
||||
} as unknown as User;
|
||||
|
||||
const messageChannelSend = jest.fn();
|
||||
|
||||
const message = {
|
||||
channel: {
|
||||
send: messageChannelSend
|
||||
},
|
||||
author: user
|
||||
} as unknown as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'command',
|
||||
args: [],
|
||||
message: message
|
||||
};
|
||||
|
||||
const errorEmbed = new LogEmbed(context, 'Event Message');
|
||||
|
||||
errorEmbed.addField = addField;
|
||||
errorEmbed.setThumbnail = setThumbnail;
|
||||
|
||||
errorEmbed.AddUser('User', user);
|
||||
|
||||
expect(addField).toBeCalledWith('User', '[object Object] `USERTAG`', true);
|
||||
expect(setThumbnail).not.toBeCalled();
|
||||
});
|
||||
|
||||
test('Given setThumbnail is true, add field WITH user thumbnail', () => {
|
||||
process.env = {
|
||||
EMBED_COLOUR: '0xd52803',
|
||||
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||
}
|
||||
|
||||
const addField = jest.fn();
|
||||
const setThumbnail = jest.fn();
|
||||
const displayAvatarURL = jest.fn()
|
||||
.mockReturnValue('image0.png');
|
||||
|
||||
const user = {
|
||||
tag: 'USERTAG',
|
||||
displayAvatarURL: displayAvatarURL
|
||||
} as unknown as User;
|
||||
|
||||
const messageChannelSend = jest.fn();
|
||||
|
||||
const message = {
|
||||
channel: {
|
||||
send: messageChannelSend
|
||||
},
|
||||
author: user
|
||||
} as unknown as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'command',
|
||||
args: [],
|
||||
message: message
|
||||
};
|
||||
|
||||
const errorEmbed = new LogEmbed(context, 'Event Message');
|
||||
|
||||
errorEmbed.addField = addField;
|
||||
errorEmbed.setThumbnail = setThumbnail;
|
||||
|
||||
errorEmbed.AddUser('User', user, true);
|
||||
|
||||
expect(addField).toBeCalledWith('User', '[object Object] `USERTAG`', true);
|
||||
expect(setThumbnail).toBeCalledWith('image0.png');
|
||||
expect(displayAvatarURL).toBeCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('SendToChannel', () => {
|
||||
test('Given channel can be found, expect embed to be sent to that channel', () => {
|
||||
process.env = {
|
||||
EMBED_COLOUR: '0xd52803',
|
||||
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||
}
|
||||
|
||||
const channelSend = jest.fn();
|
||||
|
||||
const channel = {
|
||||
send: channelSend
|
||||
} as unknown as TextChannel;
|
||||
|
||||
const guildChannelsCacheFind = jest.fn()
|
||||
.mockReturnValue(channel);
|
||||
|
||||
const guild = {
|
||||
channels: {
|
||||
cache: {
|
||||
find: guildChannelsCacheFind
|
||||
}
|
||||
}
|
||||
} as unknown as Guild;
|
||||
|
||||
const messageChannelSend = jest.fn();
|
||||
|
||||
const message = {
|
||||
channel: {
|
||||
send: messageChannelSend
|
||||
},
|
||||
guild: guild
|
||||
} as unknown as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'command',
|
||||
args: [],
|
||||
message: message
|
||||
};
|
||||
|
||||
const errorEmbed = new LogEmbed(context, 'Event Message');
|
||||
|
||||
errorEmbed.SendToChannel('channel-name');
|
||||
|
||||
expect(guildChannelsCacheFind).toBeCalledTimes(1);
|
||||
expect(channelSend).toBeCalledWith(errorEmbed);
|
||||
});
|
||||
|
||||
test('Given channel can NOT be found, expect error logged', () => {
|
||||
process.env = {
|
||||
EMBED_COLOUR: '0xd52803',
|
||||
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||
}
|
||||
|
||||
const guildChannelsCacheFind = jest.fn()
|
||||
.mockReturnValue(null);
|
||||
|
||||
const guild = {
|
||||
channels: {
|
||||
cache: {
|
||||
find: guildChannelsCacheFind
|
||||
}
|
||||
}
|
||||
} as unknown as Guild;
|
||||
|
||||
console.error = jest.fn();
|
||||
|
||||
const messageChannelSend = jest.fn();
|
||||
|
||||
const message = {
|
||||
channel: {
|
||||
send: messageChannelSend
|
||||
},
|
||||
guild: guild
|
||||
} as unknown as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'command',
|
||||
args: [],
|
||||
message: message
|
||||
};
|
||||
|
||||
const errorEmbed = new LogEmbed(context, 'Event Message');
|
||||
|
||||
errorEmbed.SendToChannel('channel-name');
|
||||
|
||||
expect(guildChannelsCacheFind).toBeCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('SendToMessageLogsChannel', () => {
|
||||
test('Given setting is set, expect SendToChannel to be called with value', async () => {
|
||||
const sendToChannel = jest.fn();
|
||||
const getSetting = jest.fn().mockResolvedValue("message-logs");
|
||||
|
||||
const guild = {
|
||||
id: "guildId"
|
||||
} as unknown as Guild;
|
||||
|
||||
const message = {
|
||||
guild: guild
|
||||
} as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'log',
|
||||
args: [],
|
||||
message: message
|
||||
};
|
||||
|
||||
SettingsHelper.GetSetting = getSetting;
|
||||
|
||||
const logEmbed = new LogEmbed(context, 'Event Message');
|
||||
|
||||
logEmbed.SendToChannel = sendToChannel;
|
||||
|
||||
await logEmbed.SendToMessageLogsChannel();
|
||||
|
||||
expect(sendToChannel).toBeCalledWith('message-logs');
|
||||
expect(getSetting).toBeCalledWith("channels.logs.message", "guildId");
|
||||
});
|
||||
|
||||
test('Given setting is not set, expect function to return', async () => {
|
||||
const sendToChannel = jest.fn();
|
||||
const getSetting = jest.fn().mockResolvedValue(undefined);
|
||||
|
||||
const guild = {
|
||||
id: "guildId"
|
||||
} as unknown as Guild;
|
||||
|
||||
const message = {
|
||||
guild: guild
|
||||
} as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'log',
|
||||
args: [],
|
||||
message: message
|
||||
};
|
||||
|
||||
SettingsHelper.GetSetting = getSetting;
|
||||
|
||||
const logEmbed = new LogEmbed(context, 'Event Message');
|
||||
|
||||
logEmbed.SendToChannel = sendToChannel;
|
||||
|
||||
await logEmbed.SendToMessageLogsChannel();
|
||||
|
||||
expect(sendToChannel).not.toBeCalled();
|
||||
expect(getSetting).toBeCalledWith("channels.logs.message", "guildId");
|
||||
});
|
||||
});
|
||||
|
||||
describe('SendToMemberLogsChannel', () => {
|
||||
test('Given setting is set, expect SendToChannel to be called with value', async () => {
|
||||
const sendToChannel = jest.fn();
|
||||
const getSetting = jest.fn().mockResolvedValue("member-logs");
|
||||
|
||||
const guild = {
|
||||
id: "guildId"
|
||||
} as unknown as Guild;
|
||||
|
||||
const message = {
|
||||
guild: guild
|
||||
} as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'log',
|
||||
args: [],
|
||||
message: message
|
||||
};
|
||||
|
||||
SettingsHelper.GetSetting = getSetting;
|
||||
|
||||
const logEmbed = new LogEmbed(context, 'Event Message');
|
||||
|
||||
logEmbed.SendToChannel = sendToChannel;
|
||||
|
||||
await logEmbed.SendToMemberLogsChannel();
|
||||
|
||||
expect(sendToChannel).toBeCalledWith('member-logs');
|
||||
expect(getSetting).toBeCalledWith("channels.logs.member", "guildId");
|
||||
});
|
||||
|
||||
test('Given setting is not set, expect function to return', async () => {
|
||||
const sendToChannel = jest.fn();
|
||||
const getSetting = jest.fn().mockResolvedValue(undefined);
|
||||
|
||||
const guild = {
|
||||
id: "guildId"
|
||||
} as unknown as Guild;
|
||||
|
||||
const message = {
|
||||
guild: guild
|
||||
} as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'log',
|
||||
args: [],
|
||||
message: message
|
||||
};
|
||||
|
||||
SettingsHelper.GetSetting = getSetting;
|
||||
|
||||
const logEmbed = new LogEmbed(context, 'Event Message');
|
||||
|
||||
logEmbed.SendToChannel = sendToChannel;
|
||||
|
||||
await logEmbed.SendToMemberLogsChannel();
|
||||
|
||||
expect(sendToChannel).not.toBeCalled();
|
||||
expect(getSetting).toBeCalledWith("channels.logs.member", "guildId");
|
||||
});
|
||||
});
|
||||
|
||||
describe('SendToModLogsChannel', () => {
|
||||
test('Given setting is set, expect SendToChannel to be called with value', async () => {
|
||||
const sendToChannel = jest.fn();
|
||||
const getSetting = jest.fn().mockResolvedValue("mod-logs");
|
||||
|
||||
const guild = {
|
||||
id: "guildId"
|
||||
} as unknown as Guild;
|
||||
|
||||
const message = {
|
||||
guild: guild
|
||||
} as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'log',
|
||||
args: [],
|
||||
message: message
|
||||
};
|
||||
|
||||
SettingsHelper.GetSetting = getSetting;
|
||||
|
||||
const logEmbed = new LogEmbed(context, 'Event Message');
|
||||
|
||||
logEmbed.SendToChannel = sendToChannel;
|
||||
|
||||
await logEmbed.SendToModLogsChannel();
|
||||
|
||||
expect(sendToChannel).toBeCalledWith('mod-logs');
|
||||
expect(getSetting).toBeCalledWith("channels.logs.mod", "guildId");
|
||||
});
|
||||
|
||||
test('Given setting is not set, expect function to return', async () => {
|
||||
const sendToChannel = jest.fn();
|
||||
const getSetting = jest.fn().mockResolvedValue(undefined);
|
||||
|
||||
const guild = {
|
||||
id: "guildId"
|
||||
} as unknown as Guild;
|
||||
|
||||
const message = {
|
||||
guild: guild
|
||||
} as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'log',
|
||||
args: [],
|
||||
message: message
|
||||
};
|
||||
|
||||
SettingsHelper.GetSetting = getSetting;
|
||||
|
||||
const logEmbed = new LogEmbed(context, 'Event Message');
|
||||
|
||||
logEmbed.SendToChannel = sendToChannel;
|
||||
|
||||
await logEmbed.SendToModLogsChannel();
|
||||
|
||||
expect(sendToChannel).not.toBeCalled();
|
||||
expect(getSetting).toBeCalledWith("channels.logs.mod", "guildId");
|
||||
});
|
||||
});
|
67
tests/helpers/embeds/PublicEmbed.test.ts
Normal file
67
tests/helpers/embeds/PublicEmbed.test.ts
Normal file
|
@ -0,0 +1,67 @@
|
|||
import { Guild, Message, TextChannel, User } from "discord.js";
|
||||
import { ICommandContext } from "../../../src/contracts/ICommandContext";
|
||||
import PublicEmbed from "../../../src/helpers/embeds/PublicEmbed";
|
||||
|
||||
beforeEach(() => {
|
||||
process.env = {};
|
||||
});
|
||||
|
||||
describe('Constructor', () => {
|
||||
test('Expect properties to be set', () => {
|
||||
process.env = {
|
||||
EMBED_COLOUR: '0xd52803',
|
||||
CHANNELS_LOGS_MESSAGE: 'message-logs',
|
||||
CHANNELS_LOGS_MEMBER: 'member-logs',
|
||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
||||
}
|
||||
|
||||
const messageChannelSend = jest.fn();
|
||||
|
||||
const message = {
|
||||
channel: {
|
||||
send: messageChannelSend
|
||||
}
|
||||
} as unknown as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'command',
|
||||
args: [],
|
||||
message: message
|
||||
};
|
||||
|
||||
const errorEmbed = new PublicEmbed(context, 'Log Message', 'Log Description');
|
||||
|
||||
expect(errorEmbed.color?.toString()).toBe('13969411'); // 0xd52803 in decimal
|
||||
expect(errorEmbed.title).toBe('Log Message');
|
||||
expect(errorEmbed.description).toBe('Log Description');
|
||||
expect(errorEmbed.context).toBe(context);
|
||||
});
|
||||
});
|
||||
|
||||
describe('SendToCurrentChannel', () => {
|
||||
test('Expect embed to be sent to the current channel in context', () => {
|
||||
process.env = {
|
||||
EMBED_COLOUR_ERROR: '0xd52803'
|
||||
}
|
||||
|
||||
const messageChannelSend = jest.fn();
|
||||
|
||||
const message = {
|
||||
channel: {
|
||||
send: messageChannelSend
|
||||
}
|
||||
} as unknown as Message;
|
||||
|
||||
const context: ICommandContext = {
|
||||
name: 'command',
|
||||
args: [],
|
||||
message: message
|
||||
};
|
||||
|
||||
const errorEmbed = new PublicEmbed(context, 'Message', 'Description');
|
||||
|
||||
errorEmbed.SendToCurrentChannel();
|
||||
|
||||
expect(messageChannelSend).toBeCalledWith(errorEmbed);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue