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 EmbedColours from "../constants/EmbedColours";
import Audit from "../entity/Audit";
import SettingsHelper from "../helpers/SettingsHelper";
import TimeLengthInput from "../helpers/TimeLengthInput"; import TimeLengthInput from "../helpers/TimeLengthInput";
import { Command } from "../type/command"; import { Command } from "../type/command";
@ -19,7 +22,8 @@ export default class Timeout extends Command {
.addStringOption(option => .addStringOption(option =>
option option
.setName("length") .setName("length")
.setDescription("How long to timeout for? (Example: 24h, 60m)")) .setDescription("How long to timeout for? (Example: 24h, 60m)")
.setRequired(true))
.addStringOption(option => .addStringOption(option =>
option option
.setName('reason') .setName('reason')
@ -46,6 +50,8 @@ export default class Timeout extends Command {
const targetMember = targetUser.member as GuildMember; const targetMember = targetUser.member as GuildMember;
const reason = reasonInput && reasonInput.value ? reasonInput.value.toString() : null; const reason = reasonInput && reasonInput.value ? reasonInput.value.toString() : null;
const timeLength = new TimeLengthInput(lengthInput.value.toString());
const logEmbed = new EmbedBuilder() const logEmbed = new EmbedBuilder()
.setColor(EmbedColours.Ok) .setColor(EmbedColours.Ok)
.setTitle("Member Timed Out") .setTitle("Member Timed Out")
@ -59,12 +65,40 @@ export default class Timeout extends Command {
name: "Reason", name: "Reason",
value: reason || "*none*", value: reason || "*none*",
}, },
{
name: "Length",
value: timeLength.GetLengthShort(),
},
{ {
name: "Until", 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'); 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 { public GetDateFromNow(): Date {
const now = Date.now(); const now = Date.now();
@ -33,15 +49,71 @@ export default class TimeLengthInput {
return new Date(dateFromNow); 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 { private GetValue(designation: string): number {
const valueSplit = this.value.split(' '); 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; if (!desString) return 0;
const desNumber = Number(desString.substring(0, desString.length - 1)); const desNumber = Number(desString.substring(0, desString.length - 1));
if (!desNumber) return 0;
return desNumber; return desNumber;
} }
} }

View file

@ -2,7 +2,6 @@ import { CoreClient } from "./client/client";
import * as dotenv from "dotenv"; import * as dotenv from "dotenv";
import registry from "./registry"; import registry from "./registry";
import { IntentsBitField } from "discord.js"; import { IntentsBitField } from "discord.js";
import TimeLengthInput from "./helpers/TimeLengthInput";
dotenv.config(); dotenv.config();
@ -31,13 +30,3 @@ registry.RegisterCommands();
registry.RegisterEvents(); registry.RegisterEvents();
client.start(); 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()}`);