From 63d7152a61ca2e8922e04461fbb869ff743647b6 Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Mon, 11 Apr 2022 12:36:33 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20shop=20role=20colors=20and=20devMod?= =?UTF-8?q?e=20and=20fix=20delete=20role?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.json.example | 3 +- src/commands/shop/index.ts | 63 ++++++++++++++------------ src/commands/shop/roles/modules/buy.ts | 25 +++++----- src/events/ready/index.ts | 31 +++++++++---- src/handlers/schedules.ts | 22 +++++---- src/helpers/deployCommands.ts | 31 +++++++------ src/index.ts | 14 +++--- 7 files changed, 109 insertions(+), 80 deletions(-) diff --git a/config.json.example b/config.json.example index f5908b2..15e1f6a 100644 --- a/config.json.example +++ b/config.json.example @@ -29,5 +29,6 @@ }, "secretKey": "SET A LONG RANDOM PASSWORD HERE, ITS FOR USE OF ENCRYPTION WITH LENGTH OF 32", "importToDB": false, - "clearUnused": false + "clearUnused": false, + "devMode": false } diff --git a/src/commands/shop/index.ts b/src/commands/shop/index.ts index 621d5cc..24d5f28 100644 --- a/src/commands/shop/index.ts +++ b/src/commands/shop/index.ts @@ -1,54 +1,61 @@ // Dependencies -import { SlashCommandBuilder } from "@discordjs/builders"; -import { CommandInteraction } from "discord.js"; +import { SlashCommandBuilder } from '@discordjs/builders'; +import { CommandInteraction } from 'discord.js'; // Modules -import pterodactyl from "./modules/pterodactyl"; +import pterodactyl from './modules/pterodactyl'; // Groups -import roles from "./roles"; +import roles from './roles'; // Handlers -import logger from "../../handlers/logger"; +import logger from '../../handlers/logger'; // Function export default { data: new SlashCommandBuilder() - .setName("shop") - .setDescription("Open our shop.") + .setName('shop') + .setDescription('Open our shop.') .addSubcommand((subcommand) => subcommand - .setName("pterodactyl") - .setDescription("Buy pterodactyl power.") + .setName('pterodactyl') + .setDescription('Buy pterodactyl power.') .addIntegerOption((option) => option - .setName("amount") - .setDescription("How much credits you want to withdraw.") + .setName('amount') + .setDescription('How much credits you want to withdraw.') ) ) .addSubcommandGroup((group) => group - .setName("roles") - .setDescription("Manage custom roles.") + .setName('roles') + .setDescription('Manage custom roles.') .addSubcommand((command) => command - .setName("buy") - .setDescription("Buy a custom role") + .setName('buy') + .setDescription('Buy a custom role') .addStringOption((option) => option - .setName("name") - .setDescription("Name of the role you wish to purchase.") + .setName('name') + .setDescription('Name of the role you wish to purchase.') + ) + .addStringOption((option) => + option + .setName('color') + .setDescription( + 'Color of the role you wish to purchase (For example RED or BLUE or GREEN).' + ) ) ) - .addSubcommand((command) => - command - .setName("cancel") - .setDescription("Cancel a custom role") - .addRoleOption((option) => - option - .setName("role") - .setDescription("Name of the role you wish to cancel.") - ) + ) + .addSubcommand((command) => + command + .setName('cancel') + .setDescription('Cancel a custom role') + .addRoleOption((option) => + option + .setName('role') + .setDescription('Name of the role you wish to cancel.') ) ), async execute(interaction: CommandInteraction) { @@ -56,13 +63,13 @@ export default { const { options, commandName, user, guild } = interaction; // Module - Pterodactyl - if (options?.getSubcommand() === "pterodactyl") { + if (options?.getSubcommand() === 'pterodactyl') { // Execute Module - Pterodactyl return pterodactyl(interaction); } // Group - Roles - else if (options?.getSubcommandGroup() === "roles") { + else if (options?.getSubcommandGroup() === 'roles') { // Execute Group - Roles return roles(interaction); } diff --git a/src/commands/shop/roles/modules/buy.ts b/src/commands/shop/roles/modules/buy.ts index 533e68d..a8c091a 100644 --- a/src/commands/shop/roles/modules/buy.ts +++ b/src/commands/shop/roles/modules/buy.ts @@ -3,31 +3,32 @@ import { CommandInteraction, ColorResolvable, GuildMemberRoleManager, -} from "discord.js"; +} from 'discord.js'; // Configurations -import config from "../../../../../config.json"; +import config from '../../../../../config.json'; // Models -import shopRolesSchema from "../../../../helpers/database/models/shopRolesSchema"; -import guildSchema from "../../../../helpers/database/models/guildSchema"; +import shopRolesSchema from '../../../../helpers/database/models/shopRolesSchema'; +import guildSchema from '../../../../helpers/database/models/guildSchema'; // Helpers -import creditNoun from "../../../../helpers/creditNoun"; -import fetchUser from "../../../../helpers/fetchUser"; +import creditNoun from '../../../../helpers/creditNoun'; +import fetchUser from '../../../../helpers/fetchUser'; // Function export default async (interaction: CommandInteraction) => { const { options, guild, user, member } = interaction; - const optionName = options?.getString("name"); + const optionName = options?.getString('name'); + const optionColor = options?.getString('color'); // If amount is null if (optionName === null) { // Embed object const embed = { - title: ":dollar: Shop - Roles [Buy]" as string, - description: "We could not read your requested name." as string, + title: ':dollar: Shop - Roles [Buy]' as string, + description: 'We could not read your requested name.' as string, color: config?.colors?.error as ColorResolvable, timestamp: new Date(), footer: { @@ -43,7 +44,7 @@ export default async (interaction: CommandInteraction) => { await guild?.roles .create({ name: optionName, - color: "RED", + color: optionColor as ColorResolvable, reason: `${user?.id} bought from shop`, }) .then(async (role) => { @@ -74,13 +75,13 @@ export default async (interaction: CommandInteraction) => { await shopRolesSchema?.find()?.then((role: any) => console.log(role)); const embed = { - title: ":shopping_cart: Shop - Roles [Buy]" as string, + title: ':shopping_cart: Shop - Roles [Buy]' as string, description: `You have bought ${role?.name} for ${guildDB?.shop?.roles?.pricePerHour} per hour.` as string, color: config?.colors?.success as ColorResolvable, fields: [ { - name: "Your balance" as string, + name: 'Your balance' as string, value: `${creditNoun(userDB?.credits)}` as string, }, ], diff --git a/src/events/ready/index.ts b/src/events/ready/index.ts index 2cb390c..c13100c 100644 --- a/src/events/ready/index.ts +++ b/src/events/ready/index.ts @@ -1,15 +1,15 @@ -import logger from "../../handlers/logger"; -import config from "../../../config.json"; -import deployCommands from "../../helpers/deployCommands"; -import dbGuildFix from "../../helpers/dbGuildFix"; -import dbMemberFix from "../../helpers/dbMemberFix"; +import logger from '../../handlers/logger'; +import config from '../../../config.json'; +import deployCommands from '../../helpers/deployCommands'; +import dbGuildFix from '../../helpers/dbGuildFix'; +import dbMemberFix from '../../helpers/dbMemberFix'; -import userSchema from "../../helpers/database/models/userSchema"; +import userSchema from '../../helpers/database/models/userSchema'; -import { Client } from "discord.js"; -import updatePresence from "../../helpers/updatePresence"; +import { Client } from 'discord.js'; +import updatePresence from '../../helpers/updatePresence'; export default { - name: "ready", + name: 'ready', once: true, async execute(client: Client) { // Send info message @@ -30,6 +30,19 @@ export default { }); } + if (client === null) return; + if (client.application === null) return; + + if (!config?.devMode) { + client?.application?.commands + ?.set([], config.bot.guildId) + .then(async (commands) => { + logger.info( + `Removed all guild based commands from ${config.bot.guildId}` + ); + }); + } + if (config.clearUnused) { await userSchema.find().then( async (result) => diff --git a/src/handlers/schedules.ts b/src/handlers/schedules.ts index dfd1940..db13154 100644 --- a/src/handlers/schedules.ts +++ b/src/handlers/schedules.ts @@ -1,12 +1,12 @@ -import schedule from "node-schedule"; -import users from "../helpers/database/models/userSchema"; -import shopRoles from "../helpers/database/models/shopRolesSchema"; -import guilds from "../helpers/database/models/guildSchema"; -import logger from "./logger"; -import { Client } from "discord.js"; +import schedule from 'node-schedule'; +import users from '../helpers/database/models/userSchema'; +import shopRoles from '../helpers/database/models/shopRolesSchema'; +import guilds from '../helpers/database/models/guildSchema'; +import logger from './logger'; +import { Client } from 'discord.js'; export default async (client: Client) => { - schedule.scheduleJob("*/5 * * * *", async () => { + schedule.scheduleJob('*/5 * * * *', async () => { shopRoles.find().then(async (shopRoles: any) => { shopRoles.map(async (shopRole: any) => { const payed = new Date(shopRole.lastPayed); @@ -37,10 +37,12 @@ export default async (client: Client) => { const rGuild = await client.guilds.cache.get(`${shopRole.guildId}`); const rMember = await rGuild?.members.fetch(`${shopRole.userId}`); + shopRoles.deleteOne({ _id: shopRole._id }); + await rMember?.roles .remove(`${shopRole.roleId}`) - .then(async (test) => console.log("4", test)) - .catch(async (test) => console.log("5", test)); // Removes all roles + .then(async (test) => console.log('4', test)) + .catch(async (test) => console.log('5', test)); // Removes all roles } shopRole.lastPayed = new Date(); @@ -54,6 +56,6 @@ export default async (client: Client) => { }); }); - await logger.debug("Checking schedules! (Every 5 minutes)"); + await logger.debug('Checking schedules! (Every 5 minutes)'); }); }; diff --git a/src/helpers/deployCommands.ts b/src/helpers/deployCommands.ts index 827b637..383ac53 100644 --- a/src/helpers/deployCommands.ts +++ b/src/helpers/deployCommands.ts @@ -20,17 +20,22 @@ export default async () => { body: commands, }); - await rest - .put( - Routes.applicationGuildCommands(config.bot.clientId, config.bot.guildId), - { - body: commands, - } - ) - .then(async () => - logger.info('Successfully registered application commands.') - ) - .catch(async (err) => { - await logger.error(err); - }); + if (config?.devMode) { + await rest + .put( + Routes.applicationGuildCommands( + config.bot.clientId, + config.bot.guildId + ), + { + body: commands, + } + ) + .then(async () => + logger.info('Successfully registered application commands.') + ) + .catch(async (err) => { + await logger.error(err); + }); + } }; diff --git a/src/index.ts b/src/index.ts index 3a27750..c67e48e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,13 @@ // Dependencies -import { Client, Intents } from "discord.js"; // discord.js +import { Client, Intents } from 'discord.js'; // discord.js -import database from "./helpers/database"; -import events from "./handlers/events"; -import commands from "./handlers/commands"; -import locale from "./handlers/locale"; -import schedules from "./handlers/schedules"; +import database from './helpers/database'; +import events from './handlers/events'; +import commands from './handlers/commands'; +import locale from './handlers/locale'; +import schedules from './handlers/schedules'; -import config from "../config.json"; // config.json +import config from '../config.json'; // config.json (async () => { // Initialize discord.js client