Compare commits
15 commits
ef18449794
...
9dff741971
Author | SHA1 | Date | |
---|---|---|---|
9dff741971 | |||
783c730e0c | |||
61633277ef | |||
40182c8777 | |||
14a1981b94 | |||
6fb448fc28 | |||
c12644d537 | |||
efd213d085 | |||
39999dfbba | |||
c02dbb30d7 | |||
ec1763be26 | |||
1c00fdae5c | |||
f9c706fdd8 | |||
b5acc58a79 | |||
d52868e5a3 |
11 changed files with 641 additions and 899 deletions
1
.dev.env
1
.dev.env
|
@ -9,6 +9,7 @@
|
||||||
BOT_TOKEN=
|
BOT_TOKEN=
|
||||||
BOT_VER=3.1
|
BOT_VER=3.1
|
||||||
BOT_AUTHOR=Vylpes
|
BOT_AUTHOR=Vylpes
|
||||||
|
BOT_DATE=08 May 2023
|
||||||
BOT_OWNERID=147392775707426816
|
BOT_OWNERID=147392775707426816
|
||||||
BOT_CLIENTID=682942374040961060
|
BOT_CLIENTID=682942374040961060
|
||||||
|
|
||||||
|
|
19
package.json
19
package.json
|
@ -26,22 +26,21 @@
|
||||||
"funding": "https://ko-fi.com/vylpes",
|
"funding": "https://ko-fi.com/vylpes",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@discordjs/rest": "^1.1.0",
|
"@discordjs/rest": "^1.1.0",
|
||||||
"@types/jest": "^27.0.3",
|
"@types/jest": "^29.0.0",
|
||||||
"@types/uuid": "^9.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": "^9.2.0",
|
"emoji-regex": "^10.0.0",
|
||||||
"jest": "^29.5.0",
|
"jest": "^29.0.0",
|
||||||
"jest-mock-extended": "^3.0.4",
|
"jest-mock-extended": "^3.0.0",
|
||||||
"minimatch": "7.4.2",
|
"minimatch": "7.4.6",
|
||||||
"mysql": "^2.18.1",
|
"mysql": "^2.18.1",
|
||||||
"random-bunny": "^2.0.5",
|
"random-bunny": "^2.0.5",
|
||||||
"ts-jest": "^29.0.5",
|
"ts-jest": "^29.0.0",
|
||||||
"typeorm": "^0.2.44",
|
"typeorm": "0.3.14"
|
||||||
"uuid": "^9.0.0"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^18.0.0",
|
"@types/node": "^20.0.0",
|
||||||
"typescript": "^5.0.2"
|
"typescript": "^5.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,12 +14,17 @@ export default class Ignore extends Command {
|
||||||
|
|
||||||
public override async execute(interaction: CommandInteraction) {
|
public override async execute(interaction: CommandInteraction) {
|
||||||
if (!interaction.guildId) return;
|
if (!interaction.guildId) return;
|
||||||
|
|
||||||
const isChannelIgnored = await IgnoredChannel.IsChannelIgnored(interaction.guildId);
|
const isChannelIgnored = await IgnoredChannel.IsChannelIgnored(interaction.guildId);
|
||||||
|
|
||||||
if (isChannelIgnored) {
|
if (isChannelIgnored) {
|
||||||
const entity = await IgnoredChannel.FetchOneById(IgnoredChannel, interaction.guildId);
|
const entity = await IgnoredChannel.FetchOneById(IgnoredChannel, interaction.guildId);
|
||||||
|
|
||||||
|
if (!entity) {
|
||||||
|
await interaction.reply('Unable to find channel.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await IgnoredChannel.Remove(IgnoredChannel, entity);
|
await IgnoredChannel.Remove(IgnoredChannel, entity);
|
||||||
|
|
||||||
await interaction.reply('This channel will start being logged again.');
|
await interaction.reply('This channel will start being logged again.');
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import { Column, DeepPartial, EntityTarget, getConnection, PrimaryColumn } from "typeorm";
|
import { Column, DeepPartial, EntityTarget, getConnection, PrimaryColumn, ObjectLiteral, FindOptionsWhere } from "typeorm";
|
||||||
import { v4 } from "uuid";
|
import { v4 } from "uuid";
|
||||||
|
|
||||||
export default class BaseEntity {
|
export default class BaseEntity {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.Id = v4();
|
this.Id = v4();
|
||||||
|
|
||||||
this.WhenCreated = new Date();
|
this.WhenCreated = new Date();
|
||||||
this.WhenUpdated = new Date();
|
this.WhenUpdated = new Date();
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ export default class BaseEntity {
|
||||||
@Column()
|
@Column()
|
||||||
WhenUpdated: Date;
|
WhenUpdated: Date;
|
||||||
|
|
||||||
public async Save<T>(target: EntityTarget<T>, entity: DeepPartial<T>): Promise<void> {
|
public async Save<T extends BaseEntity>(target: EntityTarget<T>, entity: DeepPartial<T>): Promise<void> {
|
||||||
this.WhenUpdated = new Date();
|
this.WhenUpdated = new Date();
|
||||||
|
|
||||||
const connection = getConnection();
|
const connection = getConnection();
|
||||||
|
@ -28,7 +28,7 @@ export default class BaseEntity {
|
||||||
await repository.save(entity);
|
await repository.save(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Remove<T>(target: EntityTarget<T>, entity: T): Promise<void> {
|
public static async Remove<T extends BaseEntity>(target: EntityTarget<T>, entity: T): Promise<void> {
|
||||||
const connection = getConnection();
|
const connection = getConnection();
|
||||||
|
|
||||||
const repository = connection.getRepository<T>(target);
|
const repository = connection.getRepository<T>(target);
|
||||||
|
@ -36,7 +36,7 @@ export default class BaseEntity {
|
||||||
await repository.remove(entity);
|
await repository.remove(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async FetchAll<T>(target: EntityTarget<T>, relations?: string[]): Promise<T[]> {
|
public static async FetchAll<T extends BaseEntity>(target: EntityTarget<T>, relations?: string[]): Promise<T[]> {
|
||||||
const connection = getConnection();
|
const connection = getConnection();
|
||||||
|
|
||||||
const repository = connection.getRepository<T>(target);
|
const repository = connection.getRepository<T>(target);
|
||||||
|
@ -46,17 +46,17 @@ export default class BaseEntity {
|
||||||
return all;
|
return all;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async FetchOneById<T>(target: EntityTarget<T>, id: string, relations?: string[]): Promise<T | undefined> {
|
public static async FetchOneById<T extends BaseEntity>(target: EntityTarget<T>, id: string, relations?: string[]): Promise<T | null> {
|
||||||
const connection = getConnection();
|
const connection = getConnection();
|
||||||
|
|
||||||
const repository = connection.getRepository<T>(target);
|
const repository = connection.getRepository<T>(target);
|
||||||
|
|
||||||
const single = await repository.findOne(id, { relations: relations || [] });
|
const single = await repository.findOne({ where: ({ Id: id } as FindOptionsWhere<T>), relations: relations || {} });
|
||||||
|
|
||||||
return single;
|
return single;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Any<T>(target: EntityTarget<T>): Promise<boolean> {
|
public static async Any<T extends ObjectLiteral>(target: EntityTarget<T>): Promise<boolean> {
|
||||||
const connection = getConnection();
|
const connection = getConnection();
|
||||||
|
|
||||||
const repository = connection.getRepository<T>(target);
|
const repository = connection.getRepository<T>(target);
|
||||||
|
|
|
@ -33,12 +33,12 @@ export default class Lobby extends BaseEntity {
|
||||||
this.LastUsed = new Date();
|
this.LastUsed = new Date();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async FetchOneByChannelId(channelId: string, relations?: string[]): Promise<Lobby | undefined> {
|
public static async FetchOneByChannelId(channelId: string, relations?: string[]): Promise<Lobby | null> {
|
||||||
const connection = getConnection();
|
const connection = getConnection();
|
||||||
|
|
||||||
const repository = connection.getRepository(Lobby);
|
const repository = connection.getRepository(Lobby);
|
||||||
|
|
||||||
const single = await repository.findOne({ ChannelId: channelId }, { relations: relations || [] });
|
const single = await repository.findOne({ where: { ChannelId: channelId }, relations: relations || [] });
|
||||||
|
|
||||||
return single;
|
return single;
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,22 +34,22 @@ export default class Audit extends BaseEntity {
|
||||||
@Column()
|
@Column()
|
||||||
ServerId: string;
|
ServerId: string;
|
||||||
|
|
||||||
public static async FetchAuditsByUserId(userId: string, serverId: string): Promise<Audit[] | undefined> {
|
public static async FetchAuditsByUserId(userId: string, serverId: string): Promise<Audit[] | null> {
|
||||||
const connection = getConnection();
|
const connection = getConnection();
|
||||||
|
|
||||||
const repository = connection.getRepository(Audit);
|
const repository = connection.getRepository(Audit);
|
||||||
|
|
||||||
const all = await repository.find({ UserId: userId, ServerId: serverId });
|
const all = await repository.find({ where: { UserId: userId, ServerId: serverId } });
|
||||||
|
|
||||||
return all;
|
return all;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async FetchAuditByAuditId(auditId: string, serverId: string): Promise<Audit | undefined> {
|
public static async FetchAuditByAuditId(auditId: string, serverId: string): Promise<Audit | null> {
|
||||||
const connection = getConnection();
|
const connection = getConnection();
|
||||||
|
|
||||||
const repository = connection.getRepository(Audit);
|
const repository = connection.getRepository(Audit);
|
||||||
|
|
||||||
const single = await repository.findOne({ AuditId: auditId, ServerId: serverId });
|
const single = await repository.findOne({ where: { AuditId: auditId, ServerId: serverId } });
|
||||||
|
|
||||||
return single;
|
return single;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ export default class IgnoredChannel extends BaseEntity {
|
||||||
|
|
||||||
const repository = connection.getRepository(IgnoredChannel);
|
const repository = connection.getRepository(IgnoredChannel);
|
||||||
|
|
||||||
const single = await repository.findOne(channelId);
|
const single = await repository.findOne({ where: { Id: channelId } });
|
||||||
|
|
||||||
return single != undefined;
|
return single != undefined;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,13 +19,13 @@ export default class Role extends BaseEntity {
|
||||||
public SetServer(server: Server) {
|
public SetServer(server: Server) {
|
||||||
this.Server = server;
|
this.Server = server;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async FetchOneByRoleId(roleId: string, relations?: string[]): Promise<Role | undefined> {
|
public static async FetchOneByRoleId(roleId: string, relations?: string[]): Promise<Role | null> {
|
||||||
const connection = getConnection();
|
const connection = getConnection();
|
||||||
|
|
||||||
const repository = connection.getRepository(Role);
|
const repository = connection.getRepository(Role);
|
||||||
|
|
||||||
const single = await repository.findOne({ RoleId: roleId}, { relations: relations || [] });
|
const single = await repository.findOne({ where: { RoleId: roleId }, relations: relations || []});
|
||||||
|
|
||||||
return single;
|
return single;
|
||||||
}
|
}
|
||||||
|
@ -35,9 +35,9 @@ export default class Role extends BaseEntity {
|
||||||
|
|
||||||
const repository = connection.getRepository(Server);
|
const repository = connection.getRepository(Server);
|
||||||
|
|
||||||
const all = await repository.findOne(serverId, { relations: [
|
const all = await repository.findOne({ where: { Id: serverId }, relations: [
|
||||||
"Roles",
|
"Roles",
|
||||||
]});
|
] });
|
||||||
|
|
||||||
if (!all) {
|
if (!all) {
|
||||||
return [];
|
return [];
|
||||||
|
|
|
@ -25,12 +25,12 @@ export default class Setting extends BaseEntity {
|
||||||
this.Value = value;
|
this.Value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async FetchOneByKey(key: string, relations?: string[]): Promise<Setting | undefined> {
|
public static async FetchOneByKey(key: string, relations?: string[]): Promise<Setting | null> {
|
||||||
const connection = getConnection();
|
const connection = getConnection();
|
||||||
|
|
||||||
const repository = connection.getRepository(Setting);
|
const repository = connection.getRepository(Setting);
|
||||||
|
|
||||||
const single = await repository.findOne({ Key: key }, { relations: relations || [] });
|
const single = await repository.findOne({ where: { Key: key }, relations: relations || {} });
|
||||||
|
|
||||||
return single;
|
return single;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
import { APIEmbed, CommandInteraction, CommandInteractionOption, Embed, EmbedBuilder, Guild, GuildChannel, GuildMember, InteractionReplyOptions, JSONEncodable, SlashCommandBuilder, TextChannel, User } from "discord.js";
|
import { APIEmbed, CommandInteraction, CommandInteractionOption, DMChannel, Embed, EmbedBuilder, Guild, GuildChannel, GuildMember, InteractionReplyOptions, JSONEncodable, MessageCreateOptions, SlashCommandBuilder, TextChannel, User } from "discord.js";
|
||||||
import { mock } from "jest-mock-extended";
|
import { mock } from "jest-mock-extended";
|
||||||
import Timeout from "../../src/commands/timeout";
|
import Timeout from "../../src/commands/timeout";
|
||||||
import SettingsHelper from "../../src/helpers/SettingsHelper";
|
import SettingsHelper from "../../src/helpers/SettingsHelper";
|
||||||
import Audit from "../../src/entity/Audit";
|
import Audit from "../../src/entity/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', () => {
|
describe('Constructor', () => {
|
||||||
test('EXPECT CommandBuilder to be configured', () => {
|
test('EXPECT CommandBuilder to be configured', () => {
|
||||||
|
@ -29,16 +33,32 @@ describe('execute', () => {
|
||||||
embeds = options.embeds as APIEmbed[];
|
embeds = options.embeds as APIEmbed[];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let savedAudit: DeepPartial<Audit> | undefined;
|
||||||
|
|
||||||
const getSetting = jest.spyOn(SettingsHelper, 'GetSetting').mockResolvedValue('mod-logs');
|
const getSetting = jest.spyOn(SettingsHelper, 'GetSetting').mockResolvedValue('mod-logs');
|
||||||
const auditSave = jest.spyOn(Audit.prototype, 'Save').mockImplementation();
|
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 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 = {
|
const userInput = {
|
||||||
user: {
|
user: {
|
||||||
id: 'userId',
|
id: 'userId',
|
||||||
tag: 'userTag',
|
tag: 'userTag',
|
||||||
} as User,
|
createDM: jest.fn().mockResolvedValue(dmChannel),
|
||||||
|
} as unknown as User,
|
||||||
member: {
|
member: {
|
||||||
manageable: true,
|
manageable: true,
|
||||||
timeout: timeoutFunc,
|
timeout: timeoutFunc,
|
||||||
|
@ -55,7 +75,9 @@ describe('execute', () => {
|
||||||
|
|
||||||
const logsChannel = {
|
const logsChannel = {
|
||||||
name: 'mod-logs',
|
name: 'mod-logs',
|
||||||
send: jest.fn(),
|
send: jest.fn().mockImplementation((options: MessageCreateOptions) => {
|
||||||
|
logsChannelSentEmbeds = options.embeds;
|
||||||
|
}),
|
||||||
} as unknown as TextChannel;
|
} as unknown as TextChannel;
|
||||||
|
|
||||||
const interaction = {
|
const interaction = {
|
||||||
|
@ -65,7 +87,8 @@ describe('execute', () => {
|
||||||
find: jest.fn()
|
find: jest.fn()
|
||||||
.mockReturnValue(logsChannel),
|
.mockReturnValue(logsChannel),
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
name: "Test Guild",
|
||||||
} as unknown as Guild,
|
} as unknown as Guild,
|
||||||
guildId: 'guildId',
|
guildId: 'guildId',
|
||||||
reply: interactionReply,
|
reply: interactionReply,
|
||||||
|
@ -102,9 +125,59 @@ describe('execute', () => {
|
||||||
expect(resultEmbedDMField.name).toBe("DM Sent");
|
expect(resultEmbedDMField.name).toBe("DM Sent");
|
||||||
expect(resultEmbedDMField.value).toBe("true");
|
expect(resultEmbedDMField.value).toBe("true");
|
||||||
|
|
||||||
// TODO: DM Embed check
|
// EXPECT user to be DM's with embed
|
||||||
// TODO: Log Embed check
|
expect(dmChannel.send).toBeCalled();
|
||||||
// TODO: Audit check
|
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
|
// Null checks
|
||||||
|
|
Loading…
Reference in a new issue