♻️ replaced * cooldown functions with 1 middleware
This commit is contained in:
parent
976e8e7fdc
commit
c8fb975623
10 changed files with 129 additions and 338 deletions
|
@ -2,11 +2,11 @@ import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
|
|||
import Chance from "chance";
|
||||
import { CommandInteraction } from "discord.js";
|
||||
|
||||
import { command as CooldownCommand } from "../../../../handlers/cooldown";
|
||||
import prisma from "../../../../handlers/database";
|
||||
import deferReply from "../../../../handlers/deferReply";
|
||||
import { success as BaseEmbedSuccess } from "../../../../helpers/baseEmbeds";
|
||||
import creditsGive from "../../../../helpers/credits/give";
|
||||
import cooldown from "../../../../middlewares/cooldown";
|
||||
import logger from "../../../../middlewares/logger";
|
||||
|
||||
// 1. Export a builder function.
|
||||
|
@ -20,7 +20,7 @@ export const execute = async (interaction: CommandInteraction) => {
|
|||
await deferReply(interaction, true);
|
||||
|
||||
// 2. Destructure interaction object.
|
||||
const { guild, user } = interaction;
|
||||
const { guild, user, commandId } = interaction;
|
||||
if (!guild) throw new Error("Guild not found");
|
||||
if (!user) throw new Error("User not found");
|
||||
|
||||
|
@ -44,7 +44,7 @@ export const execute = async (interaction: CommandInteraction) => {
|
|||
if (!createGuild) throw new Error("Guild not found");
|
||||
|
||||
// 6. Create a cooldown for the user.
|
||||
await CooldownCommand(interaction, createGuild.creditsWorkTimeout);
|
||||
await cooldown(guild, user, commandId, createGuild.creditsWorkTimeout);
|
||||
|
||||
// 6. Generate a random number between 0 and creditsWorkRate.
|
||||
const creditsEarned = chance.integer({
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
|
||||
import axios from "axios";
|
||||
import { CommandInteraction, EmbedBuilder } from "discord.js";
|
||||
import { command as CooldownCommand } from "../../../../handlers/cooldown";
|
||||
import deferReply from "../../../../handlers/deferReply";
|
||||
import getEmbedConfig from "../../../../helpers/getEmbedData";
|
||||
import cooldown from "../../../../middlewares/cooldown";
|
||||
|
||||
export default {
|
||||
builder: (command: SlashCommandSubcommandBuilder) => {
|
||||
|
@ -13,9 +13,11 @@ export default {
|
|||
execute: async (interaction: CommandInteraction) => {
|
||||
await deferReply(interaction, false);
|
||||
|
||||
await CooldownCommand(interaction, 15);
|
||||
const { guild, user, commandId } = interaction;
|
||||
if (!guild) throw new Error("Guild not found");
|
||||
if (!user) throw new Error("User not found");
|
||||
|
||||
const { guild } = interaction;
|
||||
await cooldown(guild, user, commandId, 15);
|
||||
|
||||
const embedConfig = await getEmbedConfig(guild);
|
||||
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
|
||||
import { ChatInputCommandInteraction, EmbedBuilder } from "discord.js";
|
||||
import { command as CooldownCommand } from "../../../../handlers/cooldown";
|
||||
import getEmbedConfig from "../../../../helpers/getEmbedData";
|
||||
import logger from "../../../../middlewares/logger";
|
||||
import noSelfReputation from "./components/noSelfReputation";
|
||||
|
||||
import prisma from "../../../../handlers/database";
|
||||
import deferReply from "../../../../handlers/deferReply";
|
||||
import cooldown from "../../../../middlewares/cooldown";
|
||||
|
||||
export default {
|
||||
builder: (command: SlashCommandSubcommandBuilder) => {
|
||||
|
@ -36,7 +36,7 @@ export default {
|
|||
execute: async (interaction: ChatInputCommandInteraction) => {
|
||||
await deferReply(interaction, true);
|
||||
|
||||
const { options, user, guild } = interaction;
|
||||
const { options, user, guild, commandId } = interaction;
|
||||
|
||||
const { successColor, footerText, footerIcon } = await getEmbedConfig(
|
||||
guild
|
||||
|
@ -52,8 +52,10 @@ export default {
|
|||
noSelfReputation(optionTarget, user);
|
||||
|
||||
// Check if user is on cooldown otherwise create one
|
||||
await CooldownCommand(
|
||||
interaction,
|
||||
await cooldown(
|
||||
guild,
|
||||
user,
|
||||
commandId,
|
||||
parseInt(process.env.REPUTATION_TIMEOUT)
|
||||
);
|
||||
|
||||
|
|
|
@ -1,35 +1,14 @@
|
|||
// Dependencies
|
||||
import { BaseInteraction } from "discord.js";
|
||||
import { button as CooldownButton } from "../../../../handlers/cooldown";
|
||||
import deferReply from "../../../../handlers/deferReply";
|
||||
|
||||
export default async (interaction: BaseInteraction) => {
|
||||
if (!interaction.isButton()) return;
|
||||
|
||||
const { guild, customId, memberPermissions } = interaction;
|
||||
const { customId } = interaction;
|
||||
|
||||
const currentButton = await import(`../../../buttons/${customId}`);
|
||||
|
||||
if (!currentButton) throw new Error(`Unknown button ${customId}`);
|
||||
|
||||
const metadata = currentButton.metadata;
|
||||
|
||||
await deferReply(interaction, metadata.ephemeral || false);
|
||||
|
||||
if (metadata.guildOnly && !guild)
|
||||
throw new Error("This command is guild only.");
|
||||
|
||||
if (
|
||||
metadata.permissions &&
|
||||
metadata.guildOnly &&
|
||||
!memberPermissions?.has(metadata.permissions)
|
||||
)
|
||||
throw new Error("You don't have the required permissions");
|
||||
|
||||
if (metadata.dmOnly && guild)
|
||||
throw new Error("This command is only available in DM");
|
||||
|
||||
if (metadata.cooldown) await CooldownButton(interaction, metadata.cooldown);
|
||||
|
||||
await currentButton.execute(interaction);
|
||||
};
|
||||
|
|
|
@ -8,24 +8,5 @@ export default async (interaction: ChatInputCommandInteraction) => {
|
|||
const currentCommand = client.commands.get(commandName);
|
||||
if (!currentCommand) throw new Error(`Unknown command ${commandName}`);
|
||||
|
||||
// const metadata = await getCommandMetadata(interaction, currentCommand);
|
||||
// await deferReply(interaction, metadata.ephemeral || false);
|
||||
|
||||
// if (metadata.guildOnly && !interaction.guild)
|
||||
// throw new Error("This command is guild only.");
|
||||
|
||||
// if (metadata.dmOnly && interaction.guild)
|
||||
// throw new Error("This command is only available in DM");
|
||||
|
||||
// if (
|
||||
// metadata.permissions &&
|
||||
// metadata.guildOnly &&
|
||||
// !interaction.memberPermissions?.has(metadata.permissions)
|
||||
// )
|
||||
// throw new Error("You don't have the required permissions");
|
||||
|
||||
// if (metadata.cooldown) {
|
||||
// await CooldownCommand(interaction, metadata.cooldown);
|
||||
// }
|
||||
await currentCommand.execute(interaction);
|
||||
};
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { ChannelType, Message } from "discord.js";
|
||||
import { message as CooldownMessage } from "../../../../handlers/cooldown";
|
||||
import prisma from "../../../../handlers/database";
|
||||
import creditsGive from "../../../../helpers/credits/give";
|
||||
import cooldown from "../../../../middlewares/cooldown";
|
||||
import logger from "../../../../middlewares/logger";
|
||||
|
||||
export default {
|
||||
|
@ -52,12 +52,13 @@ export default {
|
|||
|
||||
if (content.length < createGuildMember.guild.creditsMinimumLength) return;
|
||||
|
||||
const isOnCooldown = await CooldownMessage(
|
||||
message,
|
||||
await cooldown(
|
||||
guild,
|
||||
author,
|
||||
"event-messageCreate-credits",
|
||||
createGuildMember.guild.creditsTimeout,
|
||||
"messageCreate-credits"
|
||||
true
|
||||
);
|
||||
if (isOnCooldown) return;
|
||||
|
||||
await creditsGive(guild, author, createGuildMember.guild.creditsRate);
|
||||
},
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { ChannelType, Message } from "discord.js";
|
||||
import { message as CooldownMessage } from "../../../../handlers/cooldown";
|
||||
import prisma from "../../../../handlers/database";
|
||||
import cooldown from "../../../../middlewares/cooldown";
|
||||
import logger from "../../../../middlewares/logger";
|
||||
|
||||
export default {
|
||||
|
@ -51,12 +51,13 @@ export default {
|
|||
|
||||
if (content.length < createGuildMember.guild.pointsMinimumLength) return;
|
||||
|
||||
const isOnCooldown = await CooldownMessage(
|
||||
message,
|
||||
await cooldown(
|
||||
guild,
|
||||
author,
|
||||
"event-messageCreate-points",
|
||||
createGuildMember.guild.pointsTimeout,
|
||||
"messageCreate-points"
|
||||
true
|
||||
);
|
||||
if (isOnCooldown) return;
|
||||
|
||||
const updateGuildMember = await prisma.guildMember.update({
|
||||
where: {
|
||||
|
|
|
@ -1,277 +0,0 @@
|
|||
// Dependencies
|
||||
import { ButtonInteraction, CommandInteraction, Message } from "discord.js";
|
||||
import addSeconds from "../../helpers/addSeconds";
|
||||
import logger from "../../middlewares/logger";
|
||||
import prisma from "../database";
|
||||
|
||||
// Command cooldown
|
||||
export const command = async (i: CommandInteraction, cooldown: number) => {
|
||||
const { guild, user, commandId } = i;
|
||||
|
||||
if (!guild) throw new Error("Guild not found");
|
||||
|
||||
// Check if user has a timeout
|
||||
const hasTimeout = await prisma.cooldown.findUnique({
|
||||
where: {
|
||||
guildId_userId_timeoutId: {
|
||||
guildId: guild.id,
|
||||
userId: user.id,
|
||||
timeoutId: commandId,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
logger.silly(hasTimeout);
|
||||
|
||||
// If user is not on timeout
|
||||
if (hasTimeout) {
|
||||
const { userId, timeoutId, createdAt } = hasTimeout;
|
||||
const overDue = addSeconds(cooldown, createdAt) < new Date();
|
||||
|
||||
if (!overDue) {
|
||||
const diff = Math.round(
|
||||
(new Date(hasTimeout.createdAt).getTime() - new Date().getTime()) / 1000
|
||||
);
|
||||
|
||||
throw new Error(
|
||||
`You must wait ${diff} seconds before using this command.`
|
||||
);
|
||||
}
|
||||
|
||||
// Delete timeout
|
||||
const deleteCooldown = await prisma.cooldown.delete({
|
||||
where: {
|
||||
guildId_userId_timeoutId: {
|
||||
guildId: guild.id,
|
||||
userId: user.id,
|
||||
timeoutId: commandId,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
logger.silly(deleteCooldown);
|
||||
|
||||
logger.debug(
|
||||
`Timeout document ${timeoutId} has been deleted from user ${userId}.`
|
||||
);
|
||||
}
|
||||
|
||||
// Create timeout
|
||||
const createCooldown = await prisma.cooldown.upsert({
|
||||
where: {
|
||||
guildId_userId_timeoutId: {
|
||||
userId: user.id,
|
||||
guildId: guild.id,
|
||||
timeoutId: commandId,
|
||||
},
|
||||
},
|
||||
update: {},
|
||||
create: {
|
||||
guild: {
|
||||
connectOrCreate: {
|
||||
create: {
|
||||
id: guild.id,
|
||||
},
|
||||
where: {
|
||||
id: guild.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
user: {
|
||||
connectOrCreate: {
|
||||
create: {
|
||||
id: user.id,
|
||||
},
|
||||
where: {
|
||||
id: user.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
timeoutId: commandId,
|
||||
cooldown,
|
||||
},
|
||||
});
|
||||
|
||||
logger.silly(createCooldown);
|
||||
};
|
||||
|
||||
// Button cooldown
|
||||
export const button = async (i: ButtonInteraction, cooldown: number) => {
|
||||
const { guild, user, customId } = i;
|
||||
|
||||
if (!guild) throw new Error("Guild not found");
|
||||
|
||||
// Check if user has a timeout
|
||||
const hasTimeout = await prisma.cooldown.findUnique({
|
||||
where: {
|
||||
guildId_userId_timeoutId: {
|
||||
guildId: guild.id,
|
||||
userId: user.id,
|
||||
timeoutId: customId,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
logger.silly(hasTimeout);
|
||||
|
||||
// If user is not on timeout
|
||||
if (hasTimeout) {
|
||||
const { userId, timeoutId, createdAt } = hasTimeout;
|
||||
const overDue = addSeconds(cooldown, createdAt) < new Date();
|
||||
|
||||
if (!overDue) {
|
||||
const diff = Math.round(
|
||||
(new Date(hasTimeout.createdAt).getTime() - new Date().getTime()) / 1000
|
||||
);
|
||||
|
||||
throw new Error(
|
||||
`You must wait ${diff} seconds before using this command.`
|
||||
);
|
||||
}
|
||||
|
||||
// Delete timeout
|
||||
const deleteCooldown = await prisma.cooldown.delete({
|
||||
where: {
|
||||
guildId_userId_timeoutId: {
|
||||
guildId: guild.id,
|
||||
userId: user.id,
|
||||
timeoutId: customId,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
logger.silly(deleteCooldown);
|
||||
|
||||
logger.debug(
|
||||
`Timeout document ${timeoutId} has been deleted from user ${userId}.`
|
||||
);
|
||||
}
|
||||
|
||||
// Create timeout
|
||||
const createCooldown = await prisma.cooldown.upsert({
|
||||
where: {
|
||||
guildId_userId_timeoutId: {
|
||||
userId: user.id,
|
||||
guildId: guild.id,
|
||||
timeoutId: customId,
|
||||
},
|
||||
},
|
||||
update: {},
|
||||
create: {
|
||||
guild: {
|
||||
connectOrCreate: {
|
||||
create: {
|
||||
id: guild.id,
|
||||
},
|
||||
where: {
|
||||
id: guild.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
user: {
|
||||
connectOrCreate: {
|
||||
create: {
|
||||
id: user.id,
|
||||
},
|
||||
where: {
|
||||
id: user.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
timeoutId: customId,
|
||||
cooldown,
|
||||
},
|
||||
});
|
||||
|
||||
logger.silly(createCooldown);
|
||||
};
|
||||
|
||||
// Message cooldown
|
||||
export const message = async (msg: Message, cooldown: number, id: string) => {
|
||||
const { guild, member } = msg;
|
||||
|
||||
if (!guild) throw new Error("Guild not found");
|
||||
if (!member) throw new Error("Member is undefined");
|
||||
|
||||
// Check if user has a timeout
|
||||
const hasTimeout = await prisma.cooldown.findUnique({
|
||||
where: {
|
||||
guildId_userId_timeoutId: {
|
||||
guildId: guild.id,
|
||||
userId: member.id,
|
||||
timeoutId: id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
logger.silly(hasTimeout);
|
||||
|
||||
// If user is not on timeout
|
||||
if (hasTimeout) {
|
||||
const { userId, timeoutId, createdAt } = hasTimeout;
|
||||
const overDue = addSeconds(cooldown, createdAt) < new Date();
|
||||
|
||||
if (!overDue) {
|
||||
const diff = Math.round(
|
||||
(new Date(hasTimeout.createdAt).getTime() - new Date().getTime()) / 1000
|
||||
);
|
||||
|
||||
return `User: ${userId} on timeout-id: ${id} with cooldown: ${cooldown} secs with remaining: ${diff} secs.`;
|
||||
}
|
||||
|
||||
// Delete timeout
|
||||
const deleteCooldown = await prisma.cooldown.delete({
|
||||
where: {
|
||||
guildId_userId_timeoutId: {
|
||||
guildId: guild.id,
|
||||
userId: member.id,
|
||||
timeoutId: id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
logger.silly(deleteCooldown);
|
||||
|
||||
logger.debug(
|
||||
`Timeout document ${timeoutId} has been deleted from user ${userId}.`
|
||||
);
|
||||
}
|
||||
|
||||
// Create timeout
|
||||
const createCooldown = await prisma.cooldown.upsert({
|
||||
where: {
|
||||
guildId_userId_timeoutId: {
|
||||
userId: member.id,
|
||||
guildId: guild.id,
|
||||
timeoutId: id,
|
||||
},
|
||||
},
|
||||
update: {},
|
||||
create: {
|
||||
guild: {
|
||||
connectOrCreate: {
|
||||
create: {
|
||||
id: guild.id,
|
||||
},
|
||||
where: {
|
||||
id: guild.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
user: {
|
||||
connectOrCreate: {
|
||||
create: {
|
||||
id: member.id,
|
||||
},
|
||||
where: {
|
||||
id: member.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
timeoutId: id,
|
||||
cooldown,
|
||||
},
|
||||
});
|
||||
|
||||
logger.silly(createCooldown);
|
||||
};
|
|
@ -1,5 +1,6 @@
|
|||
import { Client, Collection, GatewayIntentBits } from "discord.js"; // discord.js
|
||||
import "dotenv/config";
|
||||
|
||||
import { register as commandRegister } from "./handlers/command";
|
||||
import { register as eventRegister } from "./handlers/event";
|
||||
import { start as scheduleStart } from "./handlers/schedule";
|
||||
|
|
101
src/middlewares/cooldown/index.ts
Normal file
101
src/middlewares/cooldown/index.ts
Normal file
|
@ -0,0 +1,101 @@
|
|||
import { Guild, User } from "discord.js";
|
||||
import prisma from "../../handlers/database";
|
||||
import addSeconds from "../../helpers/addSeconds";
|
||||
import logger from "../logger";
|
||||
|
||||
export default async (
|
||||
guild: Guild,
|
||||
user: User,
|
||||
id: string,
|
||||
cooldown: number,
|
||||
silent?: boolean
|
||||
) => {
|
||||
// Check if user has a timeout
|
||||
const hasTimeout = await prisma.cooldown.findUnique({
|
||||
where: {
|
||||
guildId_userId_timeoutId: {
|
||||
guildId: guild.id,
|
||||
userId: user.id,
|
||||
timeoutId: id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
logger.silly(hasTimeout);
|
||||
|
||||
// If user is not on timeout
|
||||
if (hasTimeout) {
|
||||
const { userId, timeoutId, createdAt } = hasTimeout;
|
||||
const overDue = addSeconds(cooldown, createdAt) < new Date();
|
||||
|
||||
if (!overDue) {
|
||||
const diff = Math.round(
|
||||
(new Date(hasTimeout.createdAt).getTime() - new Date().getTime()) / 1000
|
||||
);
|
||||
|
||||
if (silent)
|
||||
return logger.verbose(
|
||||
`User ${userId} is on cooldown for ${timeoutId} for ${diff} seconds`
|
||||
);
|
||||
|
||||
throw new Error(
|
||||
`You must wait ${diff} seconds before using this command.`
|
||||
);
|
||||
}
|
||||
|
||||
// Delete timeout
|
||||
const deleteCooldown = await prisma.cooldown.delete({
|
||||
where: {
|
||||
guildId_userId_timeoutId: {
|
||||
guildId: guild.id,
|
||||
userId: user.id,
|
||||
timeoutId: id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
logger.silly(deleteCooldown);
|
||||
|
||||
logger.debug(
|
||||
`Timeout document ${timeoutId} has been deleted from user ${userId}.`
|
||||
);
|
||||
}
|
||||
|
||||
// Create timeout
|
||||
const createCooldown = await prisma.cooldown.upsert({
|
||||
where: {
|
||||
guildId_userId_timeoutId: {
|
||||
userId: user.id,
|
||||
guildId: guild.id,
|
||||
timeoutId: id,
|
||||
},
|
||||
},
|
||||
update: {},
|
||||
create: {
|
||||
guild: {
|
||||
connectOrCreate: {
|
||||
create: {
|
||||
id: guild.id,
|
||||
},
|
||||
where: {
|
||||
id: guild.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
user: {
|
||||
connectOrCreate: {
|
||||
create: {
|
||||
id: user.id,
|
||||
},
|
||||
where: {
|
||||
id: user.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
timeoutId: id,
|
||||
cooldown,
|
||||
},
|
||||
});
|
||||
|
||||
logger.silly(createCooldown);
|
||||
};
|
Loading…
Add table
Reference in a new issue