Create autokick command
All checks were successful
Test / build (push) Successful in 7s

This commit is contained in:
Ethan Lane 2024-12-06 20:54:47 +00:00
parent 2b4ae91584
commit 8d1befcf25
4 changed files with 97 additions and 13 deletions

View file

@ -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;
}
private async unset(interaction: CommandInteraction) {
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: ChatInputCommandInteraction) {
if (!interaction.guildId) return;
await AutoKickHelper.UnsetSetting(interaction.guildId);
await interaction.reply("Unset the auto kick configuration for this server");
}
}

View file

@ -47,4 +47,15 @@ export default class AutoKickConfig extends BaseEntity {
return query;
}
public static async FetchAllByServerId(serverId: string): Promise<AutoKickConfig[]> {
const repository = AppDataSource.getRepository(AutoKickConfig);
const query = repository
.createQueryBuilder("config")
.where("config.serverId = :serverId", { serverId })
.getMany();
return query;
}
}

View file

@ -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<AutoKickConfig | null> {
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);
if (!config) return;
public static async UnsetSetting(serverId: string) {
const configs = await AutoKickConfig.FetchAllByServerId(serverId);
for (let config of configs) {
await AutoKickConfig.Remove(AutoKickConfig, config);
}
}
}

View file

@ -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());