Compare commits
56 commits
main
...
feature/30
Author | SHA1 | Date | |
---|---|---|---|
056783bc44 | |||
554c274a7f | |||
952e7901ba | |||
5ed31d08d1 | |||
67eafa695c | |||
43751e5b02 | |||
974e298214 | |||
866beb0e04 | |||
8b0e93d324 | |||
2695219b20 | |||
56a04c2703 | |||
5f49117196 | |||
5beebba29a | |||
fdf52c2285 | |||
e3a583aa99 | |||
3a4665f907 | |||
60e736774d | |||
1aca8c7b60 | |||
b81f607702 | |||
4d20d9e608 | |||
10e51c8e90 | |||
476ff7cfc4 | |||
9d6c2d1bb2 | |||
789d5d6410 | |||
06eb7dbbb1 | |||
3f04286eac | |||
bc349c838a | |||
bf50aa6ed9 | |||
36d6cf91cf | |||
612ac0e012 | |||
dc90a7b4be | |||
400a3bb5a2 | |||
674a4ac614 | |||
e2ec042fcd | |||
8e87fd6618 | |||
d64f42ba3e | |||
da7e94c199 | |||
6fa462608e | |||
0505c8a77c | |||
a09e734eea | |||
8fd4ef4cc6 | |||
cadf47b1e9 | |||
76c2461efd | |||
2b35629506 | |||
0ce9ff5453 | |||
b1afc79dda | |||
7ccfa34562 | |||
f1c7cdc6d6 | |||
1371a30eee | |||
34398e5657 | |||
cd0737cf3b | |||
5f22a31398 | |||
932d3ac3d5 | |||
6412ad0ad4 | |||
f1c513bd16 | |||
942fea12bf |
66 changed files with 2275 additions and 9383 deletions
|
@ -7,7 +7,7 @@
|
||||||
# any secret values.
|
# any secret values.
|
||||||
|
|
||||||
BOT_TOKEN=
|
BOT_TOKEN=
|
||||||
BOT_VER=3.2.3
|
BOT_VER=3.3.0
|
||||||
BOT_AUTHOR=Vylpes
|
BOT_AUTHOR=Vylpes
|
||||||
BOT_OWNERID=147392775707426816
|
BOT_OWNERID=147392775707426816
|
||||||
BOT_CLIENTID=682942374040961060
|
BOT_CLIENTID=682942374040961060
|
||||||
|
@ -24,3 +24,5 @@ DB_AUTH_USER=dev
|
||||||
DB_AUTH_PASS=dev
|
DB_AUTH_PASS=dev
|
||||||
DB_SYNC=true
|
DB_SYNC=true
|
||||||
DB_LOGGING=true
|
DB_LOGGING=true
|
||||||
|
DB_ROOT_HOST=0.0.0.0
|
||||||
|
DB_DATA_LOCATION=./.temp/database
|
||||||
|
|
|
@ -12,11 +12,11 @@ jobs:
|
||||||
runs-on: node
|
runs-on: node
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v4
|
||||||
- name: Use Node.js
|
- name: Use Node.js
|
||||||
uses: actions/setup-node@v1
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 18.x
|
node-version: 20.x
|
||||||
- run: yarn install --frozen-lockfile
|
- run: yarn install --frozen-lockfile
|
||||||
- run: yarn build
|
- run: yarn build
|
||||||
- run: yarn test
|
- run: yarn test
|
||||||
|
@ -29,7 +29,7 @@ jobs:
|
||||||
needs: build
|
needs: build
|
||||||
runs-on: node
|
runs-on: node
|
||||||
steps:
|
steps:
|
||||||
- uses: https://github.com/appleboy/ssh-action@v1.0.0
|
- uses: https://github.com/appleboy/ssh-action@v1.1.0
|
||||||
env:
|
env:
|
||||||
DB_NAME: ${{ secrets.PROD_DB_NAME }}
|
DB_NAME: ${{ secrets.PROD_DB_NAME }}
|
||||||
DB_AUTH_USER: ${{ secrets.PROD_DB_AUTH_USER }}
|
DB_AUTH_USER: ${{ secrets.PROD_DB_AUTH_USER }}
|
||||||
|
|
|
@ -12,11 +12,11 @@ jobs:
|
||||||
runs-on: node
|
runs-on: node
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v4
|
||||||
- name: Use Node.js
|
- name: Use Node.js
|
||||||
uses: actions/setup-node@v1
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 18.x
|
node-version: 20.x
|
||||||
- run: yarn install --frozen-lockfile
|
- run: yarn install --frozen-lockfile
|
||||||
- run: yarn build
|
- run: yarn build
|
||||||
- run: yarn test
|
- run: yarn test
|
||||||
|
@ -29,7 +29,7 @@ jobs:
|
||||||
needs: build
|
needs: build
|
||||||
runs-on: node
|
runs-on: node
|
||||||
steps:
|
steps:
|
||||||
- uses: https://github.com/appleboy/ssh-action@v1.0.0
|
- uses: https://github.com/appleboy/ssh-action@v1.1.0
|
||||||
env:
|
env:
|
||||||
DB_NAME: ${{ secrets.STAGE_DB_NAME }}
|
DB_NAME: ${{ secrets.STAGE_DB_NAME }}
|
||||||
DB_AUTH_USER: ${{ secrets.STAGE_DB_AUTH_USER }}
|
DB_AUTH_USER: ${{ secrets.STAGE_DB_AUTH_USER }}
|
||||||
|
|
|
@ -14,11 +14,11 @@ jobs:
|
||||||
runs-on: node
|
runs-on: node
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v4
|
||||||
- name: Use Node.js
|
- name: Use Node.js
|
||||||
uses: actions/setup-node@v1
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: 18.x
|
node-version: 20.x
|
||||||
- run: yarn install --frozen-lockfile
|
- run: yarn install --frozen-lockfile
|
||||||
- run: yarn build
|
- run: yarn build
|
||||||
- run: yarn test
|
- run: yarn test
|
225
3
Normal file
225
3
Normal file
|
@ -0,0 +1,225 @@
|
||||||
|
import {ButtonInteraction} from "discord.js";
|
||||||
|
import List from "../../../../src/buttonEvents/304276391837302787/moons/list";
|
||||||
|
import UserSetting from "../../../../src/database/entities/UserSetting";
|
||||||
|
import Moon from "../../../../src/database/entities/304276391837302787/Moon";
|
||||||
|
|
||||||
|
describe("GIVEN interaction.guild is null", () => {
|
||||||
|
const interaction = {
|
||||||
|
guild: null,
|
||||||
|
reply: jest.fn(),
|
||||||
|
update: jest.fn(),
|
||||||
|
} as unknown as ButtonInteraction;
|
||||||
|
|
||||||
|
UserSetting.FetchOneByKey = jest.fn();
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT function to return", () => {
|
||||||
|
expect(interaction.reply).not.toHaveBeenCalled();
|
||||||
|
expect(interaction.update).not.toHaveBeenCalled();
|
||||||
|
expect(UserSetting.FetchOneByKey).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN userId parameter is undefined", () => {
|
||||||
|
const interaction = {
|
||||||
|
guild: {},
|
||||||
|
reply: jest.fn(),
|
||||||
|
update: jest.fn(),
|
||||||
|
customId: "moons list",
|
||||||
|
} as unknown as ButtonInteraction;
|
||||||
|
|
||||||
|
UserSetting.FetchOneByKey = jest.fn();
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT function to return", () => {
|
||||||
|
expect(interaction.reply).not.toHaveBeenCalled();
|
||||||
|
expect(interaction.update).not.toHaveBeenCalled();
|
||||||
|
expect(UserSetting.FetchOneByKey).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN page parameter is undefined", () => {
|
||||||
|
const interaction = {
|
||||||
|
guild: {},
|
||||||
|
reply: jest.fn(),
|
||||||
|
update: jest.fn(),
|
||||||
|
customId: "moons list userId",
|
||||||
|
} as unknown as ButtonInteraction;
|
||||||
|
|
||||||
|
UserSetting.FetchOneByKey = jest.fn();
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT function to return", () => {
|
||||||
|
expect(interaction.reply).not.toHaveBeenCalled();
|
||||||
|
expect(interaction.update).not.toHaveBeenCalled();
|
||||||
|
expect(UserSetting.FetchOneByKey).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN no moons for the user is returned", () => {
|
||||||
|
const interaction = {
|
||||||
|
guild: {
|
||||||
|
members: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue({
|
||||||
|
user: {
|
||||||
|
username: "username",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
update: jest.fn(),
|
||||||
|
customId: "moons list userId 0",
|
||||||
|
} as unknown as ButtonInteraction;
|
||||||
|
|
||||||
|
UserSetting.FetchOneByKey = jest.fn();
|
||||||
|
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue(undefined)
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT moons function to be called", () => {
|
||||||
|
expect(Moon.FetchPaginatedMoonsByUserId).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT error replied", () => {
|
||||||
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.reply).toHaveBeenCalledWith("username does not have any moons or page is invalid.");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN member is not in cache", () => {
|
||||||
|
const interaction = {
|
||||||
|
guild: {
|
||||||
|
members: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue(undefined),
|
||||||
|
},
|
||||||
|
fetch: jest.fn().mockResolvedValue({
|
||||||
|
user: {
|
||||||
|
username: "username",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
update: jest.fn(),
|
||||||
|
customId: "moons list userId 0",
|
||||||
|
} as unknown as ButtonInteraction;
|
||||||
|
|
||||||
|
UserSetting.FetchOneByKey = jest.fn();
|
||||||
|
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue(undefined)
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT API to be called", () => {
|
||||||
|
expect(interaction.guild?.members.fetch).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.guild?.members.fetch).toHaveBeenCalledWith("userId");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT error replied with username", () => {
|
||||||
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.reply).toHaveBeenCalledWith("username does not have any moons or page is invalid.");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN member can not be found", () => {
|
||||||
|
const interaction = {
|
||||||
|
guild: {
|
||||||
|
members: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue(undefined),
|
||||||
|
},
|
||||||
|
fetch: jest.fn().mockResolvedValue(undefined),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
update: jest.fn(),
|
||||||
|
customId: "moons list userId 0",
|
||||||
|
} as unknown as ButtonInteraction;
|
||||||
|
|
||||||
|
UserSetting.FetchOneByKey = jest.fn();
|
||||||
|
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue(undefined)
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT API to be called", () => {
|
||||||
|
expect(interaction.guild?.members.fetch).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.guild?.members.fetch).toHaveBeenCalledWith("userId");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT error replied with username", () => {
|
||||||
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.reply).toHaveBeenCalledWith("This user does not have any moons or page is invalid.");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN no moons on current page", () => {
|
||||||
|
const interaction = {
|
||||||
|
guild: {
|
||||||
|
members: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue({
|
||||||
|
user: {
|
||||||
|
username: "username",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
update: jest.fn(),
|
||||||
|
customId: "moons list userId 0",
|
||||||
|
} as unknown as ButtonInteraction;
|
||||||
|
|
||||||
|
UserSetting.FetchOneByKey = jest.fn();
|
||||||
|
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue([
|
||||||
|
[],
|
||||||
|
0,
|
||||||
|
]);
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
test.todo("EXPECT description to say so");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN happy flow", () => {
|
||||||
|
test.todo("EXPECT moons to be fetched");
|
||||||
|
|
||||||
|
test.todo("EXPECT embed to be updated");
|
||||||
|
|
||||||
|
test.todo("EXPECT row to be updated");
|
||||||
|
|
||||||
|
describe("GIVEN it is the first page", () => {
|
||||||
|
test.todo("EXPECT Previous button to be disabled");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN it is the last page", () => {
|
||||||
|
test.todo("EXPECT Next button to be disabled");
|
||||||
|
|
||||||
|
describe("GIVEN there are more moons in the counter than in the database", () => {
|
||||||
|
test.todo("EXPECT untracked counter to be present");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN no moons on the current page", () => {
|
||||||
|
test.todo("EXPECT Next button to be disabled");
|
||||||
|
});
|
||||||
|
});
|
|
@ -7,7 +7,6 @@ commands.disabled: Disabled commands, separated by commas (Default: "")
|
||||||
|
|
||||||
role.moderator: The moderator role name (Default: "Moderator")
|
role.moderator: The moderator role name (Default: "Moderator")
|
||||||
role.administrator: The administrator role name (Default: "Administrator")
|
role.administrator: The administrator role name (Default: "Administrator")
|
||||||
role.muted: The muted role name (Default: "Muted")
|
|
||||||
|
|
||||||
rules.file: The location of the rules file (Default: "data/rules/rules")
|
rules.file: The location of the rules file (Default: "data/rules/rules")
|
||||||
|
|
||||||
|
|
10
database/3.3.0/1719856023429-CreateMoon/Up/01-Moon.sql
Normal file
10
database/3.3.0/1719856023429-CreateMoon/Up/01-Moon.sql
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
CREATE TABLE `moon` (
|
||||||
|
`Id` varchar(255) NOT NULL,
|
||||||
|
`WhenCreated` datetime NOT NULL,
|
||||||
|
`WhenUpdated` datetime NOT NULL,
|
||||||
|
`MoonNumber` int NOT NULL,
|
||||||
|
`UserId` varchar(255) NOT NULL,
|
||||||
|
`Description` varchar(255) NOT NULL,
|
||||||
|
`WhenArchived` datetime NULL
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
CREATE TABLE `user_setting` (
|
||||||
|
`Id` varchar(255) NOT NULL,
|
||||||
|
`WhenCreated` datetime NOT NULL,
|
||||||
|
`WhenUpdated` datetime NOT NULL,
|
||||||
|
`UserId` varchar(255) NOT NULL,
|
||||||
|
`Key` varchar(255) NOT NULL,
|
||||||
|
`Value` varchar(255) NOT NULL
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
@ -0,0 +1,2 @@
|
||||||
|
ALTER TABLE user_setting
|
||||||
|
ADD PRIMARY KEY (Id);
|
20
package.json
20
package.json
|
@ -8,7 +8,7 @@
|
||||||
"clean": "rm -rf node_modules/ dist/",
|
"clean": "rm -rf node_modules/ dist/",
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"start": "node ./dist/vylbot",
|
"start": "node ./dist/vylbot",
|
||||||
"test": "echo true",
|
"test": "jest",
|
||||||
"db:up": "typeorm migration:run -d dist/database/dataSources/appDataSource.js",
|
"db:up": "typeorm migration:run -d dist/database/dataSources/appDataSource.js",
|
||||||
"db:down": "typeorm migration:revert -d dist/database/dataSources/appDataSource.js",
|
"db:down": "typeorm migration:revert -d dist/database/dataSources/appDataSource.js",
|
||||||
"db:create": "typeorm migration:create ./src/database/migrations",
|
"db:create": "typeorm migration:create ./src/database/migrations",
|
||||||
|
@ -28,26 +28,24 @@
|
||||||
"funding": "https://ko-fi.com/vylpes",
|
"funding": "https://ko-fi.com/vylpes",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@discordjs/rest": "^2.0.0",
|
"@discordjs/rest": "^2.0.0",
|
||||||
"@types/jest": "^29.0.0",
|
"@types/uuid": "^10.0.0",
|
||||||
"@types/uuid": "^9.0.0",
|
|
||||||
"discord.js": "^14.3.0",
|
"discord.js": "^14.3.0",
|
||||||
"dotenv": "^16.0.0",
|
"dotenv": "^16.0.0",
|
||||||
"emoji-regex": "^10.0.0",
|
"emoji-regex": "^10.0.0",
|
||||||
"jest": "^29.0.0",
|
"minimatch": "10.0.1",
|
||||||
"jest-mock-extended": "^3.0.0",
|
|
||||||
"minimatch": "9.0.3",
|
|
||||||
"mysql": "^2.18.1",
|
"mysql": "^2.18.1",
|
||||||
"random-bunny": "^2.1.6",
|
"random-bunny": "^2.1.6",
|
||||||
"ts-jest": "^29.0.0",
|
"typeorm": "^0.3.20"
|
||||||
"typeorm": "0.3.20"
|
|
||||||
},
|
},
|
||||||
"resolutions": {
|
"resolutions": {
|
||||||
"**/semver": "^7.5.2",
|
|
||||||
"**/undici": "^6.0.0"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^20.0.0",
|
"@types/jest": "^29.5.12",
|
||||||
|
"@types/node": "^22.0.0",
|
||||||
|
"jest": "^29.7.0",
|
||||||
|
"jest-mock-extended": "^3.0.7",
|
||||||
"np": "^10.0.0",
|
"np": "^10.0.0",
|
||||||
|
"ts-jest": "^29.2.4",
|
||||||
"typescript": "^5.0.0"
|
"typescript": "^5.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
14
src/buttonEvents/304276391837302787/moons.ts
Normal file
14
src/buttonEvents/304276391837302787/moons.ts
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import {ButtonInteraction} from "discord.js";
|
||||||
|
import {ButtonEvent} from "../../type/buttonEvent";
|
||||||
|
import List from "./moons/list";
|
||||||
|
|
||||||
|
export default class Moons extends ButtonEvent {
|
||||||
|
public override async execute(interaction: ButtonInteraction): Promise<void> {
|
||||||
|
const action = interaction.customId.split(" ")[1];
|
||||||
|
|
||||||
|
switch (action) {
|
||||||
|
case "list":
|
||||||
|
await List(interaction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
68
src/buttonEvents/304276391837302787/moons/list.ts
Normal file
68
src/buttonEvents/304276391837302787/moons/list.ts
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
import {ActionRowBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, EmbedBuilder} from "discord.js";
|
||||||
|
import Moon from "../../../database/entities/304276391837302787/Moon";
|
||||||
|
import EmbedColours from "../../../constants/EmbedColours";
|
||||||
|
import UserSetting from "../../../database/entities/UserSetting";
|
||||||
|
|
||||||
|
export default async function List(interaction: ButtonInteraction) {
|
||||||
|
if (!interaction.guild) return;
|
||||||
|
|
||||||
|
const userId = interaction.customId.split(" ")[2];
|
||||||
|
const page = interaction.customId.split(" ")[3];
|
||||||
|
|
||||||
|
if (!userId || !page) return;
|
||||||
|
|
||||||
|
const pageNumber = Number(page);
|
||||||
|
|
||||||
|
const member = interaction.guild.members.cache.find(x => x.user.id == userId) || await interaction.guild.members.fetch(userId);
|
||||||
|
|
||||||
|
const pageLength = 10;
|
||||||
|
|
||||||
|
const moons = await Moon.FetchPaginatedMoonsByUserId(userId, pageLength, pageNumber);
|
||||||
|
|
||||||
|
if (!moons) {
|
||||||
|
await interaction.reply(`${member?.user.username ?? "This user"} does not have any moons or page is invalid.`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const moonSetting = await UserSetting.FetchOneByKey(userId, "moons");
|
||||||
|
const totalMoons = moonSetting && Number(moonSetting.Value) ? Number(moonSetting.Value) : 0;
|
||||||
|
|
||||||
|
const totalPages = Math.ceil(moons[1] / pageLength);
|
||||||
|
|
||||||
|
let description = ["*none*"];
|
||||||
|
|
||||||
|
if (moons[0].length > 0) {
|
||||||
|
description = moons[0].flatMap(x => `**${x.MoonNumber} -** ${x.Description.slice(0, 15)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const moonDifference = totalMoons - moons[1];
|
||||||
|
const isLastPage = pageNumber + 1 == totalPages || moons[0].length == 0;
|
||||||
|
|
||||||
|
if (isLastPage && moonDifference > 0) {
|
||||||
|
description.push(`...plus ${moonDifference} more untracked`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setTitle(`${member.user.username}'s Moons`)
|
||||||
|
.setColor(EmbedColours.Ok)
|
||||||
|
.setDescription(description.join("\n"))
|
||||||
|
.setFooter({ text: `Page ${pageNumber + 1} of ${totalPages} · ${totalMoons} moons` });
|
||||||
|
|
||||||
|
const row = new ActionRowBuilder<ButtonBuilder>()
|
||||||
|
.addComponents(
|
||||||
|
new ButtonBuilder()
|
||||||
|
.setCustomId(`moons list ${userId} ${pageNumber - 1}`)
|
||||||
|
.setLabel("Previous")
|
||||||
|
.setStyle(ButtonStyle.Primary)
|
||||||
|
.setDisabled(pageNumber == 0),
|
||||||
|
new ButtonBuilder()
|
||||||
|
.setCustomId(`moons list ${userId} ${pageNumber + 1}`)
|
||||||
|
.setLabel("Next")
|
||||||
|
.setStyle(ButtonStyle.Primary)
|
||||||
|
.setDisabled(isLastPage));
|
||||||
|
|
||||||
|
await interaction.update({
|
||||||
|
embeds: [ embed ],
|
||||||
|
components: [ row ],
|
||||||
|
});
|
||||||
|
}
|
48
src/commands/304276391837302787/moons.ts
Normal file
48
src/commands/304276391837302787/moons.ts
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
import { Command } from "../../type/command";
|
||||||
|
import { CommandInteraction, SlashCommandBuilder } from "discord.js";
|
||||||
|
import ListMoons from "./moons/list";
|
||||||
|
import AddMoon from "./moons/add";
|
||||||
|
|
||||||
|
export default class Moons extends Command {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.CommandBuilder = new SlashCommandBuilder()
|
||||||
|
.setName("moons")
|
||||||
|
.setDescription("View and create moons")
|
||||||
|
.addSubcommand(subcommand =>
|
||||||
|
subcommand
|
||||||
|
.setName('list')
|
||||||
|
.setDescription('List moons you have obtained')
|
||||||
|
.addUserOption(option =>
|
||||||
|
option
|
||||||
|
.setName("user")
|
||||||
|
.setDescription("The user to view (Defaults to yourself)"))
|
||||||
|
.addNumberOption(option =>
|
||||||
|
option
|
||||||
|
.setName("page")
|
||||||
|
.setDescription("The page to start with")))
|
||||||
|
.addSubcommand(subcommand =>
|
||||||
|
subcommand
|
||||||
|
.setName('add')
|
||||||
|
.setDescription('Add a moon to your count!')
|
||||||
|
.addStringOption(option =>
|
||||||
|
option
|
||||||
|
.setName("description")
|
||||||
|
.setDescription("What deserved a moon?")
|
||||||
|
.setRequired(true)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async execute(interaction: CommandInteraction) {
|
||||||
|
if (!interaction.isChatInputCommand()) return;
|
||||||
|
|
||||||
|
switch (interaction.options.getSubcommand()) {
|
||||||
|
case "list":
|
||||||
|
await ListMoons(interaction);
|
||||||
|
break;
|
||||||
|
case "add":
|
||||||
|
await AddMoon(interaction);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
40
src/commands/304276391837302787/moons/add.ts
Normal file
40
src/commands/304276391837302787/moons/add.ts
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
import {CommandInteraction, EmbedBuilder} from "discord.js";
|
||||||
|
import Moon from "../../../database/entities/304276391837302787/Moon";
|
||||||
|
import EmbedColours from "../../../constants/EmbedColours";
|
||||||
|
import UserSetting from "../../../database/entities/UserSetting";
|
||||||
|
|
||||||
|
export default async function AddMoon(interaction: CommandInteraction) {
|
||||||
|
const description = interaction.options.get("description", true).value?.toString();
|
||||||
|
|
||||||
|
if (!description || description.length > 255) {
|
||||||
|
await interaction.reply("Name must be less than 255 characters!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let moonSetting = await UserSetting.FetchOneByKey(interaction.user.id, "moons");
|
||||||
|
const moonCount = moonSetting && Number(moonSetting.Value) ? Number(moonSetting.Value) : 0;
|
||||||
|
|
||||||
|
if (moonSetting) {
|
||||||
|
moonSetting.UpdateValue(`${moonCount + 1}`);
|
||||||
|
} else {
|
||||||
|
moonSetting = new UserSetting(interaction.user.id, "moons", `${moonCount + 1}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
await moonSetting.Save(UserSetting, moonSetting);
|
||||||
|
|
||||||
|
const allMoons = await Moon.FetchMoonCountByUserId(interaction.user.id);
|
||||||
|
|
||||||
|
const moonNumber = allMoons + 1;
|
||||||
|
|
||||||
|
const moon = new Moon(moonNumber, description, interaction.user.id);
|
||||||
|
|
||||||
|
await moon.Save(Moon, moon);
|
||||||
|
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setTitle(`${interaction.user.globalName} Got A Moon!`)
|
||||||
|
.setColor(EmbedColours.Moon)
|
||||||
|
.setDescription(`**${moon.MoonNumber} -** ${moon.Description}`)
|
||||||
|
.setThumbnail("https://cdn.discordapp.com/emojis/374131312182689793.webp?size=96&quality=lossless");
|
||||||
|
|
||||||
|
await interaction.reply({ embeds: [ embed ] });
|
||||||
|
}
|
55
src/commands/304276391837302787/moons/list.ts
Normal file
55
src/commands/304276391837302787/moons/list.ts
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
import {ActionRowBuilder, ButtonBuilder, ButtonStyle, CommandInteraction, EmbedBuilder} from "discord.js";
|
||||||
|
import Moon from "../../../database/entities/304276391837302787/Moon";
|
||||||
|
import EmbedColours from "../../../constants/EmbedColours";
|
||||||
|
import UserSetting from "../../../database/entities/UserSetting";
|
||||||
|
|
||||||
|
export default async function ListMoons(interaction: CommandInteraction) {
|
||||||
|
const user = interaction.options.get("user")?.user ?? interaction.user;
|
||||||
|
const page = interaction.options.get("page")?.value as number ?? 0;
|
||||||
|
|
||||||
|
const pageLength = 10;
|
||||||
|
|
||||||
|
const moons = await Moon.FetchPaginatedMoonsByUserId(user.id, pageLength, page);
|
||||||
|
|
||||||
|
const moonSetting = await UserSetting.FetchOneByKey(interaction.user.id, "moons");
|
||||||
|
const totalMoons = moonSetting && Number(moonSetting.Value) ? Number(moonSetting.Value) : 0;
|
||||||
|
|
||||||
|
const totalPages = Math.ceil(moons[1] / pageLength);
|
||||||
|
|
||||||
|
let description = ["*none*"];
|
||||||
|
|
||||||
|
if (moons[0].length > 0) {
|
||||||
|
description = moons[0].flatMap(x => `**${x.MoonNumber} -** ${x.Description.slice(0, 15)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const moonDifference = totalMoons - moons[1];
|
||||||
|
const isLastPage = page + 1 == totalPages || moons[0].length == 0;
|
||||||
|
|
||||||
|
if (isLastPage && moonDifference > 0) {
|
||||||
|
description.push(`...plus ${moonDifference} more untracked`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setTitle(`${user.username}'s Moons`)
|
||||||
|
.setColor(EmbedColours.Ok)
|
||||||
|
.setDescription(description.join("\n"))
|
||||||
|
.setFooter({ text: `Page ${page + 1} of ${totalPages} · ${totalMoons} moons` });
|
||||||
|
|
||||||
|
const row = new ActionRowBuilder<ButtonBuilder>()
|
||||||
|
.addComponents(
|
||||||
|
new ButtonBuilder()
|
||||||
|
.setCustomId(`moons list ${user.id} ${page - 1}`)
|
||||||
|
.setLabel("Previous")
|
||||||
|
.setStyle(ButtonStyle.Primary)
|
||||||
|
.setDisabled(page == 0),
|
||||||
|
new ButtonBuilder()
|
||||||
|
.setCustomId(`moons list ${user.id} ${page + 1}`)
|
||||||
|
.setLabel("Next")
|
||||||
|
.setStyle(ButtonStyle.Primary)
|
||||||
|
.setDisabled(isLastPage));
|
||||||
|
|
||||||
|
await interaction.reply({
|
||||||
|
embeds: [ embed ],
|
||||||
|
components: [ row ],
|
||||||
|
});
|
||||||
|
}
|
|
@ -93,7 +93,7 @@ export default class Audits extends Command {
|
||||||
private async SendAuditForUser(interaction: CommandInteraction) {
|
private async SendAuditForUser(interaction: CommandInteraction) {
|
||||||
if (!interaction.guildId) return;
|
if (!interaction.guildId) return;
|
||||||
|
|
||||||
const user = interaction.options.get('target')?.user;
|
const user = interaction.options.get('target', true).user!;
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
await interaction.reply("User not found.");
|
await interaction.reply("User not found.");
|
||||||
|
@ -191,7 +191,7 @@ export default class Audits extends Command {
|
||||||
private async AddAudit(interaction: CommandInteraction) {
|
private async AddAudit(interaction: CommandInteraction) {
|
||||||
if (!interaction.guildId) return;
|
if (!interaction.guildId) return;
|
||||||
|
|
||||||
const user = interaction.options.get('target')?.user;
|
const user = interaction.options.get('target', true).user!;
|
||||||
const auditType = interaction.options.get('type');
|
const auditType = interaction.options.get('type');
|
||||||
const reasonInput = interaction.options.get('reason');
|
const reasonInput = interaction.options.get('reason');
|
||||||
|
|
||||||
|
|
|
@ -62,7 +62,6 @@ export default class Ban extends Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
await member.ban();
|
await member.ban();
|
||||||
await interaction.reply(`\`${targetUser.user.tag}\` has been banned.`);
|
|
||||||
|
|
||||||
const channelName = await SettingsHelper.GetSetting('channels.logs.mod', interaction.guildId);
|
const channelName = await SettingsHelper.GetSetting('channels.logs.mod', interaction.guildId);
|
||||||
|
|
||||||
|
@ -74,7 +73,29 @@ export default class Ban extends Command {
|
||||||
await channel.send({ embeds: [ logEmbed ]});
|
await channel.send({ embeds: [ logEmbed ]});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const dmEmbed = new EmbedBuilder()
|
||||||
|
.setColor(EmbedColours.Ok)
|
||||||
|
.setTitle(interaction.guild.name)
|
||||||
|
.setDescription("You have been banned by a moderator.")
|
||||||
|
.addFields([
|
||||||
|
{
|
||||||
|
name: "Reason",
|
||||||
|
value: reason,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
let replyText = "Successfully banned user.";
|
||||||
|
|
||||||
|
try {
|
||||||
|
const dmChannel = await targetUser.user!.createDM();
|
||||||
|
await dmChannel.send({ embeds: [ dmEmbed ] });
|
||||||
|
} catch {
|
||||||
|
replyText += " *Note: I was unable to DM the user the reason.*";
|
||||||
|
}
|
||||||
|
|
||||||
const audit = new Audit(targetUser.user.id, AuditType.Ban, reason, interaction.user.id, interaction.guildId);
|
const audit = new Audit(targetUser.user.id, AuditType.Ban, reason, interaction.user.id, interaction.guildId);
|
||||||
await audit.Save(Audit, audit);
|
await audit.Save(Audit, audit);
|
||||||
|
|
||||||
|
await interaction.reply(replyText);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -62,7 +62,6 @@ export default class Kick extends Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
await member.kick();
|
await member.kick();
|
||||||
await interaction.reply(`\`${targetUser.user.tag}\` has been kicked.`);
|
|
||||||
|
|
||||||
const channelName = await SettingsHelper.GetSetting('channels.logs.mod', interaction.guildId);
|
const channelName = await SettingsHelper.GetSetting('channels.logs.mod', interaction.guildId);
|
||||||
|
|
||||||
|
@ -74,7 +73,29 @@ export default class Kick extends Command {
|
||||||
await channel.send({ embeds: [ logEmbed ]});
|
await channel.send({ embeds: [ logEmbed ]});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const dmEmbed = new EmbedBuilder()
|
||||||
|
.setColor(EmbedColours.Ok)
|
||||||
|
.setTitle(interaction.guild.name)
|
||||||
|
.setDescription("You have been kicked from the server.")
|
||||||
|
.addFields([
|
||||||
|
{
|
||||||
|
name: "Reason",
|
||||||
|
value: reason,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
let replyText = "Successfully kicked user.";
|
||||||
|
|
||||||
|
try {
|
||||||
|
const dmChannel = await targetUser.user!.createDM();
|
||||||
|
await dmChannel.send({ embeds: [ dmEmbed ] });
|
||||||
|
} catch {
|
||||||
|
replyText += " *Note: I was unable to DM the user the reason.*";
|
||||||
|
}
|
||||||
|
|
||||||
const audit = new Audit(targetUser.user.id, AuditType.Kick, reason, interaction.user.id, interaction.guildId);
|
const audit = new Audit(targetUser.user.id, AuditType.Kick, reason, interaction.user.id, interaction.guildId);
|
||||||
await audit.Save(Audit, audit);
|
await audit.Save(Audit, audit);
|
||||||
|
|
||||||
|
await interaction.reply(replyText);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,93 +0,0 @@
|
||||||
import { CommandInteraction, EmbedBuilder, GuildMember, PermissionsBitField, SlashCommandBuilder, TextChannel } from "discord.js";
|
|
||||||
import { AuditType } from "../constants/AuditType";
|
|
||||||
import EmbedColours from "../constants/EmbedColours";
|
|
||||||
import Audit from "../database/entities/Audit";
|
|
||||||
import SettingsHelper from "../helpers/SettingsHelper";
|
|
||||||
import { Command } from "../type/command";
|
|
||||||
|
|
||||||
export default class Mute extends Command {
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
|
|
||||||
this.CommandBuilder = new SlashCommandBuilder()
|
|
||||||
.setName("mute")
|
|
||||||
.setDescription("(DEPRECATED) Mute a member in the server with an optional reason")
|
|
||||||
.setDefaultMemberPermissions(PermissionsBitField.Flags.ModerateMembers)
|
|
||||||
.addUserOption(option =>
|
|
||||||
option
|
|
||||||
.setName('target')
|
|
||||||
.setDescription('The user')
|
|
||||||
.setRequired(true))
|
|
||||||
.addStringOption(option =>
|
|
||||||
option
|
|
||||||
.setName('reason')
|
|
||||||
.setDescription('The reason'));
|
|
||||||
}
|
|
||||||
|
|
||||||
public override async execute(interaction: CommandInteraction) {
|
|
||||||
if (!interaction.guild || !interaction.guildId) return;
|
|
||||||
|
|
||||||
const targetUser = interaction.options.get('target');
|
|
||||||
const reasonInput = interaction.options.get('reason');
|
|
||||||
|
|
||||||
if (!targetUser || !targetUser.user || !targetUser.member) {
|
|
||||||
await interaction.reply('Fields are required.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const targetMember = targetUser.member as GuildMember;
|
|
||||||
const reason = reasonInput && reasonInput.value ? reasonInput.value.toString() : "*none*";
|
|
||||||
|
|
||||||
const logEmbed = new EmbedBuilder()
|
|
||||||
.setColor(EmbedColours.Ok)
|
|
||||||
.setTitle("Member Muted")
|
|
||||||
.setDescription(`<@${targetUser.user.id}> \`${targetUser.user.tag}\``)
|
|
||||||
.setThumbnail(targetUser.user.avatarURL())
|
|
||||||
.addFields([
|
|
||||||
{
|
|
||||||
name: "Moderator",
|
|
||||||
value: `<@${interaction.user.id}>`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Reason",
|
|
||||||
value: reason,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
const mutedRoleName = await SettingsHelper.GetSetting('role.muted', interaction.guildId);
|
|
||||||
|
|
||||||
if (!mutedRoleName) {
|
|
||||||
await interaction.reply('Unable to find configuration. Please contact the bot author.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const mutedRole = interaction.guild.roles.cache.find(role => role.name == mutedRoleName);
|
|
||||||
|
|
||||||
if (!mutedRole) {
|
|
||||||
await interaction.reply('Muted role not found.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!targetMember.manageable) {
|
|
||||||
await interaction.reply('Insufficient permissions. Please contact a moderator.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await targetMember.roles.add(mutedRole);
|
|
||||||
|
|
||||||
const channelName = await SettingsHelper.GetSetting('channels.logs.mod', interaction.guildId);
|
|
||||||
|
|
||||||
if (!channelName) return;
|
|
||||||
|
|
||||||
const channel = interaction.guild.channels.cache.find(x => x.name == channelName) as TextChannel;
|
|
||||||
|
|
||||||
if (channel) {
|
|
||||||
await channel.send({ embeds: [ logEmbed ]});
|
|
||||||
}
|
|
||||||
|
|
||||||
const audit = new Audit(targetUser.user.id, AuditType.Mute, reason, interaction.user.id, interaction.guildId);
|
|
||||||
await audit.Save(Audit, audit);
|
|
||||||
|
|
||||||
await interaction.reply("Please note the mute and unmute commands have been deprecated and will be removed in a future update. Please use timeout instead");
|
|
||||||
}
|
|
||||||
}
|
|
24
src/commands/say.ts
Normal file
24
src/commands/say.ts
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, CommandInteraction, EmbedBuilder, SlashCommandBuilder } from "discord.js";
|
||||||
|
import EmbedColours from "../constants/EmbedColours";
|
||||||
|
import { Command } from "../type/command";
|
||||||
|
|
||||||
|
export default class Say extends Command {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.CommandBuilder = new SlashCommandBuilder()
|
||||||
|
.setName('say')
|
||||||
|
.setDescription('Have the bot reply with your message')
|
||||||
|
.addStringOption(x =>
|
||||||
|
x
|
||||||
|
.setName("message")
|
||||||
|
.setDescription("The message to repeat")
|
||||||
|
.setRequired(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async execute(interaction: CommandInteraction) {
|
||||||
|
const message = interaction.options.get("message", true);
|
||||||
|
|
||||||
|
await interaction.reply(message.value as string);
|
||||||
|
}
|
||||||
|
}
|
|
@ -104,54 +104,30 @@ export default class Timeout extends Command {
|
||||||
await channel.send({ embeds: [ logEmbed ]});
|
await channel.send({ embeds: [ logEmbed ]});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const dmEmbed = new EmbedBuilder()
|
||||||
|
.setColor(EmbedColours.Ok)
|
||||||
|
.setTitle(interaction.guild.name)
|
||||||
|
.setDescription("You have been given a warning by a moderator.")
|
||||||
|
.addFields([
|
||||||
|
{
|
||||||
|
name: "Reason",
|
||||||
|
value: reason || "*none*",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
let replyText = "Successfully warned user.";
|
||||||
|
|
||||||
|
try {
|
||||||
|
const dmChannel = await targetUser.user!.createDM();
|
||||||
|
await dmChannel.send({ embeds: [ dmEmbed ] });
|
||||||
|
} catch {
|
||||||
|
replyText += " *Note: I was unable to DM the user the reason.*";
|
||||||
|
}
|
||||||
|
|
||||||
// Create Audit
|
// Create Audit
|
||||||
const audit = new Audit(targetUser.user.id, AuditType.Timeout, reason || "*none*", interaction.user.id, interaction.guildId);
|
const audit = new Audit(targetUser.user.id, AuditType.Timeout, reason || "*none*", interaction.user.id, interaction.guildId);
|
||||||
await audit.Save(Audit, audit);
|
await audit.Save(Audit, audit);
|
||||||
|
|
||||||
// DM User, if possible
|
await interaction.reply(replyText);
|
||||||
const resultEmbed = new EmbedBuilder()
|
|
||||||
.setColor(EmbedColours.Ok)
|
|
||||||
.setDescription(`<@${targetUser.user.id}> has been timed out`);
|
|
||||||
|
|
||||||
const dmEmbed = new EmbedBuilder()
|
|
||||||
.setColor(EmbedColours.Ok)
|
|
||||||
.setDescription(`You have been timed out in ${interaction.guild.name}`)
|
|
||||||
.addFields([
|
|
||||||
{
|
|
||||||
name: "Reason",
|
|
||||||
value: reason || "*none*"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Length",
|
|
||||||
value: timeLength.GetLengthShort(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Until",
|
|
||||||
value: timeLength.GetDateFromNow().toString(),
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const dmChannel = await targetUser.user.createDM();
|
|
||||||
|
|
||||||
await dmChannel.send({ embeds: [ dmEmbed ]});
|
|
||||||
|
|
||||||
resultEmbed.addFields([
|
|
||||||
{
|
|
||||||
name: "DM Sent",
|
|
||||||
value: "true",
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
} catch {
|
|
||||||
resultEmbed.addFields([
|
|
||||||
{
|
|
||||||
name: "DM Sent",
|
|
||||||
value: "false",
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Success Reply
|
|
||||||
await interaction.reply({ embeds: [ resultEmbed ]});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,88 +0,0 @@
|
||||||
import { CommandInteraction, EmbedBuilder, GuildMember, PermissionsBitField, SlashCommandBuilder, TextChannel } from "discord.js";
|
|
||||||
import EmbedColours from "../constants/EmbedColours";
|
|
||||||
import SettingsHelper from "../helpers/SettingsHelper";
|
|
||||||
import { Command } from "../type/command";
|
|
||||||
|
|
||||||
export default class Unmute extends Command {
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
|
|
||||||
this.CommandBuilder = new SlashCommandBuilder()
|
|
||||||
.setName("unmute")
|
|
||||||
.setDescription("(DEPRECATED) Unmute a member in the server with an optional reason")
|
|
||||||
.setDefaultMemberPermissions(PermissionsBitField.Flags.ModerateMembers)
|
|
||||||
.addUserOption(option =>
|
|
||||||
option
|
|
||||||
.setName('target')
|
|
||||||
.setDescription('The user')
|
|
||||||
.setRequired(true))
|
|
||||||
.addStringOption(option =>
|
|
||||||
option
|
|
||||||
.setName('reason')
|
|
||||||
.setDescription('The reason'));
|
|
||||||
}
|
|
||||||
|
|
||||||
public override async execute(interaction: CommandInteraction) {
|
|
||||||
if (!interaction.guild || !interaction.guildId) return;
|
|
||||||
|
|
||||||
const targetUser = interaction.options.get('target');
|
|
||||||
const reasonInput = interaction.options.get('reason');
|
|
||||||
|
|
||||||
if (!targetUser || !targetUser.user || !targetUser.member) {
|
|
||||||
await interaction.reply('Fields are required.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const targetMember = targetUser.member as GuildMember;
|
|
||||||
const reason = reasonInput && reasonInput.value ? reasonInput.value.toString() : "*none*";
|
|
||||||
|
|
||||||
const logEmbed = new EmbedBuilder()
|
|
||||||
.setColor(EmbedColours.Ok)
|
|
||||||
.setTitle("Member Unmuted")
|
|
||||||
.setDescription(`<@${targetUser.user.id}> \`${targetUser.user.tag}\``)
|
|
||||||
.setThumbnail(targetUser.user.avatarURL())
|
|
||||||
.addFields([
|
|
||||||
{
|
|
||||||
name: "Moderator",
|
|
||||||
value: `<@${interaction.user.id}>`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Reason",
|
|
||||||
value: reason,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
const mutedRoleName = await SettingsHelper.GetSetting('role.muted', interaction.guildId);
|
|
||||||
|
|
||||||
if (!mutedRoleName) {
|
|
||||||
await interaction.reply('Unable to find configuration. Please contact the bot author.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const mutedRole = interaction.guild.roles.cache.find(role => role.name == mutedRoleName);
|
|
||||||
|
|
||||||
if (!mutedRole) {
|
|
||||||
await interaction.reply('Muted role not found.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!targetMember.manageable) {
|
|
||||||
await interaction.reply('Insufficient permissions. Please contact a moderator.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await targetMember.roles.remove(mutedRole);
|
|
||||||
|
|
||||||
const channelName = await SettingsHelper.GetSetting('channels.logs.mod', interaction.guildId);
|
|
||||||
|
|
||||||
if (!channelName) return;
|
|
||||||
|
|
||||||
const channel = interaction.guild.channels.cache.find(x => x.name == channelName) as TextChannel;
|
|
||||||
|
|
||||||
if (channel) {
|
|
||||||
await channel.send({ embeds: [ logEmbed ]});
|
|
||||||
}
|
|
||||||
|
|
||||||
await interaction.reply("Please note the mute and unmute commands have been deprecated and will be removed in a future update. Please use timeout instead");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -63,9 +63,29 @@ export default class Warn extends Command {
|
||||||
await channel.send({ embeds: [ logEmbed ]});
|
await channel.send({ embeds: [ logEmbed ]});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const dmEmbed = new EmbedBuilder()
|
||||||
|
.setColor(EmbedColours.Ok)
|
||||||
|
.setTitle(interaction.guild.name)
|
||||||
|
.setDescription("You have been given a warning by a moderator.")
|
||||||
|
.addFields([
|
||||||
|
{
|
||||||
|
name: "Reason",
|
||||||
|
value: reason,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
let replyText = "Successfully warned user.";
|
||||||
|
|
||||||
|
try {
|
||||||
|
const dmChannel = await targetUser.user!.createDM();
|
||||||
|
await dmChannel.send({ embeds: [ dmEmbed ] });
|
||||||
|
} catch {
|
||||||
|
replyText += " *Note: I was unable to DM the user the reason.*";
|
||||||
|
}
|
||||||
|
|
||||||
const audit = new Audit(targetUser.user.id, AuditType.Warn, reason, interaction.user.id, interaction.guildId);
|
const audit = new Audit(targetUser.user.id, AuditType.Warn, reason, interaction.user.id, interaction.guildId);
|
||||||
await audit.Save(Audit, audit);
|
await audit.Save(Audit, audit);
|
||||||
|
|
||||||
await interaction.reply('Successfully warned user.');
|
await interaction.reply(replyText);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
export default class EmbedColours {
|
export default class EmbedColours {
|
||||||
public static readonly Ok = 0x3050ba;
|
public static readonly Ok = 0x3050ba;
|
||||||
|
public static readonly Moon = 0x50C878;
|
||||||
}
|
}
|
57
src/database/entities/304276391837302787/Moon.ts
Normal file
57
src/database/entities/304276391837302787/Moon.ts
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
import { Column, Entity, IsNull } from "typeorm";
|
||||||
|
import BaseEntity from "../../../contracts/BaseEntity";
|
||||||
|
import AppDataSource from "../../dataSources/appDataSource";
|
||||||
|
|
||||||
|
@Entity()
|
||||||
|
export default class Moon extends BaseEntity {
|
||||||
|
constructor(moonNumber: number, description: string, userId: string) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.MoonNumber = moonNumber;
|
||||||
|
this.Description = description;
|
||||||
|
this.UserId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
MoonNumber: number;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
Description: string;
|
||||||
|
|
||||||
|
@Column({ nullable: true })
|
||||||
|
WhenArchived?: Date;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
UserId: string;
|
||||||
|
|
||||||
|
public static async FetchMoonsByUserId(userId: string): Promise<Moon[] | null> {
|
||||||
|
const repository = AppDataSource.getRepository(Moon);
|
||||||
|
|
||||||
|
const all = await repository.find({ where: { UserId: userId } });
|
||||||
|
|
||||||
|
return all;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async FetchPaginatedMoonsByUserId(userId: string, pageLength: number, page: number): Promise<[ Moon[], number ]> {
|
||||||
|
const rangeStart = page * pageLength;
|
||||||
|
|
||||||
|
const repository = AppDataSource.getRepository(Moon);
|
||||||
|
|
||||||
|
const moons = await repository.findAndCount({
|
||||||
|
where: { UserId: userId, WhenArchived: IsNull() },
|
||||||
|
order: { MoonNumber: "ASC" },
|
||||||
|
skip: rangeStart,
|
||||||
|
take: pageLength,
|
||||||
|
});
|
||||||
|
|
||||||
|
return moons;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async FetchMoonCountByUserId(userId: string): Promise<number> {
|
||||||
|
const repository = AppDataSource.getRepository(Moon);
|
||||||
|
|
||||||
|
const count = await repository.count({ where: { UserId: userId } });
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
35
src/database/entities/UserSetting.ts
Normal file
35
src/database/entities/UserSetting.ts
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
import { Column, Entity} from "typeorm";
|
||||||
|
import AppDataSource from "../dataSources/appDataSource";
|
||||||
|
import BaseEntity from "../../contracts/BaseEntity";
|
||||||
|
|
||||||
|
@Entity()
|
||||||
|
export default class UserSetting extends BaseEntity {
|
||||||
|
constructor(userId: string, key: string, value: string) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.UserId = userId;
|
||||||
|
this.Key = key;
|
||||||
|
this.Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
UserId: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
Key: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
Value: string;
|
||||||
|
|
||||||
|
public UpdateValue(value: string) {
|
||||||
|
this.Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async FetchOneByKey(userId: string, key: string, relations?: string[]): Promise<UserSetting | null> {
|
||||||
|
const repository = AppDataSource.getRepository(UserSetting);
|
||||||
|
|
||||||
|
const single = await repository.findOne({ where: { UserId: userId, Key: key }, relations: relations || {} });
|
||||||
|
|
||||||
|
return single;
|
||||||
|
}
|
||||||
|
}
|
15
src/database/migrations/3.3/1719856023429-CreateMoon.ts
Normal file
15
src/database/migrations/3.3/1719856023429-CreateMoon.ts
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import { MigrationInterface, QueryRunner } from "typeorm"
|
||||||
|
import MigrationHelper from "../../../helpers/MigrationHelper"
|
||||||
|
|
||||||
|
export class CreateMoon1719856023429 implements MigrationInterface {
|
||||||
|
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
MigrationHelper.Up('1719856023429-CreateMoon', '3.3.0', [
|
||||||
|
"01-Moon",
|
||||||
|
], queryRunner);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||||
|
import MigrationHelper from "../../../helpers/MigrationHelper";
|
||||||
|
|
||||||
|
export class CreateUserSetting1727286976268 implements MigrationInterface {
|
||||||
|
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
MigrationHelper.Up('1727286976268-CreateUserSetting', '3.3.0', [
|
||||||
|
"01-UserSetting",
|
||||||
|
"02-UserSettingKey",
|
||||||
|
], queryRunner);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -13,14 +13,13 @@ import Disable from "./commands/disable";
|
||||||
import Ignore from "./commands/ignore";
|
import Ignore from "./commands/ignore";
|
||||||
import Kick from "./commands/kick";
|
import Kick from "./commands/kick";
|
||||||
import Linkonly from "./commands/linkonly";
|
import Linkonly from "./commands/linkonly";
|
||||||
import Mute from "./commands/mute";
|
|
||||||
import Poll from "./commands/poll";
|
import Poll from "./commands/poll";
|
||||||
import Role from "./commands/Role/role";
|
import Role from "./commands/Role/role";
|
||||||
import ConfigRole from "./commands/Role/config";
|
import ConfigRole from "./commands/Role/config";
|
||||||
import Rules from "./commands/rules";
|
import Rules from "./commands/rules";
|
||||||
|
import Say from "./commands/say";
|
||||||
import Setup from "./commands/setup";
|
import Setup from "./commands/setup";
|
||||||
import Timeout from "./commands/timeout";
|
import Timeout from "./commands/timeout";
|
||||||
import Unmute from "./commands/unmute";
|
|
||||||
import Warn from "./commands/warn";
|
import Warn from "./commands/warn";
|
||||||
|
|
||||||
// Command Imports: MankBot
|
// Command Imports: MankBot
|
||||||
|
@ -30,6 +29,9 @@ import AddLobby from "./commands/501231711271780357/Lobby/add";
|
||||||
import RemoveLobby from "./commands/501231711271780357/Lobby/remove";
|
import RemoveLobby from "./commands/501231711271780357/Lobby/remove";
|
||||||
import ListLobby from "./commands/501231711271780357/Lobby/list";
|
import ListLobby from "./commands/501231711271780357/Lobby/list";
|
||||||
|
|
||||||
|
// Command Imports: Potato Talk
|
||||||
|
import Moons from "./commands/304276391837302787/moons";
|
||||||
|
|
||||||
// Event Imports
|
// Event Imports
|
||||||
import GuildMemberAdd from "./events/MemberEvents/GuildMemberAdd";
|
import GuildMemberAdd from "./events/MemberEvents/GuildMemberAdd";
|
||||||
import GuildMemberRemove from "./events/MemberEvents/GuildMemberRemove";
|
import GuildMemberRemove from "./events/MemberEvents/GuildMemberRemove";
|
||||||
|
@ -40,6 +42,7 @@ import MessageCreate from "./events/MessageEvents/MessageCreate";
|
||||||
|
|
||||||
// Button Event Imports
|
// Button Event Imports
|
||||||
import Verify from "./buttonEvents/verify";
|
import Verify from "./buttonEvents/verify";
|
||||||
|
import MoonsButtonEvent from "./buttonEvents/304276391837302787/moons";
|
||||||
|
|
||||||
export default class Registry {
|
export default class Registry {
|
||||||
public static RegisterCommands() {
|
public static RegisterCommands() {
|
||||||
|
@ -54,12 +57,11 @@ export default class Registry {
|
||||||
CoreClient.RegisterCommand("ignore", new Ignore());
|
CoreClient.RegisterCommand("ignore", new Ignore());
|
||||||
CoreClient.RegisterCommand("kick", new Kick());
|
CoreClient.RegisterCommand("kick", new Kick());
|
||||||
CoreClient.RegisterCommand("linkonly", new Linkonly());
|
CoreClient.RegisterCommand("linkonly", new Linkonly());
|
||||||
CoreClient.RegisterCommand("mute", new Mute());
|
|
||||||
CoreClient.RegisterCommand("poll", new Poll());
|
CoreClient.RegisterCommand("poll", new Poll());
|
||||||
CoreClient.RegisterCommand("rules", new Rules());
|
CoreClient.RegisterCommand("rules", new Rules());
|
||||||
|
CoreClient.RegisterCommand("say", new Say());
|
||||||
CoreClient.RegisterCommand("setup", new Setup());
|
CoreClient.RegisterCommand("setup", new Setup());
|
||||||
CoreClient.RegisterCommand("timeout", new Timeout());
|
CoreClient.RegisterCommand("timeout", new Timeout());
|
||||||
CoreClient.RegisterCommand("unmute", new Unmute());
|
|
||||||
CoreClient.RegisterCommand("warn", new Warn());
|
CoreClient.RegisterCommand("warn", new Warn());
|
||||||
|
|
||||||
CoreClient.RegisterCommand("role", new Role());
|
CoreClient.RegisterCommand("role", new Role());
|
||||||
|
@ -72,12 +74,16 @@ export default class Registry {
|
||||||
CoreClient.RegisterCommand("listlobby", new ListLobby(), "501231711271780357");
|
CoreClient.RegisterCommand("listlobby", new ListLobby(), "501231711271780357");
|
||||||
CoreClient.RegisterCommand("entry", new Entry(), "501231711271780357");
|
CoreClient.RegisterCommand("entry", new Entry(), "501231711271780357");
|
||||||
|
|
||||||
|
// Exclusive Commands: Potato Talk
|
||||||
|
CoreClient.RegisterCommand("moons", new Moons(), "304276391837302787");
|
||||||
|
|
||||||
// Add Exclusive Commands to Test Server
|
// Add Exclusive Commands to Test Server
|
||||||
CoreClient.RegisterCommand("lobby", new Lobby(), "442730357897429002");
|
CoreClient.RegisterCommand("lobby", new Lobby(), "442730357897429002");
|
||||||
CoreClient.RegisterCommand("addlobby", new AddLobby(), "442730357897429002");
|
CoreClient.RegisterCommand("addlobby", new AddLobby(), "442730357897429002");
|
||||||
CoreClient.RegisterCommand("removelobby", new RemoveLobby(), "442730357897429002");
|
CoreClient.RegisterCommand("removelobby", new RemoveLobby(), "442730357897429002");
|
||||||
CoreClient.RegisterCommand("listlobby", new ListLobby(), "442730357897429002");
|
CoreClient.RegisterCommand("listlobby", new ListLobby(), "442730357897429002");
|
||||||
CoreClient.RegisterCommand("entry", new Entry(), "442730357897429002");
|
CoreClient.RegisterCommand("entry", new Entry(), "442730357897429002");
|
||||||
|
CoreClient.RegisterCommand("moons", new Moons(), "442730357897429002");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static RegisterEvents() {
|
public static RegisterEvents() {
|
||||||
|
@ -92,5 +98,6 @@ export default class Registry {
|
||||||
|
|
||||||
public static RegisterButtonEvents() {
|
public static RegisterButtonEvents() {
|
||||||
CoreClient.RegisterButtonEvent("verify", new Verify());
|
CoreClient.RegisterButtonEvent("verify", new Verify());
|
||||||
|
CoreClient.RegisterButtonEvent("moons", new MoonsButtonEvent());
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,10 +0,0 @@
|
||||||
import { Command } from "../../../src/type/command";
|
|
||||||
|
|
||||||
export default class MockCmd extends Command {
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
|
|
||||||
super._category = "General";
|
|
||||||
super._roles = ["Moderator"];
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,13 +0,0 @@
|
||||||
[
|
|
||||||
{
|
|
||||||
"image": "IMAGEURL"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"title": "TITLE 1",
|
|
||||||
"description": [
|
|
||||||
"DESCRIPTION 1A",
|
|
||||||
"DESCRIPTION 1B"
|
|
||||||
],
|
|
||||||
"footer": "FOOTER 1"
|
|
||||||
}
|
|
||||||
]
|
|
21
tests/buttonEvents/304276391837302787/moons.test.ts
Normal file
21
tests/buttonEvents/304276391837302787/moons.test.ts
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
import { ButtonInteraction } from "discord.js";
|
||||||
|
import Moons from "../../../src/buttonEvents/304276391837302787/moons";
|
||||||
|
import * as List from "../../../src/buttonEvents/304276391837302787/moons/list";
|
||||||
|
|
||||||
|
describe("GIVEN interaction action is list", () => {
|
||||||
|
const interaction = {
|
||||||
|
customId: "moons list",
|
||||||
|
} as unknown as ButtonInteraction;
|
||||||
|
|
||||||
|
const listSpy = jest.spyOn(List, "default");
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const moons = new Moons();
|
||||||
|
await moons.execute(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT List function to be called", () => {
|
||||||
|
expect(listSpy).toHaveBeenCalledTimes(1);
|
||||||
|
expect(listSpy).toHaveBeenCalledWith(interaction);
|
||||||
|
});
|
||||||
|
});
|
|
@ -0,0 +1,41 @@
|
||||||
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
|
exports[`GIVEN happy flow EXPECT embed to be updated 1`] = `
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"color": 3166394,
|
||||||
|
"description": "**1 -** Test Descriptio",
|
||||||
|
"footer": {
|
||||||
|
"icon_url": undefined,
|
||||||
|
"text": "Page 1 of 1 · 0 moons",
|
||||||
|
},
|
||||||
|
"title": "username's Moons",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`GIVEN happy flow EXPECT row to be updated 1`] = `
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"components": [
|
||||||
|
{
|
||||||
|
"custom_id": "moons list userId -1",
|
||||||
|
"disabled": true,
|
||||||
|
"emoji": undefined,
|
||||||
|
"label": "Previous",
|
||||||
|
"style": 1,
|
||||||
|
"type": 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"custom_id": "moons list userId 1",
|
||||||
|
"disabled": true,
|
||||||
|
"emoji": undefined,
|
||||||
|
"label": "Next",
|
||||||
|
"style": 1,
|
||||||
|
"type": 2,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"type": 1,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
`;
|
268
tests/buttonEvents/304276391837302787/moons/list.test.ts
Normal file
268
tests/buttonEvents/304276391837302787/moons/list.test.ts
Normal file
|
@ -0,0 +1,268 @@
|
||||||
|
import {ActionRowBuilder, ButtonBuilder, ButtonInteraction, EmbedBuilder} from "discord.js";
|
||||||
|
import List from "../../../../src/buttonEvents/304276391837302787/moons/list";
|
||||||
|
import UserSetting from "../../../../src/database/entities/UserSetting";
|
||||||
|
import Moon from "../../../../src/database/entities/304276391837302787/Moon";
|
||||||
|
|
||||||
|
describe("GIVEN happy flow", () => {
|
||||||
|
let updatedWithEmbeds: EmbedBuilder[] | undefined;
|
||||||
|
let updatedWithRows: ActionRowBuilder<ButtonBuilder>[] | undefined;
|
||||||
|
|
||||||
|
const interaction = {
|
||||||
|
guild: {
|
||||||
|
members: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue({
|
||||||
|
user: {
|
||||||
|
username: "username",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
update: jest.fn((options: any) => {
|
||||||
|
updatedWithEmbeds = options.embeds;
|
||||||
|
updatedWithRows = options.components;
|
||||||
|
}),
|
||||||
|
customId: "moons list userId 0",
|
||||||
|
} as unknown as ButtonInteraction;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
UserSetting.FetchOneByKey = jest.fn();
|
||||||
|
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue([
|
||||||
|
[
|
||||||
|
{
|
||||||
|
MoonNumber: 1,
|
||||||
|
Description: "Test Description",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
1,
|
||||||
|
]);
|
||||||
|
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT moons to be fetched", () => {
|
||||||
|
expect(Moon.FetchPaginatedMoonsByUserId).toHaveBeenCalledTimes(1);
|
||||||
|
expect(Moon.FetchPaginatedMoonsByUserId).toHaveBeenCalledWith("userId", 10, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT interaction.update to be called", () => {
|
||||||
|
expect(interaction.update).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT embed to be updated", () => {
|
||||||
|
expect(updatedWithEmbeds).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT row to be updated", () => {
|
||||||
|
expect(updatedWithRows).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN interaction.guild is null", () => {
|
||||||
|
const interaction = {
|
||||||
|
guild: null,
|
||||||
|
reply: jest.fn(),
|
||||||
|
update: jest.fn(),
|
||||||
|
} as unknown as ButtonInteraction;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
UserSetting.FetchOneByKey = jest.fn();
|
||||||
|
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT function to return", () => {
|
||||||
|
expect(interaction.reply).not.toHaveBeenCalled();
|
||||||
|
expect(interaction.update).not.toHaveBeenCalled();
|
||||||
|
expect(UserSetting.FetchOneByKey).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN userId parameter is undefined", () => {
|
||||||
|
const interaction = {
|
||||||
|
guild: {},
|
||||||
|
reply: jest.fn(),
|
||||||
|
update: jest.fn(),
|
||||||
|
customId: "moons list",
|
||||||
|
} as unknown as ButtonInteraction;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
UserSetting.FetchOneByKey = jest.fn();
|
||||||
|
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT function to return", () => {
|
||||||
|
expect(interaction.reply).not.toHaveBeenCalled();
|
||||||
|
expect(interaction.update).not.toHaveBeenCalled();
|
||||||
|
expect(UserSetting.FetchOneByKey).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN page parameter is undefined", () => {
|
||||||
|
const interaction = {
|
||||||
|
guild: {},
|
||||||
|
reply: jest.fn(),
|
||||||
|
update: jest.fn(),
|
||||||
|
customId: "moons list userId",
|
||||||
|
} as unknown as ButtonInteraction;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
UserSetting.FetchOneByKey = jest.fn();
|
||||||
|
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT function to return", () => {
|
||||||
|
expect(interaction.reply).not.toHaveBeenCalled();
|
||||||
|
expect(interaction.update).not.toHaveBeenCalled();
|
||||||
|
expect(UserSetting.FetchOneByKey).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN no moons for the user is returned", () => {
|
||||||
|
const interaction = {
|
||||||
|
guild: {
|
||||||
|
members: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue({
|
||||||
|
user: {
|
||||||
|
username: "username",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
update: jest.fn(),
|
||||||
|
customId: "moons list userId 0",
|
||||||
|
} as unknown as ButtonInteraction;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
UserSetting.FetchOneByKey = jest.fn();
|
||||||
|
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue(undefined)
|
||||||
|
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT moons function to be called", () => {
|
||||||
|
expect(Moon.FetchPaginatedMoonsByUserId).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT error replied", () => {
|
||||||
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.reply).toHaveBeenCalledWith("username does not have any moons or page is invalid.");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN member is not in cache", () => {
|
||||||
|
const interaction = {
|
||||||
|
guild: {
|
||||||
|
members: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue(undefined),
|
||||||
|
},
|
||||||
|
fetch: jest.fn().mockResolvedValue({
|
||||||
|
user: {
|
||||||
|
username: "username",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
update: jest.fn(),
|
||||||
|
customId: "moons list userId 0",
|
||||||
|
} as unknown as ButtonInteraction;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
UserSetting.FetchOneByKey = jest.fn();
|
||||||
|
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue(undefined)
|
||||||
|
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT API to be called", () => {
|
||||||
|
expect(interaction.guild?.members.fetch).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.guild?.members.fetch).toHaveBeenCalledWith("userId");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT error replied with username", () => {
|
||||||
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.reply).toHaveBeenCalledWith("username does not have any moons or page is invalid.");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN member can not be found", () => {
|
||||||
|
const interaction = {
|
||||||
|
guild: {
|
||||||
|
members: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue(undefined),
|
||||||
|
},
|
||||||
|
fetch: jest.fn().mockResolvedValue(undefined),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
update: jest.fn(),
|
||||||
|
customId: "moons list userId 0",
|
||||||
|
} as unknown as ButtonInteraction;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
UserSetting.FetchOneByKey = jest.fn();
|
||||||
|
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue(undefined)
|
||||||
|
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT API to be called", () => {
|
||||||
|
expect(interaction.guild?.members.fetch).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.guild?.members.fetch).toHaveBeenCalledWith("userId");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT error replied with username", () => {
|
||||||
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.reply).toHaveBeenCalledWith("This user does not have any moons or page is invalid.");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN no moons on current page", () => {
|
||||||
|
let updatedWith: EmbedBuilder[] | undefined;
|
||||||
|
|
||||||
|
const interaction = {
|
||||||
|
guild: {
|
||||||
|
members: {
|
||||||
|
cache: {
|
||||||
|
find: jest.fn().mockReturnValue({
|
||||||
|
user: {
|
||||||
|
username: "username",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reply: jest.fn(),
|
||||||
|
update: jest.fn((options: any) => {
|
||||||
|
updatedWith = options.embeds;
|
||||||
|
}),
|
||||||
|
customId: "moons list userId 0",
|
||||||
|
} as unknown as ButtonInteraction;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
UserSetting.FetchOneByKey = jest.fn();
|
||||||
|
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue([
|
||||||
|
[],
|
||||||
|
0,
|
||||||
|
]);
|
||||||
|
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT description to say so", () => {
|
||||||
|
expect(updatedWith).toBeDefined();
|
||||||
|
expect(updatedWith?.length).toBe(1);
|
||||||
|
|
||||||
|
expect(updatedWith![0].data.description).toBe("*none*");
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,153 +0,0 @@
|
||||||
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";
|
|
||||||
import * as dotenv from "dotenv";
|
|
||||||
import { Events } from "../../src/client/events";
|
|
||||||
import { Util } from "../../src/client/util";
|
|
||||||
import { Command } from "../../src/type/command";
|
|
||||||
import { Event } from "../../src/type/event";
|
|
||||||
import DefaultValues from "../../src/constants/DefaultValues";
|
|
||||||
import { Connection, Repository, SelectQueryBuilder } from "typeorm";
|
|
||||||
import Setting from "../../src/entity/Setting";
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
jest.resetAllMocks();
|
|
||||||
jest.resetModules();
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('Constructor', () => {
|
|
||||||
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', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: "TOKEN",
|
|
||||||
};
|
|
||||||
|
|
||||||
const coreClient = new CoreClient();
|
|
||||||
|
|
||||||
await coreClient.start();
|
|
||||||
|
|
||||||
expect(coreClient.on).toBeCalledWith("message", expect.any(Function));
|
|
||||||
expect(coreClient.on).toBeCalledWith("ready", expect.any(Function));
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given BOT_TOKEN Is Null, Expect Failure', async () => {
|
|
||||||
process.env = {};
|
|
||||||
|
|
||||||
const consoleError = jest.fn();
|
|
||||||
|
|
||||||
console.error = consoleError;
|
|
||||||
|
|
||||||
const coreClient = new CoreClient();
|
|
||||||
|
|
||||||
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', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: '',
|
|
||||||
}
|
|
||||||
|
|
||||||
const consoleError = jest.fn();
|
|
||||||
|
|
||||||
console.error = consoleError;
|
|
||||||
|
|
||||||
const coreClient = new CoreClient();
|
|
||||||
|
|
||||||
await coreClient.start();
|
|
||||||
|
|
||||||
expect(consoleError).toBeCalledWith("BOT_TOKEN is not defined in .env");
|
|
||||||
expect(coreClient.on).not.toBeCalled();
|
|
||||||
expect(coreClient.login).not.toBeCalled();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('RegisterCommand', () => {
|
|
||||||
test('Expect command added to register', () => {
|
|
||||||
const cmd = mock<Command>();
|
|
||||||
|
|
||||||
const client = new CoreClient();
|
|
||||||
client.RegisterCommand("test", cmd);
|
|
||||||
|
|
||||||
expect(client.commandItems.length).toBe(1);
|
|
||||||
expect(client.commandItems[0].Name).toBe("test");
|
|
||||||
expect(client.commandItems[0].Command).toBe(cmd);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('RegisterEvent', () => {
|
|
||||||
test('Expect event added to register', () => {
|
|
||||||
const evt = mock<Event>();
|
|
||||||
|
|
||||||
const client = new CoreClient();
|
|
||||||
client.RegisterEvent(evt);
|
|
||||||
|
|
||||||
expect(client.eventItems.length).toBe(1);
|
|
||||||
expect(client.eventItems[0].Event).toBe(evt);
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,241 +0,0 @@
|
||||||
import { Events } from "../../src/client/events";
|
|
||||||
import { Message } from "discord.js";
|
|
||||||
import { Util } from "../../src/client/util";
|
|
||||||
import ICommandItem from "../../src/contracts/ICommandItem";
|
|
||||||
import { Command } from "../../src/type/command";
|
|
||||||
import { mock } from "jest-mock-extended";
|
|
||||||
|
|
||||||
jest.mock("../../src/client/util");
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
Util.prototype.loadCommand = jest.fn();
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('OnMessage', () => {
|
|
||||||
test('Given Message Is Valid Expect Message Sent', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
};
|
|
||||||
|
|
||||||
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
guild: {},
|
|
||||||
author: {
|
|
||||||
bot: false,
|
|
||||||
},
|
|
||||||
content: "!test first",
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const cmd = mock<Command>();
|
|
||||||
|
|
||||||
const commandItem: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: cmd
|
|
||||||
};
|
|
||||||
|
|
||||||
const commands: ICommandItem[] = [ commandItem ];
|
|
||||||
|
|
||||||
const events = new Events();
|
|
||||||
|
|
||||||
const result = await events.onMessage(message, commands);
|
|
||||||
|
|
||||||
expect(result.valid).toBeTruthy();
|
|
||||||
|
|
||||||
expect(result.context?.prefix).toBe('!');
|
|
||||||
expect(result.context?.name).toBe('test');
|
|
||||||
expect(result.context?.args.length).toBe(1);
|
|
||||||
expect(result.context?.args[0]).toBe('first');
|
|
||||||
expect(result.context?.message).toBe(message);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given Guild Is Null, Expect Failed Result', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
guild: null,
|
|
||||||
author: {
|
|
||||||
bot: false,
|
|
||||||
},
|
|
||||||
content: "!test first",
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const cmd = mock<Command>();
|
|
||||||
|
|
||||||
const commandItem: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: cmd
|
|
||||||
};
|
|
||||||
|
|
||||||
const commands: ICommandItem[] = [ commandItem ];
|
|
||||||
|
|
||||||
const events = new Events();
|
|
||||||
|
|
||||||
const result = await events.onMessage(message, commands);
|
|
||||||
|
|
||||||
expect(result.valid).toBeFalsy();
|
|
||||||
expect(result.message).toBe("Message was not sent in a guild, ignoring.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given Author Is A Bot, Expect Failed Result', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
guild: {},
|
|
||||||
author: {
|
|
||||||
bot: true,
|
|
||||||
},
|
|
||||||
content: "!test first",
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const cmd = mock<Command>();
|
|
||||||
|
|
||||||
const commandItem: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: cmd
|
|
||||||
};
|
|
||||||
|
|
||||||
const commands: ICommandItem[] = [ commandItem ];
|
|
||||||
|
|
||||||
const events = new Events();
|
|
||||||
|
|
||||||
const result = await events.onMessage(message, commands);
|
|
||||||
|
|
||||||
expect(result.valid).toBeFalsy();
|
|
||||||
expect(result.message).toBe("Message was sent by a bot, ignoring.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given Message Content Was Not A Command, Expect Failed Result', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
guild: {},
|
|
||||||
author: {
|
|
||||||
bot: false,
|
|
||||||
},
|
|
||||||
content: "This is a standard message",
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const cmd = mock<Command>();
|
|
||||||
|
|
||||||
const commandItem: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: cmd
|
|
||||||
};
|
|
||||||
|
|
||||||
const commands: ICommandItem[] = [ commandItem ];
|
|
||||||
|
|
||||||
const events = new Events();
|
|
||||||
|
|
||||||
const result = await events.onMessage(message, commands);
|
|
||||||
|
|
||||||
expect(result.valid).toBeFalsy();
|
|
||||||
expect(result.message).toBe("Message was not a command, ignoring.");
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given Message Had No Command Name, Expect Failed Result', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: true });
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
guild: {},
|
|
||||||
author: {
|
|
||||||
bot: false,
|
|
||||||
},
|
|
||||||
content: "!",
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const cmd = mock<Command>();
|
|
||||||
|
|
||||||
const commandItem: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: cmd
|
|
||||||
};
|
|
||||||
|
|
||||||
const commands: ICommandItem[] = [ commandItem ];
|
|
||||||
|
|
||||||
const events = new Events();
|
|
||||||
|
|
||||||
const result = await events.onMessage(message, commands);
|
|
||||||
|
|
||||||
expect(result.valid).toBeFalsy();
|
|
||||||
expect(result.message).toBe("Command name was not found");
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given Command Failed To Execute, Expect Failed Result', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
Util.prototype.loadCommand = jest.fn().mockReturnValue({ valid: false, message: "Command failed" });
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
guild: {},
|
|
||||||
author: {
|
|
||||||
bot: false,
|
|
||||||
},
|
|
||||||
content: "!test first",
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const cmd = mock<Command>();
|
|
||||||
|
|
||||||
const commandItem: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: cmd
|
|
||||||
};
|
|
||||||
|
|
||||||
const commands: ICommandItem[] = [ commandItem ];
|
|
||||||
|
|
||||||
const events = new Events();
|
|
||||||
|
|
||||||
const result = await events.onMessage(message, commands);
|
|
||||||
|
|
||||||
expect(result.valid).toBeFalsy();
|
|
||||||
expect(result.message).toBe("Command failed");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('OnReady', () => {
|
|
||||||
test('Expect Console Log', () => {
|
|
||||||
console.log = jest.fn();
|
|
||||||
|
|
||||||
const events = new Events();
|
|
||||||
|
|
||||||
events.onReady();
|
|
||||||
|
|
||||||
expect(console.log).toBeCalledWith("Ready");
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,370 +0,0 @@
|
||||||
import { Util } from "../../src/client/util";
|
|
||||||
|
|
||||||
import { Client, Message } from "discord.js";
|
|
||||||
import fs from "fs";
|
|
||||||
import { mock } from "jest-mock-extended";
|
|
||||||
import { Command } from "../../src/type/command";
|
|
||||||
import ICommandItem from "../../src/contracts/ICommandItem";
|
|
||||||
import IEventItem from "../../src/contracts/IEventItem";
|
|
||||||
import { Event } from "../../src/type/event";
|
|
||||||
|
|
||||||
jest.mock("fs");
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
fs.existsSync = jest.fn();
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('LoadCommand', () => {
|
|
||||||
test('Given Successful Exection, Expect Successful Result', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
member: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(true),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const cmd = mock<Command>();
|
|
||||||
|
|
||||||
const commandItem: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: cmd
|
|
||||||
};
|
|
||||||
|
|
||||||
const commands: ICommandItem[] = [ commandItem ];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
|
|
||||||
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', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
member: null
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const cmd = mock<Command>();
|
|
||||||
|
|
||||||
const commandItem: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: cmd
|
|
||||||
};
|
|
||||||
|
|
||||||
const commands: ICommandItem[] = [ commandItem ];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
|
|
||||||
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', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
member: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(true),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const cmd = mock<Command>();
|
|
||||||
|
|
||||||
const commandItem: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: cmd
|
|
||||||
};
|
|
||||||
|
|
||||||
const commands: ICommandItem[] = [ commandItem ];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
|
|
||||||
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', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
member: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(false),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const cmd = mock<Command>();
|
|
||||||
cmd._roles = [ "Moderator" ];
|
|
||||||
|
|
||||||
const commandItem: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: cmd
|
|
||||||
};
|
|
||||||
|
|
||||||
const commands: ICommandItem[] = [ commandItem ];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
|
|
||||||
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', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
COMMANDS_DISABLED: 'test',
|
|
||||||
COMMANDS_DISABLED_MESSAGE: 'disabled',
|
|
||||||
}
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
member: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(true),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const messageReply = jest.spyOn(message, 'reply');
|
|
||||||
|
|
||||||
const cmd = mock<Command>();
|
|
||||||
|
|
||||||
const commandItem: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: cmd
|
|
||||||
};
|
|
||||||
|
|
||||||
const commands: ICommandItem[] = [ commandItem ];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
|
|
||||||
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
|
||||||
|
|
||||||
expect(result.valid).toBeFalsy();
|
|
||||||
expect(result.message).toBe("Command is disabled");
|
|
||||||
expect(messageReply).toBeCalledWith("disabled");
|
|
||||||
expect(cmd.execute).not.toBeCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given command COMMANDS_DISABLED_MESSAGE is empty, Expect default message sent', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
COMMANDS_DISABLED: 'test',
|
|
||||||
}
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
member: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(true),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const messageReply = jest.spyOn(message, 'reply');
|
|
||||||
|
|
||||||
const cmd = mock<Command>();
|
|
||||||
|
|
||||||
const commandItem: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: cmd
|
|
||||||
};
|
|
||||||
|
|
||||||
const commands: ICommandItem[] = [ commandItem ];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
|
|
||||||
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
|
||||||
|
|
||||||
expect(result.valid).toBeFalsy();
|
|
||||||
expect(result.message).toBe("Command is disabled");
|
|
||||||
expect(messageReply).toBeCalledWith("This command is disabled.");
|
|
||||||
expect(cmd.execute).not.toBeCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given a different command is disabled, Expect command to still fire', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
COMMANDS_DISABLED: 'other',
|
|
||||||
}
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
member: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(true),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const cmd = mock<Command>();
|
|
||||||
const otherCmd = mock<Command>();
|
|
||||||
|
|
||||||
const commandItem: ICommandItem = {
|
|
||||||
Name: "test",
|
|
||||||
Command: cmd
|
|
||||||
};
|
|
||||||
|
|
||||||
const otherCommandItem: ICommandItem = {
|
|
||||||
Name: "other",
|
|
||||||
Command: otherCmd,
|
|
||||||
}
|
|
||||||
|
|
||||||
const commands: ICommandItem[] = [ commandItem, otherCommandItem ];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
|
|
||||||
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', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
member: {
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(true),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const commands: ICommandItem[] = [];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
|
|
||||||
const result = await util.loadCommand("test", [ "first" ], message, commands);
|
|
||||||
|
|
||||||
expect(result.valid).toBeFalsy();
|
|
||||||
expect(result.message).toBe('Command not found');
|
|
||||||
expect(message.reply).toBeCalledWith('Command not found');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('LoadEvents', () => {
|
|
||||||
test('Given Events Are Loaded, Expect Successful Result', () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
const client = {
|
|
||||||
on: jest.fn(),
|
|
||||||
} as unknown as Client;
|
|
||||||
|
|
||||||
const evt = mock<Event>();
|
|
||||||
|
|
||||||
const eventItem: IEventItem = {
|
|
||||||
Event: evt
|
|
||||||
};
|
|
||||||
|
|
||||||
const eventItems: IEventItem[] = [ eventItem ];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
|
|
||||||
const result = util.loadEvents(client, eventItems);
|
|
||||||
|
|
||||||
const clientOn = jest.spyOn(client, 'on');
|
|
||||||
|
|
||||||
expect(result.valid).toBeTruthy();
|
|
||||||
expect(clientOn).toBeCalledTimes(13);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given No Events Found, Expect Successful Result', () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_TOKEN: 'TOKEN',
|
|
||||||
BOT_PREFIX: '!',
|
|
||||||
FOLDERS_COMMANDS: 'commands',
|
|
||||||
FOLDERS_EVENTS: 'events',
|
|
||||||
}
|
|
||||||
|
|
||||||
const client = {
|
|
||||||
on: jest.fn(),
|
|
||||||
} as unknown as Client;
|
|
||||||
|
|
||||||
const eventItems: IEventItem[] = [];
|
|
||||||
|
|
||||||
const util = new Util();
|
|
||||||
|
|
||||||
const result = util.loadEvents(client, eventItems);
|
|
||||||
|
|
||||||
const clientOn = jest.spyOn(client, 'on');
|
|
||||||
|
|
||||||
expect(result.valid).toBeTruthy();
|
|
||||||
expect(clientOn).toBeCalledTimes(0);
|
|
||||||
});
|
|
||||||
});
|
|
17
tests/commands/304276391837302787/moons.test.ts
Normal file
17
tests/commands/304276391837302787/moons.test.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
describe("constructor", () => {
|
||||||
|
test.todo("EXPECT CommandBuilder to be defined correctly");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("execute", () => {
|
||||||
|
describe("GIVEN interaction is not a chat input command", () => {
|
||||||
|
test.todo("EXPECT nothing to happen");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN interaction subcommand is list", () => {
|
||||||
|
test.todo("EXPECT ListMoons to be called");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN interaction subcommand is add", () => {
|
||||||
|
test.todo("EXPECT AddMoon to be called");
|
||||||
|
});
|
||||||
|
});
|
|
@ -0,0 +1,25 @@
|
||||||
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
|
exports[`GIVEN happy flow EXPECT embed to be replied 1`] = `
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"color": 5294200,
|
||||||
|
"description": "**2 -** Test Description",
|
||||||
|
"thumbnail": {
|
||||||
|
"url": "https://cdn.discordapp.com/emojis/374131312182689793.webp?size=96&quality=lossless",
|
||||||
|
},
|
||||||
|
"title": "undefined Got A Moon!",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`GIVEN happy flow EXPECT moon to be saved 1`] = `
|
||||||
|
{
|
||||||
|
"Description": "Test Description",
|
||||||
|
"Id": Any<String>,
|
||||||
|
"MoonNumber": 2,
|
||||||
|
"UserId": "userId",
|
||||||
|
"WhenCreated": Any<Date>,
|
||||||
|
"WhenUpdated": Any<Date>,
|
||||||
|
}
|
||||||
|
`;
|
|
@ -0,0 +1,40 @@
|
||||||
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
|
exports[`GIVEN happy flow EXPECT interaction to be replied 1`] = `
|
||||||
|
{
|
||||||
|
"components": [
|
||||||
|
{
|
||||||
|
"components": [
|
||||||
|
{
|
||||||
|
"custom_id": "moons list userId -1",
|
||||||
|
"disabled": true,
|
||||||
|
"emoji": undefined,
|
||||||
|
"label": "Previous",
|
||||||
|
"style": 1,
|
||||||
|
"type": 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"custom_id": "moons list userId 01",
|
||||||
|
"disabled": true,
|
||||||
|
"emoji": undefined,
|
||||||
|
"label": "Next",
|
||||||
|
"style": 1,
|
||||||
|
"type": 2,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"type": 1,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"embeds": [
|
||||||
|
{
|
||||||
|
"color": 3166394,
|
||||||
|
"description": "**1 -** Test Descriptio",
|
||||||
|
"footer": {
|
||||||
|
"icon_url": undefined,
|
||||||
|
"text": "Page 01 of 1 · 0 moons",
|
||||||
|
},
|
||||||
|
"title": "undefined's Moons",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
`;
|
96
tests/commands/304276391837302787/moons/add.test.ts
Normal file
96
tests/commands/304276391837302787/moons/add.test.ts
Normal file
|
@ -0,0 +1,96 @@
|
||||||
|
import { CommandInteraction, EmbedBuilder } from "discord.js";
|
||||||
|
import AddMoon from "../../../../src/commands/304276391837302787/moons/add";
|
||||||
|
import Moon from "../../../../src/database/entities/304276391837302787/Moon";
|
||||||
|
import UserSetting from "../../../../src/database/entities/UserSetting";
|
||||||
|
|
||||||
|
describe("GIVEN happy flow", () => {
|
||||||
|
let repliedWithEmbed: EmbedBuilder[] | undefined;
|
||||||
|
let savedMoon: Moon | undefined;
|
||||||
|
|
||||||
|
const interaction = {
|
||||||
|
reply: jest.fn((options: any) => {
|
||||||
|
repliedWithEmbed = options.embeds;
|
||||||
|
}),
|
||||||
|
options: {
|
||||||
|
get: jest.fn()
|
||||||
|
.mockReturnValueOnce({
|
||||||
|
value: "Test Description",
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
id: "userId",
|
||||||
|
},
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
|
const userSetting = {
|
||||||
|
Value: 1,
|
||||||
|
UpdateValue: jest.fn(),
|
||||||
|
Save: jest.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
Moon.FetchMoonCountByUserId = jest.fn().mockResolvedValue(1);
|
||||||
|
Moon.prototype.Save = jest.fn().mockImplementation((_, entity: Moon) => {
|
||||||
|
savedMoon = entity;
|
||||||
|
});
|
||||||
|
|
||||||
|
UserSetting.FetchOneByKey = jest.fn().mockResolvedValue(userSetting);
|
||||||
|
|
||||||
|
await AddMoon(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT description option to have been fetched", () => {
|
||||||
|
expect(interaction.options.get).toHaveBeenCalledTimes(1);
|
||||||
|
expect(interaction.options.get).toHaveBeenCalledWith("description", true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT UserSetting to have been fetched", () => {
|
||||||
|
expect(UserSetting.FetchOneByKey).toHaveBeenCalledTimes(1);
|
||||||
|
expect(UserSetting.FetchOneByKey).toHaveBeenCalledWith("userId", "moons");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT moonCount to be updated +1", () => {
|
||||||
|
expect(userSetting.UpdateValue).toHaveBeenCalledTimes(1);
|
||||||
|
expect(userSetting.UpdateValue).toHaveBeenCalledWith("2");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT setting to be saved", () => {
|
||||||
|
expect(userSetting.Save).toHaveBeenCalledTimes(1);
|
||||||
|
expect(userSetting.Save).toHaveBeenCalledWith(UserSetting, userSetting);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT moon to be saved", () => {
|
||||||
|
expect(Moon.prototype.Save).toHaveBeenCalledTimes(1);
|
||||||
|
expect(Moon.prototype.Save).toHaveBeenCalledWith(Moon, expect.any(Moon));
|
||||||
|
|
||||||
|
expect(savedMoon).toBeDefined();
|
||||||
|
expect(savedMoon).toMatchSnapshot({
|
||||||
|
Id: expect.any(String),
|
||||||
|
WhenCreated: expect.any(Date),
|
||||||
|
WhenUpdated: expect.any(Date),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT embed to be replied", () => {
|
||||||
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
|
expect(repliedWithEmbed).toBeDefined();
|
||||||
|
expect(repliedWithEmbed).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN description is null", () => {
|
||||||
|
test.todo("EXPECT error replied");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN description is greater than 255 characters", () => {
|
||||||
|
test.todo("EXPECT error replied");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN moon count setting exists", () => {
|
||||||
|
test.todo("EXPECT existing entity to be updated");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN moon count setting does not exist", () => {
|
||||||
|
test.todo("EXPECT new entity to be created");
|
||||||
|
});
|
249
tests/commands/304276391837302787/moons/list.test.ts
Normal file
249
tests/commands/304276391837302787/moons/list.test.ts
Normal file
|
@ -0,0 +1,249 @@
|
||||||
|
import { ActionRowBuilder, APIEmbed, ButtonBuilder, CommandInteraction, EmbedBuilder, InteractionReplyOptions } from "discord.js";
|
||||||
|
import List from "../../../../src/commands/304276391837302787/moons/list";
|
||||||
|
import Moon from "../../../../src/database/entities/304276391837302787/Moon";
|
||||||
|
import UserSetting from "../../../../src/database/entities/UserSetting";
|
||||||
|
|
||||||
|
describe("GIVEN happy flow", () => {
|
||||||
|
let repliedWith: InteractionReplyOptions | undefined;
|
||||||
|
|
||||||
|
const interaction = {
|
||||||
|
reply: jest.fn((options) => {
|
||||||
|
repliedWith = options;
|
||||||
|
}),
|
||||||
|
options: {
|
||||||
|
get: jest.fn()
|
||||||
|
.mockReturnValueOnce(undefined) // User
|
||||||
|
.mockReturnValue({
|
||||||
|
value: "0",
|
||||||
|
}), // Page
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
id: "userId",
|
||||||
|
}
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue([
|
||||||
|
[
|
||||||
|
{
|
||||||
|
MoonNumber: 1,
|
||||||
|
Description: "Test Description",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
1,
|
||||||
|
]);
|
||||||
|
|
||||||
|
UserSetting.FetchOneByKey = jest.fn().mockResolvedValue({
|
||||||
|
Value: "0",
|
||||||
|
});
|
||||||
|
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT interaction to be replied", () => {
|
||||||
|
expect(interaction.reply).toHaveBeenCalledTimes(1);
|
||||||
|
expect(repliedWith).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN moons returned is empty", () => {
|
||||||
|
let repliedWith: InteractionReplyOptions | undefined;
|
||||||
|
|
||||||
|
const interaction = {
|
||||||
|
reply: jest.fn((options) => {
|
||||||
|
repliedWith = options;
|
||||||
|
}),
|
||||||
|
options: {
|
||||||
|
get: jest.fn()
|
||||||
|
.mockReturnValueOnce(undefined) // User
|
||||||
|
.mockReturnValue({
|
||||||
|
value: "0",
|
||||||
|
}), // Page
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
id: "userId",
|
||||||
|
}
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue([
|
||||||
|
[],
|
||||||
|
0,
|
||||||
|
]);
|
||||||
|
|
||||||
|
UserSetting.FetchOneByKey = jest.fn().mockResolvedValue({
|
||||||
|
Value: "0",
|
||||||
|
});
|
||||||
|
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT none description", () => {
|
||||||
|
expect(repliedWith).toBeDefined();
|
||||||
|
|
||||||
|
expect(repliedWith!.embeds).toBeDefined();
|
||||||
|
expect(repliedWith!.embeds!.length).toBe(1);
|
||||||
|
|
||||||
|
const repliedWithEmbed = repliedWith!.embeds![0] as EmbedBuilder;
|
||||||
|
|
||||||
|
expect(repliedWithEmbed.data.description).toBe("*none*");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN it is the first page", () => {
|
||||||
|
let repliedWith: InteractionReplyOptions | undefined;
|
||||||
|
|
||||||
|
const interaction = {
|
||||||
|
reply: jest.fn((options) => {
|
||||||
|
repliedWith = options;
|
||||||
|
}),
|
||||||
|
options: {
|
||||||
|
get: jest.fn()
|
||||||
|
.mockReturnValueOnce(undefined) // User
|
||||||
|
.mockReturnValue({
|
||||||
|
value: "0",
|
||||||
|
}), // Page
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
id: "userId",
|
||||||
|
}
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue([
|
||||||
|
[
|
||||||
|
{
|
||||||
|
MoonNumber: 1,
|
||||||
|
Description: "Test Description",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
1,
|
||||||
|
]);
|
||||||
|
|
||||||
|
UserSetting.FetchOneByKey = jest.fn().mockResolvedValue({
|
||||||
|
Value: "0",
|
||||||
|
});
|
||||||
|
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT Previous button to be disabled", () => {
|
||||||
|
expect(repliedWith).toBeDefined();
|
||||||
|
|
||||||
|
expect(repliedWith!.components).toBeDefined();
|
||||||
|
expect(repliedWith!.components!.length).toBe(1);
|
||||||
|
|
||||||
|
const repliedWithRow = repliedWith!.components![0] as ActionRowBuilder<ButtonBuilder>;
|
||||||
|
|
||||||
|
expect(repliedWithRow.components[0].data.disabled).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN it is the last page", () => {
|
||||||
|
let repliedWith: InteractionReplyOptions | undefined;
|
||||||
|
|
||||||
|
const interaction = {
|
||||||
|
reply: jest.fn((options) => {
|
||||||
|
repliedWith = options;
|
||||||
|
}),
|
||||||
|
options: {
|
||||||
|
get: jest.fn()
|
||||||
|
.mockReturnValueOnce(undefined) // User
|
||||||
|
.mockReturnValue({
|
||||||
|
value: "0",
|
||||||
|
}), // Page
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
id: "userId",
|
||||||
|
}
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue([
|
||||||
|
[
|
||||||
|
{
|
||||||
|
MoonNumber: 1,
|
||||||
|
Description: "Test Description",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
1,
|
||||||
|
]);
|
||||||
|
|
||||||
|
UserSetting.FetchOneByKey = jest.fn().mockResolvedValue({
|
||||||
|
Value: "0",
|
||||||
|
});
|
||||||
|
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT Next button to be disabled", () => {
|
||||||
|
expect(repliedWith).toBeDefined();
|
||||||
|
|
||||||
|
expect(repliedWith!.components).toBeDefined();
|
||||||
|
expect(repliedWith!.components!.length).toBe(1);
|
||||||
|
|
||||||
|
const repliedWithRow = repliedWith!.components![0] as ActionRowBuilder<ButtonBuilder>;
|
||||||
|
|
||||||
|
expect(repliedWithRow.components[1].data.disabled).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN moon count is greater than the amount of moons in the database", () => {
|
||||||
|
beforeAll(async () => {
|
||||||
|
UserSetting.FetchOneByKey = jest.fn().mockResolvedValue({
|
||||||
|
Value: "2",
|
||||||
|
});
|
||||||
|
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT untracked counter to be shown", () => {
|
||||||
|
const repliedWithEmbed = repliedWith!.embeds![0] as EmbedBuilder;
|
||||||
|
|
||||||
|
expect(repliedWithEmbed.data.description).toContain("...plus 1 more untracked");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("GIVEN moon count is empty", () => {
|
||||||
|
let repliedWith: InteractionReplyOptions | undefined;
|
||||||
|
|
||||||
|
const interaction = {
|
||||||
|
reply: jest.fn((options) => {
|
||||||
|
repliedWith = options;
|
||||||
|
}),
|
||||||
|
options: {
|
||||||
|
get: jest.fn()
|
||||||
|
.mockReturnValueOnce(undefined) // User
|
||||||
|
.mockReturnValue({
|
||||||
|
value: "0",
|
||||||
|
}), // Page
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
id: "userId",
|
||||||
|
}
|
||||||
|
} as unknown as CommandInteraction;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
Moon.FetchPaginatedMoonsByUserId = jest.fn().mockResolvedValue([
|
||||||
|
[],
|
||||||
|
0,
|
||||||
|
]);
|
||||||
|
|
||||||
|
UserSetting.FetchOneByKey = jest.fn().mockResolvedValue({
|
||||||
|
Value: "0",
|
||||||
|
});
|
||||||
|
|
||||||
|
await List(interaction);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("EXPECT Next button to be disabled", () => {
|
||||||
|
expect(repliedWith).toBeDefined();
|
||||||
|
|
||||||
|
expect(repliedWith!.components).toBeDefined();
|
||||||
|
expect(repliedWith!.components!.length).toBe(1);
|
||||||
|
|
||||||
|
const repliedWithRow = repliedWith!.components![0] as ActionRowBuilder<ButtonBuilder>;
|
||||||
|
|
||||||
|
expect(repliedWithRow.components[1].data.disabled).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,152 +0,0 @@
|
||||||
import { Message } from "discord.js";
|
|
||||||
import { mock } from "jest-mock-extended";
|
|
||||||
import About from "../../src/commands/about";
|
|
||||||
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
|
||||||
import PublicEmbed from "../../src/helpers/embeds/PublicEmbed";
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
process.env = {};
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Constructor', () => {
|
|
||||||
test('Expect values set', () => {
|
|
||||||
const about = new About();
|
|
||||||
|
|
||||||
expect(about._category).toBe("General");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Execute', () => {
|
|
||||||
test('Expect embed to be made and sent to the current channel', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_VER: "BOT_VER",
|
|
||||||
BOT_AUTHOR: "BOT_AUTHOR",
|
|
||||||
BOT_DATE: "BOT_DATE"
|
|
||||||
};
|
|
||||||
|
|
||||||
const message = mock<Message>();
|
|
||||||
message.channel.send = jest.fn();
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: "about",
|
|
||||||
args: [],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const about = new About();
|
|
||||||
|
|
||||||
const result = await about.execute(context);
|
|
||||||
|
|
||||||
expect(message.channel.send).toBeCalledTimes(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Expect embed send to have values', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_VER: "BOT_VER",
|
|
||||||
BOT_AUTHOR: "BOT_AUTHOR",
|
|
||||||
BOT_DATE: "BOT_DATE"
|
|
||||||
};
|
|
||||||
|
|
||||||
const message = mock<Message>();
|
|
||||||
message.channel.send = jest.fn();
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: "about",
|
|
||||||
args: [],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const about = new About();
|
|
||||||
|
|
||||||
const result = await about.execute(context);
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
const embed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embed.title).toBe('About');
|
|
||||||
expect(embed.description).toBe('');
|
|
||||||
expect(embed.fields.length).toBe(3);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Expect version field to have values', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_VER: "BOT_VER",
|
|
||||||
BOT_AUTHOR: "BOT_AUTHOR",
|
|
||||||
BOT_DATE: "BOT_DATE"
|
|
||||||
};
|
|
||||||
|
|
||||||
const message = mock<Message>();
|
|
||||||
message.channel.send = jest.fn();
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: "about",
|
|
||||||
args: [],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const about = new About();
|
|
||||||
|
|
||||||
const result = await about.execute(context);
|
|
||||||
|
|
||||||
const embed = result.embeds[0];
|
|
||||||
const field = embed.fields[0];
|
|
||||||
|
|
||||||
expect(field.name).toBe('Version');
|
|
||||||
expect(field.value).toBe('BOT_VER');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Expect author field to have values', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_VER: "BOT_VER",
|
|
||||||
BOT_AUTHOR: "BOT_AUTHOR",
|
|
||||||
BOT_DATE: "BOT_DATE"
|
|
||||||
};
|
|
||||||
|
|
||||||
const message = mock<Message>();
|
|
||||||
message.channel.send = jest.fn();
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: "about",
|
|
||||||
args: [],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const about = new About();
|
|
||||||
|
|
||||||
const result = await about.execute(context);
|
|
||||||
|
|
||||||
const embed = result.embeds[0];
|
|
||||||
const field = embed.fields[1];
|
|
||||||
|
|
||||||
expect(field.name).toBe('Author');
|
|
||||||
expect(field.value).toBe('BOT_AUTHOR');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Expect version field to have values', async () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_VER: "BOT_VER",
|
|
||||||
BOT_AUTHOR: "BOT_AUTHOR",
|
|
||||||
BOT_DATE: "BOT_DATE"
|
|
||||||
};
|
|
||||||
|
|
||||||
const message = mock<Message>();
|
|
||||||
message.channel.send = jest.fn();
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: "about",
|
|
||||||
args: [],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const about = new About();
|
|
||||||
|
|
||||||
const result = await about.execute(context);
|
|
||||||
|
|
||||||
const embed = result.embeds[0];
|
|
||||||
const field = embed.fields[2];
|
|
||||||
|
|
||||||
expect(field.name).toBe('Date');
|
|
||||||
expect(field.value).toBe('BOT_DATE');
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,724 +0,0 @@
|
||||||
import { GuildMember, Message, TextChannel, User } from "discord.js";
|
|
||||||
import Ban from "../../src/commands/ban";
|
|
||||||
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
process.env = {};
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Constructor', () => {
|
|
||||||
test('Expect values to be set', () => {
|
|
||||||
process.env.ROLES_MODERATOR = 'Moderator';
|
|
||||||
|
|
||||||
const ban = new Ban();
|
|
||||||
|
|
||||||
expect(ban._category).toBe('Moderation');
|
|
||||||
expect(ban._roles.length).toBe(1);
|
|
||||||
expect(ban._roles[0]).toBe('Moderator');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Execute', () => {
|
|
||||||
test('Given user has permission, expect user to be banned', async () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: 'Moderator',
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
};
|
|
||||||
|
|
||||||
const mentionedUser = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
const mentionedMember = {
|
|
||||||
bannable: true,
|
|
||||||
ban: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(mentionedUser);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(mentionedMember);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember ,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'ban',
|
|
||||||
args: ['ban', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const ban = new Ban();
|
|
||||||
|
|
||||||
const result = await ban.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(logChannel.send).toBeCalledTimes(1);
|
|
||||||
expect(mentionedMember.ban).toBeCalledWith({ reason: 'Moderator: AUTHORTAG, Reason: Test Reason' });
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given moderator did not supply a reason, expect default message', async () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: 'Moderator',
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
};
|
|
||||||
|
|
||||||
const mentionedUser = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
const mentionedMember = {
|
|
||||||
bannable: true,
|
|
||||||
ban: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(mentionedUser);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(mentionedMember);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember ,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'ban',
|
|
||||||
args: ['ban'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const ban = new Ban();
|
|
||||||
|
|
||||||
const result = await ban.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(logChannel.send).toBeCalledTimes(1);
|
|
||||||
expect(mentionedMember.ban).toBeCalledWith({ reason: 'Moderator: AUTHORTAG, Reason: *none*' });
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given user has permissions, expect embeds to be correct', async () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: 'Moderator',
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
};
|
|
||||||
|
|
||||||
const mentionedUser = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
const mentionedMember = {
|
|
||||||
bannable: true,
|
|
||||||
ban: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(mentionedUser);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(mentionedMember);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember ,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'ban',
|
|
||||||
args: ['ban', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const ban = new Ban();
|
|
||||||
|
|
||||||
const result = await ban.execute(context);
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(2);
|
|
||||||
|
|
||||||
const logEmbed = result.embeds[0];
|
|
||||||
const publicEmbed = result.embeds[1];
|
|
||||||
|
|
||||||
expect(logEmbed.title).toBe('Member Banned');
|
|
||||||
expect(publicEmbed.title).toBe("");
|
|
||||||
expect(publicEmbed.description).toBe('[object Object] has been banned');
|
|
||||||
expect(logEmbed.fields.length).toBe(3);
|
|
||||||
expect(publicEmbed.fields.length).toBe(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given user has permission, expect logEmbed fields to be correct', async () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: 'Moderator',
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
};
|
|
||||||
|
|
||||||
const mentionedUser = {
|
|
||||||
displayAvatarURL: jest.fn().mockReturnValue('URL'),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
const mentionedMember = {
|
|
||||||
bannable: true,
|
|
||||||
ban: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(mentionedUser);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(mentionedMember);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember ,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'ban',
|
|
||||||
args: ['ban', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const ban = new Ban();
|
|
||||||
|
|
||||||
const result = await ban.execute(context);
|
|
||||||
|
|
||||||
const logEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
const fieldUser = logEmbed.fields[0];
|
|
||||||
const fieldModerator = logEmbed.fields[1];
|
|
||||||
const fieldReason = logEmbed.fields[2];
|
|
||||||
|
|
||||||
expect(fieldUser.name).toBe("User");
|
|
||||||
expect(fieldUser.value).toBe("[object Object] `USERTAG`");
|
|
||||||
expect(logEmbed.thumbnail?.url).toBe("URL");
|
|
||||||
|
|
||||||
expect(fieldModerator.name).toBe('Moderator');
|
|
||||||
expect(fieldModerator.value).toBe('[object Object] `AUTHORTAG`');
|
|
||||||
|
|
||||||
expect(fieldReason.name).toBe('Reason');
|
|
||||||
expect(fieldReason.value).toBe('Test Reason');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given moderator did not supply a reason, expect reason field to be default message', async () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: 'Moderator',
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
};
|
|
||||||
|
|
||||||
const mentionedUser = {
|
|
||||||
displayAvatarURL: jest.fn().mockReturnValue('URL'),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
const mentionedMember = {
|
|
||||||
bannable: true,
|
|
||||||
ban: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(mentionedUser);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(mentionedMember);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember ,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'ban',
|
|
||||||
args: ['ban'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const ban = new Ban();
|
|
||||||
|
|
||||||
const result = await ban.execute(context);
|
|
||||||
|
|
||||||
const logEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
const fieldUser = logEmbed.fields[0];
|
|
||||||
const fieldModerator = logEmbed.fields[1];
|
|
||||||
const fieldReason = logEmbed.fields[2];
|
|
||||||
|
|
||||||
expect(fieldUser.name).toBe("User");
|
|
||||||
expect(fieldUser.value).toBe("[object Object] `USERTAG`");
|
|
||||||
expect(logEmbed.thumbnail?.url).toBe("URL");
|
|
||||||
|
|
||||||
expect(fieldModerator.name).toBe('Moderator');
|
|
||||||
expect(fieldModerator.value).toBe('[object Object] `AUTHORTAG`');
|
|
||||||
|
|
||||||
expect(fieldReason.name).toBe('Reason');
|
|
||||||
expect(fieldReason.value).toBe('*none*');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given user is not mentioned, expect error embed to be sent', async () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: 'Moderator',
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
};
|
|
||||||
|
|
||||||
const mentionedMember = {
|
|
||||||
bannable: true,
|
|
||||||
ban: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(null);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(mentionedMember);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember ,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'ban',
|
|
||||||
args: ['ban', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const ban = new Ban();
|
|
||||||
|
|
||||||
const result = await ban.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(logChannel.send).not.toBeCalled();
|
|
||||||
expect(mentionedMember.ban).not.toBeCalled();
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
const embedError = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embedError.description).toBe('User does not exist');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given member is not in server, expect error embed to be sent', async () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: 'Moderator',
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
};
|
|
||||||
|
|
||||||
const mentionedUser = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
const mentionedMember = {
|
|
||||||
bannable: true,
|
|
||||||
ban: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(mentionedUser);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(null);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember ,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'ban',
|
|
||||||
args: ['ban', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const ban = new Ban();
|
|
||||||
|
|
||||||
const result = await ban.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(logChannel.send).not.toBeCalled();
|
|
||||||
expect(mentionedMember.ban).not.toBeCalled();
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
const embedError = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embedError.description).toBe('User is not in this server');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given guild is unavailable, expect return and do nothing', async () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: 'Moderator',
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
};
|
|
||||||
|
|
||||||
const mentionedUser = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
const mentionedMember = {
|
|
||||||
bannable: true,
|
|
||||||
ban: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(mentionedUser);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(mentionedMember);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember ,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: false
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'ban',
|
|
||||||
args: ['ban', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const ban = new Ban();
|
|
||||||
|
|
||||||
const result = await ban.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).not.toBeCalled();
|
|
||||||
expect(logChannel.send).not.toBeCalled();
|
|
||||||
expect(mentionedMember.ban).not.toBeCalled();
|
|
||||||
expect(result.embeds.length).toBe(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given bot cant ban user, expect error embed to be sent', async () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: 'Moderator',
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
};
|
|
||||||
|
|
||||||
const mentionedUser = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
const mentionedMember = {
|
|
||||||
bannable: false,
|
|
||||||
ban: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(mentionedUser);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(mentionedMember);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember ,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'ban',
|
|
||||||
args: ['ban', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const ban = new Ban();
|
|
||||||
|
|
||||||
const result = await ban.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(logChannel.send).not.toBeCalled();
|
|
||||||
expect(mentionedMember.ban).not.toBeCalled();
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
const embedError = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embedError.description).toBe('Unable to do this action, am I missing permissions?');
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,178 +0,0 @@
|
||||||
import { Message } from "discord.js";
|
|
||||||
import Clear from "../../src/commands/clear";
|
|
||||||
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
process.env = {};
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Constructor', () => {
|
|
||||||
test('Expect values to be set', () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: "Moderator"
|
|
||||||
};
|
|
||||||
|
|
||||||
const clear = new Clear();
|
|
||||||
|
|
||||||
expect(clear._category).toBe('Moderation');
|
|
||||||
expect(clear._roles.length).toBe(1);
|
|
||||||
expect(clear._roles[0]).toBe('Moderator');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Execute', () => {
|
|
||||||
test('Given valid arguments, expect messages to be cleared', async () => {
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageChannelBulkDelete = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend,
|
|
||||||
bulkDelete: messageChannelBulkDelete
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'clear',
|
|
||||||
args: ['5'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const clear = new Clear();
|
|
||||||
const result = await clear.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(messageChannelBulkDelete).toBeCalledWith(5);
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// PublicEmbed
|
|
||||||
const publicEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(publicEmbed.title).toBe('');
|
|
||||||
expect(publicEmbed.description).toBe('5 message(s) were removed');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given argument is not given, expect error embed to be sent', async () => {
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageChannelBulkDelete = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend,
|
|
||||||
bulkDelete: messageChannelBulkDelete
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'clear',
|
|
||||||
args: [],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const clear = new Clear();
|
|
||||||
const result = await clear.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(messageChannelBulkDelete).not.toBeCalled();
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// ErrorEmbed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.title).toBeNull();
|
|
||||||
expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given argument is not a number, expect error embed to be sent', async () => {
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageChannelBulkDelete = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend,
|
|
||||||
bulkDelete: messageChannelBulkDelete
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'clear',
|
|
||||||
args: ['A'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const clear = new Clear();
|
|
||||||
const result = await clear.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(messageChannelBulkDelete).not.toBeCalled();
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// ErrorEmbed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.title).toBeNull();
|
|
||||||
expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given argument is less than 1, expect error embed to be sent', async () => {
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageChannelBulkDelete = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend,
|
|
||||||
bulkDelete: messageChannelBulkDelete
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'clear',
|
|
||||||
args: ['0'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const clear = new Clear();
|
|
||||||
const result = await clear.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(messageChannelBulkDelete).not.toBeCalled();
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// ErrorEmbed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.title).toBeNull();
|
|
||||||
expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given argument is more than 100, expect error embed to be sent', async () => {
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageChannelBulkDelete = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend,
|
|
||||||
bulkDelete: messageChannelBulkDelete
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'clear',
|
|
||||||
args: ['101'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const clear = new Clear();
|
|
||||||
const result = await clear.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(messageChannelBulkDelete).not.toBeCalled();
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// ErrorEmbed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.title).toBeNull();
|
|
||||||
expect(errorEmbed.description).toBe('Please specify an amount between 1 and 100');
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,136 +0,0 @@
|
||||||
import { Message } from "discord.js";
|
|
||||||
import Evaluate from "../../src/commands/eval";
|
|
||||||
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
process.env = {};
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Constructor', () => {
|
|
||||||
test('Expect values to be set', () => {
|
|
||||||
const evaluate = new Evaluate();
|
|
||||||
|
|
||||||
expect(evaluate._category).toBe('Owner');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Execute', () => {
|
|
||||||
test('Given user has permission, expect eval statement ran', () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_OWNERID: 'OWNERID'
|
|
||||||
};
|
|
||||||
|
|
||||||
console.log = jest.fn();
|
|
||||||
global.eval = jest.fn()
|
|
||||||
.mockReturnValue('General Kenobi');
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
author: {
|
|
||||||
id: 'OWNERID'
|
|
||||||
},
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'eval',
|
|
||||||
args: ['echo', 'Hello', 'there'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const evaluate = new Evaluate();
|
|
||||||
|
|
||||||
const result = evaluate.execute(context);
|
|
||||||
|
|
||||||
expect(console.log).toBeCalledWith('Eval Statement: echo Hello there');
|
|
||||||
expect(global.eval).toBeCalledWith('echo Hello there');
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// PublicEmbed
|
|
||||||
const publicEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(publicEmbed.title).toBe('');
|
|
||||||
expect(publicEmbed.description).toBe('General Kenobi');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given user does not have permission, expect nothing to occur', () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_OWNERID: 'DIFFERENT'
|
|
||||||
};
|
|
||||||
|
|
||||||
console.log = jest.fn();
|
|
||||||
global.eval = jest.fn()
|
|
||||||
.mockReturnValue('General Kenobi');
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
author: {
|
|
||||||
id: 'OWNERID'
|
|
||||||
},
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'eval',
|
|
||||||
args: ['echo', 'Hello', 'there'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const evaluate = new Evaluate();
|
|
||||||
|
|
||||||
const result = evaluate.execute(context);
|
|
||||||
|
|
||||||
expect(console.log).not.toBeCalled();
|
|
||||||
expect(global.eval).not.toBeCalled();
|
|
||||||
expect(result.embeds.length).toBe(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given eval failed, expect error embed to be sent', () => {
|
|
||||||
process.env = {
|
|
||||||
BOT_OWNERID: 'OWNERID'
|
|
||||||
};
|
|
||||||
|
|
||||||
console.log = jest.fn();
|
|
||||||
global.eval = jest.fn()
|
|
||||||
.mockImplementation(() => {
|
|
||||||
throw new Error('Error message');
|
|
||||||
});
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
author: {
|
|
||||||
id: 'OWNERID'
|
|
||||||
},
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'eval',
|
|
||||||
args: ['echo', 'Hello', 'there'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const evaluate = new Evaluate();
|
|
||||||
|
|
||||||
const result = evaluate.execute(context);
|
|
||||||
|
|
||||||
expect(console.log).toBeCalledWith('Eval Statement: echo Hello there');
|
|
||||||
expect(global.eval).toBeCalledWith('echo Hello there');
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// ErrorEmbed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.title).toBeNull();
|
|
||||||
expect(errorEmbed.description).toBe('Error: Error message');
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,267 +0,0 @@
|
||||||
import Help, { ICommandData } from "../../src/commands/help";
|
|
||||||
import { Message } from "discord.js";
|
|
||||||
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
|
||||||
|
|
||||||
const oldCwd = process.cwd();
|
|
||||||
|
|
||||||
describe('Constructor', () => {
|
|
||||||
test('Expect properties to be set', () => {
|
|
||||||
const help = new Help();
|
|
||||||
|
|
||||||
expect(help._category).toBe('General');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Execute', () => {
|
|
||||||
test('Given no arguments were given, expect SendAll to be executed', () => {
|
|
||||||
const message = {} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'help',
|
|
||||||
args: [],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const help = new Help();
|
|
||||||
|
|
||||||
help.SendAll = jest.fn();
|
|
||||||
help.SendSingle = jest.fn();
|
|
||||||
|
|
||||||
help.execute(context);
|
|
||||||
|
|
||||||
expect(help.SendAll).toBeCalled();
|
|
||||||
expect(help.SendSingle).not.toBeCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given an argument was given, expect SendSingle to be executed', () => {
|
|
||||||
const message = {} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'help',
|
|
||||||
args: ['about'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const help = new Help();
|
|
||||||
|
|
||||||
help.SendAll = jest.fn();
|
|
||||||
help.SendSingle = jest.fn();
|
|
||||||
|
|
||||||
help.execute(context);
|
|
||||||
|
|
||||||
expect(help.SendAll).not.toBeCalled();
|
|
||||||
expect(help.SendSingle).toBeCalled();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('SendAll', () => {
|
|
||||||
test('Expect embed with all commands to be sent', () => {
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'help',
|
|
||||||
args: [],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const help = new Help();
|
|
||||||
|
|
||||||
const commandData0: ICommandData = {
|
|
||||||
Exists: true,
|
|
||||||
Name: 'about',
|
|
||||||
Category: 'general',
|
|
||||||
Roles: []
|
|
||||||
};
|
|
||||||
|
|
||||||
const commandData1: ICommandData = {
|
|
||||||
Exists: true,
|
|
||||||
Name: 'role',
|
|
||||||
Category: 'general',
|
|
||||||
Roles: []
|
|
||||||
};
|
|
||||||
|
|
||||||
help.GetAllCommandData = jest.fn()
|
|
||||||
.mockReturnValue([commandData0, commandData1]);
|
|
||||||
|
|
||||||
const result = help.SendAll(context);
|
|
||||||
|
|
||||||
expect(help.GetAllCommandData).toBeCalled();
|
|
||||||
expect(messageChannelSend).toBeCalled();
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// PublicEmbed
|
|
||||||
const publicEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(publicEmbed.fields.length).toBe(1);
|
|
||||||
|
|
||||||
// PublicEmbed -> GeneralCategory Field
|
|
||||||
const publicEmbedFieldGeneral = publicEmbed.fields[0];
|
|
||||||
|
|
||||||
expect(publicEmbedFieldGeneral.name).toBe('General');
|
|
||||||
expect(publicEmbedFieldGeneral.value).toBe('about, role');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('SendSingle', () => {
|
|
||||||
test('Given command exists, expect embed to be sent with command fields', () => {
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'help',
|
|
||||||
args: ['about'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const commandData: ICommandData = {
|
|
||||||
Exists: true,
|
|
||||||
Name: 'about',
|
|
||||||
Category: 'general',
|
|
||||||
Roles: ['role1', 'role2']
|
|
||||||
};
|
|
||||||
|
|
||||||
const help = new Help();
|
|
||||||
|
|
||||||
help.GetCommandData = jest.fn()
|
|
||||||
.mockReturnValue(commandData);
|
|
||||||
|
|
||||||
const result = help.SendSingle(context);
|
|
||||||
|
|
||||||
expect(help.GetCommandData).toBeCalledWith('about');
|
|
||||||
expect(messageChannelSend).toBeCalled();
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// PublicEmbed
|
|
||||||
const publicEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(publicEmbed.title).toBe('About');
|
|
||||||
expect(publicEmbed.description).toBe('');
|
|
||||||
expect(publicEmbed.fields.length).toBe(2);
|
|
||||||
|
|
||||||
// PublicEmbed -> Category Field
|
|
||||||
const fieldCategory = publicEmbed.fields[0];
|
|
||||||
|
|
||||||
expect(fieldCategory.name).toBe('Category');
|
|
||||||
expect(fieldCategory.value).toBe('General');
|
|
||||||
|
|
||||||
// PublicEmbed -> RequiredRoles Field
|
|
||||||
const fieldRoles = publicEmbed.fields[1];
|
|
||||||
|
|
||||||
expect(fieldRoles.name).toBe('Required Roles');
|
|
||||||
expect(fieldRoles.value).toBe('Role1, Role2');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given command does not exist, expect error embed to be sent', () => {
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'help',
|
|
||||||
args: ['about'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const commandData: ICommandData = {
|
|
||||||
Exists: false
|
|
||||||
};
|
|
||||||
|
|
||||||
const help = new Help();
|
|
||||||
|
|
||||||
help.GetCommandData = jest.fn()
|
|
||||||
.mockReturnValue(commandData);
|
|
||||||
|
|
||||||
const result = help.SendSingle(context);
|
|
||||||
|
|
||||||
expect(help.GetCommandData).toBeCalledWith('about');
|
|
||||||
expect(messageChannelSend).toBeCalled();
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// ErrorEmbed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.description).toBe('Command does not exist');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('GetAllCommandData', () => {
|
|
||||||
test('Expect array of command data to be returned', () => {
|
|
||||||
process.env = {
|
|
||||||
FOLDERS_COMMANDS: "commands"
|
|
||||||
};
|
|
||||||
|
|
||||||
process.cwd = jest.fn()
|
|
||||||
.mockReturnValue(`${oldCwd}/tests/_mocks`);
|
|
||||||
|
|
||||||
const help = new Help();
|
|
||||||
|
|
||||||
const result = help.GetAllCommandData();
|
|
||||||
|
|
||||||
expect(result.length).toBe(1);
|
|
||||||
|
|
||||||
// Mock Command
|
|
||||||
const mockCommand = result[0];
|
|
||||||
|
|
||||||
expect(mockCommand.Exists).toBeTruthy();
|
|
||||||
expect(mockCommand.Name).toBe("mockCmd");
|
|
||||||
expect(mockCommand.Category).toBe("General");
|
|
||||||
|
|
||||||
expect(mockCommand.Roles!.length).toBe(1);
|
|
||||||
expect(mockCommand.Roles![0]).toBe("Moderator");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('GetCommandData', () => {
|
|
||||||
test('Given command exists, expect data to be returned', () => {
|
|
||||||
process.env = {
|
|
||||||
FOLDERS_COMMANDS: "commands"
|
|
||||||
};
|
|
||||||
|
|
||||||
process.cwd = jest.fn()
|
|
||||||
.mockReturnValue(`${oldCwd}/tests/_mocks`);
|
|
||||||
|
|
||||||
const help = new Help();
|
|
||||||
|
|
||||||
const result = help.GetCommandData('mockCmd');
|
|
||||||
|
|
||||||
expect(result.Exists).toBeTruthy();
|
|
||||||
expect(result.Name).toBe("mockCmd");
|
|
||||||
expect(result.Category).toBe("General");
|
|
||||||
|
|
||||||
expect(result.Roles!.length).toBe(1);
|
|
||||||
expect(result.Roles![0]).toBe("Moderator");
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given command does not exist, expect exists false to be returned', () => {
|
|
||||||
process.env = {
|
|
||||||
FOLDERS_COMMANDS: "commands"
|
|
||||||
};
|
|
||||||
|
|
||||||
const oldCwd = process.cwd();
|
|
||||||
|
|
||||||
process.cwd = jest.fn()
|
|
||||||
.mockReturnValue(`${oldCwd}/tests/_mocks`);
|
|
||||||
|
|
||||||
const help = new Help();
|
|
||||||
|
|
||||||
const result = help.GetCommandData('none');
|
|
||||||
|
|
||||||
expect(result.Exists).toBeFalsy();
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,549 +0,0 @@
|
||||||
import { mock } from "jest-mock-extended";
|
|
||||||
|
|
||||||
import { GuildMember, Message, TextChannel, User } from "discord.js";
|
|
||||||
import Kick from "../../src/commands/kick";
|
|
||||||
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
process.env = {};
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Constructor', () => {
|
|
||||||
test('Expect properties to be set', () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: "Moderator"
|
|
||||||
};
|
|
||||||
|
|
||||||
const kick = new Kick();
|
|
||||||
|
|
||||||
expect(kick._category).toBe('Moderation');
|
|
||||||
expect(kick._roles.length).toBe(1);
|
|
||||||
expect(kick._roles[0]).toBe('Moderator');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Execute', () => {
|
|
||||||
test('Given user has permission, expect user to be kicked', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
};
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
kickable: true,
|
|
||||||
kick: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(user);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(member);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: "kick",
|
|
||||||
args: ["USER", "Test", "Reason"],
|
|
||||||
message: message
|
|
||||||
}
|
|
||||||
|
|
||||||
const kick = new Kick();
|
|
||||||
|
|
||||||
const result = await kick.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(logChannel.send).toBeCalledTimes(1);
|
|
||||||
expect(member.kick).toBeCalledWith('Moderator: AUTHORTAG, Reason: Test Reason');
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(2);
|
|
||||||
|
|
||||||
// Log Embed
|
|
||||||
const logEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(logEmbed.title).toBe('Member Kicked');
|
|
||||||
expect(logEmbed.fields.length).toBe(3);
|
|
||||||
|
|
||||||
// Log Embed -> User Field
|
|
||||||
const logEmbedFieldUser = logEmbed.fields[0];
|
|
||||||
|
|
||||||
expect(logEmbedFieldUser.name).toBe('User');
|
|
||||||
expect(logEmbedFieldUser.value).toBe('[object Object] `USERTAG`');
|
|
||||||
expect(logEmbedFieldUser.inline).toBeTruthy();
|
|
||||||
|
|
||||||
// Log Embed -> Moderator Field
|
|
||||||
const logEmbedFieldModerator = logEmbed.fields[1];
|
|
||||||
|
|
||||||
expect(logEmbedFieldModerator.name).toBe('Moderator');
|
|
||||||
expect(logEmbedFieldModerator.value).toBe('[object Object] `AUTHORTAG`');
|
|
||||||
|
|
||||||
// Log Embed -> Reason Field
|
|
||||||
const logEmbedFieldReason = logEmbed.fields[2];
|
|
||||||
|
|
||||||
expect(logEmbedFieldReason.name).toBe('Reason');
|
|
||||||
expect(logEmbedFieldReason.value).toBe('Test Reason');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given moderator did not supply a reason, expect default reason to be added', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
};
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
kickable: true,
|
|
||||||
kick: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(user);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(member);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: "kick",
|
|
||||||
args: ["USER"],
|
|
||||||
message: message
|
|
||||||
}
|
|
||||||
|
|
||||||
const kick = new Kick();
|
|
||||||
|
|
||||||
const result = await kick.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(logChannel.send).toBeCalledTimes(1);
|
|
||||||
expect(member.kick).toBeCalledWith('Moderator: AUTHORTAG, Reason: *none*');
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(2);
|
|
||||||
|
|
||||||
// Log Embed
|
|
||||||
const logEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(logEmbed.title).toBe('Member Kicked');
|
|
||||||
expect(logEmbed.fields.length).toBe(3);
|
|
||||||
|
|
||||||
// Log Embed -> User Field
|
|
||||||
const logEmbedFieldUser = logEmbed.fields[0];
|
|
||||||
|
|
||||||
expect(logEmbedFieldUser.name).toBe('User');
|
|
||||||
expect(logEmbedFieldUser.value).toBe('[object Object] `USERTAG`');
|
|
||||||
expect(logEmbedFieldUser.inline).toBeTruthy();
|
|
||||||
|
|
||||||
// Log Embed -> Moderator Field
|
|
||||||
const logEmbedFieldModerator = logEmbed.fields[1];
|
|
||||||
|
|
||||||
expect(logEmbedFieldModerator.name).toBe('Moderator');
|
|
||||||
expect(logEmbedFieldModerator.value).toBe('[object Object] `AUTHORTAG`');
|
|
||||||
|
|
||||||
// Log Embed -> Reason Field
|
|
||||||
const logEmbedFieldReason = logEmbed.fields[2];
|
|
||||||
|
|
||||||
expect(logEmbedFieldReason.name).toBe('Reason');
|
|
||||||
expect(logEmbedFieldReason.value).toBe('*none*');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given target user is not found, expect user does not exist error', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
kickable: true,
|
|
||||||
kick: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(null);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(member);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: "kick",
|
|
||||||
args: ["USER", "Test", "Reason"],
|
|
||||||
message: message
|
|
||||||
}
|
|
||||||
|
|
||||||
const kick = new Kick();
|
|
||||||
|
|
||||||
const result = await kick.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(logChannel.send).not.toBeCalled();
|
|
||||||
expect(member.kick).not.toBeCalled();
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Embed
|
|
||||||
const embed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embed.title).toBe(null);
|
|
||||||
expect(embed.description).toBe('User does not exist');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given target member is not found, expect user is not in this server error', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
kickable: true,
|
|
||||||
kick: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(user);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(null);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: "kick",
|
|
||||||
args: ["USER", "Test", "Reason"],
|
|
||||||
message: message
|
|
||||||
}
|
|
||||||
|
|
||||||
const kick = new Kick();
|
|
||||||
|
|
||||||
const result = await kick.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(logChannel.send).not.toBeCalled();
|
|
||||||
expect(member.kick).not.toBeCalled();
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Embed
|
|
||||||
const embed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embed.title).toBe(null);
|
|
||||||
expect(embed.description).toBe('User is not in this server');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given guild is not available, expect to stop', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
kickable: true,
|
|
||||||
kick: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(user);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(member);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: false
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: "kick",
|
|
||||||
args: ["USER", "Test", "Reason"],
|
|
||||||
message: message
|
|
||||||
}
|
|
||||||
|
|
||||||
const kick = new Kick();
|
|
||||||
|
|
||||||
const result = await kick.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).not.toBeCalled();
|
|
||||||
expect(logChannel.send).not.toBeCalled();
|
|
||||||
expect(member.kick).not.toBeCalled();
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given client can not kick member, expect error', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
kickable: false,
|
|
||||||
kick: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(user);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(member);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: "kick",
|
|
||||||
args: ["USER", "Test", "Reason"],
|
|
||||||
message: message
|
|
||||||
}
|
|
||||||
|
|
||||||
const kick = new Kick();
|
|
||||||
|
|
||||||
const result = await kick.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(logChannel.send).not.toBeCalled();
|
|
||||||
expect(member.kick).not.toBeCalled();
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Embed
|
|
||||||
const embed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embed.title).toBe(null);
|
|
||||||
expect(embed.description).toBe('Unable to do this action, am I missing permissions?');
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,815 +0,0 @@
|
||||||
import { mock } from "jest-mock-extended";
|
|
||||||
|
|
||||||
import { GuildMember, Message, Role, TextChannel, User } from "discord.js";
|
|
||||||
import Mute from "../../src/commands/mute";
|
|
||||||
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
process.env = {};
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Constructor', () => {
|
|
||||||
test('Expect properties to be set', () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: 'Moderator'
|
|
||||||
};
|
|
||||||
|
|
||||||
const mute = new Mute();
|
|
||||||
|
|
||||||
expect(mute._category).toBe("Moderation");
|
|
||||||
expect(mute._roles.length).toBe(1);
|
|
||||||
expect(mute._roles[0]).toBe('Moderator');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Execute', () => {
|
|
||||||
test('Given user has permission, expect user to be given muted role', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
||||||
ROLES_MUTED: 'Muted'
|
|
||||||
};
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const messageAuthor = {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
manageable: true,
|
|
||||||
roles: {
|
|
||||||
add: jest.fn()
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const role = {
|
|
||||||
name: 'Muted'
|
|
||||||
} as unknown as Role;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(user);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(member);
|
|
||||||
const messageGuildRolesCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): Role | undefined => {
|
|
||||||
const result = callback(role);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return role;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
available: true,
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildRolesCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
author: messageAuthor
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'mute',
|
|
||||||
args: ['USER', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const mute = new Mute();
|
|
||||||
|
|
||||||
const result = await mute.execute(context);
|
|
||||||
|
|
||||||
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildMember).toBeCalledWith(user);
|
|
||||||
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildChannelsCacheFind).toBeCalledTimes(1);
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(member.roles.add).toBeCalledWith(role, 'Moderator: AUTHORTAG, Reason: Test Reason');
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(2);
|
|
||||||
|
|
||||||
// Log Embed
|
|
||||||
const logEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(logEmbed.title).toBe('Member Muted');
|
|
||||||
expect(logEmbed.fields.length).toBe(3);
|
|
||||||
|
|
||||||
// Log Embed -> User Field
|
|
||||||
const logEmbedUserField = logEmbed.fields[0];
|
|
||||||
|
|
||||||
expect(logEmbedUserField.name).toBe('User');
|
|
||||||
expect(logEmbedUserField.value).toBe('[object Object] `USERTAG`');
|
|
||||||
expect(logEmbedUserField.inline).toBeTruthy();
|
|
||||||
|
|
||||||
// Log Embed -> Moderator Field
|
|
||||||
const logEmbedModeratorField = logEmbed.fields[1];
|
|
||||||
|
|
||||||
expect(logEmbedModeratorField.name).toBe('Moderator');
|
|
||||||
expect(logEmbedModeratorField.value).toBe('[object Object] `AUTHORTAG`');
|
|
||||||
|
|
||||||
// Log Embed -> Reason Field
|
|
||||||
const logEmbedFieldReason = logEmbed.fields[2];
|
|
||||||
|
|
||||||
expect(logEmbedFieldReason.name).toBe('Reason');
|
|
||||||
expect(logEmbedFieldReason.value).toBe('Test Reason');
|
|
||||||
|
|
||||||
// Public Embed
|
|
||||||
const publicEmbed = result.embeds[1];
|
|
||||||
|
|
||||||
expect(publicEmbed.title).toBe('');
|
|
||||||
expect(publicEmbed.description).toBe('[object Object] has been muted');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given moderator did not supply a reason, expect default reason used', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
||||||
ROLES_MUTED: 'Muted'
|
|
||||||
};
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const messageAuthor = {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
manageable: true,
|
|
||||||
roles: {
|
|
||||||
add: jest.fn()
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const role = {
|
|
||||||
name: 'Muted'
|
|
||||||
} as unknown as Role;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(user);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(member);
|
|
||||||
const messageGuildRolesCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): Role | undefined => {
|
|
||||||
const result = callback(role);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return role;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
available: true,
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildRolesCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
author: messageAuthor
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'mute',
|
|
||||||
args: ['USER'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const mute = new Mute();
|
|
||||||
|
|
||||||
const result = await mute.execute(context);
|
|
||||||
|
|
||||||
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildMember).toBeCalledWith(user);
|
|
||||||
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildChannelsCacheFind).toBeCalledTimes(1);
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(member.roles.add).toBeCalledWith(role, 'Moderator: AUTHORTAG, Reason: *none*');
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(2);
|
|
||||||
|
|
||||||
// Log Embed
|
|
||||||
const logEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(logEmbed.title).toBe('Member Muted');
|
|
||||||
expect(logEmbed.fields.length).toBe(3);
|
|
||||||
|
|
||||||
// Log Embed -> User Field
|
|
||||||
const logEmbedUserField = logEmbed.fields[0];
|
|
||||||
|
|
||||||
expect(logEmbedUserField.name).toBe('User');
|
|
||||||
expect(logEmbedUserField.value).toBe('[object Object] `USERTAG`');
|
|
||||||
expect(logEmbedUserField.inline).toBeTruthy();
|
|
||||||
|
|
||||||
// Log Embed -> Moderator Field
|
|
||||||
const logEmbedModeratorField = logEmbed.fields[1];
|
|
||||||
|
|
||||||
expect(logEmbedModeratorField.name).toBe('Moderator');
|
|
||||||
expect(logEmbedModeratorField.value).toBe('[object Object] `AUTHORTAG`');
|
|
||||||
|
|
||||||
// Log Embed -> Reason Field
|
|
||||||
const logEmbedFieldReason = logEmbed.fields[2];
|
|
||||||
|
|
||||||
expect(logEmbedFieldReason.name).toBe('Reason');
|
|
||||||
expect(logEmbedFieldReason.value).toBe('*none*');
|
|
||||||
|
|
||||||
// Public Embed
|
|
||||||
const publicEmbed = result.embeds[1];
|
|
||||||
|
|
||||||
expect(publicEmbed.title).toBe('');
|
|
||||||
expect(publicEmbed.description).toBe('[object Object] has been muted');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given user did not mention a user, expect user not to exist', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
||||||
ROLES_MUTED: 'Muted'
|
|
||||||
};
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const messageAuthor = {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
manageable: true,
|
|
||||||
roles: {
|
|
||||||
add: jest.fn()
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const role = {
|
|
||||||
name: 'Muted'
|
|
||||||
} as unknown as Role;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(null);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(member);
|
|
||||||
const messageGuildRolesCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): Role | undefined => {
|
|
||||||
const result = callback(role);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return role;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
available: true,
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildRolesCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
author: messageAuthor
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'mute',
|
|
||||||
args: ['USER', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const mute = new Mute();
|
|
||||||
|
|
||||||
const result = await mute.execute(context);
|
|
||||||
|
|
||||||
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildMember).not.toBeCalled();
|
|
||||||
expect(messageGuildRolesCacheFind).not.toBeCalled();
|
|
||||||
expect(messageGuildChannelsCacheFind).not.toBeCalled();
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Error Embed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.description).toBe('User does not exist');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given member can not be found from user, expect user to not be in server', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
||||||
ROLES_MUTED: 'Muted'
|
|
||||||
};
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const messageAuthor = {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
manageable: true,
|
|
||||||
roles: {
|
|
||||||
add: jest.fn()
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const role = {
|
|
||||||
name: 'Muted'
|
|
||||||
} as unknown as Role;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(user);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(null);
|
|
||||||
const messageGuildRolesCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): Role | undefined => {
|
|
||||||
const result = callback(role);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return role;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
available: true,
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildRolesCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
author: messageAuthor
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'mute',
|
|
||||||
args: ['USER', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const mute = new Mute();
|
|
||||||
|
|
||||||
const result = await mute.execute(context);
|
|
||||||
|
|
||||||
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildMember).toBeCalledWith(user);
|
|
||||||
expect(messageGuildRolesCacheFind).not.toBeCalled();
|
|
||||||
expect(messageGuildChannelsCacheFind).not.toBeCalled();
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Error Embed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.description).toBe('User is not in this server');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given guild is unavailable, expect execution to stop', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
||||||
ROLES_MUTED: 'Muted'
|
|
||||||
};
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const messageAuthor = {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
manageable: true,
|
|
||||||
roles: {
|
|
||||||
add: jest.fn()
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const role = {
|
|
||||||
name: 'Muted'
|
|
||||||
} as unknown as Role;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(user);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(member);
|
|
||||||
const messageGuildRolesCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): Role | undefined => {
|
|
||||||
const result = callback(role);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return role;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
available: false,
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildRolesCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
author: messageAuthor
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'mute',
|
|
||||||
args: ['USER', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const mute = new Mute();
|
|
||||||
|
|
||||||
const result = await mute.execute(context);
|
|
||||||
|
|
||||||
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildMember).toBeCalledWith(user);
|
|
||||||
expect(messageGuildRolesCacheFind).not.toBeCalled();
|
|
||||||
expect(messageGuildChannelsCacheFind).not.toBeCalled();
|
|
||||||
expect(messageChannelSend).not.toBeCalled();
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given client can not manage user, expect insufficient permissions', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
||||||
ROLES_MUTED: 'Muted'
|
|
||||||
};
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const messageAuthor = {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
manageable: false,
|
|
||||||
roles: {
|
|
||||||
add: jest.fn()
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const role = {
|
|
||||||
name: 'Muted'
|
|
||||||
} as unknown as Role;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(user);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(member);
|
|
||||||
const messageGuildRolesCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): Role | undefined => {
|
|
||||||
const result = callback(role);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return role;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
available: true,
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildRolesCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
author: messageAuthor
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'mute',
|
|
||||||
args: ['USER', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const mute = new Mute();
|
|
||||||
|
|
||||||
const result = await mute.execute(context);
|
|
||||||
|
|
||||||
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildMember).toBeCalledWith(user);
|
|
||||||
expect(messageGuildRolesCacheFind).not.toBeCalled();
|
|
||||||
expect(messageGuildChannelsCacheFind).not.toBeCalled();
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Error Embed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.description).toBe('Unable to do this action, am I missing permissions?');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given muted role can not be found, expect role not found', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
||||||
ROLES_MUTED: 'Muted'
|
|
||||||
};
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const messageAuthor = {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
manageable: true,
|
|
||||||
roles: {
|
|
||||||
add: jest.fn()
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const role = {
|
|
||||||
name: 'Muted'
|
|
||||||
} as unknown as Role;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(user);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(member);
|
|
||||||
const messageGuildRolesCacheFind = jest.fn()
|
|
||||||
.mockReturnValue(undefined);
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
available: true,
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildRolesCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
author: messageAuthor
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'mute',
|
|
||||||
args: ['USER', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const mute = new Mute();
|
|
||||||
|
|
||||||
const result = await mute.execute(context);
|
|
||||||
|
|
||||||
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildMember).toBeCalledWith(user);
|
|
||||||
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildChannelsCacheFind).not.toBeCalled();
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Error Embed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.description).toBe('Unable to find role');
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,262 +0,0 @@
|
||||||
import { Message, MessageEmbed } from "discord.js";
|
|
||||||
import Poll from "../../src/commands/poll";
|
|
||||||
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
|
||||||
|
|
||||||
describe('Constructor', () => {
|
|
||||||
test('Expect properties to be set', () => {
|
|
||||||
const poll = new Poll();
|
|
||||||
|
|
||||||
expect(poll._category).toBe('General');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Execute', () => {
|
|
||||||
test('Given input is valid, expect poll to be generated', async () => {
|
|
||||||
const returnMessageReact = jest.fn();
|
|
||||||
|
|
||||||
const returnMessage = {
|
|
||||||
react: returnMessageReact
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn()
|
|
||||||
.mockReturnValue(returnMessage);
|
|
||||||
const messageDelete = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
delete: messageDelete,
|
|
||||||
deletable: true
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'poll',
|
|
||||||
args: ['Test', 'title;', 'one;', 'two'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const poll = new Poll();
|
|
||||||
|
|
||||||
const result = await poll.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(messageDelete).toBeCalledTimes(1);
|
|
||||||
expect(returnMessageReact).toBeCalledTimes(2);
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Embed
|
|
||||||
const embed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embed.title).toBe('Test title');
|
|
||||||
expect(embed.description).toBe(':one: one\n:two: two');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given message is not deletable by client, expect it not to attempt deletion', async () => {
|
|
||||||
const returnMessageReact = jest.fn();
|
|
||||||
|
|
||||||
const returnMessage = {
|
|
||||||
react: returnMessageReact
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn()
|
|
||||||
.mockReturnValue(returnMessage);
|
|
||||||
const messageDelete = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
delete: messageDelete,
|
|
||||||
deletable: false
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'poll',
|
|
||||||
args: ['Test', 'title;', 'one;', 'two'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const poll = new Poll();
|
|
||||||
|
|
||||||
const result = await poll.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(messageDelete).not.toBeCalled();
|
|
||||||
expect(returnMessageReact).toBeCalledTimes(2);
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Embed
|
|
||||||
const embed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embed.title).toBe('Test title');
|
|
||||||
expect(embed.description).toBe(':one: one\n:two: two');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given no arguments, expect error embed', async () => {
|
|
||||||
const returnMessageReact = jest.fn();
|
|
||||||
|
|
||||||
const returnMessage = {
|
|
||||||
react: returnMessageReact
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn()
|
|
||||||
.mockReturnValue(returnMessage);
|
|
||||||
const messageDelete = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
delete: messageDelete,
|
|
||||||
deletable: true
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'poll',
|
|
||||||
args: [],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const poll = new Poll();
|
|
||||||
|
|
||||||
const result = await poll.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(messageDelete).not.toBeCalled();
|
|
||||||
expect(returnMessageReact).not.toBeCalled();
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Error Embed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.description).toBe('Usage: <title>;<option 1>;<option 2>... (separate options with semicolons), maximum of 9 options');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given only 1 option, expect error embed', async () => {
|
|
||||||
const returnMessageReact = jest.fn();
|
|
||||||
|
|
||||||
const returnMessage = {
|
|
||||||
react: returnMessageReact
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn()
|
|
||||||
.mockReturnValue(returnMessage);
|
|
||||||
const messageDelete = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
delete: messageDelete,
|
|
||||||
deletable: true
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'poll',
|
|
||||||
args: ['Test', 'title;', 'one'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const poll = new Poll();
|
|
||||||
|
|
||||||
const result = await poll.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(messageDelete).not.toBeCalled();
|
|
||||||
expect(returnMessageReact).not.toBeCalled();
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Error Embed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.description).toBe('Usage: <title>;<option 1>;<option 2>... (separate options with semicolons), maximum of 9 options');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given 9 options, expect poll to be generated', async () => {
|
|
||||||
const returnMessageReact = jest.fn();
|
|
||||||
|
|
||||||
const returnMessage = {
|
|
||||||
react: returnMessageReact
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn()
|
|
||||||
.mockReturnValue(returnMessage);
|
|
||||||
const messageDelete = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
delete: messageDelete,
|
|
||||||
deletable: true
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'poll',
|
|
||||||
args: ['Test', 'title;', 'one;', 'two;', 'three;', 'four;', 'five;', 'six;', 'seven;', 'eight;', 'nine'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const poll = new Poll();
|
|
||||||
|
|
||||||
const result = await poll.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(messageDelete).toBeCalledTimes(1);
|
|
||||||
expect(returnMessageReact).toBeCalledTimes(9);
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Embed
|
|
||||||
const embed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embed.title).toBe('Test title');
|
|
||||||
expect(embed.description).toBe(':one: one\n:two: two\n:three: three\n:four: four\n:five: five\n:six: six\n:seven: seven\n:eight: eight\n:nine: nine');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given 10 options, expect error embed', async () => {
|
|
||||||
const returnMessageReact = jest.fn();
|
|
||||||
|
|
||||||
const returnMessage = {
|
|
||||||
react: returnMessageReact
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn()
|
|
||||||
.mockReturnValue(returnMessage);
|
|
||||||
const messageDelete = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
delete: messageDelete,
|
|
||||||
deletable: true
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'poll',
|
|
||||||
args: ['Test', 'title;', 'one;', 'two;', 'three;', 'four;', 'five;', 'six;', 'seven;', 'eight;', 'nine;', 'ten'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const poll = new Poll();
|
|
||||||
|
|
||||||
const result = await poll.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(messageDelete).not.toBeCalled();
|
|
||||||
expect(returnMessageReact).not.toBeCalled();
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Error Embed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.description).toBe('Usage: <title>;<option 1>;<option 2>... (separate options with semicolons), maximum of 9 options');
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,411 +0,0 @@
|
||||||
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');
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,106 +0,0 @@
|
||||||
import { Message } from "discord.js";
|
|
||||||
import Rules from "../../src/commands/rules";
|
|
||||||
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
|
||||||
|
|
||||||
const oldCwd = process.cwd();
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
process.env = {};
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Constructor', () => {
|
|
||||||
test('Expect properties to be set', () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: "Moderator"
|
|
||||||
};
|
|
||||||
|
|
||||||
const rules = new Rules();
|
|
||||||
|
|
||||||
expect(rules._category).toBe("Admin");
|
|
||||||
expect(rules._roles.length).toBe(1);
|
|
||||||
expect(rules._roles[0]).toBe("Moderator");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Execute', () => {
|
|
||||||
test('Given rules exist, expect rules to be sent to current channel', () => {
|
|
||||||
process.env = {
|
|
||||||
COMMANDS_RULES_FILE: 'rules/rules.json'
|
|
||||||
};
|
|
||||||
|
|
||||||
process.cwd = jest.fn()
|
|
||||||
.mockReturnValue(`${oldCwd}/tests/_mocks`);
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'rules',
|
|
||||||
args: [],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const rules = new Rules();
|
|
||||||
|
|
||||||
const result = rules.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(2);
|
|
||||||
expect(result.embeds.length).toBe(2);
|
|
||||||
|
|
||||||
// Header Embed
|
|
||||||
const embedHeader = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embedHeader.title).toBe("");
|
|
||||||
expect(embedHeader.description).toBe("");
|
|
||||||
expect(embedHeader.image?.url).toBe("IMAGEURL");
|
|
||||||
expect(embedHeader.footer?.text).toBe("");
|
|
||||||
|
|
||||||
// Main Embed
|
|
||||||
const embedMain = result.embeds[1];
|
|
||||||
|
|
||||||
expect(embedMain.title).toBe("TITLE 1");
|
|
||||||
expect(embedMain.description).toBe("DESCRIPTION 1A\nDESCRIPTION 1B");
|
|
||||||
expect(embedMain.image?.url).toBe("");
|
|
||||||
expect(embedMain.footer?.text).toBe("FOOTER 1");
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given rules file does not exist, expect does not exist error', () => {
|
|
||||||
process.env = {
|
|
||||||
COMMANDS_RULES_FILE: 'rules/none.json'
|
|
||||||
};
|
|
||||||
|
|
||||||
process.cwd = jest.fn()
|
|
||||||
.mockReturnValue(`${oldCwd}/tests/_mocks`);
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'rules',
|
|
||||||
args: [],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const rules = new Rules();
|
|
||||||
|
|
||||||
const result = rules.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Error Embed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.description).toBe("Rules file doesn't exist");
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,766 +0,0 @@
|
||||||
import { APIEmbed, CacheType, CommandInteraction, CommandInteractionOption, DMChannel, Embed, EmbedBuilder, EmbedField, Guild, GuildChannel, GuildMember, InteractionReplyOptions, JSONEncodable, Message, MessageCreateOptions, MessagePayload, SlashCommandBuilder, TextChannel, User } from "discord.js";
|
|
||||||
import { mock } from "jest-mock-extended";
|
|
||||||
import Timeout from "../../src/commands/timeout";
|
|
||||||
import SettingsHelper from "../../src/helpers/SettingsHelper";
|
|
||||||
import Audit from "../../src/database/entities/Audit";
|
|
||||||
import EmbedColours from "../../src/constants/EmbedColours";
|
|
||||||
import { DeepPartial, EntityTarget } from "typeorm";
|
|
||||||
import BaseEntity from "../../src/contracts/BaseEntity";
|
|
||||||
import { AuditType } from "../../src/constants/AuditType";
|
|
||||||
|
|
||||||
describe('Constructor', () => {
|
|
||||||
test('EXPECT CommandBuilder to be configured', () => {
|
|
||||||
const command = new Timeout();
|
|
||||||
|
|
||||||
expect(command.CommandBuilder).toBeDefined();
|
|
||||||
|
|
||||||
const commandBuilder = command.CommandBuilder as SlashCommandBuilder;
|
|
||||||
|
|
||||||
expect(commandBuilder.name).toBe("timeout");
|
|
||||||
expect(commandBuilder.description).toBe("Timeouts a user out, sending them a DM with the reason if possible");
|
|
||||||
expect(commandBuilder.options.length).toBe(3);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('execute', () => {
|
|
||||||
// Happy flow
|
|
||||||
test('GIVEN all checks have passed, EXPECT user to be timed out', async () => {
|
|
||||||
let embeds: APIEmbed[] | undefined;
|
|
||||||
|
|
||||||
const command = new Timeout();
|
|
||||||
|
|
||||||
const interactionReply = jest.fn((options: InteractionReplyOptions) => {
|
|
||||||
embeds = options.embeds as APIEmbed[];
|
|
||||||
});
|
|
||||||
|
|
||||||
let savedAudit: DeepPartial<Audit> | undefined;
|
|
||||||
|
|
||||||
const getSetting = jest.spyOn(SettingsHelper, 'GetSetting').mockResolvedValue('mod-logs');
|
|
||||||
const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation((target: EntityTarget<BaseEntity>, entity: DeepPartial<BaseEntity>): Promise<void> => {
|
|
||||||
savedAudit = entity;
|
|
||||||
|
|
||||||
return Promise.resolve();
|
|
||||||
});
|
|
||||||
|
|
||||||
const timeoutFunc = jest.fn();
|
|
||||||
|
|
||||||
let dmChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
|
||||||
let logsChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
|
||||||
|
|
||||||
const dmChannel = {
|
|
||||||
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
|
|
||||||
dmChannelSentEmbeds = options.embeds;
|
|
||||||
}),
|
|
||||||
} as unknown as DMChannel;
|
|
||||||
|
|
||||||
const userInput = {
|
|
||||||
user: {
|
|
||||||
id: 'userId',
|
|
||||||
tag: 'userTag',
|
|
||||||
createDM: jest.fn().mockResolvedValue(dmChannel),
|
|
||||||
} as unknown as User,
|
|
||||||
member: {
|
|
||||||
manageable: true,
|
|
||||||
timeout: timeoutFunc,
|
|
||||||
} as unknown as GuildMember,
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
|
|
||||||
const lengthInput = {
|
|
||||||
value: '1s',
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
|
|
||||||
const reasonInput = {
|
|
||||||
value: 'Test reason',
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
|
|
||||||
const logsChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
|
|
||||||
logsChannelSentEmbeds = options.embeds;
|
|
||||||
}),
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guild: {
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn()
|
|
||||||
.mockReturnValue(logsChannel),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
name: "Test Guild",
|
|
||||||
} as unknown as Guild,
|
|
||||||
guildId: 'guildId',
|
|
||||||
reply: interactionReply,
|
|
||||||
options: {
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce(userInput)
|
|
||||||
.mockReturnValueOnce(lengthInput)
|
|
||||||
.mockReturnValue(reasonInput),
|
|
||||||
},
|
|
||||||
user: {
|
|
||||||
id: 'moderatorId'
|
|
||||||
}
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// EXPECT user to be timed out
|
|
||||||
expect(timeoutFunc).toBeCalledWith(1000, 'Test reason');
|
|
||||||
|
|
||||||
// EXPECT embeds to be sent
|
|
||||||
expect(embeds).toBeDefined();
|
|
||||||
expect(embeds!.length).toBe(1);
|
|
||||||
|
|
||||||
// EXPECT resultEmbed to be correctly configured
|
|
||||||
const resultEmbed = embeds![0] as EmbedBuilder;
|
|
||||||
|
|
||||||
expect(resultEmbed.data.description).toBe('<@userId> has been timed out');
|
|
||||||
expect(resultEmbed.data.fields).toBeDefined();
|
|
||||||
expect(resultEmbed.data.fields!.length).toBe(1);
|
|
||||||
|
|
||||||
// EXPECT DM field to be configured
|
|
||||||
const resultEmbedDMField = resultEmbed.data.fields![0];
|
|
||||||
|
|
||||||
expect(resultEmbedDMField.name).toBe("DM Sent");
|
|
||||||
expect(resultEmbedDMField.value).toBe("true");
|
|
||||||
|
|
||||||
// EXPECT user to be DM's with embed
|
|
||||||
expect(dmChannel.send).toBeCalled();
|
|
||||||
expect(dmChannelSentEmbeds).toBeDefined();
|
|
||||||
expect(dmChannelSentEmbeds?.length).toBe(1);
|
|
||||||
|
|
||||||
const dmChannelSentEmbed = (dmChannelSentEmbeds![0] as any).data;
|
|
||||||
|
|
||||||
expect(dmChannelSentEmbed.color).toBe(EmbedColours.Ok);
|
|
||||||
expect(dmChannelSentEmbed.description).toBe("You have been timed out in Test Guild");
|
|
||||||
expect(dmChannelSentEmbed.fields?.length).toBe(3);
|
|
||||||
|
|
||||||
expect(dmChannelSentEmbed.fields![0].name).toBe("Reason");
|
|
||||||
expect(dmChannelSentEmbed.fields![0].value).toBe("Test reason");
|
|
||||||
|
|
||||||
expect(dmChannelSentEmbed.fields![1].name).toBe("Length");
|
|
||||||
expect(dmChannelSentEmbed.fields![1].value).toBe("1s");
|
|
||||||
|
|
||||||
expect(dmChannelSentEmbed.fields![2].name).toBe("Until");
|
|
||||||
expect(dmChannelSentEmbed.fields![2].value).toBeDefined();
|
|
||||||
|
|
||||||
// EXPECT log embed to be sent
|
|
||||||
expect(logsChannel.send).toBeCalled();
|
|
||||||
expect(logsChannelSentEmbeds).toBeDefined();
|
|
||||||
expect(logsChannelSentEmbeds?.length).toBe(1);
|
|
||||||
|
|
||||||
const logsChannelSentEmbed = (logsChannelSentEmbeds![0] as any).data;
|
|
||||||
|
|
||||||
expect(logsChannelSentEmbed.color).toBe(EmbedColours.Ok);
|
|
||||||
expect(logsChannelSentEmbed.title).toBe("Member Timed Out");
|
|
||||||
expect(logsChannelSentEmbed.description).toBe("<@userId> `userTag`");
|
|
||||||
expect(logsChannelSentEmbed.fields?.length).toBe(4);
|
|
||||||
|
|
||||||
expect(logsChannelSentEmbed.fields![0].name).toBe("Moderator");
|
|
||||||
expect(logsChannelSentEmbed.fields![0].value).toBe("<@moderatorId>");
|
|
||||||
|
|
||||||
expect(logsChannelSentEmbed.fields![1].name).toBe("Reason");
|
|
||||||
expect(logsChannelSentEmbed.fields![1].value).toBe("Test reason");
|
|
||||||
|
|
||||||
expect(logsChannelSentEmbed.fields![2].name).toBe("Length");
|
|
||||||
expect(logsChannelSentEmbed.fields![2].value).toBe("1s");
|
|
||||||
|
|
||||||
expect(logsChannelSentEmbed.fields![3].name).toBe("Until");
|
|
||||||
expect(logsChannelSentEmbed.fields![3].value).toBeDefined();
|
|
||||||
|
|
||||||
// EXPECT Audit to be saved
|
|
||||||
expect(auditSave).toBeCalled();
|
|
||||||
|
|
||||||
expect(savedAudit).toBeDefined();
|
|
||||||
expect(savedAudit?.UserId).toBe('userId');
|
|
||||||
expect(savedAudit?.AuditType).toBe(AuditType.Timeout);
|
|
||||||
expect(savedAudit?.Reason).toBe("Test reason");
|
|
||||||
expect(savedAudit?.ModeratorId).toBe('moderatorId');
|
|
||||||
expect(savedAudit?.ServerId).toBe('guildId');
|
|
||||||
});
|
|
||||||
|
|
||||||
// Null checks
|
|
||||||
test('GIVEN interaction.guild IS NULL, EXPECT nothing to happen', async () => {
|
|
||||||
const command = new Timeout();
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guild: null,
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).not.toBeCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('GIVEN interaction.guildId IS NULL, EXPECT nothing to happen', async () => {
|
|
||||||
const command = new Timeout();
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guild: mock<Guild>(),
|
|
||||||
guildId: null,
|
|
||||||
reply: jest.fn(),
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).not.toBeCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Validation
|
|
||||||
test('GIVEN targetUser IS NULL, EXPECT validation error', async () => {
|
|
||||||
const command = new Timeout();
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
guild: mock<Guild>(),
|
|
||||||
guildId: 'guildId',
|
|
||||||
options: {
|
|
||||||
get: jest.fn().mockReturnValue(undefined),
|
|
||||||
}
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toBeCalledWith('Fields are required.');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('GIVEN targetUser.user IS NULL, EXPECT validation error', async () => {
|
|
||||||
const command = new Timeout();
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
guild: mock<Guild>(),
|
|
||||||
guildId: 'guildId',
|
|
||||||
options: {
|
|
||||||
get: jest.fn((value: string): CommandInteractionOption<CacheType> | null => {
|
|
||||||
switch (value) {
|
|
||||||
case 'target':
|
|
||||||
return {} as CommandInteractionOption;
|
|
||||||
case 'length':
|
|
||||||
return {
|
|
||||||
value: '1m',
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
case 'reason':
|
|
||||||
return {
|
|
||||||
value: 'Test reason',
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toBeCalledWith('Fields are required.');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('GIVEN targetUser.member IS NULL, EXPECT validation error', async () => {
|
|
||||||
const command = new Timeout();
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
guild: mock<Guild>(),
|
|
||||||
guildId: 'guildId',
|
|
||||||
options: {
|
|
||||||
get: jest.fn((value: string): CommandInteractionOption<CacheType> | null => {
|
|
||||||
switch (value) {
|
|
||||||
case 'target':
|
|
||||||
return {
|
|
||||||
user: {} as User,
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
case 'length':
|
|
||||||
return {
|
|
||||||
value: '1m',
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
case 'reason':
|
|
||||||
return {
|
|
||||||
value: 'Test reason',
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toBeCalledWith('Fields are required.');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('GIVEN lengthInput IS NULL, EXPECT validation error', async () => {
|
|
||||||
const command = new Timeout();
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
guild: mock<Guild>(),
|
|
||||||
guildId: 'guildId',
|
|
||||||
options: {
|
|
||||||
get: jest.fn((value: string): CommandInteractionOption<CacheType> | null => {
|
|
||||||
switch (value) {
|
|
||||||
case 'target':
|
|
||||||
return {
|
|
||||||
user: {} as User,
|
|
||||||
member: {} as GuildMember
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
case 'length':
|
|
||||||
return null;
|
|
||||||
case 'reason':
|
|
||||||
return {
|
|
||||||
value: 'Test reason',
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toBeCalledWith('Fields are required.');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('GIVEN lengthInput.value IS NULL, EXPECT validation error', async () => {
|
|
||||||
const command = new Timeout();
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
guild: mock<Guild>(),
|
|
||||||
guildId: 'guildId',
|
|
||||||
options: {
|
|
||||||
get: jest.fn((value: string): CommandInteractionOption<CacheType> | null => {
|
|
||||||
switch (value) {
|
|
||||||
case 'target':
|
|
||||||
return {
|
|
||||||
user: {} as User,
|
|
||||||
member: {} as GuildMember
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
case 'length':
|
|
||||||
return {
|
|
||||||
value: undefined,
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
case 'reason':
|
|
||||||
return {
|
|
||||||
value: 'Test reason',
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toBeCalledWith('Fields are required.');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('GIVEN targetUser is a bot, EXPECT error', async () => {
|
|
||||||
const interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
guild: mock<Guild>(),
|
|
||||||
guildId: 'guildId',
|
|
||||||
options: {
|
|
||||||
get: jest.fn((value: string): CommandInteractionOption<CacheType> | null => {
|
|
||||||
switch (value) {
|
|
||||||
case 'target':
|
|
||||||
return {
|
|
||||||
user: {
|
|
||||||
bot: true,
|
|
||||||
} as User,
|
|
||||||
member: {} as GuildMember
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
case 'length':
|
|
||||||
return {
|
|
||||||
value: '1m',
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
case 'reason':
|
|
||||||
return {
|
|
||||||
value: 'Test reason',
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
const command = new Timeout();
|
|
||||||
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toBeCalledWith('Cannot timeout bots.');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('GIVEN targetMember IS NOT manageable by the bot, EXPECT insufficient permissions error', async () => {
|
|
||||||
const command = new Timeout();
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
guild: mock<Guild>(),
|
|
||||||
guildId: 'guildId',
|
|
||||||
user: {
|
|
||||||
id: 'moderatorId',
|
|
||||||
},
|
|
||||||
options: {
|
|
||||||
get: jest.fn((value: string): CommandInteractionOption<CacheType> | null => {
|
|
||||||
switch (value) {
|
|
||||||
case 'target':
|
|
||||||
return {
|
|
||||||
user: {
|
|
||||||
id: 'userId',
|
|
||||||
tag: 'userTag',
|
|
||||||
} as User,
|
|
||||||
member: {
|
|
||||||
manageable: false,
|
|
||||||
} as GuildMember
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
case 'length':
|
|
||||||
return {
|
|
||||||
value: '1m',
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
case 'reason':
|
|
||||||
return {
|
|
||||||
value: 'Test reason',
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(interaction.reply).toBeCalledWith('Insufficient bot permissions. Please contact a moderator.');
|
|
||||||
});
|
|
||||||
|
|
||||||
// Reason variable
|
|
||||||
test('GIVEN reason IS NULL, EXPECT to be ran with empty string', async () => {
|
|
||||||
const command = new Timeout();
|
|
||||||
|
|
||||||
let savedAudit: DeepPartial<Audit> | undefined;
|
|
||||||
|
|
||||||
const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation((target: EntityTarget<BaseEntity>, entity: DeepPartial<BaseEntity>): Promise<void> => {
|
|
||||||
savedAudit = entity;
|
|
||||||
|
|
||||||
return Promise.resolve();
|
|
||||||
});
|
|
||||||
|
|
||||||
const timeoutFunc = jest.fn();
|
|
||||||
|
|
||||||
const sentEmbeds: EmbedBuilder[] = [];
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
guild: {
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(mock<TextChannel>()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guildId: 'guildId',
|
|
||||||
user: {
|
|
||||||
id: 'moderatorId',
|
|
||||||
},
|
|
||||||
options: {
|
|
||||||
get: jest.fn((value: string): CommandInteractionOption<CacheType> | null => {
|
|
||||||
switch (value) {
|
|
||||||
case 'target':
|
|
||||||
return {
|
|
||||||
user: {
|
|
||||||
id: 'userId',
|
|
||||||
tag: 'userTag',
|
|
||||||
createDM: jest.fn().mockReturnValue({
|
|
||||||
send: jest.fn(async (options: MessageCreateOptions): Promise<Message<false>> => {
|
|
||||||
sentEmbeds.push(options.embeds![0] as EmbedBuilder);
|
|
||||||
|
|
||||||
return mock<Message<false>>();
|
|
||||||
})
|
|
||||||
}) as unknown as DMChannel,
|
|
||||||
} as unknown as User,
|
|
||||||
member: {
|
|
||||||
manageable: true,
|
|
||||||
timeout: timeoutFunc,
|
|
||||||
} as unknown as GuildMember
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
case 'length':
|
|
||||||
return {
|
|
||||||
value: '1m'
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
case 'reason':
|
|
||||||
return {
|
|
||||||
value: undefined,
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(timeoutFunc).toBeCalledWith(1000 * 60 * 1, "");
|
|
||||||
expect(savedAudit?.Reason).toBe("*none*");
|
|
||||||
|
|
||||||
const dmEmbed = (sentEmbeds[0] as any).data;
|
|
||||||
const dmEmbedReasonField = dmEmbed.fields![0] as EmbedField;
|
|
||||||
|
|
||||||
expect(dmEmbedReasonField.value).toBe("*none*");
|
|
||||||
});
|
|
||||||
|
|
||||||
// Log embed
|
|
||||||
test('GIVEN channelName IS NULL, EXPECT execution to return', async () => {
|
|
||||||
const command = new Timeout();
|
|
||||||
|
|
||||||
let savedAudit: DeepPartial<Audit> | undefined;
|
|
||||||
|
|
||||||
const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation((target: EntityTarget<BaseEntity>, entity: DeepPartial<BaseEntity>): Promise<void> => {
|
|
||||||
savedAudit = entity;
|
|
||||||
|
|
||||||
return Promise.resolve();
|
|
||||||
});
|
|
||||||
|
|
||||||
const settingsGet = jest.spyOn(SettingsHelper, 'GetSetting').mockResolvedValue(undefined);
|
|
||||||
|
|
||||||
const timeoutFunc = jest.fn();
|
|
||||||
|
|
||||||
const sentEmbeds: EmbedBuilder[] = [];
|
|
||||||
|
|
||||||
const logChannelSendFunc = jest.fn();
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
guild: {
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue({
|
|
||||||
send: logChannelSendFunc,
|
|
||||||
} as unknown as TextChannel),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guildId: 'guildId',
|
|
||||||
user: {
|
|
||||||
id: 'moderatorId',
|
|
||||||
},
|
|
||||||
options: {
|
|
||||||
get: jest.fn((value: string): CommandInteractionOption<CacheType> | null => {
|
|
||||||
switch (value) {
|
|
||||||
case 'target':
|
|
||||||
return {
|
|
||||||
user: {
|
|
||||||
id: 'userId',
|
|
||||||
tag: 'userTag',
|
|
||||||
createDM: jest.fn().mockReturnValue({
|
|
||||||
send: jest.fn(async (options: MessageCreateOptions): Promise<Message<false>> => {
|
|
||||||
sentEmbeds.push(options.embeds![0] as EmbedBuilder);
|
|
||||||
|
|
||||||
return mock<Message<false>>();
|
|
||||||
})
|
|
||||||
}) as unknown as DMChannel,
|
|
||||||
} as unknown as User,
|
|
||||||
member: {
|
|
||||||
manageable: true,
|
|
||||||
timeout: timeoutFunc,
|
|
||||||
} as unknown as GuildMember
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
case 'length':
|
|
||||||
return {
|
|
||||||
value: '1m'
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
case 'reason':
|
|
||||||
return {
|
|
||||||
value: 'Test reason',
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(timeoutFunc).toBeCalled();
|
|
||||||
expect(sentEmbeds.length).toBe(0);
|
|
||||||
expect(logChannelSendFunc).not.toBeCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('GIVEN channel IS NULL, EXPECT embed to not be sent', async () => {
|
|
||||||
const command = new Timeout();
|
|
||||||
|
|
||||||
let savedAudit: DeepPartial<Audit> | undefined;
|
|
||||||
|
|
||||||
const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation((target: EntityTarget<BaseEntity>, entity: DeepPartial<BaseEntity>): Promise<void> => {
|
|
||||||
savedAudit = entity;
|
|
||||||
|
|
||||||
return Promise.resolve();
|
|
||||||
});
|
|
||||||
|
|
||||||
const settingsGet = jest.spyOn(SettingsHelper, 'GetSetting').mockResolvedValue('mod-logs');
|
|
||||||
|
|
||||||
const timeoutFunc = jest.fn();
|
|
||||||
|
|
||||||
const sentEmbeds: EmbedBuilder[] = [];
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
reply: jest.fn(),
|
|
||||||
guild: {
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn().mockReturnValue(undefined),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guildId: 'guildId',
|
|
||||||
user: {
|
|
||||||
id: 'moderatorId',
|
|
||||||
},
|
|
||||||
options: {
|
|
||||||
get: jest.fn((value: string): CommandInteractionOption<CacheType> | null => {
|
|
||||||
switch (value) {
|
|
||||||
case 'target':
|
|
||||||
return {
|
|
||||||
user: {
|
|
||||||
id: 'userId',
|
|
||||||
tag: 'userTag',
|
|
||||||
createDM: jest.fn().mockReturnValue({
|
|
||||||
send: jest.fn(async (options: MessageCreateOptions): Promise<Message<false>> => {
|
|
||||||
sentEmbeds.push(options.embeds![0] as EmbedBuilder);
|
|
||||||
|
|
||||||
return mock<Message<false>>();
|
|
||||||
})
|
|
||||||
}) as unknown as DMChannel,
|
|
||||||
} as unknown as User,
|
|
||||||
member: {
|
|
||||||
manageable: true,
|
|
||||||
timeout: timeoutFunc,
|
|
||||||
} as unknown as GuildMember
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
case 'length':
|
|
||||||
return {
|
|
||||||
value: '1m'
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
case 'reason':
|
|
||||||
return {
|
|
||||||
value: 'Test reason',
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
expect(timeoutFunc).toBeCalled();
|
|
||||||
expect(sentEmbeds.length).toBeGreaterThan(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
// DM user
|
|
||||||
test('GIVEN user can NOT be messaged, EXPECT resultEmbed to contain "DM Sent = false"', async () => {
|
|
||||||
let embeds: APIEmbed[] | undefined;
|
|
||||||
|
|
||||||
const command = new Timeout();
|
|
||||||
|
|
||||||
const interactionReply = jest.fn((options: InteractionReplyOptions) => {
|
|
||||||
embeds = options.embeds as APIEmbed[];
|
|
||||||
});
|
|
||||||
|
|
||||||
let savedAudit: DeepPartial<Audit> | undefined;
|
|
||||||
|
|
||||||
const getSetting = jest.spyOn(SettingsHelper, 'GetSetting').mockResolvedValue('mod-logs');
|
|
||||||
const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation((target: EntityTarget<BaseEntity>, entity: DeepPartial<BaseEntity>): Promise<void> => {
|
|
||||||
savedAudit = entity;
|
|
||||||
|
|
||||||
return Promise.resolve();
|
|
||||||
});
|
|
||||||
|
|
||||||
const timeoutFunc = jest.fn();
|
|
||||||
|
|
||||||
let dmChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
|
||||||
let logsChannelSentEmbeds: (APIEmbed | JSONEncodable<APIEmbed>)[] | undefined;
|
|
||||||
|
|
||||||
const dmChannel = {
|
|
||||||
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
|
|
||||||
dmChannelSentEmbeds = options.embeds;
|
|
||||||
}),
|
|
||||||
} as unknown as DMChannel;
|
|
||||||
|
|
||||||
const userInput = {
|
|
||||||
user: {
|
|
||||||
id: 'userId',
|
|
||||||
tag: 'userTag',
|
|
||||||
createDM: jest.fn().mockRejectedValue(undefined),
|
|
||||||
} as unknown as User,
|
|
||||||
member: {
|
|
||||||
manageable: true,
|
|
||||||
timeout: timeoutFunc,
|
|
||||||
} as unknown as GuildMember,
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
|
|
||||||
const lengthInput = {
|
|
||||||
value: '1s',
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
|
|
||||||
const reasonInput = {
|
|
||||||
value: 'Test reason',
|
|
||||||
} as CommandInteractionOption;
|
|
||||||
|
|
||||||
const logsChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
|
|
||||||
logsChannelSentEmbeds = options.embeds;
|
|
||||||
}),
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const interaction = {
|
|
||||||
guild: {
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: jest.fn()
|
|
||||||
.mockReturnValue(logsChannel),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
name: "Test Guild",
|
|
||||||
} as unknown as Guild,
|
|
||||||
guildId: 'guildId',
|
|
||||||
reply: interactionReply,
|
|
||||||
options: {
|
|
||||||
get: jest.fn()
|
|
||||||
.mockReturnValueOnce(userInput)
|
|
||||||
.mockReturnValueOnce(lengthInput)
|
|
||||||
.mockReturnValue(reasonInput),
|
|
||||||
},
|
|
||||||
user: {
|
|
||||||
id: 'moderatorId'
|
|
||||||
}
|
|
||||||
} as unknown as CommandInteraction;
|
|
||||||
|
|
||||||
await command.execute(interaction);
|
|
||||||
|
|
||||||
// EXPECT embeds to be sent
|
|
||||||
expect(embeds).toBeDefined();
|
|
||||||
expect(embeds!.length).toBe(1);
|
|
||||||
|
|
||||||
const resultEmbed = embeds![0] as EmbedBuilder;
|
|
||||||
|
|
||||||
// EXPECT DM field to be configured
|
|
||||||
const resultEmbedDMField = resultEmbed.data.fields![0];
|
|
||||||
|
|
||||||
expect(resultEmbedDMField.name).toBe("DM Sent");
|
|
||||||
expect(resultEmbedDMField.value).toBe("false");
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,813 +0,0 @@
|
||||||
import { GuildMember, Message, Role, TextChannel, User } from "discord.js";
|
|
||||||
import Unmute from "../../src/commands/unmute";
|
|
||||||
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
process.env = {};
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Constructor', () => {
|
|
||||||
test('Expect properties to be set', () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: 'Moderator'
|
|
||||||
};
|
|
||||||
|
|
||||||
const mute = new Unmute();
|
|
||||||
|
|
||||||
expect(mute._category).toBe("Moderation");
|
|
||||||
expect(mute._roles.length).toBe(1);
|
|
||||||
expect(mute._roles[0]).toBe('Moderator');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Execute', () => {
|
|
||||||
test('Given user has permission, expect user to be given muted role', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
||||||
ROLES_MUTED: 'Muted'
|
|
||||||
};
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const messageAuthor = {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
manageable: true,
|
|
||||||
roles: {
|
|
||||||
remove: jest.fn()
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const role = {
|
|
||||||
name: 'Muted'
|
|
||||||
} as unknown as Role;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(user);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(member);
|
|
||||||
const messageGuildRolesCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): Role | undefined => {
|
|
||||||
const result = callback(role);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return role;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
available: true,
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildRolesCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
author: messageAuthor
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'mute',
|
|
||||||
args: ['USER', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const mute = new Unmute();
|
|
||||||
|
|
||||||
const result = await mute.execute(context);
|
|
||||||
|
|
||||||
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildMember).toBeCalledWith(user);
|
|
||||||
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildChannelsCacheFind).toBeCalledTimes(1);
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(member.roles.remove).toBeCalledWith(role, 'Moderator: AUTHORTAG, Reason: Test Reason');
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(2);
|
|
||||||
|
|
||||||
// Log Embed
|
|
||||||
const logEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(logEmbed.title).toBe('Member Unmuted');
|
|
||||||
expect(logEmbed.fields.length).toBe(3);
|
|
||||||
|
|
||||||
// Log Embed -> User Field
|
|
||||||
const logEmbedUserField = logEmbed.fields[0];
|
|
||||||
|
|
||||||
expect(logEmbedUserField.name).toBe('User');
|
|
||||||
expect(logEmbedUserField.value).toBe('[object Object] `USERTAG`');
|
|
||||||
expect(logEmbedUserField.inline).toBeTruthy();
|
|
||||||
|
|
||||||
// Log Embed -> Moderator Field
|
|
||||||
const logEmbedModeratorField = logEmbed.fields[1];
|
|
||||||
|
|
||||||
expect(logEmbedModeratorField.name).toBe('Moderator');
|
|
||||||
expect(logEmbedModeratorField.value).toBe('[object Object] `AUTHORTAG`');
|
|
||||||
|
|
||||||
// Log Embed -> Reason Field
|
|
||||||
const logEmbedFieldReason = logEmbed.fields[2];
|
|
||||||
|
|
||||||
expect(logEmbedFieldReason.name).toBe('Reason');
|
|
||||||
expect(logEmbedFieldReason.value).toBe('Test Reason');
|
|
||||||
|
|
||||||
// Public Embed
|
|
||||||
const publicEmbed = result.embeds[1];
|
|
||||||
|
|
||||||
expect(publicEmbed.title).toBe('');
|
|
||||||
expect(publicEmbed.description).toBe('[object Object] has been unmuted');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given moderator did not supply a reason, expect default reason is used', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
||||||
ROLES_MUTED: 'Muted'
|
|
||||||
};
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const messageAuthor = {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
manageable: true,
|
|
||||||
roles: {
|
|
||||||
remove: jest.fn()
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const role = {
|
|
||||||
name: 'Muted'
|
|
||||||
} as unknown as Role;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(user);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(member);
|
|
||||||
const messageGuildRolesCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): Role | undefined => {
|
|
||||||
const result = callback(role);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return role;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
available: true,
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildRolesCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
author: messageAuthor
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'mute',
|
|
||||||
args: ['USER'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const mute = new Unmute();
|
|
||||||
|
|
||||||
const result = await mute.execute(context);
|
|
||||||
|
|
||||||
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildMember).toBeCalledWith(user);
|
|
||||||
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildChannelsCacheFind).toBeCalledTimes(1);
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(member.roles.remove).toBeCalledWith(role, 'Moderator: AUTHORTAG, Reason: *none*');
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(2);
|
|
||||||
|
|
||||||
// Log Embed
|
|
||||||
const logEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(logEmbed.title).toBe('Member Unmuted');
|
|
||||||
expect(logEmbed.fields.length).toBe(3);
|
|
||||||
|
|
||||||
// Log Embed -> User Field
|
|
||||||
const logEmbedUserField = logEmbed.fields[0];
|
|
||||||
|
|
||||||
expect(logEmbedUserField.name).toBe('User');
|
|
||||||
expect(logEmbedUserField.value).toBe('[object Object] `USERTAG`');
|
|
||||||
expect(logEmbedUserField.inline).toBeTruthy();
|
|
||||||
|
|
||||||
// Log Embed -> Moderator Field
|
|
||||||
const logEmbedModeratorField = logEmbed.fields[1];
|
|
||||||
|
|
||||||
expect(logEmbedModeratorField.name).toBe('Moderator');
|
|
||||||
expect(logEmbedModeratorField.value).toBe('[object Object] `AUTHORTAG`');
|
|
||||||
|
|
||||||
// Log Embed -> Reason Field
|
|
||||||
const logEmbedFieldReason = logEmbed.fields[2];
|
|
||||||
|
|
||||||
expect(logEmbedFieldReason.name).toBe('Reason');
|
|
||||||
expect(logEmbedFieldReason.value).toBe('*none*');
|
|
||||||
|
|
||||||
// Public Embed
|
|
||||||
const publicEmbed = result.embeds[1];
|
|
||||||
|
|
||||||
expect(publicEmbed.title).toBe('');
|
|
||||||
expect(publicEmbed.description).toBe('[object Object] has been unmuted');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given user did not mention a user, expect user not to exist', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
||||||
ROLES_MUTED: 'Muted'
|
|
||||||
};
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const messageAuthor = {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
manageable: true,
|
|
||||||
roles: {
|
|
||||||
remove: jest.fn()
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const role = {
|
|
||||||
name: 'Muted'
|
|
||||||
} as unknown as Role;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(null);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(member);
|
|
||||||
const messageGuildRolesCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): Role | undefined => {
|
|
||||||
const result = callback(role);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return role;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
available: true,
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildRolesCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
author: messageAuthor
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'mute',
|
|
||||||
args: ['USER', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const mute = new Unmute();
|
|
||||||
|
|
||||||
const result = await mute.execute(context);
|
|
||||||
|
|
||||||
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildMember).not.toBeCalled();
|
|
||||||
expect(messageGuildRolesCacheFind).not.toBeCalled();
|
|
||||||
expect(messageGuildChannelsCacheFind).not.toBeCalled();
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Error Embed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.description).toBe('User does not exist');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given member can not be found from user, expect user to not be in server', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
||||||
ROLES_MUTED: 'Muted'
|
|
||||||
};
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const messageAuthor = {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
manageable: true,
|
|
||||||
roles: {
|
|
||||||
remove: jest.fn()
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const role = {
|
|
||||||
name: 'Muted'
|
|
||||||
} as unknown as Role;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(user);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(null);
|
|
||||||
const messageGuildRolesCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): Role | undefined => {
|
|
||||||
const result = callback(role);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return role;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
available: true,
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildRolesCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
author: messageAuthor
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'mute',
|
|
||||||
args: ['USER', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const mute = new Unmute();
|
|
||||||
|
|
||||||
const result = await mute.execute(context);
|
|
||||||
|
|
||||||
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildMember).toBeCalledWith(user);
|
|
||||||
expect(messageGuildRolesCacheFind).not.toBeCalled();
|
|
||||||
expect(messageGuildChannelsCacheFind).not.toBeCalled();
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Error Embed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.description).toBe('User is not in this server');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given guild is unavailable, expect execution to stop', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
||||||
ROLES_MUTED: 'Muted'
|
|
||||||
};
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const messageAuthor = {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
manageable: true,
|
|
||||||
roles: {
|
|
||||||
remove: jest.fn()
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const role = {
|
|
||||||
name: 'Muted'
|
|
||||||
} as unknown as Role;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(user);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(member);
|
|
||||||
const messageGuildRolesCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): Role | undefined => {
|
|
||||||
const result = callback(role);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return role;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
available: false,
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildRolesCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
author: messageAuthor
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'mute',
|
|
||||||
args: ['USER', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const mute = new Unmute();
|
|
||||||
|
|
||||||
const result = await mute.execute(context);
|
|
||||||
|
|
||||||
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildMember).toBeCalledWith(user);
|
|
||||||
expect(messageGuildRolesCacheFind).not.toBeCalled();
|
|
||||||
expect(messageGuildChannelsCacheFind).not.toBeCalled();
|
|
||||||
expect(messageChannelSend).not.toBeCalled();
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given client can not manage user, expect insufficient permissions', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
||||||
ROLES_MUTED: 'Muted'
|
|
||||||
};
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const messageAuthor = {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
manageable: false,
|
|
||||||
roles: {
|
|
||||||
remove: jest.fn()
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const role = {
|
|
||||||
name: 'Muted'
|
|
||||||
} as unknown as Role;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(user);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(member);
|
|
||||||
const messageGuildRolesCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): Role | undefined => {
|
|
||||||
const result = callback(role);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return role;
|
|
||||||
});
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
available: true,
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildRolesCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
author: messageAuthor
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'mute',
|
|
||||||
args: ['USER', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const mute = new Unmute();
|
|
||||||
|
|
||||||
const result = await mute.execute(context);
|
|
||||||
|
|
||||||
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildMember).toBeCalledWith(user);
|
|
||||||
expect(messageGuildRolesCacheFind).not.toBeCalled();
|
|
||||||
expect(messageGuildChannelsCacheFind).not.toBeCalled();
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Error Embed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.description).toBe('Unable to do this action, am I missing permissions?');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given muted role can not be found, expect role not found', async () => {
|
|
||||||
process.env = {
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs',
|
|
||||||
ROLES_MUTED: 'Muted'
|
|
||||||
};
|
|
||||||
|
|
||||||
const user = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const messageAuthor = {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const member = {
|
|
||||||
manageable: true,
|
|
||||||
roles: {
|
|
||||||
remove: jest.fn()
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const role = {
|
|
||||||
name: 'Muted'
|
|
||||||
} as unknown as Role;
|
|
||||||
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(user);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(member);
|
|
||||||
const messageGuildRolesCacheFind = jest.fn()
|
|
||||||
.mockReturnValue(undefined);
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember,
|
|
||||||
available: true,
|
|
||||||
roles: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildRolesCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
author: messageAuthor
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'mute',
|
|
||||||
args: ['USER', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const mute = new Unmute();
|
|
||||||
|
|
||||||
const result = await mute.execute(context);
|
|
||||||
|
|
||||||
expect(messageMentionsUsersFirst).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildMember).toBeCalledWith(user);
|
|
||||||
expect(messageGuildRolesCacheFind).toBeCalledTimes(1);
|
|
||||||
expect(messageGuildChannelsCacheFind).not.toBeCalled();
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Error Embed
|
|
||||||
const errorEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(errorEmbed.description).toBe('Unable to find role');
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,485 +0,0 @@
|
||||||
import { GuildMember, Message, TextChannel, User } from "discord.js";
|
|
||||||
import Warn from "../../src/commands/warn";
|
|
||||||
import { ICommandContext } from "../../src/contracts/ICommandContext";
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
process.env = {};
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Constructor', () => {
|
|
||||||
test('Expect values to be set', () => {
|
|
||||||
process.env.ROLES_MODERATOR = 'Moderator';
|
|
||||||
|
|
||||||
const warn = new Warn();
|
|
||||||
|
|
||||||
expect(warn._category).toBe('Moderation');
|
|
||||||
expect(warn._roles.length).toBe(1);
|
|
||||||
expect(warn._roles[0]).toBe('Moderator');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Execute', () => {
|
|
||||||
test('Given user has permission, expect user to be warnned', async () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: 'Moderator',
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
};
|
|
||||||
|
|
||||||
const mentionedUser = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
const mentionedMember = {
|
|
||||||
warnnable: true,
|
|
||||||
warn: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(mentionedUser);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(mentionedMember);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember ,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'warn',
|
|
||||||
args: ['warn', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const warn = new Warn();
|
|
||||||
|
|
||||||
const result = await warn.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(logChannel.send).toBeCalledTimes(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given user has permissions, expect embeds to be correct', async () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: 'Moderator',
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
};
|
|
||||||
|
|
||||||
const mentionedUser = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
const mentionedMember = {
|
|
||||||
warnnable: true,
|
|
||||||
warn: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(mentionedUser);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(mentionedMember);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember ,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'warn',
|
|
||||||
args: ['warn', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const warn = new Warn();
|
|
||||||
|
|
||||||
const result = await warn.execute(context);
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(2);
|
|
||||||
|
|
||||||
const logEmbed = result.embeds[0];
|
|
||||||
const publicEmbed = result.embeds[1];
|
|
||||||
|
|
||||||
expect(logEmbed.title).toBe('Member Warned');
|
|
||||||
expect(publicEmbed.title).toBe("");
|
|
||||||
expect(publicEmbed.description).toBe('[object Object] has been warned');
|
|
||||||
expect(logEmbed.fields.length).toBe(3);
|
|
||||||
expect(publicEmbed.fields.length).toBe(1);
|
|
||||||
expect(publicEmbed.fields[0].name).toBe('Reason');
|
|
||||||
expect(publicEmbed.fields[0].value).toBe('Test Reason');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given user has permission, expect logEmbed fields to be correct', async () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: 'Moderator',
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
};
|
|
||||||
|
|
||||||
const mentionedUser = {
|
|
||||||
displayAvatarURL: jest.fn().mockReturnValue('URL'),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
const mentionedMember = {
|
|
||||||
warnnable: true,
|
|
||||||
warn: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(mentionedUser);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(mentionedMember);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember ,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'warn',
|
|
||||||
args: ['warn', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const warn = new Warn();
|
|
||||||
|
|
||||||
const result = await warn.execute(context);
|
|
||||||
|
|
||||||
const logEmbed = result.embeds[0];
|
|
||||||
|
|
||||||
const fieldUser = logEmbed.fields[0];
|
|
||||||
const fieldModerator = logEmbed.fields[1];
|
|
||||||
const fieldReason = logEmbed.fields[2];
|
|
||||||
|
|
||||||
expect(fieldUser.name).toBe("User");
|
|
||||||
expect(fieldUser.value).toBe("[object Object] `USERTAG`");
|
|
||||||
expect(logEmbed.thumbnail?.url).toBe("URL");
|
|
||||||
|
|
||||||
expect(fieldModerator.name).toBe('Moderator');
|
|
||||||
expect(fieldModerator.value).toBe('[object Object] `AUTHORTAG`');
|
|
||||||
|
|
||||||
expect(fieldReason.name).toBe('Reason');
|
|
||||||
expect(fieldReason.value).toBe('Test Reason');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given user is not mentioned, expect error embed to be sent', async () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: 'Moderator',
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
};
|
|
||||||
|
|
||||||
const mentionedMember = {
|
|
||||||
warnnable: true,
|
|
||||||
warn: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(null);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(mentionedMember);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember ,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'warn',
|
|
||||||
args: ['warn', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const warn = new Warn();
|
|
||||||
|
|
||||||
const result = await warn.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(logChannel.send).not.toBeCalled();
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
const embedError = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embedError.description).toBe('User does not exist');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given member is not in server, expect error embed to be sent', async () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: 'Moderator',
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
};
|
|
||||||
|
|
||||||
const mentionedUser = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
const mentionedMember = {
|
|
||||||
warnnable: true,
|
|
||||||
warn: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(mentionedUser);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(null);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember ,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: true
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'warn',
|
|
||||||
args: ['warn', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const warn = new Warn();
|
|
||||||
|
|
||||||
const result = await warn.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).toBeCalledTimes(1);
|
|
||||||
expect(logChannel.send).not.toBeCalled();
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
const embedError = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embedError.description).toBe('User is not in this server');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given guild is unavailable, expect return and do nothing', async () => {
|
|
||||||
process.env = {
|
|
||||||
ROLES_MODERATOR: 'Moderator',
|
|
||||||
CHANNELS_LOGS_MOD: 'mod-logs'
|
|
||||||
};
|
|
||||||
|
|
||||||
const mentionedUser = {
|
|
||||||
displayAvatarURL: jest.fn(),
|
|
||||||
tag: 'USERTAG'
|
|
||||||
} as unknown as User;
|
|
||||||
const mentionedMember = {
|
|
||||||
warnnable: true,
|
|
||||||
warn: jest.fn()
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
const logChannel = {
|
|
||||||
name: 'mod-logs',
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const messageChannelSend = jest.fn();
|
|
||||||
const messageMentionsUsersFirst = jest.fn()
|
|
||||||
.mockReturnValue(mentionedUser);
|
|
||||||
const messageGuildMember = jest.fn()
|
|
||||||
.mockReturnValue(mentionedMember);
|
|
||||||
const messageGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockImplementation((callback): TextChannel | undefined => {
|
|
||||||
const result = callback(logChannel);
|
|
||||||
|
|
||||||
if (!result) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return logChannel;
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
channel: {
|
|
||||||
send: messageChannelSend
|
|
||||||
},
|
|
||||||
mentions: {
|
|
||||||
users: {
|
|
||||||
first: messageMentionsUsersFirst
|
|
||||||
}
|
|
||||||
},
|
|
||||||
guild: {
|
|
||||||
member: messageGuildMember ,
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: messageGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
},
|
|
||||||
available: false
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
tag: 'AUTHORTAG'
|
|
||||||
}
|
|
||||||
} as unknown as Message;
|
|
||||||
|
|
||||||
const context: ICommandContext = {
|
|
||||||
name: 'warn',
|
|
||||||
args: ['warn', 'Test', 'Reason'],
|
|
||||||
message: message
|
|
||||||
};
|
|
||||||
|
|
||||||
const warn = new Warn();
|
|
||||||
|
|
||||||
const result = await warn.execute(context);
|
|
||||||
|
|
||||||
expect(messageChannelSend).not.toBeCalled();
|
|
||||||
expect(logChannel.send).not.toBeCalled();
|
|
||||||
expect(result.embeds.length).toBe(0);
|
|
||||||
});
|
|
||||||
});
|
|
11
tests/database/entites/UserSetting.test.ts
Normal file
11
tests/database/entites/UserSetting.test.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
describe("constructor", () => {
|
||||||
|
test.todo("EXPECT settings to be configured");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("UpdateValue", () => {
|
||||||
|
test.todo("EXPECT value to be updated");
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("FetchOneByKey", () => {
|
||||||
|
test.todo("EXPECT single entity returned");
|
||||||
|
});
|
|
@ -1,164 +0,0 @@
|
||||||
import { GuildMember, TextChannel, User } from "discord.js";
|
|
||||||
import MemberEvents from "../../src/events/MemberEvents";
|
|
||||||
import GuildMemberUpdate from "../../src/events/MemberEvents/GuildMemberUpdate";
|
|
||||||
|
|
||||||
describe('GuildMemberAdd', () => {
|
|
||||||
test('When event is fired, expect embed to be sent to logs channel', async () => {
|
|
||||||
const currentDate = new Date();
|
|
||||||
|
|
||||||
const textChannel = {
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const memberGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockReturnValue(textChannel);
|
|
||||||
const userDisplayAvatarURL = jest.fn();
|
|
||||||
|
|
||||||
const guildUser = {
|
|
||||||
tag: 'USERTAG',
|
|
||||||
createdAt: currentDate,
|
|
||||||
id: 'USERID',
|
|
||||||
displayAvatarURL: userDisplayAvatarURL
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const guildMember = {
|
|
||||||
user: guildUser,
|
|
||||||
guild: {
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: memberGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const memberEvents = new MemberEvents();
|
|
||||||
|
|
||||||
const result = await memberEvents.guildMemberAdd(guildMember);
|
|
||||||
|
|
||||||
expect(textChannel.send).toBeCalledTimes(1);
|
|
||||||
expect(userDisplayAvatarURL).toBeCalledTimes(1);
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Embed
|
|
||||||
const embed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embed.title).toBe("Member Joined");
|
|
||||||
expect(embed.footer?.text).toBe("Id: USERID");
|
|
||||||
expect(embed.fields.length).toBe(2);
|
|
||||||
|
|
||||||
// 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 -> Created Field
|
|
||||||
const embedFieldCreated = embed.fields[1];
|
|
||||||
|
|
||||||
expect(embedFieldCreated.name).toBe("Created");
|
|
||||||
expect(embedFieldCreated.value).toBe(currentDate.toString());
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('GuildMemberRemove', () => {
|
|
||||||
test('When event is fired, expect embed to be sent to logs channel', async () => {
|
|
||||||
const currentDate = new Date();
|
|
||||||
|
|
||||||
const textChannel = {
|
|
||||||
send: jest.fn()
|
|
||||||
} as unknown as TextChannel;
|
|
||||||
|
|
||||||
const memberGuildChannelsCacheFind = jest.fn()
|
|
||||||
.mockReturnValue(textChannel);
|
|
||||||
const userDisplayAvatarURL = jest.fn();
|
|
||||||
|
|
||||||
const guildUser = {
|
|
||||||
tag: 'USERTAG',
|
|
||||||
createdAt: currentDate,
|
|
||||||
id: 'USERID',
|
|
||||||
displayAvatarURL: userDisplayAvatarURL
|
|
||||||
} as unknown as User;
|
|
||||||
|
|
||||||
const guildMember = {
|
|
||||||
user: guildUser,
|
|
||||||
guild: {
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: memberGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
joinedAt: currentDate
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const memberEvents = new MemberEvents();
|
|
||||||
|
|
||||||
const result = await memberEvents.guildMemberRemove(guildMember);
|
|
||||||
|
|
||||||
expect(textChannel.send).toBeCalledTimes(1);
|
|
||||||
expect(userDisplayAvatarURL).toBeCalledTimes(1);
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Embed
|
|
||||||
const embed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embed.title).toBe("Member Left");
|
|
||||||
expect(embed.footer?.text).toBe("Id: USERID");
|
|
||||||
expect(embed.fields.length).toBe(2);
|
|
||||||
|
|
||||||
// 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 -> Joined Field
|
|
||||||
const embedFieldJoined = embed.fields[1];
|
|
||||||
|
|
||||||
expect(embedFieldJoined.name).toBe("Joined");
|
|
||||||
expect(embedFieldJoined.value).toBe(currentDate.toString());
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('GuildMemberUpdate', () => {
|
|
||||||
test('Given nicknames are the same, expect NicknameChanged NOT to be called', async () => {
|
|
||||||
const member = {
|
|
||||||
nickname: 'member'
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const nicknameChanged = jest.fn();
|
|
||||||
|
|
||||||
GuildMemberUpdate.prototype.NicknameChanged = nicknameChanged;
|
|
||||||
|
|
||||||
const memberEvents = new MemberEvents();
|
|
||||||
|
|
||||||
const result = await memberEvents.guildMemberUpdate(member, member);
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(0);
|
|
||||||
expect(nicknameChanged).not.toBeCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given nicknames are the different, expect NicknameChanged to be called', async () => {
|
|
||||||
const oldMember = {
|
|
||||||
nickname: 'oldMember'
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const newMember = {
|
|
||||||
nickname: 'newMember'
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const nicknameChanged = jest.fn();
|
|
||||||
|
|
||||||
GuildMemberUpdate.prototype.NicknameChanged = nicknameChanged;
|
|
||||||
|
|
||||||
const memberEvents = new MemberEvents();
|
|
||||||
|
|
||||||
const result = await memberEvents.guildMemberUpdate(oldMember, newMember);
|
|
||||||
|
|
||||||
expect(result.embeds.length).toBe(0);
|
|
||||||
expect(nicknameChanged).toBeCalledTimes(1);
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,235 +0,0 @@
|
||||||
import { GuildMember, TextChannel } from "discord.js";
|
|
||||||
import GuildMemberUpdate from "../../../src/events/MemberEvents/GuildMemberUpdate";
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
process.env = {};
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Constructor', () => {
|
|
||||||
test('Expect properties are set', () => {
|
|
||||||
const oldMember = {
|
|
||||||
nickname: 'Old Nickname'
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const newMember = {
|
|
||||||
nickname: 'New Nickname'
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
|
|
||||||
|
|
||||||
expect(guildMemberUpdate.oldMember).toBe(oldMember);
|
|
||||||
expect(guildMemberUpdate.newMember).toBe(newMember);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('NicknameChanged', () => {
|
|
||||||
test('Given nickname has changed from one to another, expect embed to be sent with both', 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 memberUserDisplayAvatarURL = jest.fn();
|
|
||||||
|
|
||||||
const oldMember = {
|
|
||||||
nickname: 'Old Nickname'
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const newMember = {
|
|
||||||
nickname: 'New Nickname',
|
|
||||||
guild: {
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: memberGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
user: {
|
|
||||||
tag: 'USERTAG',
|
|
||||||
id: 'USERID',
|
|
||||||
displayAvatarURL: memberUserDisplayAvatarURL
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
|
|
||||||
|
|
||||||
const result = await guildMemberUpdate.NicknameChanged();
|
|
||||||
|
|
||||||
expect(channelSend).toBeCalledTimes(1);
|
|
||||||
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
|
|
||||||
expect(memberUserDisplayAvatarURL).toBeCalledTimes(1);
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Embed
|
|
||||||
const embed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embed.title).toBe('Nickname Changed');
|
|
||||||
expect(embed.footer?.text).toBe('Id: USERID');
|
|
||||||
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 -> Before Field
|
|
||||||
const embedFieldBefore = embed.fields[1];
|
|
||||||
|
|
||||||
expect(embedFieldBefore.name).toBe('Before');
|
|
||||||
expect(embedFieldBefore.value).toBe('Old Nickname');
|
|
||||||
|
|
||||||
// Embed -> After Field
|
|
||||||
const embedFieldAfter = embed.fields[2];
|
|
||||||
|
|
||||||
expect(embedFieldAfter.name).toBe('After');
|
|
||||||
expect(embedFieldAfter.value).toBe('New Nickname');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given old nickname was null, expect embed to say old nickname was none', 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 memberUserDisplayAvatarURL = jest.fn();
|
|
||||||
|
|
||||||
const oldMember = {} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const newMember = {
|
|
||||||
nickname: 'New Nickname',
|
|
||||||
guild: {
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: memberGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
user: {
|
|
||||||
tag: 'USERTAG',
|
|
||||||
id: 'USERID',
|
|
||||||
displayAvatarURL: memberUserDisplayAvatarURL
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
|
|
||||||
|
|
||||||
const result = await guildMemberUpdate.NicknameChanged();
|
|
||||||
|
|
||||||
expect(channelSend).toBeCalledTimes(1);
|
|
||||||
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
|
|
||||||
expect(memberUserDisplayAvatarURL).toBeCalledTimes(1);
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Embed
|
|
||||||
const embed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embed.title).toBe('Nickname Changed');
|
|
||||||
expect(embed.footer?.text).toBe('Id: USERID');
|
|
||||||
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 -> Before Field
|
|
||||||
const embedFieldBefore = embed.fields[1];
|
|
||||||
|
|
||||||
expect(embedFieldBefore.name).toBe('Before');
|
|
||||||
expect(embedFieldBefore.value).toBe('*none*');
|
|
||||||
|
|
||||||
// Embed -> After Field
|
|
||||||
const embedFieldAfter = embed.fields[2];
|
|
||||||
|
|
||||||
expect(embedFieldAfter.name).toBe('After');
|
|
||||||
expect(embedFieldAfter.value).toBe('New Nickname');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Given new nickname was null, expect embed to say new nickname was none', 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 memberUserDisplayAvatarURL = jest.fn();
|
|
||||||
|
|
||||||
const oldMember = {
|
|
||||||
nickname: 'Old Nickname'
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const newMember = {
|
|
||||||
guild: {
|
|
||||||
channels: {
|
|
||||||
cache: {
|
|
||||||
find: memberGuildChannelsCacheFind
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
user: {
|
|
||||||
tag: 'USERTAG',
|
|
||||||
id: 'USERID',
|
|
||||||
displayAvatarURL: memberUserDisplayAvatarURL
|
|
||||||
}
|
|
||||||
} as unknown as GuildMember;
|
|
||||||
|
|
||||||
const guildMemberUpdate = new GuildMemberUpdate(oldMember, newMember);
|
|
||||||
|
|
||||||
const result = await guildMemberUpdate.NicknameChanged();
|
|
||||||
|
|
||||||
expect(channelSend).toBeCalledTimes(1);
|
|
||||||
expect(memberGuildChannelsCacheFind).toBeCalledTimes(1);
|
|
||||||
expect(memberUserDisplayAvatarURL).toBeCalledTimes(1);
|
|
||||||
expect(result.embeds.length).toBe(1);
|
|
||||||
|
|
||||||
// Embed
|
|
||||||
const embed = result.embeds[0];
|
|
||||||
|
|
||||||
expect(embed.title).toBe('Nickname Changed');
|
|
||||||
expect(embed.footer?.text).toBe('Id: USERID');
|
|
||||||
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 -> Before Field
|
|
||||||
const embedFieldBefore = embed.fields[1];
|
|
||||||
|
|
||||||
expect(embedFieldBefore.name).toBe('Before');
|
|
||||||
expect(embedFieldBefore.value).toBe('Old Nickname');
|
|
||||||
|
|
||||||
// Embed -> After Field
|
|
||||||
const embedFieldAfter = embed.fields[2];
|
|
||||||
|
|
||||||
expect(embedFieldAfter.name).toBe('After');
|
|
||||||
expect(embedFieldAfter.value).toBe('*none*');
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,648 +0,0 @@
|
||||||
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*```');
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,11 +0,0 @@
|
||||||
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');
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,57 +0,0 @@
|
||||||
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);
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,292 +0,0 @@
|
||||||
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");
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,407 +0,0 @@
|
||||||
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");
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,67 +0,0 @@
|
||||||
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…
Reference in a new issue