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
This commit is contained in:
Vylpes 2022-03-29 18:19:54 +01:00 committed by GitHub
parent c8edd1b4c5
commit 6a00c49ef3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
53 changed files with 1816 additions and 373 deletions

View file

@ -1,3 +1,44 @@
import { mock } from "jest-mock-extended";
const connectionMock = mock<Connection>();
const qbuilderMock = mock<SelectQueryBuilder<any>>();
let repositoryMock = mock<Repository<any>>();
let settingMock = mock<Setting>();
jest.mock('typeorm', () => {
qbuilderMock.where.mockReturnThis();
qbuilderMock.select.mockReturnThis();
repositoryMock.createQueryBuilder.mockReturnValue(qbuilderMock);
repositoryMock.findOne.mockImplementation(async () => {
return settingMock;
});
connectionMock.getRepository.mockReturnValue(repositoryMock);
return {
getConnection: () => connectionMock,
createConnection: () => connectionMock,
BaseEntity: class Mock {},
ObjectType: () => {},
Entity: () => {},
InputType: () => {},
Index: () => {},
PrimaryColumn: () => {},
Column: () => {},
CreateDateColumn: () => {},
UpdateDateColumn: () => {},
OneToMany: () => {},
ManyToOne: () => {},
}
});
jest.mock("discord.js");
jest.mock("dotenv");
jest.mock("../../src/client/events");
jest.mock("../../src/client/util");
jest.mock("../../src/constants/DefaultValues");
import { CoreClient } from "../../src/client/client";
import { Client } from "discord.js";
@ -5,139 +46,84 @@ import * as dotenv from "dotenv";
import { Events } from "../../src/client/events";
import { Util } from "../../src/client/util";
import { Command } from "../../src/type/command";
import { mock } from "jest-mock-extended";
import { Event } from "../../src/type/event";
import DefaultValues from "../../src/constants/DefaultValues";
import { Connection, Repository, SelectQueryBuilder } from "typeorm";
import Setting from "../../src/entity/Setting";
jest.mock("discord.js");
jest.mock("dotenv");
jest.mock("../../src/client/events");
jest.mock("../../src/client/util");
beforeEach(() => {
jest.resetAllMocks();
jest.resetModules();
})
describe('Constructor', () => {
test('Constructor_ExpectSuccessfulInitialisation', () => {
test('Expect Successful Initialisation', () => {
const coreClient = new CoreClient();
expect(coreClient).toBeInstanceOf(Client);
expect(dotenv.config).toBeCalledTimes(1);
expect(Events).toBeCalledTimes(1);
expect(Util).toBeCalledTimes(1);
expect(DefaultValues.useDevPrefix).toBe(false);
});
test('Given devmode parameter is true, Expect devmode prefix to be true', () => {
const coreClient = new CoreClient(true);
expect(coreClient).toBeInstanceOf(Client);
expect(dotenv.config).toBeCalledTimes(1);
expect(Events).toBeCalledTimes(1);
expect(Util).toBeCalledTimes(1);
expect(DefaultValues.useDevPrefix).toBe(true);
});
});
describe('Start', () => {
test('Given Env Is Valid, Expect Successful Start', () => {
test('Given Env Is Valid, Expect Successful Start', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
BOT_TOKEN: "TOKEN",
};
const coreClient = new CoreClient();
await coreClient.start();
expect(() => coreClient.start()).not.toThrow();
expect(coreClient.on).toBeCalledWith("message", expect.any(Function));
expect(coreClient.on).toBeCalledWith("ready", expect.any(Function));
});
test('Given BOT_TOKEN Is Null, Expect Failure', () => {
process.env = {
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
test('Given BOT_TOKEN Is Null, Expect Failure', async () => {
process.env = {};
const consoleError = jest.fn();
console.error = consoleError;
const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("BOT_TOKEN is not defined in .env");
await coreClient.start();
expect(consoleError).toBeCalledWith("BOT_TOKEN is not defined in .env");
expect(coreClient.on).not.toBeCalled();
expect(coreClient.login).not.toBeCalled();
});
test('Given BOT_TOKEN Is Empty, Expect Failure', () => {
test('Given BOT_TOKEN Is Empty, Expect Failure', async () => {
process.env = {
BOT_TOKEN: '',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
const consoleError = jest.fn();
console.error = consoleError;
const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("BOT_TOKEN is not defined in .env");
});
test('Given BOT_PREFIX Is Null, Expect Failure', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("BOT_PREFIX is not defined in .env");
});
test('Given BOT_PREFIX Is Empty, Expect Failure', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: 'events',
}
const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("BOT_PREFIX is not defined in .env");
});
test('Given FOLDERS_COMMANDS Is Null, Expect Failure', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_EVENTS: 'events',
}
const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("FOLDERS_COMMANDS is not defined in .env");
});
test('Given FOLDERS_COMMANDS Is Empty, Expect Failure', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: '',
FOLDERS_EVENTS: 'events',
}
const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("FOLDERS_COMMANDS is not defined in .env");
});
test('Given FOLDERS_EVENTS Is Null, Expect Failure', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
}
const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("FOLDERS_EVENTS is not defined in .env");
});
test('Given FOLDERS_EVENTS Is Empty, Expect Failure', () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
FOLDERS_COMMANDS: 'commands',
FOLDERS_EVENTS: '',
}
const coreClient = new CoreClient();
expect(() => coreClient.start()).toThrow("FOLDERS_EVENTS is not defined in .env");
await coreClient.start();
expect(consoleError).toBeCalledWith("BOT_TOKEN is not defined in .env");
expect(coreClient.on).not.toBeCalled();
expect(coreClient.login).not.toBeCalled();
});
});

View file

@ -15,7 +15,7 @@ beforeEach(() => {
});
describe('LoadCommand', () => {
test('Given Successful Exection, Expect Successful Result', () => {
test('Given Successful Exection, Expect Successful Result', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
@ -45,13 +45,13 @@ describe('LoadCommand', () => {
const util = new Util();
const result = util.loadCommand("test", [ "first" ], message, commands);
const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeTruthy();
expect(cmd.execute).toBeCalled();
});
test('Given Member Is Null, Expect Failed Result', () => {
test('Given Member Is Null, Expect Failed Result', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
@ -74,14 +74,14 @@ describe('LoadCommand', () => {
const util = new Util();
const result = util.loadCommand("test", [ "first" ], message, commands);
const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Member is not part of message");
expect(cmd.execute).not.toBeCalled();
});
test('Given User Does Have Role, Expect Successful Result', () => {
test('Given User Does Have Role, Expect Successful Result', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
@ -111,13 +111,13 @@ describe('LoadCommand', () => {
const util = new Util();
const result = util.loadCommand("test", [ "first" ], message, commands);
const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeTruthy();
expect(cmd.execute).toBeCalled();
});
test('Given User Does Not Have Role, Expect Failed Result', () => {
test('Given User Does Not Have Role, Expect Failed Result', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
@ -148,14 +148,14 @@ describe('LoadCommand', () => {
const util = new Util();
const result = util.loadCommand("test", [ "first" ], message, commands);
const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("You require the `Moderator` role to run this command");
expect(cmd.execute).not.toBeCalled();
});
test('Given command is set to disabled, Expect command to not fire', () => {
test('Given command is set to disabled, Expect command to not fire', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
@ -189,7 +189,7 @@ describe('LoadCommand', () => {
const util = new Util();
const result = util.loadCommand("test", [ "first" ], message, commands);
const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command is disabled");
@ -197,7 +197,7 @@ describe('LoadCommand', () => {
expect(cmd.execute).not.toBeCalled();
});
test('Given command COMMANDS_DISABLED_MESSAGE is empty, Expect default message sent', () => {
test('Given command COMMANDS_DISABLED_MESSAGE is empty, Expect default message sent', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
@ -230,7 +230,7 @@ describe('LoadCommand', () => {
const util = new Util();
const result = util.loadCommand("test", [ "first" ], message, commands);
const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeFalsy();
expect(result.message).toBe("Command is disabled");
@ -238,7 +238,7 @@ describe('LoadCommand', () => {
expect(cmd.execute).not.toBeCalled();
});
test('Given a different command is disabled, Expect command to still fire', () => {
test('Given a different command is disabled, Expect command to still fire', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
@ -275,14 +275,14 @@ describe('LoadCommand', () => {
const util = new Util();
const result = util.loadCommand("test", [ "first" ], message, commands);
const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeTruthy();
expect(cmd.execute).toBeCalled();
expect(otherCmd.execute).not.toBeCalled();
});
test('Given command is not found in register, expect command not found error', () => {
test('Given command is not found in register, expect command not found error', async () => {
process.env = {
BOT_TOKEN: 'TOKEN',
BOT_PREFIX: '!',
@ -305,7 +305,7 @@ describe('LoadCommand', () => {
const util = new Util();
const result = util.loadCommand("test", [ "first" ], message, commands);
const result = await util.loadCommand("test", [ "first" ], message, commands);
expect(result.valid).toBeFalsy();
expect(result.message).toBe('Command not found');