* 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>
411 lines
12 KiB
TypeScript
411 lines
12 KiB
TypeScript
import { GuildMemberRoleManager, Message, Role as DiscordRole } from "discord.js";
|
|
import { mock } from "jest-mock-extended";
|
|
import Role from "../../src/commands/role";
|
|
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
|
|
|
beforeEach(() => {
|
|
process.env = {};
|
|
});
|
|
|
|
describe('Constructor', () => {
|
|
test('Expect properties are set', () => {
|
|
const role = new Role();
|
|
|
|
expect(role._category).toBe("General");
|
|
});
|
|
});
|
|
|
|
describe('Execute', () => {
|
|
test('Given no arguments were given, expect SendRolesList to be executed', async () => {
|
|
process.env = {
|
|
COMMANDS_ROLE_ROLES: 'One,Two'
|
|
};
|
|
|
|
const message = {} as unknown as Message;
|
|
|
|
const context: ICommandContext = {
|
|
name: 'role',
|
|
args: [],
|
|
message: message
|
|
};
|
|
|
|
const role = new Role();
|
|
|
|
role.SendRolesList = jest.fn();
|
|
role.ToggleRole = jest.fn();
|
|
|
|
await role.execute(context);
|
|
|
|
expect(role.SendRolesList).toBeCalledWith(context, ['One', 'Two']);
|
|
expect(role.ToggleRole).not.toBeCalled();
|
|
});
|
|
|
|
test('Given an argument was given, expect ToggleRole to be executed', async () => {
|
|
process.env = {
|
|
COMMANDS_ROLE_ROLES: 'One,Two'
|
|
};
|
|
|
|
const message = {} as unknown as Message;
|
|
|
|
const context: ICommandContext = {
|
|
name: 'role',
|
|
args: ['One'],
|
|
message: message
|
|
};
|
|
|
|
const role = new Role();
|
|
|
|
role.SendRolesList = jest.fn();
|
|
role.ToggleRole = jest.fn();
|
|
|
|
await role.execute(context);
|
|
|
|
expect(role.SendRolesList).not.toBeCalled();
|
|
expect(role.ToggleRole).toBeCalledWith(context, ['One', 'Two']);
|
|
});
|
|
});
|
|
|
|
describe('SendRolesList', () => {
|
|
test('Expect embed with roles to be sent to the current channel', () => {
|
|
process.env = {
|
|
BOT_PREFIX: '!'
|
|
};
|
|
|
|
const messageChannelSend = jest.fn();
|
|
|
|
const message = {
|
|
channel: {
|
|
send: messageChannelSend
|
|
}
|
|
} as unknown as Message;
|
|
|
|
const context: ICommandContext = {
|
|
name: 'role',
|
|
args: [],
|
|
message: message
|
|
};
|
|
|
|
const roles = ['One', 'Two'];
|
|
|
|
const role = new Role();
|
|
|
|
const result = role.SendRolesList(context, roles);
|
|
|
|
expect(messageChannelSend).toBeCalledTimes(1);
|
|
expect(result.embeds.length).toBe(1);
|
|
|
|
// Embed
|
|
const embed = result.embeds[0];
|
|
|
|
expect(embed.title).toBe('Roles');
|
|
expect(embed.description).toBe('Do !role <role> to get the role!\nOne\nTwo');
|
|
});
|
|
});
|
|
|
|
describe('ToggleRole', () => {
|
|
test('Given role name is a valid role AND user does not have the role, expect role to be added', async () => {
|
|
const discordRole = {} as unknown as DiscordRole;
|
|
|
|
const messageMemberRolesCacheFind = jest.fn()
|
|
.mockReturnValue(undefined);
|
|
const messageGuildRolesCacheFind = jest.fn()
|
|
.mockReturnValue(discordRole);
|
|
const messageChannelSend = jest.fn();
|
|
|
|
const message = {
|
|
member: {
|
|
roles: {
|
|
cache: {
|
|
find: messageMemberRolesCacheFind
|
|
}
|
|
}
|
|
},
|
|
guild: {
|
|
roles: {
|
|
cache: {
|
|
find: messageGuildRolesCacheFind
|
|
}
|
|
}
|
|
},
|
|
channel: {
|
|
send: messageChannelSend
|
|
}
|
|
} as unknown as Message;
|
|
|
|
const context: ICommandContext = {
|
|
name: 'role',
|
|
args: ['One'],
|
|
message: message
|
|
};
|
|
|
|
const roles = ['One', 'Two'];
|
|
|
|
const role = new Role();
|
|
|
|
role.AddRole = jest.fn();
|
|
role.RemoveRole = jest.fn();
|
|
|
|
const result = await role.ToggleRole(context, roles);
|
|
|
|
expect(messageMemberRolesCacheFind).toBeCalledTimes(1);
|
|
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
|
|
expect(messageChannelSend).not.toBeCalled();
|
|
expect(role.AddRole).toBeCalledWith(context, discordRole);
|
|
expect(role.RemoveRole).not.toBeCalled();
|
|
|
|
expect(result.embeds.length).toBe(0);
|
|
});
|
|
|
|
test('Given role name is a valid role AND user has the role, expect role to be removed', async () => {
|
|
const discordRole = {} as unknown as DiscordRole;
|
|
|
|
const messageMemberRolesCacheFind = jest.fn()
|
|
.mockReturnValue(discordRole);
|
|
const messageGuildRolesCacheFind = jest.fn()
|
|
.mockReturnValue(discordRole);
|
|
const messageChannelSend = jest.fn();
|
|
|
|
const message = {
|
|
member: {
|
|
roles: {
|
|
cache: {
|
|
find: messageMemberRolesCacheFind
|
|
}
|
|
}
|
|
},
|
|
guild: {
|
|
roles: {
|
|
cache: {
|
|
find: messageGuildRolesCacheFind
|
|
}
|
|
}
|
|
},
|
|
channel: {
|
|
send: messageChannelSend
|
|
}
|
|
} as unknown as Message;
|
|
|
|
const context: ICommandContext = {
|
|
name: 'role',
|
|
args: ['One'],
|
|
message: message
|
|
};
|
|
|
|
const roles = ['One', 'Two'];
|
|
|
|
const role = new Role();
|
|
|
|
role.AddRole = jest.fn();
|
|
role.RemoveRole = jest.fn();
|
|
|
|
const result = await role.ToggleRole(context, roles);
|
|
|
|
expect(messageMemberRolesCacheFind).toBeCalledTimes(1);
|
|
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
|
|
expect(messageChannelSend).not.toBeCalled();
|
|
expect(role.AddRole).not.toBeCalled();
|
|
expect(role.RemoveRole).toBeCalledWith(context, discordRole);
|
|
|
|
expect(result.embeds.length).toBe(0);
|
|
});
|
|
|
|
test('Given role requested is not in the roles array, expect role not assignable error', async () => {
|
|
const discordRole = {} as unknown as DiscordRole;
|
|
|
|
const messageMemberRolesCacheFind = jest.fn()
|
|
.mockReturnValue(undefined);
|
|
const messageGuildRolesCacheFind = jest.fn()
|
|
.mockReturnValue(discordRole);
|
|
const messageChannelSend = jest.fn();
|
|
|
|
const message = {
|
|
member: {
|
|
roles: {
|
|
cache: {
|
|
find: messageMemberRolesCacheFind
|
|
}
|
|
}
|
|
},
|
|
guild: {
|
|
roles: {
|
|
cache: {
|
|
find: messageGuildRolesCacheFind
|
|
}
|
|
}
|
|
},
|
|
channel: {
|
|
send: messageChannelSend
|
|
}
|
|
} as unknown as Message;
|
|
|
|
const context: ICommandContext = {
|
|
name: 'role',
|
|
args: ['Three'],
|
|
message: message
|
|
};
|
|
|
|
const roles = ['One', 'Two'];
|
|
|
|
const role = new Role();
|
|
|
|
role.AddRole = jest.fn();
|
|
role.RemoveRole = jest.fn();
|
|
|
|
const result = await role.ToggleRole(context, roles);
|
|
|
|
expect(messageMemberRolesCacheFind).not.toBeCalled();
|
|
expect(messageGuildRolesCacheFind).not.toBeCalled();
|
|
expect(messageChannelSend).toBeCalledTimes(1);
|
|
expect(role.AddRole).not.toBeCalled();
|
|
expect(role.RemoveRole).not.toBeCalled();
|
|
|
|
expect(result.embeds.length).toBe(1);
|
|
|
|
// Error Embed
|
|
const errorEmbed = result.embeds[0];
|
|
|
|
expect(errorEmbed.description).toBe("This role isn't marked as assignable, to see a list of assignable roles, run this command without any parameters");
|
|
});
|
|
|
|
test('Given the role is not in the guild, expect error', async () => {
|
|
const discordRole = {} as unknown as DiscordRole;
|
|
|
|
const messageMemberRolesCacheFind = jest.fn()
|
|
.mockReturnValue(undefined);
|
|
const messageGuildRolesCacheFind = jest.fn()
|
|
.mockReturnValue(undefined);
|
|
const messageChannelSend = jest.fn();
|
|
|
|
const message = {
|
|
member: {
|
|
roles: {
|
|
cache: {
|
|
find: messageMemberRolesCacheFind
|
|
}
|
|
}
|
|
},
|
|
guild: {
|
|
roles: {
|
|
cache: {
|
|
find: messageGuildRolesCacheFind
|
|
}
|
|
}
|
|
},
|
|
channel: {
|
|
send: messageChannelSend
|
|
}
|
|
} as unknown as Message;
|
|
|
|
const context: ICommandContext = {
|
|
name: 'role',
|
|
args: ['One'],
|
|
message: message
|
|
};
|
|
|
|
const roles = ['One', 'Two'];
|
|
|
|
const role = new Role();
|
|
|
|
role.AddRole = jest.fn();
|
|
role.RemoveRole = jest.fn();
|
|
|
|
const result = await role.ToggleRole(context, roles);
|
|
|
|
expect(messageMemberRolesCacheFind).not.toBeCalled();
|
|
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
|
|
expect(messageChannelSend).toBeCalledTimes(1);
|
|
expect(role.AddRole).not.toBeCalled();
|
|
expect(role.RemoveRole).not.toBeCalled();
|
|
|
|
expect(result.embeds.length).toBe(1);
|
|
|
|
// Error Embed
|
|
const errorEmbed = result.embeds[0];
|
|
|
|
expect(errorEmbed.description).toBe("The current server doesn't have this role. Please contact the server's moderators");
|
|
});
|
|
});
|
|
|
|
describe('AddRole', () => {
|
|
test('Expect role to be added to user', async () => {
|
|
const messageChannelSend = jest.fn();
|
|
|
|
const guildMemberRoleManager = mock<GuildMemberRoleManager>();
|
|
|
|
const message = {
|
|
member: {
|
|
roles: guildMemberRoleManager
|
|
},
|
|
channel: {
|
|
send: messageChannelSend
|
|
}
|
|
} as unknown as Message;
|
|
|
|
const context: ICommandContext = {
|
|
name: 'role',
|
|
args: ['One'],
|
|
message: message
|
|
};
|
|
|
|
const discordRole = {
|
|
name: 'One'
|
|
} as unknown as DiscordRole;
|
|
|
|
const role = new Role();
|
|
|
|
const result = await role.AddRole(context, discordRole);
|
|
|
|
expect(guildMemberRoleManager.add).toBeCalledWith(discordRole, "Toggled with role command");
|
|
expect(messageChannelSend).toBeCalled();
|
|
|
|
expect(result.embeds.length).toBe(1);
|
|
|
|
// Embed
|
|
const embed = result.embeds[0];
|
|
|
|
expect(embed.title).toBe('');
|
|
expect(embed.description).toBe('Gave role: One');
|
|
});
|
|
});
|
|
|
|
describe('RemoveRole', () => {
|
|
test('Expect role to be removed from user', async () => {
|
|
const messageChannelSend = jest.fn();
|
|
|
|
const guildMemberRoleManager = mock<GuildMemberRoleManager>();
|
|
|
|
const message = {
|
|
member: {
|
|
roles: guildMemberRoleManager
|
|
},
|
|
channel: {
|
|
send: messageChannelSend
|
|
}
|
|
} as unknown as Message;
|
|
|
|
const context: ICommandContext = {
|
|
name: 'role',
|
|
args: ['One'],
|
|
message: message
|
|
};
|
|
|
|
const discordRole = {
|
|
name: 'One'
|
|
} as unknown as DiscordRole;
|
|
|
|
const role = new Role();
|
|
|
|
const result = await role.RemoveRole(context, discordRole);
|
|
|
|
expect(guildMemberRoleManager.remove).toBeCalledWith(discordRole, "Toggled with role command");
|
|
expect(messageChannelSend).toBeCalled();
|
|
|
|
expect(result.embeds.length).toBe(1);
|
|
|
|
// Embed
|
|
const embed = result.embeds[0];
|
|
|
|
expect(embed.title).toBe('');
|
|
expect(embed.description).toBe('Removed role: One');
|
|
});
|
|
}); |