From 8d1befcf25b4645472f70027414d8043c9b9c613 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 6 Dec 2024 20:54:47 +0000 Subject: [PATCH] Create autokick command --- src/commands/autokick.ts | 65 +++++++++++++++++++++++-- src/database/entities/AutoKickConfig.ts | 11 +++++ src/helpers/AutoKickHelper.ts | 32 ++++++++---- src/registry.ts | 2 + 4 files changed, 97 insertions(+), 13 deletions(-) diff --git a/src/commands/autokick.ts b/src/commands/autokick.ts index 09318b3..760441e 100644 --- a/src/commands/autokick.ts +++ b/src/commands/autokick.ts @@ -1,5 +1,7 @@ -import {CommandInteraction, PermissionFlagsBits, SlashCommandBuilder} from "discord.js"; +import {ChatInputCommandInteraction, CommandInteraction, EmbedBuilder, PermissionFlagsBits, SlashCommandBuilder} from "discord.js"; import {Command} from "../type/command"; +import TimeLengthInput from "../helpers/TimeLengthInput"; +import AutoKickHelper from "../helpers/AutoKickHelper"; export default class Autokick extends Command { constructor() { @@ -46,9 +48,66 @@ export default class Autokick extends Command { } } - private async set(interaction: CommandInteraction) { + private async set(interaction: ChatInputCommandInteraction) { + if (!interaction.guildId) return; + + const roleOption = interaction.options.getRole("role", true); + const kickTimeOption = interaction.options.getString("kicktime", true); + const noticeTimeOption = interaction.options.getString("noticetime"); + const noticeChannelOption = interaction.options.getChannel("noticechannel"); + + const roleId = roleOption.id; + const kickTimeInput = new TimeLengthInput(kickTimeOption); + const noticeTimeInput = noticeTimeOption ? new TimeLengthInput(noticeTimeOption) : undefined; + const noticeChannelId = noticeChannelOption?.id; + + if ((noticeTimeInput && !noticeTimeOption) || (!noticeTimeInput && noticeChannelOption)) { + await interaction.reply("Both `noticetime` and `noticechannel` must be set if you want a notification embed"); + return; + } + + await AutoKickHelper.SetSetting(interaction.guildId, roleId, kickTimeInput.GetMilliseconds(), noticeTimeInput?.GetMilliseconds(), noticeChannelId); + + const embed = new EmbedBuilder() + .setTitle("Auto Kick") + .setDescription("Configured auto kick for this server") + .addFields([ + { + name: "Role", + value: roleOption.name, + inline: true, + }, + { + name: "Kick Time", + value: kickTimeInput.GetLengthShort(), + inline: true, + }, + ]); + + if (noticeTimeInput) { + embed.addFields([ + { + name: "Notice Time", + value: noticeTimeInput.GetLengthShort(), + }, + { + name: "Notice Channel", + value: noticeChannelOption!.name!, + inline: true, + }, + ]); + } + + await interaction.reply({ + embeds: [ embed ], + }); } - private async unset(interaction: CommandInteraction) { + private async unset(interaction: ChatInputCommandInteraction) { + if (!interaction.guildId) return; + + await AutoKickHelper.UnsetSetting(interaction.guildId); + + await interaction.reply("Unset the auto kick configuration for this server"); } } diff --git a/src/database/entities/AutoKickConfig.ts b/src/database/entities/AutoKickConfig.ts index 1ee3625..6a82ba5 100644 --- a/src/database/entities/AutoKickConfig.ts +++ b/src/database/entities/AutoKickConfig.ts @@ -47,4 +47,15 @@ export default class AutoKickConfig extends BaseEntity { return query; } + + public static async FetchAllByServerId(serverId: string): Promise { + const repository = AppDataSource.getRepository(AutoKickConfig); + + const query = repository + .createQueryBuilder("config") + .where("config.serverId = :serverId", { serverId }) + .getMany(); + + return query; + } } diff --git a/src/helpers/AutoKickHelper.ts b/src/helpers/AutoKickHelper.ts index 7c3e2aa..a02a934 100644 --- a/src/helpers/AutoKickHelper.ts +++ b/src/helpers/AutoKickHelper.ts @@ -1,25 +1,37 @@ import AutoKickConfig from "../database/entities/AutoKickConfig"; export default class AutoKickHelper { - public static async SetSetting(serverId: string, roleId: string, kickTime: number, noticeTime?: number, noticeChannelId?: string) { - const config = await AutoKickConfig.FetchOneByServerIdAndRoleId(serverId, roleId); + public static async GetSetting(serverId: string): Promise { + const configs = await AutoKickConfig.FetchAllByServerId(serverId); - if (!config) { - const newConfig = new AutoKickConfig(serverId, roleId, kickTime, noticeTime, noticeChannelId); - await newConfig.Save(AutoKickConfig, newConfig); + if (configs.length != 1) { + return null; + } + + return configs[0]; + } + + public static async SetSetting(serverId: string, roleId: string, kickTime: number, noticeTime?: number, noticeChannelId?: string) { + const configs = await AutoKickConfig.FetchAllByServerId(serverId); + + if (configs.length == 0) { + const config = new AutoKickConfig(serverId, roleId, kickTime, noticeTime, noticeChannelId); + await config.Save(AutoKickConfig, config); return; } + const config = configs[0]; + config.UpdateBasicDetails(roleId, kickTime, noticeTime, noticeChannelId); await config.Save(AutoKickConfig, config); } - public static async ResetSetting(serverId: string, roleId: string) { - const config = await AutoKickConfig.FetchOneByServerIdAndRoleId(serverId, roleId); + public static async UnsetSetting(serverId: string) { + const configs = await AutoKickConfig.FetchAllByServerId(serverId); - if (!config) return; - - await AutoKickConfig.Remove(AutoKickConfig, config); + for (let config of configs) { + await AutoKickConfig.Remove(AutoKickConfig, config); + } } } diff --git a/src/registry.ts b/src/registry.ts index a2d7391..36dd021 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -4,6 +4,7 @@ import { EventType } from "./constants/EventType"; // Command Imports import About from "./commands/about"; import Audits from "./commands/audits"; +import Autokick from "./commands/autokick"; import Ban from "./commands/ban"; import Bunny from "./commands/bunny"; import Clear from "./commands/clear"; @@ -45,6 +46,7 @@ export default class Registry { public static RegisterCommands() { CoreClient.RegisterCommand("about", new About()); CoreClient.RegisterCommand("audits", new Audits()); + CoreClient.RegisterCommand("autokick", new Autokick()); CoreClient.RegisterCommand("ban", new Ban()); CoreClient.RegisterCommand("bunny", new Bunny()); CoreClient.RegisterCommand("clear", new Clear());