vylbot-app/tests/events/MessageEvents.test.ts
Vylpes 04a4a6204c
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>
2022-04-24 14:46:37 +01:00

648 lines
No EOL
20 KiB
TypeScript

import { Collection, Message, MessageAttachment, TextChannel } from "discord.js";
import MessageEvents from "../../src/events/MessageEvents";
beforeEach(() => {
process.env = {};
});
describe('MessageDelete', () => {
test('Given message was in a guild AND user was NOT a bot, expect message deleted embed to be sent', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const messageAttachments = new Collection<string, MessageAttachment>([
[
"0",
{
url: 'image0.png'
} as unknown as MessageAttachment
],
[
"1",
{
url: 'image1.png'
} as unknown as MessageAttachment
]
]);
const message = {
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
author: {
bot: false,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
channel: {},
content: 'Message Content',
attachments: messageAttachments
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageDelete(message);
expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
expect(messageAuthorDisplayAvatarURL).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('Message Deleted');
expect(embed.fields.length).toBe(4);
// Embed -> User Field
const embedFieldUser = embed.fields[0];
expect(embedFieldUser.name).toBe('User');
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
// Embed -> Channel Field
const embedFieldChannel = embed.fields[1];
expect(embedFieldChannel.name).toBe('Channel');
expect(embedFieldChannel.value).toBe('[object Object]');
// Embed -> Content Field
const embedFieldContent = embed.fields[2];
expect(embedFieldContent.name).toBe('Content');
expect(embedFieldContent.value).toBe('```Message Content```');
// Embed -> Attachments Field
const embedFieldAttachments = embed.fields[3];
expect(embedFieldAttachments.name).toBe('Attachments');
expect(embedFieldAttachments.value).toBe('```image0.png\nimage1.png```');
});
test('Given message was not sent in a guild, expect execution stopped', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const messageAttachments = new Collection<string, MessageAttachment>([
[
"0",
{
url: 'image0.png'
} as unknown as MessageAttachment
],
[
"1",
{
url: 'image1.png'
} as unknown as MessageAttachment
]
]);
const message = {
author: {
bot: false,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
channel: {},
content: 'Message Content',
attachments: messageAttachments
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageDelete(message);
expect(channelSend).not.toBeCalled();
expect(memberGuildChannelsCacheFind).not.toBeCalled();
expect(messageAuthorDisplayAvatarURL).not.toBeCalled();
expect(result.embeds.length).toBe(0);
});
test('Given author is a bot, expect execution stopped', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const messageAttachments = new Collection<string, MessageAttachment>([
[
"0",
{
url: 'image0.png'
} as unknown as MessageAttachment
],
[
"1",
{
url: 'image1.png'
} as unknown as MessageAttachment
]
]);
const message = {
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
author: {
bot: true,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
channel: {},
content: 'Message Content',
attachments: messageAttachments
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageDelete(message);
expect(channelSend).not.toBeCalled();
expect(memberGuildChannelsCacheFind).not.toBeCalled();
expect(messageAuthorDisplayAvatarURL).not.toBeCalled();
expect(result.embeds.length).toBe(0);
});
test('Given message does not contain any attachments, expect attachments field to be omitted', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const messageAttachments = new Collection<string, MessageAttachment>([]);
const message = {
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
author: {
bot: false,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
channel: {},
content: 'Message Content',
attachments: messageAttachments
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageDelete(message);
expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
expect(messageAuthorDisplayAvatarURL).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('Message Deleted');
expect(embed.fields.length).toBe(3);
// Embed -> User Field
const embedFieldUser = embed.fields[0];
expect(embedFieldUser.name).toBe('User');
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
// Embed -> Channel Field
const embedFieldChannel = embed.fields[1];
expect(embedFieldChannel.name).toBe('Channel');
expect(embedFieldChannel.value).toBe('[object Object]');
// Embed -> Content Field
const embedFieldContent = embed.fields[2];
expect(embedFieldContent.name).toBe('Content');
expect(embedFieldContent.value).toBe('```Message Content```');
});
});
describe('MessageUpdate', () => {
test('Given message is in a guild AND user is not a bot AND the content has actually changed, e xpect log embed to be sent', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const oldMessage = {
content: 'Old Message'
} as unknown as Message;
const newMessage = {
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
author: {
bot: false,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
content: 'New Message',
channel: {},
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
expect(messageAuthorDisplayAvatarURL).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('Message Edited');
expect(embed.fields.length).toBe(4);
// Embed -> User Field
const embedFieldUser = embed.fields[0];
expect(embedFieldUser.name).toBe('User');
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
expect(embedFieldUser.inline).toBeTruthy();
// Embed -> Channel Field
const embedFieldChannel = embed.fields[1];
expect(embedFieldChannel.name).toBe('Channel');
expect(embedFieldChannel.value).toBe('[object Object]');
expect(embedFieldChannel.inline).toBeTruthy();
// Embed -> Before Field
const embedFieldBefore = embed.fields[2];
expect(embedFieldBefore.name).toBe('Before');
expect(embedFieldBefore.value).toBe('```Old Message```');
// Embed -> After Field
const embedFieldAfter = embed.fields[3];
expect(embedFieldAfter.name).toBe('After');
expect(embedFieldAfter.value).toBe('```New Message```');
});
test('Given message was not in a guild, expect execution stopped', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const oldMessage = {
content: 'Old Message'
} as unknown as Message;
const newMessage = {
author: {
bot: false,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
content: 'New Message',
channel: {},
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).not.toBeCalled();
expect(memberGuildChannelsCacheFind).not.toBeCalled();
expect(messageAuthorDisplayAvatarURL).not.toBeCalled();
expect(result.embeds.length).toBe(0);
});
test('Given author is a bot, expect execution stopped', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const oldMessage = {
content: 'Old Message'
} as unknown as Message;
const newMessage = {
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
author: {
bot: true,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
content: 'New Message',
channel: {},
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).not.toBeCalled();
expect(memberGuildChannelsCacheFind).not.toBeCalled();
expect(messageAuthorDisplayAvatarURL).not.toBeCalled();
expect(result.embeds.length).toBe(0);
});
test('Given the message contents are the same, expect execution stopped', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const oldMessage = {
content: 'Message'
} as unknown as Message;
const newMessage = {
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
author: {
bot: false,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
content: 'Message',
channel: {},
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).not.toBeCalled();
expect(memberGuildChannelsCacheFind).not.toBeCalled();
expect(messageAuthorDisplayAvatarURL).not.toBeCalled();
expect(result.embeds.length).toBe(0);
});
test('Given Old Message did not have a content, expect field to account for this', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const oldMessage = {} as unknown as Message;
const newMessage = {
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
author: {
bot: false,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
content: 'New Message',
channel: {},
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
expect(messageAuthorDisplayAvatarURL).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('Message Edited');
expect(embed.fields.length).toBe(4);
// Embed -> User Field
const embedFieldUser = embed.fields[0];
expect(embedFieldUser.name).toBe('User');
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
expect(embedFieldUser.inline).toBeTruthy();
// Embed -> Channel Field
const embedFieldChannel = embed.fields[1];
expect(embedFieldChannel.name).toBe('Channel');
expect(embedFieldChannel.value).toBe('[object Object]');
expect(embedFieldChannel.inline).toBeTruthy();
// Embed -> Before Field
const embedFieldBefore = embed.fields[2];
expect(embedFieldBefore.name).toBe('Before');
expect(embedFieldBefore.value).toBe('```*none*```');
// Embed -> After Field
const embedFieldAfter = embed.fields[3];
expect(embedFieldAfter.name).toBe('After');
expect(embedFieldAfter.value).toBe('```New Message```');
});
test('Given New Message does not have a content, expect field to account for this', async () => {
process.env = {
CHANNELS_LOGS_MOD: 'mod-logs'
}
const channelSend = jest.fn();
const textChannel = {
name: 'mod-logs',
send: channelSend
} as unknown as TextChannel;
const memberGuildChannelsCacheFind = jest.fn()
.mockReturnValue(textChannel);
const messageAuthorDisplayAvatarURL = jest.fn();
const oldMessage = {
content: 'Old Message'
} as unknown as Message;
const newMessage = {
guild: {
channels: {
cache: {
find: memberGuildChannelsCacheFind
}
}
},
author: {
bot: false,
displayAvatarURL: messageAuthorDisplayAvatarURL,
tag: 'USERTAG'
},
channel: {},
} as unknown as Message;
const messageEvents = new MessageEvents();
const result = await messageEvents.messageUpdate(oldMessage, newMessage);
expect(channelSend).toBeCalledTimes(1);
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
expect(messageAuthorDisplayAvatarURL).toBeCalledTimes(1);
expect(result.embeds.length).toBe(1);
// Embed
const embed = result.embeds[0];
expect(embed.title).toBe('Message Edited');
expect(embed.fields.length).toBe(4);
// Embed -> User Field
const embedFieldUser = embed.fields[0];
expect(embedFieldUser.name).toBe('User');
expect(embedFieldUser.value).toBe('[object Object] `USERTAG`');
expect(embedFieldUser.inline).toBeTruthy();
// Embed -> Channel Field
const embedFieldChannel = embed.fields[1];
expect(embedFieldChannel.name).toBe('Channel');
expect(embedFieldChannel.value).toBe('[object Object]');
expect(embedFieldChannel.inline).toBeTruthy();
// Embed -> Before Field
const embedFieldBefore = embed.fields[2];
expect(embedFieldBefore.name).toBe('Before');
expect(embedFieldBefore.value).toBe('```Old Message```');
// Embed -> After Field
const embedFieldAfter = embed.fields[3];
expect(embedFieldAfter.name).toBe('After');
expect(embedFieldAfter.value).toBe('```*none*```');
});
});