Create timeout command #302

Merged
Vylpes merged 16 commits from feature/98-timeout-command into develop 2023-06-16 18:01:46 +01:00
3 changed files with 113 additions and 18 deletions
Showing only changes of commit 18f3bd746b - Show all commits

View file

@ -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 ]});
}
}

View file

@ -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;
}
}

View file

@ -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()}`);
client.start();