From 95adb324f4a4504f2b9bee2913edb243c9f70181 Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Sun, 10 Apr 2022 15:39:05 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20admin=20commands?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/admin/counter/addons/add.ts | 89 --------- src/commands/admin/counter/addons/index.ts | 4 - src/commands/admin/counter/addons/remove.ts | 40 ---- src/commands/admin/counter/index.ts | 46 ----- src/commands/admin/counters/index.ts | 34 ++++ src/commands/admin/counters/modules/add.ts | 97 ++++++++++ src/commands/admin/counters/modules/remove.ts | 48 +++++ src/commands/admin/credits/addons/give.ts | 107 ----------- src/commands/admin/credits/addons/index.ts | 11 -- src/commands/admin/credits/addons/set.ts | 108 ----------- src/commands/admin/credits/addons/take.ts | 108 ----------- src/commands/admin/credits/addons/transfer.ts | 138 -------------- src/commands/admin/credits/index.ts | 77 +++----- src/commands/admin/credits/modules/give.ts | 135 +++++++++++++ src/commands/admin/credits/modules/set.ts | 117 ++++++++++++ src/commands/admin/credits/modules/take.ts | 135 +++++++++++++ .../admin/credits/modules/transfer.ts | 177 ++++++++++++++++++ src/commands/admin/index.ts | 62 ++++-- 18 files changed, 820 insertions(+), 713 deletions(-) delete mode 100644 src/commands/admin/counter/addons/add.ts delete mode 100644 src/commands/admin/counter/addons/index.ts delete mode 100644 src/commands/admin/counter/addons/remove.ts delete mode 100644 src/commands/admin/counter/index.ts create mode 100644 src/commands/admin/counters/index.ts create mode 100644 src/commands/admin/counters/modules/add.ts create mode 100644 src/commands/admin/counters/modules/remove.ts delete mode 100644 src/commands/admin/credits/addons/give.ts delete mode 100644 src/commands/admin/credits/addons/index.ts delete mode 100644 src/commands/admin/credits/addons/set.ts delete mode 100644 src/commands/admin/credits/addons/take.ts delete mode 100644 src/commands/admin/credits/addons/transfer.ts create mode 100644 src/commands/admin/credits/modules/give.ts create mode 100644 src/commands/admin/credits/modules/set.ts create mode 100644 src/commands/admin/credits/modules/take.ts create mode 100644 src/commands/admin/credits/modules/transfer.ts diff --git a/src/commands/admin/counter/addons/add.ts b/src/commands/admin/counter/addons/add.ts deleted file mode 100644 index 420dcd5..0000000 --- a/src/commands/admin/counter/addons/add.ts +++ /dev/null @@ -1,89 +0,0 @@ -import { Permissions, CommandInteraction } from 'discord.js'; -import config from '../../../../../config.json'; -import logger from '../../../../handlers/logger'; - -// Database models -import counters from '../../../../helpers/database/models/counterSchema'; - -export default async (interaction: CommandInteraction) => { - // Destructure member - const { member } = interaction; - - // Check permission - if (!interaction?.memberPermissions?.has(Permissions.FLAGS.MANAGE_GUILD)) { - // Create embed object - const embed = { - title: 'Admin', - color: config.colors.error as any, - description: 'You do not have permission to manage this!', - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); - } - - // Get options - const channel = await interaction.options.getChannel('channel'); - const word = await interaction.options.getString('word'); - const start = await interaction.options.getNumber('start'); - - if (channel?.type !== 'GUILD_TEXT') { - // Create embed object - const embed = { - title: 'Admin - Counter', - description: `That channel is not supported, it needs to be a text channel.`, - timestamp: new Date(), - color: config.colors.error as any, - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); - } - - const counterExist = await counters.findOne({ - guildId: interaction?.guild?.id, - channelId: channel?.id, - word, - }); - - if (!counterExist) { - await counters.create({ - guildId: interaction?.guild?.id, - channelId: channel?.id, - word, - counter: start || 0, - }); - // Create embed object - const embed = { - title: 'Admin - Counter', - description: `${channel} is now counting when hearing word ${word} and it starts at number ${ - start || 0 - }.`, - timestamp: new Date(), - color: config.colors.success as any, - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send debug message - await logger.debug( - `Guild: ${interaction?.guild?.id} User: ${interaction?.user?.id} added ${channel.id} as a counter using word "${word}" for counting.` - ); - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); - } - // Create embed object - const embed = { - title: 'Admin - Counter', - description: `${channel} is already a counting channel.`, - timestamp: new Date(), - color: config.colors.error as any, - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); -}; diff --git a/src/commands/admin/counter/addons/index.ts b/src/commands/admin/counter/addons/index.ts deleted file mode 100644 index 8b1faf0..0000000 --- a/src/commands/admin/counter/addons/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -import add from './add'; -import remove from './remove'; - -export default { add, remove }; diff --git a/src/commands/admin/counter/addons/remove.ts b/src/commands/admin/counter/addons/remove.ts deleted file mode 100644 index 6d82c5f..0000000 --- a/src/commands/admin/counter/addons/remove.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { Permissions, CommandInteraction } from 'discord.js'; -import config from '../../../../../config.json'; -import logger from '../../../../handlers/logger'; - -// Database models -import counters from '../../../../helpers/database/models/counterSchema'; - -export default async (interaction: CommandInteraction) => { - // Destructure member - const { member } = interaction; - - // Check permission - if (!interaction?.memberPermissions?.has(Permissions.FLAGS.MANAGE_GUILD)) { - // Create embed object - const embed = { - title: 'Admin', - color: config.colors.error as any, - description: 'You do not have permission to manage this!', - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); - } - - // Get options - const channel = await interaction.options.getChannel('channel'); - - await counters - .deleteOne({ guildId: interaction?.guild?.id, channelId: channel?.id }) - .then(async () => { - interaction.editReply({ content: 'Removed' }); - }); - - // Send debug message - await logger.debug( - `Guild: ${interaction?.guild?.id} User: ${interaction?.user?.id} executed remove counter.` - ); -}; diff --git a/src/commands/admin/counter/index.ts b/src/commands/admin/counter/index.ts deleted file mode 100644 index 2e8668f..0000000 --- a/src/commands/admin/counter/index.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { Permissions, CommandInteraction } from 'discord.js'; -import config from '../../../../config.json'; -import logger from '../../../handlers/logger'; -import add from './addons/add'; -import remove from './addons/remove'; - -export default async (interaction: CommandInteraction) => { - // Destructure member - const { member } = interaction; - - // Check permission - if (!interaction?.memberPermissions?.has(Permissions.FLAGS.MANAGE_GUILD)) { - // Create embed object - const embed = { - title: ':toolbox: Admin - Counter', - color: config.colors.error as any, - description: 'You do not have permission to manage this!', - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - await interaction.editReply({ embeds: [embed] }); - } - - // If subcommand is give - if (interaction.options.getSubcommand() === 'add') { - // Execute give addon - await add(interaction); - } - - // If subcommand is take - else if (interaction.options.getSubcommand() === 'remove') { - // Execute take addon - await remove(interaction); - } - - // Send debug message - await logger.debug( - `Guild: ${interaction?.guild?.id} User: ${ - interaction?.user?.id - } executed /${ - interaction.commandName - } ${interaction.options.getSubcommandGroup()} ${interaction.options.getSubcommand()}` - ); -}; diff --git a/src/commands/admin/counters/index.ts b/src/commands/admin/counters/index.ts new file mode 100644 index 0000000..50fa2f5 --- /dev/null +++ b/src/commands/admin/counters/index.ts @@ -0,0 +1,34 @@ +// Dependencies +import { CommandInteraction } from 'discord.js'; + +// Handlers +import logger from '../../../handlers/logger'; + +// Modules +import add from './modules/add'; +import remove from './modules/remove'; + +// Function +export default async (interaction: CommandInteraction) => { + // Destructure + const { options, guild, user, commandName } = interaction; + + // Module - Add + if (options?.getSubcommand() === 'add') { + // Execute Module - Add + return await add(interaction); + } + + // Module - Remove + else if (options?.getSubcommand() === 'remove') { + // Execute Module - Remove + return await remove(interaction); + } + + // Log debug message + return logger?.debug( + `Guild: ${guild?.id} User: ${ + user?.id + } executed /${commandName} ${options?.getSubcommandGroup()} ${options?.getSubcommand()}` + ); +}; diff --git a/src/commands/admin/counters/modules/add.ts b/src/commands/admin/counters/modules/add.ts new file mode 100644 index 0000000..81c47bc --- /dev/null +++ b/src/commands/admin/counters/modules/add.ts @@ -0,0 +1,97 @@ +// Dependencies +import { ColorResolvable, CommandInteraction } from 'discord.js'; + +// Configurations +import config from '../../../../../config.json'; + +// Handlers +import logger from '../../../../handlers/logger'; + +// Models +import counterSchema from '../../../../helpers/database/models/counterSchema'; + +// Function +export default async (interaction: CommandInteraction) => { + // Destructure + const { options, guild, user } = interaction; + + // Channel option + const optionChannel = options?.getChannel('channel'); + + // Word option + const optionWord = options?.getString('word'); + + // Start option + const optionStart = options?.getNumber('start'); + + if (optionChannel?.type !== 'GUILD_TEXT') { + // Embed object + const embed = { + title: ':toolbox: Admin - Counters [Add]' as string, + description: + 'That channel is not supported, it needs to be a text channel.' as string, + timestamp: new Date() as Date, + color: config?.colors?.error as ColorResolvable, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + } + + const counterExist = await counterSchema?.findOne({ + guildId: guild?.id, + channelId: optionChannel?.id, + optionWord, + }); + + if (!counterExist) { + await counterSchema?.create({ + guildId: guild?.id, + channelId: optionChannel?.id, + optionWord, + counter: optionStart || 0, + }); + + // Embed object + const embed = { + title: ':toolbox: Admin - Counters [Add]' as string, + description: + `${optionChannel} is now counting when hearing word ${optionWord} and it starts at number ${ + optionStart || 0 + }.` as string, + timestamp: new Date() as Date, + color: config?.colors?.success as ColorResolvable, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Log debug message + logger?.debug( + `Guild: ${guild?.id} User: ${user?.id} added ${optionChannel?.id} as a counter using word "${optionWord}" for counting.` + ); + + // Return interaction reply + return interaction?.editReply({ embeds: [embed] }); + } + + // Embed object + const embed = { + title: ':toolbox: Admin - Counters [Add]' as string, + description: `${optionChannel} is already a counting channel.` as string, + timestamp: new Date() as Date, + color: config?.colors?.error as ColorResolvable, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Return interaction reply + return interaction?.editReply({ embeds: [embed] }); +}; diff --git a/src/commands/admin/counters/modules/remove.ts b/src/commands/admin/counters/modules/remove.ts new file mode 100644 index 0000000..14e0eb9 --- /dev/null +++ b/src/commands/admin/counters/modules/remove.ts @@ -0,0 +1,48 @@ +// Dependencies +import { ColorResolvable, CommandInteraction } from 'discord.js'; + +// Configurations +import config from '../../../../../config.json'; + +// Handlers +import logger from '../../../../handlers/logger'; + +// Models +import counterSchema from '../../../../helpers/database/models/counterSchema'; + +// Function +export default async (interaction: CommandInteraction) => { + // Destructure + const { options, guild, user } = interaction; + + // Get options + const optionChannel = options?.getChannel('channel'); + + await counterSchema + ?.deleteOne({ + guildId: guild?.id, + channelId: optionChannel?.id, + }) + ?.then(async () => { + // Embed object + const embed = { + title: ':toolbox: Admin - Counters [Remove]' as string, + description: + `${optionChannel} is no longer an counting channel.` as string, + timestamp: new Date() as Date, + color: config?.colors?.success as ColorResolvable, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Return interaction reply + return interaction?.editReply({ embeds: [embed] }); + }); + + // Send debug message + return logger?.debug( + `Guild: ${guild?.id} User: ${user?.id} removed ${optionChannel?.id} as a counter.` + ); +}; diff --git a/src/commands/admin/credits/addons/give.ts b/src/commands/admin/credits/addons/give.ts deleted file mode 100644 index b343b9a..0000000 --- a/src/commands/admin/credits/addons/give.ts +++ /dev/null @@ -1,107 +0,0 @@ -import { Permissions, CommandInteraction } from 'discord.js'; -import config from '../../../../../config.json'; -import logger from '../../../../handlers/logger'; - -// Database models -import users from '../../../../helpers/database/models/userSchema'; - -import creditNoun from '../../../../helpers/creditNoun'; - -export default async (interaction: CommandInteraction) => { - // Destructure member - const { guild, user } = interaction; - - // Check permission - if (!interaction?.memberPermissions?.has(Permissions.FLAGS.MANAGE_GUILD)) { - // Create embed object - const embed = { - title: ':toolbox: Admin - Credits [Give]', - color: config.colors.error as any, - description: 'You do not have permission to manage this!', - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); - } - - // Get options - const userOption = await interaction.options.getUser('user'); - const amount = await interaction.options.getInteger('amount'); - - if (amount === null) return; - - if (amount <= 0) { - // If amount is zero or below - // Create embed object - const embed = { - title: ':toolbox: Admin - Credits [Give]', - description: "You can't give zero or below.", - color: 0xbb2124, - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); - } - - // Get toUserDB object - const toUserDB = await users.findOne({ - userId: userOption?.id, - guildId: interaction?.guild?.id, - }); - - // If toUserDB has no credits - if (!toUserDB) { - // Create embed object - const embed = { - title: ':toolbox: Admin - Credits [Give]', - description: - 'That userOption has no credits, I can not give credits to the userOption', - color: config.colors.error as any, - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); - } - - // Deposit amount to toUserDB - toUserDB.credits += amount; - - // Save toUserDB - await toUserDB - .save() - - // If successful - .then(async () => { - // Create embed object - const embed = { - title: ':toolbox: Admin - Credits [Give]', - description: `Gave ${creditNoun(amount)} to ${userOption}.`, - color: 0x22bb33, - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send debug message - await logger.debug( - `Administrator: ${interaction.user.username} gave ${ - amount <= 1 ? `${amount} credit` : `${amount} credits` - } to ${userOption?.username}` - ); - - // Send interaction reply - await interaction.editReply({ embeds: [embed] }); - - // Send debug message - await logger.debug( - `Guild: ${guild?.id} User: ${user?.id} gave ${ - userOption?.id - } ${creditNoun(amount)}.` - ); - }); -}; diff --git a/src/commands/admin/credits/addons/index.ts b/src/commands/admin/credits/addons/index.ts deleted file mode 100644 index 24e6e58..0000000 --- a/src/commands/admin/credits/addons/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -import give from './give'; -import set from './set'; -import take from './take'; -import transfer from './transfer'; - -export default { - give, - set, - take, - transfer, -}; diff --git a/src/commands/admin/credits/addons/set.ts b/src/commands/admin/credits/addons/set.ts deleted file mode 100644 index b8df820..0000000 --- a/src/commands/admin/credits/addons/set.ts +++ /dev/null @@ -1,108 +0,0 @@ -import { Permissions, CommandInteraction } from 'discord.js'; -import config from '../../../../../config.json'; -import logger from '../../../../handlers/logger'; - -// Database models - -import users from '../../../../helpers/database/models/userSchema'; - -import creditNoun from '../../../../helpers/creditNoun'; - -export default async (interaction: CommandInteraction) => { - // Destructure member - const { member } = interaction; - - // Check permission - if (!interaction?.memberPermissions?.has(Permissions.FLAGS.MANAGE_GUILD)) { - // Create embed object - const embed = { - title: ':toolbox: Admin - Credits [Set]', - color: config.colors.error as any, - description: 'You do not have permission to manage this!', - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); - } - - // Get options - const user = await interaction.options.getUser('user'); - const amount = await interaction.options.getInteger('amount'); - - if (amount === null) return; - - // If amount is zero or below - if (amount <= 0) { - // Create embed object - const embed = { - title: ':toolbox: Admin - Credits [Set]', - description: "You can't give zero or below.", - color: 0xbb2124, - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); - } - - // Get toUserDB object - const toUserDB = await users.findOne({ - userId: user?.id, - guildId: interaction?.guild?.id, - }); - - // If toUserDB has no credits - if (!toUserDB) { - // Create embed object - const embed = { - title: ':toolbox: Admin - Credits [Set]', - description: - 'That user has no credits, I can not set credits to the user', - color: config.colors.error as any, - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); - } - - // Set toUserDB with amount - toUserDB.credits = amount; - - // Save toUserDB - await toUserDB - .save() - - // If successful - .then(async () => { - // Create embed object - const embed = { - title: ':toolbox: Admin - Credits [Set]', - description: `You set ${creditNoun(amount)} on ${user}.`, - color: 0x22bb33, - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send debug message - await logger.debug( - `Administrator: ${interaction.user.username} set ${ - amount <= 1 ? `${amount} credit` : `${amount} credits` - } on ${user?.username}` - ); - - // Send interaction reply - await interaction.editReply({ embeds: [embed] }); - - // Send debug message - await logger.debug( - `Guild: ${interaction?.guild?.id} User: ${interaction?.user?.id} set ${ - user?.id - } to ${creditNoun(amount)}.` - ); - }); -}; diff --git a/src/commands/admin/credits/addons/take.ts b/src/commands/admin/credits/addons/take.ts deleted file mode 100644 index fa1b9a3..0000000 --- a/src/commands/admin/credits/addons/take.ts +++ /dev/null @@ -1,108 +0,0 @@ -import { Permissions, CommandInteraction } from 'discord.js'; -import config from '../../../../../config.json'; -import logger from '../../../../handlers/logger'; - -// Database models - -import users from '../../../../helpers/database/models/userSchema'; - -import creditNoun from '../../../../helpers/creditNoun'; - -export default async (interaction: CommandInteraction) => { - // Destructure member - const { guild, user } = interaction; - - // Check permission - if (!interaction?.memberPermissions?.has(Permissions.FLAGS.MANAGE_GUILD)) { - // Create embed object - const embed = { - title: ':toolbox: Admin - Credits [Take]', - color: config.colors.error as any, - description: 'You do not have permission to manage this!', - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); - } - - // Get options - const userOption = await interaction.options.getUser('userOption'); - const amount = await interaction.options.getInteger('amount'); - - if (amount === null) return; - - // If amount is zero or below - if (amount <= 0) { - // Give embed object - const embed = { - title: ':toolbox: Admin - Credits [Take]', - description: "You can't take zero or below.", - color: config.colors.error as any, - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); - } - - // Get toUser object - const toUser = await users.findOne({ - userId: userOption?.id, - guildId: interaction?.guild?.id, - }); - - // If toUser has no credits - if (!toUser) { - // Create embed object - const embed = { - title: ':toolbox: Admin - Credits [Take]', - description: - 'That userOption has no credits, I can not take credits from the userOption', - color: config.colors.error as any, - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); - } - - // Withdraw amount from toUser - toUser.credits -= amount; - - // Save toUser - await toUser - .save() - - // If successful - .then(async () => { - // Create embed object - const embed = { - title: ':toolbox: Admin - Credits [Take]', - description: `You took ${creditNoun(amount)} to ${userOption}.`, - color: 0x22bb33, - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send debug message - await logger.debug( - `Administrator: ${interaction.user.username} took ${ - amount <= 1 ? `${amount} credit` : `${amount} credits` - } from ${userOption?.username}` - ); - - // Send interaction reply - await interaction.editReply({ embeds: [embed] }); - - // Send debug message - await logger.debug( - `Guild: ${guild?.id} User: ${user?.id} took ${creditNoun( - amount - )} from ${user.id}.` - ); - }); -}; diff --git a/src/commands/admin/credits/addons/transfer.ts b/src/commands/admin/credits/addons/transfer.ts deleted file mode 100644 index 1feb452..0000000 --- a/src/commands/admin/credits/addons/transfer.ts +++ /dev/null @@ -1,138 +0,0 @@ -import { Permissions, CommandInteraction } from 'discord.js'; -import config from '../../../../../config.json'; -import logger from '../../../../handlers/logger'; - -// Database models - -import users from '../../../../helpers/database/models/userSchema'; - -import creditNoun from '../../../../helpers/creditNoun'; -import saveUser from '../../../../helpers/saveUser'; - -export default async (interaction: CommandInteraction) => { - // Destructure member - const { member } = interaction; - - // Check permission - if (!interaction?.memberPermissions?.has(Permissions.FLAGS.MANAGE_GUILD)) { - // Create embed object - const embed = { - title: ':toolbox: Admin - Credits [Transfer]', - color: config.colors.error as any, - description: 'You do not have permission to manage this!', - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); - } - - // Get options - const from = await interaction.options.getUser('from'); - const to = await interaction.options.getUser('to'); - const amount = await interaction.options.getInteger('amount'); - - // Get fromUser object - const fromUser = await users.findOne({ - userId: from?.id, - guildId: interaction?.guild?.id, - }); - - // Get toUser object - const toUser = await users.findOne({ - userId: to?.id, - guildId: interaction?.guild?.id, - }); - - // If fromUser has no credits - if (!fromUser) { - // Create embed object - const embed = { - title: ':toolbox: Admin - Credits [Transfer]', - description: - 'That user has no credits, I can not transfer credits from the user', - color: config.colors.error as any, - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); - } - - // If toUser has no credits - if (!toUser) { - // Create embed object - const embed = { - title: ':toolbox: Admin - Credits [Transfer]', - description: - 'That user has no credits, I can not transfer credits to the user', - color: config.colors.error as any, - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); - } - - if (amount === null) return; - - // If amount is zero or below - if (amount <= 0) { - // Create embed object - const embed = { - title: ':toolbox: Admin - Credits [Transfer]', - description: "You can't transfer zero or below.", - color: config.colors.error as any, - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - return interaction.editReply({ embeds: [embed] }); - } - - // Withdraw amount from fromUser - fromUser.credits -= amount; - - // Deposit amount to toUser - toUser.credits += amount; - - // Save users - await saveUser(fromUser, toUser) - // If successful - .then(async () => { - // Create embed object - const embed = { - title: ':toolbox: Admin - Credits [Transfer]', - description: `You sent ${creditNoun(amount)} from ${from} to ${to}.`, - color: config.colors.success as any, - fields: [ - { - name: `${from?.username} Balance`, - value: `${fromUser.credits}`, - inline: true, - }, - { - name: `${to?.username} Balance`, - value: `${toUser.credits}`, - inline: true, - }, - ], - timestamp: new Date(), - footer: { iconURL: config.footer.icon, text: config.footer.text }, - }; - - // Send interaction reply - await interaction.editReply({ embeds: [embed] }); - - // Send debug message - await logger.debug( - `Guild: ${interaction?.guild?.id} User: ${ - interaction?.user?.id - } transferred ${creditNoun(amount)} from ${from?.id} to ${to?.id}.` - ); - }); -}; diff --git a/src/commands/admin/credits/index.ts b/src/commands/admin/credits/index.ts index 4739d25..305c72f 100644 --- a/src/commands/admin/credits/index.ts +++ b/src/commands/admin/credits/index.ts @@ -1,61 +1,38 @@ -import { Permissions, CommandInteraction } from 'discord.js'; -import config from '../../../../config.json'; -import logger from '../../../handlers/logger'; -import give from './addons/give'; -import take from './addons/take'; -import set from './addons/set'; -import transfer from './addons/transfer'; +// Dependencies +import { CommandInteraction } from 'discord.js'; +// Modules +import give from './modules/give'; +import take from './modules/take'; +import set from './modules/set'; +import transfer from './modules/transfer'; + +// Function export default async (interaction: CommandInteraction) => { - // Destructure member - const { user, guild } = interaction; + // Destructure + const { user, guild, commandName, options } = interaction; - // Check permission - if (!interaction?.memberPermissions?.has(Permissions.FLAGS.MANAGE_GUILD)) { - // Create embed object - const embed = { - title: ':toolbox: Admin - Credits' as string, - color: config.colors.error as any, - description: 'You do not have permission to manage this!' as string, - timestamp: new Date(), - footer: { - iconURL: config.footer.icon as string, - text: config.footer.text as string, - }, - }; - - // Send interaction reply - await interaction.editReply({ embeds: [embed] }); + // Module - Give + if (options?.getSubcommand() === 'give') { + // Execute Module - Give + return await give(interaction); } - // If subcommand is give - if (interaction.options.getSubcommand() === 'give') { - // Execute give addon - await give(interaction); + // Module - Take + else if (options?.getSubcommand() === 'take') { + // Execute Module - Take + return await take(interaction); } - // If subcommand is take - else if (interaction.options.getSubcommand() === 'take') { - // Execute take addon - await take(interaction); + // Module - Set + else if (options?.getSubcommand() === 'set') { + // Execute Module - Set + return await set(interaction); } - // If subcommand is set - else if (interaction.options.getSubcommand() === 'set') { - // Execute set addon - await set(interaction); + // Module - Transfer + else if (options?.getSubcommand() === 'transfer') { + // Execute Module - Transfer + return await transfer(interaction); } - - // If subcommand is transfer - else if (interaction.options.getSubcommand() === 'transfer') { - // Execute transfer addon - await transfer(interaction); - } - - // Send debug message - await logger.debug( - `Guild: ${guild?.id} User: ${user?.id} executed /${ - interaction.commandName - } ${interaction.options.getSubcommandGroup()} ${interaction.options.getSubcommand()}` - ); }; diff --git a/src/commands/admin/credits/modules/give.ts b/src/commands/admin/credits/modules/give.ts new file mode 100644 index 0000000..d2165c7 --- /dev/null +++ b/src/commands/admin/credits/modules/give.ts @@ -0,0 +1,135 @@ +// Dependencies +import { CommandInteraction, ColorResolvable } from 'discord.js'; + +// Configurations +import config from '../../../../../config.json'; + +// Handlers +import logger from '../../../../handlers/logger'; + +// Helpers +import creditNoun from '../../../../helpers/creditNoun'; + +// Models +import userSchema from '../../../../helpers/database/models/userSchema'; + +// Function +export default async (interaction: CommandInteraction) => { + // Destructure + const { guild, user, options } = interaction; + + // User option + const optionUser = options?.getUser('user'); + + // Amount option + const optionAmount = options?.getInteger('amount'); + + // If amount option is null + if (optionAmount === null) { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Give]' as string, + description: 'We could not read your requested amount.' as string, + color: config?.colors?.error as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + } + + // If amount is zero or below + if (optionAmount <= 0) { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Give]' as string, + description: 'You can not give zero credits or below.' as string, + color: config?.colors?.error as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + } + + // toUser Information + const toUser = await userSchema?.findOne({ + userId: optionUser?.id, + guildId: guild?.id, + }); + + // If toUser does not exist + if (!toUser) { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Give]' as string, + description: `We could not find ${optionUser} in our database.` as string, + color: config?.colors?.error as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + } + + // If toUser.credits does not exist + if (!toUser?.credits) { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Give]' as string, + description: + `We could not find credits for ${optionUser} in our database.` as string, + color: config?.colors?.error as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + } + + // Deposit amount to toUser + toUser.credits += optionAmount; + + // Save toUser + await toUser?.save()?.then(async () => { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Give]' as string, + description: `We have given ${optionUser}, ${creditNoun( + optionAmount + )}.` as string, + color: config?.colors?.success as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Log debug message + logger?.debug( + `Guild: ${guild?.id} User: ${user?.id} gave ${ + optionUser?.id + } ${creditNoun(optionAmount)}.` + ); + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + }); +}; diff --git a/src/commands/admin/credits/modules/set.ts b/src/commands/admin/credits/modules/set.ts new file mode 100644 index 0000000..17f107a --- /dev/null +++ b/src/commands/admin/credits/modules/set.ts @@ -0,0 +1,117 @@ +// Dependencies +import { Permissions, CommandInteraction, ColorResolvable } from 'discord.js'; + +// Configurations +import config from '../../../../../config.json'; + +// Handlers +import logger from '../../../../handlers/logger'; + +// Helpers +import creditNoun from '../../../../helpers/creditNoun'; + +// Models +import userSchema from '../../../../helpers/database/models/userSchema'; + +// Function +export default async (interaction: CommandInteraction) => { + // Destructure + const { options, user, guild } = interaction; + + // User Option + const optionUser = options.getUser('user'); + + // Amount Option + const optionAmount = options.getInteger('amount'); + + // If amount is null + if (optionAmount === null) { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Set]' as string, + description: 'We could not read your requested amount.' as string, + color: config?.colors?.error as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Send interaction reply + return await interaction?.editReply({ embeds: [embed] }); + } + + // toUser Information + const toUser = await userSchema?.findOne({ + userId: optionUser?.id, + guildId: guild?.id, + }); + + // If toUser does not exist + if (!toUser) { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Set]' as string, + description: `We could not find ${optionUser} in our database.` as string, + color: config?.colors?.error as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + } + + // If toUser.credits does not exist + if (!toUser?.credits) { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Set]' as string, + description: + `We could not find credits for ${optionUser} in our database.` as string, + color: config?.colors?.error as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + } + + // Set toUser with amount + toUser.credits = optionAmount; + + // Save toUser + await toUser?.save()?.then(async () => { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Set]' as string, + description: `We have set ${optionUser} to ${creditNoun( + optionAmount + )}` as string, + color: config?.colors?.success as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Log debug message + logger?.debug( + `Guild: ${guild?.id} User: ${user?.id} set ${ + optionUser?.id + } to ${creditNoun(optionAmount)}.` + ); + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + }); +}; diff --git a/src/commands/admin/credits/modules/take.ts b/src/commands/admin/credits/modules/take.ts new file mode 100644 index 0000000..36e6e51 --- /dev/null +++ b/src/commands/admin/credits/modules/take.ts @@ -0,0 +1,135 @@ +// Dependencies +import { Permissions, CommandInteraction, ColorResolvable } from 'discord.js'; + +// Configurations +import config from '../../../../../config.json'; + +// Handlers +import logger from '../../../../handlers/logger'; + +// Helpers +import creditNoun from '../../../../helpers/creditNoun'; + +// Models +import userSchema from '../../../../helpers/database/models/userSchema'; + +// Function +export default async (interaction: CommandInteraction) => { + // Destructure + const { guild, user, options } = interaction; + + // User option + const optionUser = options?.getUser('user'); + + // Amount option + const optionAmount = options?.getInteger('amount'); + + // If amount is null + if (optionAmount === null) { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Take]' as string, + description: 'We could not read your requested amount.' as string, + color: config?.colors?.error as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Send interaction reply + return await interaction?.editReply({ embeds: [embed] }); + } + + // If amount is zero or below + if (optionAmount <= 0) { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Take]' as string, + description: 'You can not take zero credits or below.' as string, + color: config?.colors?.error as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + } + + // toUser Information + const toUser = await userSchema?.findOne({ + userId: optionUser?.id, + guildId: guild?.id, + }); + + // If toUser does not exist + if (!toUser) { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Take]' as string, + description: `We could not find ${optionUser} in our database.` as string, + color: config?.colors?.error as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + } + + // If toUser.credits does not exist + if (!toUser?.credits) { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Take]' as string, + description: + `We could not find credits for ${optionUser} in our database.` as string, + color: config?.colors?.error as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + } + + // Withdraw amount from toUser + toUser.credits -= optionAmount; + + // Save toUser + await toUser?.save()?.then(async () => { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Set]' as string, + description: `We have taken ${creditNoun( + optionAmount + )} from ${optionUser}` as string, + color: config?.colors?.success as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Log debug message + logger?.debug( + `Guild: ${guild?.id} User: ${user?.id} set ${ + optionUser?.id + } to ${creditNoun(optionAmount)}.` + ); + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + }); +}; diff --git a/src/commands/admin/credits/modules/transfer.ts b/src/commands/admin/credits/modules/transfer.ts new file mode 100644 index 0000000..0a09d51 --- /dev/null +++ b/src/commands/admin/credits/modules/transfer.ts @@ -0,0 +1,177 @@ +// Dependencies +import { CommandInteraction, ColorResolvable } from 'discord.js'; + +// Configurations +import config from '../../../../../config.json'; + +// Handlers +import logger from '../../../../handlers/logger'; + +// Helpers +import creditNoun from '../../../../helpers/creditNoun'; +import saveUser from '../../../../helpers/saveUser'; + +// Models +import userSchema from '../../../../helpers/database/models/userSchema'; + +// Function +export default async (interaction: CommandInteraction) => { + // Destructure member + const { guild, options, user } = interaction; + + // Get options + const optionFromUser = options?.getUser('from'); + const optionToUser = options?.getUser('to'); + const optionAmount = options?.getInteger('amount'); + + // If amount is null + if (optionAmount === null) { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Transfer]' as string, + description: 'We could not read your requested amount.' as string, + color: config?.colors?.error as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Send interaction reply + return await interaction?.editReply({ embeds: [embed] }); + } + + // Get fromUser object + const fromUser = await userSchema?.findOne({ + userId: optionFromUser?.id, + guildId: guild?.id, + }); + + // Get toUser object + const toUser = await userSchema.findOne({ + userId: optionToUser?.id, + guildId: guild?.id, + }); + + // If toUser does not exist + if (!fromUser) { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Transfer]' as string, + description: + `We could not find ${optionFromUser} in our database.` as string, + color: config?.colors?.error as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + } + + // If toUser.credits does not exist + if (!fromUser?.credits) { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Transfer]' as string, + description: + `We could not find credits for ${optionFromUser} in our database.` as string, + color: config?.colors?.error as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + } + + // If toUser does not exist + if (!toUser) { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Transfer]' as string, + description: + `We could not find ${optionToUser} in our database.` as string, + color: config?.colors?.error as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + } + + // If toUser.credits does not exist + if (!toUser?.credits) { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Transfer]' as string, + description: + `We could not find credits for ${optionToUser} in our database.` as string, + color: config?.colors?.error as ColorResolvable, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + } + + // Withdraw amount from fromUser + fromUser.credits -= optionAmount; + + // Deposit amount to toUser + toUser.credits += optionAmount; + + // Save users + await saveUser(fromUser, toUser)?.then(async () => { + // Embed object + const embed = { + title: ':toolbox: Admin - Credits [Transfer]' as string, + description: `You sent ${creditNoun( + optionAmount + )} from ${optionFromUser} to ${optionToUser}.` as string, + color: config?.colors?.success as ColorResolvable, + fields: [ + { + name: `${optionFromUser?.username as string} Balance`, + value: `${fromUser?.credits as string}`, + inline: true, + }, + { + name: `${optionToUser?.username as string} Balance`, + value: `${toUser?.credits as string}`, + inline: true, + }, + ], + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Log debug message + logger?.debug( + `Guild: ${guild?.id} User: ${user?.id} transferred ${creditNoun( + optionAmount + )} from ${optionFromUser?.id} to ${optionToUser?.id}.` + ); + + // Return interaction reply + return await interaction?.editReply({ embeds: [embed] }); + }); +}; diff --git a/src/commands/admin/index.ts b/src/commands/admin/index.ts index 43f0daa..fcb3435 100644 --- a/src/commands/admin/index.ts +++ b/src/commands/admin/index.ts @@ -1,9 +1,18 @@ +//Dependencies import { SlashCommandBuilder } from '@discordjs/builders'; +import { CommandInteraction, ColorResolvable, Permissions } from 'discord.js'; + +// Configurations +import config from '../../../config.json'; + +// Handlers +import logger from '../../handlers/logger'; + +// Groups import credits from './credits'; -import counter from './counter'; - -import { CommandInteraction } from 'discord.js'; +import counters from './counters'; +// Function export default { data: new SlashCommandBuilder() .setName('admin') @@ -89,7 +98,7 @@ export default { ) .addSubcommandGroup((group) => group - .setName('counter') + .setName('counters') .setDescription('Manage counters.') .addSubcommand((command) => command @@ -124,16 +133,45 @@ export default { ) ), async execute(interaction: CommandInteraction) { - // If subcommand group is credits - if (interaction.options.getSubcommandGroup() === 'credits') { - // Execute credits group - await credits(interaction); + // Destructure + const { memberPermissions, options, user, commandName, guild } = + interaction; + + // Check permission + if (!memberPermissions?.has(Permissions?.FLAGS?.MANAGE_GUILD)) { + // Embed object + const embed = { + title: ':toolbox: Admin' as string, + color: config?.colors?.error as ColorResolvable, + description: 'You do not have permission to manage this!' as string, + timestamp: new Date() as Date, + footer: { + iconURL: config?.footer?.icon as string, + text: config?.footer?.text as string, + }, + }; + + // Return interaction reply + return interaction?.editReply({ embeds: [embed] }); } - // If subcommand group is credits - else if (interaction.options.getSubcommandGroup() === 'counter') { - // Execute credits group - await counter(interaction); + // Group - Credits + if (options?.getSubcommandGroup() === 'credits') { + // Execute Group - Credits + return await credits(interaction); } + + // Group - Counters + else if (options?.getSubcommandGroup() === 'counters') { + // Execute Group - Counters + return await counters(interaction); + } + + // Send debug message + return logger?.debug( + `Guild: ${guild?.id} User: ${ + user?.id + } executed /${commandName} ${options?.getSubcommandGroup()} ${options?.getSubcommand()}` + ); }, };