metadata for timeouts and better jobs #345 #346

This commit is contained in:
Axel Olausson Holtenäs 2022-05-30 20:00:08 +02:00
parent 44eadfc618
commit cea6be5740
No known key found for this signature in database
GPG key ID: 7BF6826B76382CBA
6 changed files with 124 additions and 1 deletions

View file

@ -1,5 +1,6 @@
# Custom Dictionary Words
Controlpanel
cooldown
cpgg
dagen
discordjs

View file

@ -5,7 +5,7 @@ import { CommandInteraction, MessageEmbed } from "discord.js";
import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
export default {
metadata: { guildOnly: false, ephemeral: false },
metadata: { guildOnly: false, ephemeral: false, cooldown: 5 },
builder: (command: SlashCommandSubcommandBuilder) => {
return command.setName("meme").setDescription("Get a meme from r/memes)");

View file

@ -7,6 +7,8 @@ import deferReply from "../../../helpers/deferReply";
import getEmbedConfig from "../../../helpers/getEmbedConfig";
import getCommandMetadata from "../../../helpers/getCommandMetadata";
import capitalizeFirstLetter from "../../../helpers/capitalizeFirstLetter";
import timeoutSchema from "../../../models/timeout";
import addSeconds from "../../../helpers/addSeconds";
export default async (interaction: CommandInteraction) => {
if (!interaction.isCommand()) return;
@ -45,6 +47,77 @@ export default async (interaction: CommandInteraction) => {
});
}
if (metadata.cooldown) {
console.log("cooldown");
// console.log(interaction);
// Check if user has a timeout
const isTimeout = await timeoutSchema.findOne({
guildId: guild.id,
userId: user.id,
cooldown: metadata.cooldown,
timeoutId: interaction.commandId,
});
// If user is not on timeout
if (isTimeout) {
logger?.silly(`User is on timeout`);
const { guildId, userId, timeoutId, cooldown, createdAt } = isTimeout;
const overDue = (await addSeconds(cooldown, createdAt)) < new Date();
if (overDue) {
timeoutSchema
.deleteOne({
guildId,
userId,
timeoutId,
cooldown,
})
.then(async () => {
logger.debug(
`Timeout document ${timeoutId} has been deleted from user ${userId}.`
);
});
} else {
const diff = Math.round(
((new Date(isTimeout.createdAt).getTime() - new Date().getTime()) *
-1) /
1000
);
return interaction?.editReply({
embeds: [
{
title: `[:x:] ${capitalizeFirstLetter(
interaction.options.getSubcommand()
)}`,
description: `
You are currently on timeout, please wait ${diff} seconds.
If it still doesn't work, please wait for a maximum of **1 hour** before contacting bot owner.
`,
timestamp: new Date(),
color: errorColor,
footer: {
iconURL: footerIcon,
text: footerText,
},
},
],
});
}
}
await timeoutSchema.create({
guildId: guild.id,
userId: user.id,
cooldown: metadata.cooldown,
timeoutId: interaction.commandId,
});
}
if (metadata.guildOnly) {
if (!guild) {
logger.debug(`Guild is null`);

View file

@ -0,0 +1,5 @@
export default async (numOfSeconds: number, date = new Date()) => {
date.setSeconds(date.getSeconds() + numOfSeconds);
return date;
};

35
src/jobs/timeouts.ts Normal file
View file

@ -0,0 +1,35 @@
import logger from "../logger";
import timeoutSchema from "../models/timeout";
import addSeconds from "../helpers/addSeconds";
export const options = {
schedule: "*/30 * * * *", // https://crontab.guru/
};
export const execute = async () => {
const timeouts = await timeoutSchema.find();
await Promise.all(
timeouts.map(async (timeout) => {
const { guildId, userId, timeoutId, cooldown, createdAt } = timeout;
const overDue = (await addSeconds(cooldown, createdAt)) < new Date();
if (overDue) {
timeoutSchema
.deleteOne({
guildId,
userId,
timeoutId,
cooldown,
})
.then(async () => {
logger.debug(
`Timeout document ${timeoutId} has been deleted from user ${userId}.`
);
});
}
})
);
};

View file

@ -4,7 +4,10 @@ import { Schema, model } from "mongoose";
export interface ITimeout {
userId: Snowflake;
guildId: Snowflake;
cooldown: number;
timeoutId: string;
createdAt: Date;
updatedAt: Date;
}
const timeoutSchema = new Schema<ITimeout>(
@ -21,6 +24,12 @@ const timeoutSchema = new Schema<ITimeout>(
unique: false,
index: true,
},
cooldown: {
type: Number,
required: true,
unique: false,
index: true,
},
timeoutId: { type: String },
},
{ timestamps: true }