From 6eca2fe0a641f2e02c8ecb00daac45bfbf19f99b Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Wed, 21 Dec 2022 07:40:34 +0100 Subject: [PATCH 1/6] Update docker-image.yml --- .github/workflows/docker-image.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 1bfb931..830f727 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -8,9 +8,6 @@ on: - "**" tags: - "v*.*.*" - pull_request: - branches: - - "**" jobs: docker: From 01cd5643ebb70ef506fef9179c19b81419593e26 Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Tue, 20 Dec 2022 09:10:47 +0100 Subject: [PATCH 2/6] refactor: :technologist: refactor config audits Refactored and improved UX for the /config audits command --- .../config/subcommands/audits/index.ts | 68 ++++++++----------- 1 file changed, 29 insertions(+), 39 deletions(-) diff --git a/src/commands/config/subcommands/audits/index.ts b/src/commands/config/subcommands/audits/index.ts index c1c551d..1a93c30 100644 --- a/src/commands/config/subcommands/audits/index.ts +++ b/src/commands/config/subcommands/audits/index.ts @@ -1,30 +1,27 @@ import { + channelMention, ChannelType, ChatInputCommandInteraction, - EmbedBuilder, PermissionsBitField, SlashCommandSubcommandBuilder, } from "discord.js"; import prisma from "../../../../handlers/database"; import deferReply from "../../../../handlers/deferReply"; +import { success as embedSuccess } from "../../../../helpers/baseEmbeds"; import checkPermission from "../../../../helpers/checkPermission"; -import getEmbedConfig from "../../../../helpers/getEmbedData"; import logger from "../../../../middlewares/logger"; export const builder = (command: SlashCommandSubcommandBuilder) => { return command .setName("audits") - .setDescription("Audits") + .setDescription("Configure audits module") .addBooleanOption((option) => - option - .setName("status") - .setDescription("Should audits be enabled?") - .setRequired(true) + option.setName("status").setDescription("Module status").setRequired(true) ) .addChannelOption((option) => option .setName("channel") - .setDescription("Channel for audit messages.") + .setDescription("Log channel") .addChannelTypes(ChannelType.GuildText) .setRequired(true) ); @@ -36,15 +33,14 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { checkPermission(interaction, PermissionsBitField.Flags.ManageGuild); const { guild, options } = interaction; - const { successColor, footerText, footerIcon } = await getEmbedConfig(guild); + if (!guild) throw new Error("Guild unavailable"); + const status = options.getBoolean("status"); const channel = options.getChannel("channel"); + if (status === null) throw new Error("Status must be set"); + if (!channel) throw new Error("Channel unavailable"); - if (!guild) throw new Error("Guild not found."); - if (!channel) throw new Error("Channel not found."); - if (status === null) throw new Error("Status not found."); - - const createGuild = await prisma.guildConfigAudits.upsert({ + const upsertGuildConfigAudits = await prisma.guildConfigAudits.upsert({ where: { id: guild.id, }, @@ -59,34 +55,28 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { }, }); - logger.silly(createGuild); + logger.silly(upsertGuildConfigAudits); - const embedSuccess = new EmbedBuilder() - .setTitle("[:hammer:] Audits") - .setDescription("Guild configuration updated successfully.") - .setColor(successColor) - .addFields( - { - name: "🤖 Status", - value: `${ - createGuild.status ? ":white_check_mark: Enabled" : ":x: Disabled" - }`, - inline: true, - }, - { - name: "🌊 Channel", - value: `<#${createGuild.channelId}>`, - inline: true, - } - ) - .setTimestamp() - .setFooter({ - iconURL: footerIcon, - text: footerText, - }); + const successEmbed = await embedSuccess( + guild, + ":gear:︱Configuration of Audits" + ); + + successEmbed.setDescription("Configuration updated successfully!").addFields( + { + name: "Status", + value: `${upsertGuildConfigAudits.status ? "Enabled" : "Disabled"}`, + inline: true, + }, + { + name: "Channel", + value: `${channelMention(upsertGuildConfigAudits.channelId)}`, + inline: true, + } + ); await interaction.editReply({ - embeds: [embedSuccess], + embeds: [successEmbed], }); return; }; From 147a5efdd951bc54ed053a0e6f86fa9800974c7d Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Tue, 20 Dec 2022 09:20:30 +0100 Subject: [PATCH 3/6] refactor: :technologist: refactor config cpgg Refactored and improved UX for /config cpgg --- src/commands/config/subcommands/cpgg/index.ts | 80 +++++++++---------- 1 file changed, 39 insertions(+), 41 deletions(-) diff --git a/src/commands/config/subcommands/cpgg/index.ts b/src/commands/config/subcommands/cpgg/index.ts index 286c77c..edda2c8 100644 --- a/src/commands/config/subcommands/cpgg/index.ts +++ b/src/commands/config/subcommands/cpgg/index.ts @@ -1,24 +1,23 @@ import { ChatInputCommandInteraction, - EmbedBuilder, PermissionsBitField, SlashCommandSubcommandBuilder, } from "discord.js"; import prisma from "../../../../handlers/database"; import deferReply from "../../../../handlers/deferReply"; +import { success as embedSuccess } from "../../../../helpers/baseEmbeds"; import checkPermission from "../../../../helpers/checkPermission"; import encryption from "../../../../helpers/encryption"; -import getEmbedConfig from "../../../../helpers/getEmbedData"; import logger from "../../../../middlewares/logger"; export const builder = (command: SlashCommandSubcommandBuilder) => { return command .setName("cpgg") - .setDescription("Controlpanel.gg") + .setDescription("Controlpanel.gg API") .addStringOption((option) => option .setName("scheme") - .setDescription(`Controlpanel.gg Scheme`) + .setDescription(`API protocol`) .setRequired(true) .setChoices( { name: "HTTPS (secure)", value: "https" }, @@ -26,16 +25,10 @@ export const builder = (command: SlashCommandSubcommandBuilder) => { ) ) .addStringOption((option) => - option - .setName("domain") - .setDescription(`Controlpanel.gg Domain`) - .setRequired(true) + option.setName("domain").setDescription(`API domain`).setRequired(true) ) .addStringOption((option) => - option - .setName("token") - .setDescription(`Controlpanel.gg Application API`) - .setRequired(true) + option.setName("token").setDescription(`API Token`).setRequired(true) ); }; @@ -44,22 +37,22 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { checkPermission(interaction, PermissionsBitField.Flags.ManageGuild); - const { successColor, footerText, footerIcon } = await getEmbedConfig( - interaction.guild - ); const { options, guild } = interaction; + if (!guild) throw new Error("Guild unavailable"); - const tokenData = options.getString("token"); const scheme = options.getString("scheme"); const domain = options.getString("domain"); - const token = tokenData && encryption.encrypt(tokenData); - const url = scheme && domain && encryption.encrypt(`${scheme}://${domain}`); + const tokenData = options.getString("token"); + if (!scheme) throw new Error("Scheme must be set"); + if (!domain) throw new Error("Domain must be set"); + if (!tokenData) throw new Error("Token must be set"); - if (!guild) throw new Error("No guild found"); - if (!token) throw new Error("Token not found"); - if (!url) throw new Error("URL not found"); + const url = encryption.encrypt(`${scheme}://${domain}`); + const token = encryption.encrypt(tokenData); + if (!url) throw new Error("URL must be set"); + if (!token) throw new Error("Token must be set"); - const createGuild = await prisma.guildConfigApisCpgg.upsert({ + const upsertGuildConfigApisCpgg = await prisma.guildConfigApisCpgg.upsert({ where: { id: guild.id, }, @@ -78,28 +71,33 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { }, }); - logger.silly(createGuild); + logger.silly(upsertGuildConfigApisCpgg); - logger?.silly(`Updated API credentials.`); + const successEmbed = await embedSuccess( + guild, + ":gear:︱Configuration of CPGG" + ); - const interactionEmbed = new EmbedBuilder() - .setTitle("[:tools:] CPGG") - .setDescription( - `The following configuration will be used. + successEmbed.setDescription("Configuration updated successfully!").addFields( + { + name: "Scheme", + value: `${scheme}`, + inline: true, + }, + { + name: "Domain", + value: `${domain}`, + inline: true, + }, + { + name: "Token", + value: `ends with ${tokenData.slice(-4)}`, + inline: true, + } + ); -**Scheme**: ${scheme} -**Domain**: ${domain} -**Token**: ends with ${tokenData?.slice(-4)}` - ) - .setColor(successColor) - .setTimestamp() - .setFooter({ - iconURL: footerIcon, - text: footerText, - }); - - await interaction?.editReply({ - embeds: [interactionEmbed], + await interaction.editReply({ + embeds: [successEmbed], }); return; }; From f9c6ad883633d662b412170c8792823de9a99bad Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Tue, 20 Dec 2022 09:32:03 +0100 Subject: [PATCH 4/6] refactor: :children_crossing: refactor config credits Refactored and improved UX for /config credits --- .../config/subcommands/credits/index.ts | 132 ++++++++---------- 1 file changed, 57 insertions(+), 75 deletions(-) diff --git a/src/commands/config/subcommands/credits/index.ts b/src/commands/config/subcommands/credits/index.ts index 8eeb1e9..1d81cac 100644 --- a/src/commands/config/subcommands/credits/index.ts +++ b/src/commands/config/subcommands/credits/index.ts @@ -1,61 +1,51 @@ import { ChatInputCommandInteraction, - EmbedBuilder, PermissionsBitField, SlashCommandSubcommandBuilder, } from "discord.js"; import prisma from "../../../../handlers/database"; import deferReply from "../../../../handlers/deferReply"; +import { success as embedSuccess } from "../../../../helpers/baseEmbeds"; import checkPermission from "../../../../helpers/checkPermission"; -import getEmbedConfig from "../../../../helpers/getEmbedData"; import logger from "../../../../middlewares/logger"; export const builder = (command: SlashCommandSubcommandBuilder) => { return command .setName("credits") - .setDescription(`Configure this guild's credits module.`) + .setDescription(`Configure credits module`) .addBooleanOption((option) => - option - .setName("enabled") - .setDescription("Do you want to activate the credit module?") - .setRequired(true) + option.setName("status").setDescription("Module Status").setRequired(true) ) .addNumberOption((option) => option .setName("rate") - .setDescription("Credit rate per message.") + .setDescription("Credits per message") .setRequired(true) .setMinValue(1) ) .addNumberOption((option) => option .setName("minimum-length") - .setDescription("Minimum message length to receive credit.") + .setDescription("Minimum length per message") .setRequired(true) ) .addNumberOption((option) => option .setName("work-rate") - .setDescription( - "The maximum amount of credit that can be obtained within a working day." - ) + .setDescription("Maximum credits per workshift") .setRequired(true) .setMinValue(1) ) .addNumberOption((option) => option .setName("work-timeout") - .setDescription( - "How long you need to wait before you can work again provided in seconds." - ) + .setDescription("Time between workshifts") .setRequired(true) ) .addNumberOption((option) => option .setName("timeout") - .setDescription( - "How long you need to wait before you can earn more credits." - ) + .setDescription("Time between messages") .setRequired(true) ); }; @@ -65,12 +55,9 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { checkPermission(interaction, PermissionsBitField.Flags.ManageGuild); - const { successColor, footerText, footerIcon } = await getEmbedConfig( - interaction.guild - ); const { guild, options } = interaction; - const enabled = options.getBoolean("enabled"); + const status = options.getBoolean("status"); const rate = options.getNumber("rate"); const timeout = options.getNumber("timeout"); const minimumLength = options.getNumber("minimum-length"); @@ -78,23 +65,22 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { const workTimeout = options.getNumber("work-timeout"); if (!guild) throw new Error("Guild not found."); - if (typeof enabled !== "boolean") - throw new Error("Enabled option is not a boolean."); - if (typeof rate !== "number") throw new Error("Rate is not a number."); + if (typeof status !== "boolean") throw new Error("Status must be an boolean"); + if (typeof rate !== "number") throw new Error("Rate must be a number"); if (typeof workRate !== "number") - throw new Error("Work rate is not a number."); + throw new Error("Work rate must be a number"); if (typeof workTimeout !== "number") - throw new Error("Work timeout is not a number."); - if (typeof timeout !== "number") throw new Error("Timeout is not a number."); + throw new Error("Work timeout must be a number"); + if (typeof timeout !== "number") throw new Error("Timeout must be a number"); if (typeof minimumLength !== "number") - throw new Error("Minimum length is not a number."); + throw new Error("Minimum length must be a number"); - const createGuild = await prisma.guildConfigCredits.upsert({ + const upsertGuildConfigCredits = await prisma.guildConfigCredits.upsert({ where: { id: guild.id, }, update: { - status: enabled, + status, rate, timeout, workRate, @@ -103,7 +89,7 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { }, create: { id: guild.id, - status: enabled, + status, rate, timeout, workRate, @@ -112,52 +98,48 @@ export const execute = async (interaction: ChatInputCommandInteraction) => { }, }); - logger.silly(createGuild); + logger.silly(upsertGuildConfigCredits); - const interactionEmbed = new EmbedBuilder() - .setTitle("[:tools:] Credits") - .setDescription("Credits settings updated") - .setColor(successColor) - .addFields( - { - name: "🤖 Enabled?", - value: `${createGuild.status}`, - inline: true, - }, - { - name: "📈 Rate", - value: `${createGuild.rate}`, - inline: true, - }, - { - name: "📈 Work Rate", - value: `${createGuild.workRate}`, - inline: true, - }, - { - name: "🔨 Minimum Length", - value: `${createGuild.minimumLength}`, - inline: true, - }, - { - name: "⏰ Timeout", - value: `${createGuild.timeout}`, - inline: true, - }, - { - name: "⏰ Work Timeout", - value: `${createGuild.workTimeout}`, - inline: true, - } - ) - .setTimestamp() - .setFooter({ - iconURL: footerIcon, - text: footerText, - }); + const successEmbed = await embedSuccess( + guild, + ":gear:︱Configuration of Credits" + ); + + successEmbed.setDescription("Configuration updated successfully!").addFields( + { + name: "Status", + value: `${upsertGuildConfigCredits.status ? "Enabled" : "Disabled"}`, + inline: true, + }, + { + name: "Rate", + value: `${upsertGuildConfigCredits.rate}`, + inline: true, + }, + { + name: "Work Rate", + value: `${upsertGuildConfigCredits.workRate}`, + inline: true, + }, + { + name: "Minimum Length", + value: `${upsertGuildConfigCredits.minimumLength}`, + inline: true, + }, + { + name: "Timeout", + value: `${upsertGuildConfigCredits.timeout}`, + inline: true, + }, + { + name: "Work Timeout", + value: `${upsertGuildConfigCredits.workTimeout}`, + inline: true, + } + ); await interaction.editReply({ - embeds: [interactionEmbed], + embeds: [successEmbed], }); return; }; From e8bfd0cc1577396c4798972da61f15d92199f92b Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Wed, 21 Dec 2022 07:38:54 +0100 Subject: [PATCH 5/6] refactor: :technologist: refactored interactionCreate Improved handling of interactions, using correct types --- .../handlers/button/index.ts | 39 ++++++++++++--- .../handlers/chatInputCommand/index.ts | 42 ++++++++++++++++ .../handlers/command/index.ts | 12 ----- .../interactionCreate/handlers/index.ts | 50 ------------------- src/events/interactionCreate/index.ts | 39 ++++++++------- 5 files changed, 97 insertions(+), 85 deletions(-) create mode 100644 src/events/interactionCreate/handlers/chatInputCommand/index.ts delete mode 100644 src/events/interactionCreate/handlers/command/index.ts delete mode 100644 src/events/interactionCreate/handlers/index.ts diff --git a/src/events/interactionCreate/handlers/button/index.ts b/src/events/interactionCreate/handlers/button/index.ts index e3e8281..092b5dd 100644 --- a/src/events/interactionCreate/handlers/button/index.ts +++ b/src/events/interactionCreate/handlers/button/index.ts @@ -1,14 +1,41 @@ -// Dependencies -import { BaseInteraction } from "discord.js"; - -export default async (interaction: BaseInteraction) => { - if (!interaction.isButton()) return; +import { + ActionRowBuilder, + ButtonBuilder, + ButtonInteraction, + ButtonStyle, + EmbedBuilder, +} from "discord.js"; +import getEmbedData from "../../../../helpers/getEmbedData"; +export default async (interaction: ButtonInteraction) => { + const { errorColor, footerText, footerIcon } = await getEmbedData( + interaction.guild + ); const { customId } = interaction; const currentButton = await import(`../../../buttons/${customId}`); if (!currentButton) throw new Error(`Unknown button ${customId}`); - await currentButton.execute(interaction); + await currentButton.execute(interaction).catch((error: Error) => { + const buttons = new ActionRowBuilder().addComponents( + new ButtonBuilder() + .setLabel("Report Problem") + .setStyle(ButtonStyle.Link) + .setEmoji("✏️") + .setURL("https://discord.zyner.org") + ); + + return interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setTitle(`:no_entry_sign:︱Your request failed`) + .setDescription(`${error.message}`) + .setColor(errorColor) + .setTimestamp(new Date()) + .setFooter({ text: footerText, iconURL: footerIcon }), + ], + components: [buttons], + }); + }); }; diff --git a/src/events/interactionCreate/handlers/chatInputCommand/index.ts b/src/events/interactionCreate/handlers/chatInputCommand/index.ts new file mode 100644 index 0000000..78ca532 --- /dev/null +++ b/src/events/interactionCreate/handlers/chatInputCommand/index.ts @@ -0,0 +1,42 @@ +import { + ActionRowBuilder, + ButtonBuilder, + ButtonStyle, + ChatInputCommandInteraction, + EmbedBuilder, +} from "discord.js"; +import getEmbedData from "../../../../helpers/getEmbedData"; + +export default async (interaction: ChatInputCommandInteraction) => { + const { errorColor, footerText, footerIcon } = await getEmbedData( + interaction.guild + ); + + if (!interaction.isCommand()) return; + const { client, commandName } = interaction; + + const currentCommand = client.commands.get(commandName); + if (!currentCommand) throw new Error(`Unknown command ${commandName}`); + + await currentCommand.execute(interaction).catch((error: Error) => { + const buttons = new ActionRowBuilder().addComponents( + new ButtonBuilder() + .setLabel("Report Problem") + .setStyle(ButtonStyle.Link) + .setEmoji("✏️") + .setURL("https://discord.zyner.org") + ); + + return interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setTitle(`:no_entry_sign:︱Your request failed`) + .setDescription(`${error.message}`) + .setColor(errorColor) + .setTimestamp(new Date()) + .setFooter({ text: footerText, iconURL: footerIcon }), + ], + components: [buttons], + }); + }); +}; diff --git a/src/events/interactionCreate/handlers/command/index.ts b/src/events/interactionCreate/handlers/command/index.ts deleted file mode 100644 index c49f7d2..0000000 --- a/src/events/interactionCreate/handlers/command/index.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Dependencies -import { ChatInputCommandInteraction } from "discord.js"; - -export default async (interaction: ChatInputCommandInteraction) => { - if (!interaction.isCommand()) return; - const { client, commandName } = interaction; - - const currentCommand = client.commands.get(commandName); - if (!currentCommand) throw new Error(`Unknown command ${commandName}`); - - await currentCommand.execute(interaction); -}; diff --git a/src/events/interactionCreate/handlers/index.ts b/src/events/interactionCreate/handlers/index.ts deleted file mode 100644 index 8e9d8c3..0000000 --- a/src/events/interactionCreate/handlers/index.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { - ActionRowBuilder, - BaseInteraction, - ButtonBuilder, - ButtonInteraction, - ButtonStyle, - ChatInputCommandInteraction, - CommandInteraction, - EmbedBuilder, -} from "discord.js"; -import getEmbedConfig from "../../../helpers/getEmbedData"; -import button from "./button"; -import command from "./command"; - -// Send interactions to all available handlers -export const execute = async (interaction: BaseInteraction) => { - await button(interaction); - await command(interaction); -}; - -// Handle interactions from commands -export const handleCommandInteraction = async ( - interaction: CommandInteraction -) => { - const { errorColor, footerText, footerIcon } = await getEmbedConfig( - interaction.guild - ); - - await command(interaction).catch((err) => { - const buttons = new ActionRowBuilder().addComponents( - new ButtonBuilder() - .setLabel("Report Problem") - .setStyle(ButtonStyle.Link) - .setEmoji("📝") - .setURL("https://discord.zyner.org") - ); - - return interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setTitle(`:no_entry_sign:︱Your request failed`) - .setDescription(`${err.message}`) - .setColor(errorColor) - .setTimestamp(new Date()) - .setFooter({ text: footerText, iconURL: footerIcon }), - ], - components: [buttons], - }); - }); -}; diff --git a/src/events/interactionCreate/index.ts b/src/events/interactionCreate/index.ts index c7e9c63..86a21c5 100644 --- a/src/events/interactionCreate/index.ts +++ b/src/events/interactionCreate/index.ts @@ -1,34 +1,39 @@ -// 3rd party dependencies -import { - BaseInteraction, - CommandInteraction, - InteractionType, -} from "discord.js"; +import { BaseInteraction, InteractionType } from "discord.js"; +import upsertGuildMember from "../../helpers/upsertGuildMember"; import { IEventOptions } from "../../interfaces/EventOptions"; import logger from "../../middlewares/logger"; -// Dependencies import audits from "./audits"; -import { handleCommandInteraction as HandlersHandleCommandInteraction } from "./handlers"; +import button from "./handlers/button"; +import chatInputCommand from "./handlers/chatInputCommand"; export const options: IEventOptions = { type: "on", }; -// Execute the event export const execute = async (interaction: BaseInteraction) => { - const { guild, id } = interaction; + logger.silly({ interaction }); + const { guild, user } = interaction; - logger?.silly( - `New interaction: ${id} in guild: ${guild?.name} (${guild?.id})` - ); + if (guild) { + await upsertGuildMember(guild, user); + } switch (interaction.type) { - case InteractionType.ApplicationCommand: - await HandlersHandleCommandInteraction(interaction); - break; + case InteractionType.ApplicationCommand: { + if (interaction.isChatInputCommand()) { + await chatInputCommand(interaction); + return; + } + if (interaction.isButton()) { + await button(interaction); + return; + } + + break; + } default: - logger?.error(`Unknown interaction type: ${interaction.type}`); + throw new Error("Unknown interaction type"); } await audits.execute(interaction); From d62baa0b9c70094cade6baddef76dd35d96eb45e Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Wed, 21 Dec 2022 07:46:42 +0100 Subject: [PATCH 6/6] refactor: :technologist: refactored event messageCreate Remove unnecessary code in the messageCreate event --- src/events/messageCreate/index.ts | 19 +--- .../messageCreate/modules/counters/index.ts | 100 +++++++++-------- .../messageCreate/modules/credits/index.ts | 104 ++++++------------ src/events/messageCreate/modules/index.ts | 9 -- .../messageCreate/modules/points/index.ts | 102 +++++++++-------- 5 files changed, 137 insertions(+), 197 deletions(-) delete mode 100644 src/events/messageCreate/modules/index.ts diff --git a/src/events/messageCreate/index.ts b/src/events/messageCreate/index.ts index d58f5e4..627d353 100644 --- a/src/events/messageCreate/index.ts +++ b/src/events/messageCreate/index.ts @@ -1,22 +1,15 @@ import { Message } from "discord.js"; import { IEventOptions } from "../../interfaces/EventOptions"; -import modules from "./modules"; +import countersExecute from "./modules/counters"; +import creditsExecute from "./modules/credits"; +import pointsExecute from "./modules/points"; export const options: IEventOptions = { type: "on", }; -// Execute the function export const execute = async (message: Message) => { - await modules.credits.execute(message); - await modules.points.execute(message); - await modules.counters.execute(message); - - if (!message.member) return; - if (message.author.bot) return; - - // client.emit("guildMemberAdd", message.member); - // client.emit("guildMemberRemove", message.member); - // client.emit("messageDelete", message); - // client.emit("messageUpdate", message, message); + await creditsExecute(message); + await pointsExecute(message); + await countersExecute(message); }; diff --git a/src/events/messageCreate/modules/counters/index.ts b/src/events/messageCreate/modules/counters/index.ts index 416a308..8fe8664 100644 --- a/src/events/messageCreate/modules/counters/index.ts +++ b/src/events/messageCreate/modules/counters/index.ts @@ -2,68 +2,66 @@ import { ChannelType, Message } from "discord.js"; import prisma from "../../../../handlers/database"; import logger from "../../../../middlewares/logger"; -export default { - execute: async (message: Message) => { - const { guild, author, content, channel } = message; +export default async (message: Message) => { + const { guild, author, content, channel } = message; - if (!guild) return; - if (author.bot) return; - if (channel?.type !== ChannelType.GuildText) return; + if (!guild) return; + if (author.bot) return; + if (channel?.type !== ChannelType.GuildText) return; - const messages = await message.channel.messages.fetch({ limit: 2 }); - const lastMessage = messages.last(); + const messages = await message.channel.messages.fetch({ limit: 2 }); + const lastMessage = messages.last(); - const channelCounter = await prisma.guildCounters.findUnique({ - where: { - guildId_channelId: { - guildId: guild.id, - channelId: channel.id, - }, + const channelCounter = await prisma.guildCounters.findUnique({ + where: { + guildId_channelId: { + guildId: guild.id, + channelId: channel.id, }, - }); + }, + }); - if (!channelCounter) { - logger.debug("No counters found in channel."); - return; - } + if (!channelCounter) { + logger.debug("No counters found in channel."); + return; + } - if ( - lastMessage?.author.id === author.id && - channel.id === channelCounter.channelId - ) { - logger.silly( - `${author.username} sent the last message therefor not allowing again.` - ); - await message.delete(); - return; - } + if ( + lastMessage?.author.id === author.id && + channel.id === channelCounter.channelId + ) { + logger.silly( + `${author.username} sent the last message therefor not allowing again.` + ); + await message.delete(); + return; + } - if (content !== channelCounter.triggerWord) { - logger.silly( - `Counter word ${channelCounter.triggerWord} does not match message ${content}` - ); + if (content !== channelCounter.triggerWord) { + logger.silly( + `Counter word ${channelCounter.triggerWord} does not match message ${content}` + ); - await message.delete(); - return; - } + await message.delete(); + return; + } - const updateGuildCounter = await prisma.guildCounters.update({ - where: { - guildId_channelId: { - guildId: guild.id, - channelId: channel.id, - }, + const updateGuildCounter = await prisma.guildCounters.update({ + where: { + guildId_channelId: { + guildId: guild.id, + channelId: channel.id, }, - data: { - count: { - increment: 1, - }, + }, + data: { + count: { + increment: 1, }, - }); + }, + }); - logger.silly(updateGuildCounter); + logger.silly(updateGuildCounter); - if (!updateGuildCounter) - logger.error(`Failed to update counter - ${updateGuildCounter}`); - }, + if (!updateGuildCounter) + logger.error(`Failed to update counter - ${updateGuildCounter}`); }; diff --git a/src/events/messageCreate/modules/credits/index.ts b/src/events/messageCreate/modules/credits/index.ts index 22a9068..b8a7051 100644 --- a/src/events/messageCreate/modules/credits/index.ts +++ b/src/events/messageCreate/modules/credits/index.ts @@ -4,86 +4,46 @@ import creditsGive from "../../../../helpers/credits/give"; import cooldown from "../../../../middlewares/cooldown"; import logger from "../../../../middlewares/logger"; -export default { - execute: async (message: Message) => { - const { guild, author, content, channel } = message; +export default async (message: Message) => { + const { guild, author, content, channel } = message; - if (!guild) return; - if (author.bot) return; - if (channel.type !== ChannelType.GuildText) return; + if (!guild) return; + if (author.bot) return; + if (channel.type !== ChannelType.GuildText) return; - const createGuildMember = await prisma.guildMember.upsert({ - where: { - userId_guildId: { - userId: author.id, - guildId: guild.id, - }, - }, - update: {}, - create: { - user: { - connectOrCreate: { - create: { - id: author.id, - }, - where: { - id: author.id, - }, + const upsertGuildConfigCredits = await prisma.guildConfigCredits.upsert({ + where: { + id: guild.id, + }, + update: {}, + create: { + guild: { + connectOrCreate: { + create: { + id: guild.id, }, - }, - guild: { - connectOrCreate: { - create: { - id: guild.id, - }, - where: { - id: guild.id, - }, + where: { + id: guild.id, }, }, }, - include: { - user: true, - guild: true, - }, - }); + }, + include: { + guild: true, + }, + }); - logger.silly(createGuildMember); + logger.silly(upsertGuildConfigCredits); - const upsertGuildConfigCredits = await prisma.guildConfigCredits.upsert({ - where: { - id: guild.id, - }, - update: {}, - create: { - guild: { - connectOrCreate: { - create: { - id: guild.id, - }, - where: { - id: guild.id, - }, - }, - }, - }, - include: { - guild: true, - }, - }); + if (content.length < upsertGuildConfigCredits.minimumLength) return; - logger.silly(upsertGuildConfigCredits); + await cooldown( + guild, + author, + "event-messageCreate-credits", + upsertGuildConfigCredits.timeout, + true + ); - if (content.length < upsertGuildConfigCredits.minimumLength) return; - - await cooldown( - guild, - author, - "event-messageCreate-credits", - upsertGuildConfigCredits.timeout, - true - ); - - await creditsGive(guild, author, upsertGuildConfigCredits.rate); - }, + await creditsGive(guild, author, upsertGuildConfigCredits.rate); }; diff --git a/src/events/messageCreate/modules/index.ts b/src/events/messageCreate/modules/index.ts deleted file mode 100644 index aff2dd3..0000000 --- a/src/events/messageCreate/modules/index.ts +++ /dev/null @@ -1,9 +0,0 @@ -import counters from "./counters"; -import credits from "./credits"; -import points from "./points"; - -export default { - counters, - credits, - points, -}; diff --git a/src/events/messageCreate/modules/points/index.ts b/src/events/messageCreate/modules/points/index.ts index b5b5372..713d177 100644 --- a/src/events/messageCreate/modules/points/index.ts +++ b/src/events/messageCreate/modules/points/index.ts @@ -3,65 +3,63 @@ import prisma from "../../../../handlers/database"; import cooldown from "../../../../middlewares/cooldown"; import logger from "../../../../middlewares/logger"; -export default { - execute: async (message: Message) => { - const { guild, author, content, channel } = message; +export default async (message: Message) => { + const { guild, author, content, channel } = message; - if (!guild) return; - if (author.bot) return; - if (channel.type !== ChannelType.GuildText) return; + if (!guild) return; + if (author.bot) return; + if (channel.type !== ChannelType.GuildText) return; - const upsertGuildConfigPoints = await prisma.guildConfigPoints.upsert({ - where: { - id: guild.id, - }, - update: {}, - create: { - guild: { - connectOrCreate: { - create: { - id: guild.id, - }, - where: { - id: guild.id, - }, + const upsertGuildConfigPoints = await prisma.guildConfigPoints.upsert({ + where: { + id: guild.id, + }, + update: {}, + create: { + guild: { + connectOrCreate: { + create: { + id: guild.id, + }, + where: { + id: guild.id, }, }, }, - include: { - guild: true, + }, + include: { + guild: true, + }, + }); + + logger.silly(upsertGuildConfigPoints); + + if (content.length < upsertGuildConfigPoints.minimumLength) return; + + await cooldown( + guild, + author, + "event-messageCreate-points", + upsertGuildConfigPoints.timeout, + true + ); + + const updateGuildMember = await prisma.guildMember.update({ + where: { + userId_guildId: { + userId: author.id, + guildId: guild.id, }, - }); - - logger.silly(upsertGuildConfigPoints); - - if (content.length < upsertGuildConfigPoints.minimumLength) return; - - await cooldown( - guild, - author, - "event-messageCreate-points", - upsertGuildConfigPoints.timeout, - true - ); - - const updateGuildMember = await prisma.guildMember.update({ - where: { - userId_guildId: { - userId: author.id, - guildId: guild.id, - }, + }, + data: { + pointsEarned: { + increment: upsertGuildConfigPoints.rate, }, - data: { - pointsEarned: { - increment: upsertGuildConfigPoints.rate, - }, - }, - }); + }, + }); - logger.silly(updateGuildMember); + logger.silly(updateGuildMember); - if (!updateGuildMember) - throw new Error("Failed to update guildMember object"); - }, + if (!updateGuildMember) + throw new Error("Failed to update guildMember object"); };