From 18f3bd746b03a93b8b0b4e4f3c66927671083e00 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 24 Mar 2023 17:51:20 +0000 Subject: [PATCH] Base timeout command --- src/commands/timeout.ts | 44 +++++++++++++++++--- src/helpers/TimeLengthInput.ts | 74 +++++++++++++++++++++++++++++++++- src/vylbot.ts | 13 +----- 3 files changed, 113 insertions(+), 18 deletions(-) diff --git a/src/commands/timeout.ts b/src/commands/timeout.ts index f9073aa..98a4ddb 100644 --- a/src/commands/timeout.ts +++ b/src/commands/timeout.ts @@ -1,5 +1,8 @@ -import { CacheType, CommandInteraction, EmbedBuilder, GuildMember, PermissionsBitField, SlashCommandBuilder } from "discord.js"; +import { CacheType, CommandInteraction, EmbedBuilder, GuildMember, PermissionsBitField, SlashCommandBuilder, TextChannel } from "discord.js"; +import { AuditType } from "../constants/AuditType"; import EmbedColours from "../constants/EmbedColours"; +import Audit from "../entity/Audit"; +import SettingsHelper from "../helpers/SettingsHelper"; import TimeLengthInput from "../helpers/TimeLengthInput"; import { Command } from "../type/command"; @@ -19,7 +22,8 @@ export default class Timeout extends Command { .addStringOption(option => option .setName("length") - .setDescription("How long to timeout for? (Example: 24h, 60m)")) + .setDescription("How long to timeout for? (Example: 24h, 60m)") + .setRequired(true)) .addStringOption(option => option .setName('reason') @@ -46,6 +50,8 @@ export default class Timeout extends Command { const targetMember = targetUser.member as GuildMember; const reason = reasonInput && reasonInput.value ? reasonInput.value.toString() : null; + const timeLength = new TimeLengthInput(lengthInput.value.toString()); + const logEmbed = new EmbedBuilder() .setColor(EmbedColours.Ok) .setTitle("Member Timed Out") @@ -59,12 +65,40 @@ export default class Timeout extends Command { name: "Reason", value: reason || "*none*", }, + { + name: "Length", + value: timeLength.GetLengthShort(), + }, { name: "Until", - value: "TODO", - } + value: timeLength.GetDateFromNow().toString(), + }, ]); - const timeLength = new TimeLengthInput(lengthInput.value.toString()); + // if (!targetMember.manageable) { + // await interaction.reply('Insufficient bot permissions. Please contact a moderator.'); + // return; + // } + + // await targetMember.timeout(timeLength.GetMilliseconds(), reason || ""); + + 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.Timeout, reason || "*none*", interaction.user.id, interaction.guildId); + await audit.Save(Audit, audit); + + const resultEmbed = new EmbedBuilder() + .setColor(EmbedColours.Ok) + .setDescription("User successfully timed out"); + + await interaction.reply({ embeds: [ resultEmbed ]}); } } \ No newline at end of file diff --git a/src/helpers/TimeLengthInput.ts b/src/helpers/TimeLengthInput.ts index 4272a70..7c167d2 100644 --- a/src/helpers/TimeLengthInput.ts +++ b/src/helpers/TimeLengthInput.ts @@ -21,6 +21,22 @@ export default class TimeLengthInput { return this.GetValue('s'); } + public GetMilliseconds(): number { + const days = this.GetDays(); + const hours = this.GetHours(); + const minutes = this.GetMinutes(); + const seconds = this.GetSeconds(); + + let milliseconds = 0; + + milliseconds += seconds * 1000; + milliseconds += minutes * 60 * 1000; + milliseconds += hours * 60 * 60 * 1000; + milliseconds += days * 24 * 60 * 60 * 1000; + + return milliseconds; + } + public GetDateFromNow(): Date { const now = Date.now(); @@ -33,15 +49,71 @@ export default class TimeLengthInput { return new Date(dateFromNow); } + public GetLength(): string { + const days = this.GetDays(); + const hours = this.GetHours(); + const minutes = this.GetMinutes(); + const seconds = this.GetSeconds(); + + const value = []; + + if (days) { + value.push(`${days} days`); + } + + if (hours) { + value.push(`${hours} hours`); + } + + if (minutes) { + value.push(`${minutes} minutes`); + } + + if (seconds) { + value.push(`${seconds} seconds`); + } + + return value.join(", "); + } + + public GetLengthShort(): string { + const days = this.GetDays(); + const hours = this.GetHours(); + const minutes = this.GetMinutes(); + const seconds = this.GetSeconds(); + + const value = []; + + if (days) { + value.push(`${days}d`); + } + + if (hours) { + value.push(`${hours}h`); + } + + if (minutes) { + value.push(`${minutes}m`); + } + + if (seconds) { + value.push(`${seconds}s`); + } + + return value.join(" "); + } + private GetValue(designation: string): number { const valueSplit = this.value.split(' '); - const desString = valueSplit.find(x => x.charAt(x.length) == designation); + const desString = valueSplit.find(x => x.charAt(x.length - 1) == designation); if (!desString) return 0; const desNumber = Number(desString.substring(0, desString.length - 1)); + if (!desNumber) return 0; + return desNumber; } } \ No newline at end of file diff --git a/src/vylbot.ts b/src/vylbot.ts index e11917d..16589c2 100644 --- a/src/vylbot.ts +++ b/src/vylbot.ts @@ -2,7 +2,6 @@ import { CoreClient } from "./client/client"; import * as dotenv from "dotenv"; import registry from "./registry"; import { IntentsBitField } from "discord.js"; -import TimeLengthInput from "./helpers/TimeLengthInput"; dotenv.config(); @@ -30,14 +29,4 @@ const client = new CoreClient([ registry.RegisterCommands(); registry.RegisterEvents(); -client.start(); - -const val = ''; - -const timeLength = new TimeLengthInput(val); - -console.log(`Seconds: ${timeLength.GetSeconds()}`); -console.log(`Minutes: ${timeLength.GetMinutes()}`); -console.log(`Hours: ${timeLength.GetHours()}`); -console.log(`Days: ${timeLength.GetDays()}`); -console.log(`From Now: ${timeLength.GetDateFromNow()}`); \ No newline at end of file +client.start(); \ No newline at end of file