From 6ec55609b5819b9167761c8dc833e4d13a3937b0 Mon Sep 17 00:00:00 2001 From: Mohtasim Alam Sohom <94981761+codeblitz97@users.noreply.github.com> Date: Sun, 1 Oct 2023 13:20:07 +0600 Subject: [PATCH 1/2] Add message settings Add message settings so it gives user a new configuration option `messageSettings` where the user can add different types of message (such as: nsfw message, not a developer message etc) and they can config the message directly from the configuration file. --- src/commands/prefix/Nsfw/nsfw.js | 23 +++ src/events/Guild/interactionCreate.js | 225 +++++++++++++---------- src/events/Guild/messageCreate.js | 251 ++++++++++++++------------ src/example.config.js | 47 +++-- 4 files changed, 311 insertions(+), 235 deletions(-) create mode 100644 src/commands/prefix/Nsfw/nsfw.js diff --git a/src/commands/prefix/Nsfw/nsfw.js b/src/commands/prefix/Nsfw/nsfw.js new file mode 100644 index 00000000..fc9d8340 --- /dev/null +++ b/src/commands/prefix/Nsfw/nsfw.js @@ -0,0 +1,23 @@ +const { Message } = require("discord.js"); +const ExtendedClient = require("../../../class/ExtendedClient"); + +module.exports = { + structure: { + name: "nsfw", + description: "Nsfw Command", + aliases: ["ns"], + permissions: "SendMessages", + cooldown: 5000, + nsfw: true, + }, + /** + * @param {ExtendedClient} client + * @param {Message} message + * @param {string[]} args + */ + run: async (client, message, args) => { + await message.reply({ + content: "NSFW Command", + }); + }, +}; diff --git a/src/events/Guild/interactionCreate.js b/src/events/Guild/interactionCreate.js index df9827d4..54be3857 100644 --- a/src/events/Guild/interactionCreate.js +++ b/src/events/Guild/interactionCreate.js @@ -5,109 +5,134 @@ const ExtendedClient = require("../../class/ExtendedClient"); const cooldown = new Map(); module.exports = { - event: "interactionCreate", - /** - * - * @param {ExtendedClient} client - * @param {import('discord.js').Interaction} interaction - * @returns - */ - run: async (client, interaction) => { - if (!interaction.isCommand()) return; - - if ( - config.handler.commands.slash === false && - interaction.isChatInputCommand() - ) - return; - if ( - config.handler.commands.user === false && - interaction.isUserContextMenuCommand() - ) - return; + event: "interactionCreate", + /** + * + * @param {ExtendedClient} client + * @param {import('discord.js').Interaction} interaction + * @returns + */ + run: async (client, interaction) => { + if (!interaction.isCommand()) return; + + if ( + config.handler.commands.slash === false && + interaction.isChatInputCommand() + ) + return; + if ( + config.handler.commands.user === false && + interaction.isUserContextMenuCommand() + ) + return; + if ( + config.handler.commands.message === false && + interaction.isMessageContextMenuCommand() + ) + return; + + const command = client.collection.interactioncommands.get( + interaction.commandName + ); + + if (!command) return; + + try { + if (command.options?.developers) { if ( - config.handler.commands.message === false && - interaction.isMessageContextMenuCommand() - ) + config.users?.developers?.length > 0 && + !config.users?.developers?.includes(interaction.user.id) + ) { + await interaction.reply({ + content: + config.messageSettings.developerMessage !== undefined && + config.messageSettings.developerMessage !== null && + config.messageSettings.developerMessage !== "" + ? config.messageSettings.developerMessage + : "You are not authorized to use this command", + ephemeral: true, + }); + + return; + } else if (config.users?.developers?.length <= 0) { + await interaction.reply({ + content: + config.messageSettings.missingDevIDsMessage !== undefined && + config.messageSettings.missingDevIDsMessage !== null && + config.messageSettings.missingDevIDsMessage !== "" + ? config.messageSettings.missingDevIDsMessage + : "This is a developer only command, but unable to execute due to missing user IDs in configuration file.", + + ephemeral: true, + }); + + return; + } + } + + if (command.options?.nsfw && !interaction.channel.nsfw) { + await interaction.reply({ + content: + config.messageSettings.nsfwMessage !== undefined && + config.messageSettings.nsfwMessage !== null && + config.messageSettings.nsfwMessage !== "" + ? config.messageSettings.nsfwMessage + : "The current channel is not a NSFW channel", + + ephemeral: true, + }); + + return; + } + + if (command.options?.cooldown) { + const cooldownFunction = () => { + let data = cooldown.get(interaction.user.id); + + data.push(interaction.commandName); + + cooldown.set(interaction.user.id, data); + + setTimeout(() => { + let data = cooldown.get(interaction.user.id); + + data = data.filter((v) => v !== interaction.commandName); + + if (data.length <= 0) { + cooldown.delete(interaction.user.id); + } else { + cooldown.set(interaction.user.id, data); + } + }, command.options?.cooldown); + }; + + if (cooldown.has(interaction.user.id)) { + let data = cooldown.get(interaction.user.id); + + if (data.some((v) => v === interaction.commandName)) { + await interaction.reply({ + content: + config.messageSettings.cooldownMessage !== undefined && + config.messageSettings.cooldownMessage !== null && + config.messageSettings.cooldownMessage !== "" + ? config.messageSettings.cooldownMessage + : "Slow down buddy! You're too fast to use this command", + }); + return; + } else { + cooldownFunction(); + } + } else { + cooldown.set(interaction.user.id, [interaction.commandName]); - const command = client.collection.interactioncommands.get( - interaction.commandName - ); - - if (!command) return; - - try { - if (command.options?.developers) { - if (config.users?.developers?.length > 0 && !config.users?.developers?.includes(interaction.user.id)) { - await interaction.reply({ - content: `This is a developer only command.`, - ephemeral: true, - }); - - return; - } else if (config.users?.developers?.length <= 0) { - await interaction.reply({ - content: `This is a developer only command, but unable to execute due to missing user IDs in configuration file.`, - ephemeral: true, - }); - - return; - }; - }; - - if (command.options?.nsfw && !interaction.channel.nsfw) { - await interaction.reply({ - content: "The current channel is not an NSFW channel.", - ephemeral: true, - }); - - return; - }; - - if (command.options?.cooldown) { - const cooldownFunction = () => { - let data = cooldown.get(interaction.user.id); - - data.push(interaction.commandName); - - cooldown.set(interaction.user.id, data); - - setTimeout(() => { - let data = cooldown.get(interaction.user.id); - - data = data.filter((v) => v !== interaction.commandName); - - if (data.length <= 0) { - cooldown.delete(interaction.user.id); - } else { - cooldown.set(interaction.user.id, data); - }; - }, command.options?.cooldown); - }; - - if (cooldown.has(interaction.user.id)) { - let data = cooldown.get(interaction.user.id); - - if (data.some((v) => v === interaction.commandName)) { - await interaction.reply({ - content: "Slow down buddy! You're too fast to use this command.", - }); - - return; - } else { - cooldownFunction(); - }; - } else { - cooldown.set(interaction.user.id, [interaction.commandName]); - - cooldownFunction(); - }; - }; - - command.run(client, interaction); - } catch (error) { - log(error, "err"); + cooldownFunction(); } + } + + command.run(client, interaction); + } catch (error) { + log(error, "err"); } + }, }; diff --git a/src/events/Guild/messageCreate.js b/src/events/Guild/messageCreate.js index c5f84fb4..e30a17c3 100644 --- a/src/events/Guild/messageCreate.js +++ b/src/events/Guild/messageCreate.js @@ -7,121 +7,140 @@ const ExtendedClient = require("../../class/ExtendedClient"); const cooldown = new Map(); module.exports = { - event: "messageCreate", - /** - * - * @param {ExtendedClient} client - * @param {Message} message - * @returns - */ - run: async (client, message) => { - if (message.author.bot || message.channel.type === ChannelType.DM) return; - - if (!config.handler.commands.prefix) return; - - let prefix = config.handler.prefix; - - if (config.handler?.mongodb?.toggle) { - try { - const guildData = await GuildSchema.findOne({ guild: message.guildId }); - - if (guildData && guildData?.prefix) prefix = guildData.prefix; - } catch { - prefix = config.handler.prefix; - }; - }; - - if (!message.content.startsWith(prefix)) return; - - const args = message.content.slice(prefix.length).trim().split(/ +/g); - const commandInput = args.shift().toLowerCase(); - - if (!commandInput.length) return; - - let command = - client.collection.prefixcommands.get(commandInput) || - client.collection.prefixcommands.get( - client.collection.aliases.get(commandInput) - ); - - if (command) { - try { - if ( - command.structure?.permissions && - !message.member.permissions.has(command.structure?.permissions) - ) { - await message.reply({ - content: "You do not have the permission to use this command.", - }); - - return; - }; - - if (command.structure?.developers) { - if (!config.users.developers.includes(message.author.id)) { - setTimeout(async () => { - await message.reply({ - content: "You are not authorized to use this command.", - }); - }, 5 * 1000); - }; - - return; - }; - - if (command.structure?.nsfw && !interaction.channel.nsfw) { - await message.reply({ - content: "The current channel is not an NSFW channel." - }); - - return; - }; - - if (command.structure?.cooldown) { - const cooldownFunction = () => { - let data = cooldown.get(message.author.id); - - data.push(commandInput); - - cooldown.set(message.author.id, data); - - setTimeout(() => { - let data = cooldown.get(message.author.id); - - data = data.filter((v) => v !== commandInput); - - if (data.length <= 0) { - cooldown.delete(message.author.id); - } else { - cooldown.set(message.author.id, data); - } - }, command.structure?.cooldown); - }; - - if (cooldown.has(message.author.id)) { - let data = cooldown.get(message.author.id); - - if (data.some((v) => v === commandInput)) { - await message.reply({ - content: - "Slow down buddy! You're too fast to use this command.", - }); - - return; - } else { - cooldownFunction(); - }; - } else { - cooldown.set(message.author.id, [commandInput]); - - cooldownFunction(); - }; - }; - - command.run(client, message, args); - } catch (error) { - log(error, "err"); - }; - }; + event: "messageCreate", + /** + * + * @param {ExtendedClient} client + * @param {Message} message + * @returns + */ + run: async (client, message) => { + if (message.author.bot || message.channel.type === ChannelType.DM) return; + + if (!config.handler.commands.prefix) return; + + let prefix = config.handler.prefix; + + if (config.handler?.mongodb?.toggle) { + try { + const guildData = await GuildSchema.findOne({ guild: message.guildId }); + + if (guildData && guildData?.prefix) prefix = guildData.prefix; + } catch { + prefix = config.handler.prefix; + } } + + if (!message.content.startsWith(prefix)) return; + + const args = message.content.slice(prefix.length).trim().split(/ +/g); + const commandInput = args.shift().toLowerCase(); + + if (!commandInput.length) return; + + let command = + client.collection.prefixcommands.get(commandInput) || + client.collection.prefixcommands.get( + client.collection.aliases.get(commandInput) + ); + + if (command) { + try { + if ( + command.structure?.permissions && + !message.member.permissions.has(command.structure?.permissions) + ) { + await message.reply({ + content: + config.messageSettings.notHasPermissionMessage !== undefined && + config.messageSettings.notHasPermissionMessage !== null && + config.messageSettings.notHasPermissionMessage !== "" + ? config.messageSettings.notHasPermissionMessage + : "You do not have the permission to use this command.", + }); + + return; + } + + if (command.structure?.developers) { + if (!config.users.developers.includes(message.author.id)) { + setTimeout(async () => { + await message.reply({ + content: + config.messageSettings.developerMessage !== undefined && + config.messageSettings.developerMessage !== null && + config.messageSettings.developerMessage !== "" + ? config.messageSettings.developerMessage + : "You are not authorized to use this command", + }); + }, 5 * 1000); + } + + return; + } + + if (command.structure?.nsfw && !message.channel.nsfw) { + await message.reply({ + content: + config.messageSettings.nsfwMessage !== undefined && + config.messageSettings.nsfwMessage !== null && + config.messageSettings.nsfwMessage !== "" + ? config.messageSettings.nsfwMessage + : "The current channel is not a NSFW channel.", + }); + + return; + } + + if (command.structure?.cooldown) { + const cooldownFunction = () => { + let data = cooldown.get(message.author.id); + + data.push(commandInput); + + cooldown.set(message.author.id, data); + + setTimeout(() => { + let data = cooldown.get(message.author.id); + + data = data.filter((v) => v !== commandInput); + + if (data.length <= 0) { + cooldown.delete(message.author.id); + } else { + cooldown.set(message.author.id, data); + } + }, command.structure?.cooldown); + }; + + if (cooldown.has(message.author.id)) { + let data = cooldown.get(message.author.id); + + if (data.some((v) => v === commandInput)) { + await message.reply({ + content: + config.messageSettings.cooldownMessage !== undefined && + config.messageSettings.cooldownMessage !== null && + config.messageSettings.cooldownMessage !== "" + ? config.messageSettings.cooldownMessage + : "Slow down buddy! You're too fast to use this command.", + }); + + return; + } else { + cooldownFunction(); + } + } else { + cooldown.set(message.author.id, [commandInput]); + + cooldownFunction(); + } + } + + command.run(client, message, args); + } catch (error) { + log(error, "err"); + } + } + }, }; diff --git a/src/example.config.js b/src/example.config.js index acb9c0e9..ef4b0065 100644 --- a/src/example.config.js +++ b/src/example.config.js @@ -1,23 +1,32 @@ module.exports = { - client: { - token: "Your Bot token (USE .env FOR SAFETY)", - id: "Your Bot ID (USE .env FOR SAFETY)" + client: { + token: "Your Bot token (USE .env FOR SAFETY)", + id: "Your Bot ID (USE .env FOR SAFETY)", + }, + handler: { + prefix: "?", + deploy: true, + commands: { + prefix: true, + slash: true, + user: true, + message: true, }, - handler: { - prefix: "?", - deploy: true, - commands: { - prefix: true, - slash: true, - user: true, - message: true - }, - mongodb: { - uri: "Your MongoDB URI string (USE .env FOR SAFETY)", - toggle: false - } + mongodb: { + uri: "Your MongoDB URI string (USE .env FOR SAFETY)", + toggle: false, }, - users: { - developers: ["Your account ID"] - } + }, + users: { + developers: ["Your account ID"], + }, + messageSettings: { + nsfwMessage: "The current channel is not a NSFW channel.", + developerMessage: "You are not authorized to use this command.", + cooldownMessage: "Slow down buddy! You're too fast to use this command.", + notHasPermissionMessage: + "You do not have the permission to use this command.", + missingDevIDsMessage: + "This is a developer only command, but unable to execute due to missing user IDs in configuration file.", + }, }; From 5be8bac93cfd79c29ee5f2ef83007e006f592989 Mon Sep 17 00:00:00 2001 From: Mohtasim Alam Sohom <94981761+codeblitz97@users.noreply.github.com> Date: Sun, 1 Oct 2023 14:26:07 +0600 Subject: [PATCH 2/2] Use info level on the load command instead of warning --- src/handlers/deploy.js | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/handlers/deploy.js b/src/handlers/deploy.js index a57a146e..2c021bc2 100644 --- a/src/handlers/deploy.js +++ b/src/handlers/deploy.js @@ -1,24 +1,32 @@ const { REST, Routes } = require("discord.js"); const { log } = require("../functions"); const config = require("../config"); -const ExtendedClient = require('../class/ExtendedClient'); +const ExtendedClient = require("../class/ExtendedClient"); /** - * - * @param {ExtendedClient} client + * + * @param {ExtendedClient} client */ module.exports = async (client) => { - const rest = new REST({ version: '10' }).setToken(process.env.CLIENT_TOKEN || config.client.token); + const rest = new REST({ version: "10" }).setToken( + process.env.CLIENT_TOKEN || config.client.token + ); - try { - log('Started loading application commands... (this might take minutes!)', 'warn'); + try { + log( + "Started loading application commands... (this might take minutes!)", + "info" + ); - await rest.put(Routes.applicationCommands(process.env.CLIENT_ID || config.client.id), { - body: client.applicationcommandsArray - }); + await rest.put( + Routes.applicationCommands(process.env.CLIENT_ID || config.client.id), + { + body: client.applicationcommandsArray, + } + ); - log('Successfully loaded application commands to Discord API.', 'done'); - } catch (e) { - log('Unable to load application commands to Discord API.', 'err'); - }; -}; \ No newline at end of file + log("Successfully loaded application commands to Discord API.", "done"); + } catch (e) { + log("Unable to load application commands to Discord API.", "err"); + } +};