From 12b970b23e8e5d52d7e6e45b804fd16fa44d1118 Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Wed, 13 Apr 2022 17:22:22 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=EF=B8=8F=20plugin-based=20architec?= =?UTF-8?q?ture?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/database/schemas/index.ts | 3 +++ src/events/interactionCreate/isCommand.ts | 4 +++- src/handlers/commands.ts | 21 ++++++++++++++----- src/handlers/deployCommands.ts | 4 ++-- src/helpers/index.ts | 3 +++ src/{commands => plugins}/counters/index.ts | 9 ++++---- src/plugins/counters/modules/index.ts | 3 +++ .../counters/modules/view/index.ts | 14 ++++--------- src/{commands => plugins}/credits/index.ts | 11 +++++----- .../credits/modules/balance/index.ts | 2 +- .../credits/modules/gift/index.ts | 2 +- .../credits/modules/top/index.ts | 2 +- .../credits/modules/work/index.ts | 2 +- .../manage/groups/counters/index.ts | 0 .../groups/counters/modules/create/index.ts | 0 .../groups/counters/modules/delete/index.ts | 0 .../manage/groups/credits/index.ts | 0 .../groups/credits/modules/give/index.ts | 0 .../groups/credits/modules/set/index.ts | 0 .../groups/credits/modules/take/index.ts | 0 .../groups/credits/modules/transfer/index.ts | 0 src/{commands => plugins}/manage/index.ts | 1 + src/{commands => plugins}/profile/index.ts | 1 + .../profile/modules/view.ts | 0 src/{commands => plugins}/reputation/index.ts | 1 + .../reputation/modules/give.ts | 0 .../settings/guild/addons/credits.ts | 0 .../settings/guild/addons/points.ts | 0 .../settings/guild/addons/pterodactyl.ts | 0 .../settings/guild/index.ts | 0 src/{commands => plugins}/settings/index.ts | 1 + .../settings/user/index.ts | 0 .../settings/user/modules/appearance.ts | 0 src/{commands => plugins}/shop/index.ts | 3 ++- .../shop/modules/pterodactyl.ts | 0 src/{commands => plugins}/shop/roles/index.ts | 0 .../shop/roles/modules/buy.ts | 0 .../shop/roles/modules/cancel.ts | 0 src/{commands => plugins}/utilities/index.ts | 1 + .../utilities/modules/about.ts | 0 .../utilities/modules/lookup.ts | 0 .../utilities/modules/stats.ts | 0 src/tools/index.ts | 5 +++++ 43 files changed, 61 insertions(+), 32 deletions(-) create mode 100644 src/database/schemas/index.ts create mode 100644 src/helpers/index.ts rename src/{commands => plugins}/counters/index.ts (61%) create mode 100644 src/plugins/counters/modules/index.ts rename src/{commands => plugins}/counters/modules/view/index.ts (85%) rename src/{commands => plugins}/credits/index.ts (73%) rename src/{commands => plugins}/credits/modules/balance/index.ts (97%) rename src/{commands => plugins}/credits/modules/gift/index.ts (99%) rename src/{commands => plugins}/credits/modules/top/index.ts (95%) rename src/{commands => plugins}/credits/modules/work/index.ts (98%) rename src/{commands => plugins}/manage/groups/counters/index.ts (100%) rename src/{commands => plugins}/manage/groups/counters/modules/create/index.ts (100%) rename src/{commands => plugins}/manage/groups/counters/modules/delete/index.ts (100%) rename src/{commands => plugins}/manage/groups/credits/index.ts (100%) rename src/{commands => plugins}/manage/groups/credits/modules/give/index.ts (100%) rename src/{commands => plugins}/manage/groups/credits/modules/set/index.ts (100%) rename src/{commands => plugins}/manage/groups/credits/modules/take/index.ts (100%) rename src/{commands => plugins}/manage/groups/credits/modules/transfer/index.ts (100%) rename src/{commands => plugins}/manage/index.ts (97%) rename src/{commands => plugins}/profile/index.ts (97%) rename src/{commands => plugins}/profile/modules/view.ts (100%) rename src/{commands => plugins}/reputation/index.ts (97%) rename src/{commands => plugins}/reputation/modules/give.ts (100%) rename src/{commands => plugins}/settings/guild/addons/credits.ts (100%) rename src/{commands => plugins}/settings/guild/addons/points.ts (100%) rename src/{commands => plugins}/settings/guild/addons/pterodactyl.ts (100%) rename src/{commands => plugins}/settings/guild/index.ts (100%) rename src/{commands => plugins}/settings/index.ts (99%) rename src/{commands => plugins}/settings/user/index.ts (100%) rename src/{commands => plugins}/settings/user/modules/appearance.ts (100%) rename src/{commands => plugins}/shop/index.ts (96%) rename src/{commands => plugins}/shop/modules/pterodactyl.ts (100%) rename src/{commands => plugins}/shop/roles/index.ts (100%) rename src/{commands => plugins}/shop/roles/modules/buy.ts (100%) rename src/{commands => plugins}/shop/roles/modules/cancel.ts (100%) rename src/{commands => plugins}/utilities/index.ts (98%) rename src/{commands => plugins}/utilities/modules/about.ts (100%) rename src/{commands => plugins}/utilities/modules/lookup.ts (100%) rename src/{commands => plugins}/utilities/modules/stats.ts (100%) create mode 100644 src/tools/index.ts diff --git a/src/database/schemas/index.ts b/src/database/schemas/index.ts new file mode 100644 index 0000000..5ba7679 --- /dev/null +++ b/src/database/schemas/index.ts @@ -0,0 +1,3 @@ +import counter from "./counter"; + +export default { counter }; diff --git a/src/events/interactionCreate/isCommand.ts b/src/events/interactionCreate/isCommand.ts index d5a3837..03e457d 100644 --- a/src/events/interactionCreate/isCommand.ts +++ b/src/events/interactionCreate/isCommand.ts @@ -3,6 +3,8 @@ import config from "../../../config.json"; import logger from "../../logger"; import guilds from "../../database/schemas/guild"; +import tools from "../../tools"; + export default async (interaction: CommandInteraction) => { if (!interaction.isCommand()) return; @@ -22,7 +24,7 @@ export default async (interaction: CommandInteraction) => { try { // Execute command - await command.execute(interaction); + await command.execute(interaction, tools); const { commandName, user } = interaction; diff --git a/src/handlers/commands.ts b/src/handlers/commands.ts index b98bf4b..f21c2d7 100644 --- a/src/handlers/commands.ts +++ b/src/handlers/commands.ts @@ -1,12 +1,23 @@ import fs from "fs"; // fs import { Collection } from "discord.js"; // discord.js import { Client } from "../types/common/discord"; +import logger from "../logger"; + export default async (client: Client) => { client.commands = new Collection(); - const commandFiles = fs.readdirSync("./src/commands"); - for (const file of commandFiles) { - const command = require(`../commands/${file}`); - client.commands.set(command.default.data.name, command.default); - } + fs.readdir("./src/plugins", async (error: any, plugins: any) => { + if (error) { + return logger?.error(new Error(error)); + } + + await plugins?.map(async (pluginName: any) => { + const plugin = await import(`../plugins/${pluginName}`); + + await client?.commands?.set(plugin?.default?.data?.name, plugin?.default); + logger?.silly( + `Successfully loaded plugin: ${plugin?.default?.data?.name} from ${plugin.default?.metadata?.author}` + ); + }); + }); }; diff --git a/src/handlers/deployCommands.ts b/src/handlers/deployCommands.ts index 81eab86..b68f025 100644 --- a/src/handlers/deployCommands.ts +++ b/src/handlers/deployCommands.ts @@ -6,11 +6,11 @@ import { Routes } from "discord-api-types/v9"; export default async () => { const commands = []; - const commandFiles = fs.readdirSync("./src/commands"); + const commandFiles = fs.readdirSync("./src/plugins"); for (const file of commandFiles) { // eslint-disable-next-line global-require - const command = require(`../commands/${file}`); + const command = require(`../plugins/${file}`); commands.push(command.default.data.toJSON()); } diff --git a/src/helpers/index.ts b/src/helpers/index.ts new file mode 100644 index 0000000..93be57e --- /dev/null +++ b/src/helpers/index.ts @@ -0,0 +1,3 @@ +import pluralize from "./pluralize"; + +export default { pluralize }; diff --git a/src/commands/counters/index.ts b/src/plugins/counters/index.ts similarity index 61% rename from src/commands/counters/index.ts rename to src/plugins/counters/index.ts index 485a63d..da65d12 100644 --- a/src/commands/counters/index.ts +++ b/src/plugins/counters/index.ts @@ -3,19 +3,20 @@ import { CommandInteraction } from "discord.js"; import { SlashCommandBuilder } from "@discordjs/builders"; // Modules -import moduleView from "./modules/view"; +import modules from "./modules"; // Function export default { + metadata: { author: "Zyner" }, data: new SlashCommandBuilder() .setName("counters") .setDescription("Manage counters.") - .addSubcommand(moduleView.data), - async execute(interaction: CommandInteraction) { + .addSubcommand(modules?.view?.data), + async execute(interaction: CommandInteraction, tools: any) { const { options } = interaction; if (options?.getSubcommand() === "view") { - return moduleView.execute(interaction); + return modules?.view?.execute(interaction, tools); } }, }; diff --git a/src/plugins/counters/modules/index.ts b/src/plugins/counters/modules/index.ts new file mode 100644 index 0000000..dc539f8 --- /dev/null +++ b/src/plugins/counters/modules/index.ts @@ -0,0 +1,3 @@ +import view from "./view"; + +export default { view }; diff --git a/src/commands/counters/modules/view/index.ts b/src/plugins/counters/modules/view/index.ts similarity index 85% rename from src/commands/counters/modules/view/index.ts rename to src/plugins/counters/modules/view/index.ts index 77abf9c..bac20ae 100644 --- a/src/commands/counters/modules/view/index.ts +++ b/src/plugins/counters/modules/view/index.ts @@ -1,15 +1,8 @@ // Dependencies +import { CommandInteraction, ColorResolvable, MessageEmbed } from "discord.js"; import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; import { ChannelType } from "discord-api-types/v10"; -import { CommandInteraction, ColorResolvable, MessageEmbed } from "discord.js"; -// Configurations -import { colors, footer } from "../../../../../config.json"; - -// Models -import counterSchema from "../../../../database/schemas/counter"; - -// Function export default { data: (command: SlashCommandSubcommandBuilder) => { return command @@ -23,12 +16,13 @@ export default { .addChannelType(ChannelType.GuildText as number) ); }, - execute: async (interaction: CommandInteraction) => { + execute: async (interaction: CommandInteraction, tools: any) => { const { options, guild } = interaction; + const { colors, footer } = tools.config; const discordChannel = options?.getChannel("channel"); - const counter = await counterSchema?.findOne({ + const counter = await tools.schemas.counter?.findOne({ guildId: guild?.id, channelId: discordChannel?.id, }); diff --git a/src/commands/credits/index.ts b/src/plugins/credits/index.ts similarity index 73% rename from src/commands/credits/index.ts rename to src/plugins/credits/index.ts index 614f72b..61242dc 100644 --- a/src/commands/credits/index.ts +++ b/src/plugins/credits/index.ts @@ -10,6 +10,7 @@ import moduleWork from "./modules/work"; // Function export default { + metadata: { author: "Zyner" }, data: new SlashCommandBuilder() .setName("credits") .setDescription("Manage your credits.") @@ -17,23 +18,23 @@ export default { .addSubcommand(moduleGift.data) .addSubcommand(moduleTop.data) .addSubcommand(moduleWork.data), - async execute(interaction: CommandInteraction) { + async execute(interaction: CommandInteraction, tools: any) { const { options } = interaction; if (options?.getSubcommand() === "balance") { - return moduleBalance.execute(interaction); + return moduleBalance.execute(interaction, tools); } if (options?.getSubcommand() === "gift") { - return moduleGift.execute(interaction); + return moduleGift.execute(interaction, tools); } if (options?.getSubcommand() === "top") { - return moduleTop.execute(interaction); + return moduleTop.execute(interaction, tools); } if (options?.getSubcommand() === "work") { - return moduleWork.execute(interaction); + return moduleWork.execute(interaction, tools); } }, }; diff --git a/src/commands/credits/modules/balance/index.ts b/src/plugins/credits/modules/balance/index.ts similarity index 97% rename from src/commands/credits/modules/balance/index.ts rename to src/plugins/credits/modules/balance/index.ts index f35814c..aec1989 100644 --- a/src/commands/credits/modules/balance/index.ts +++ b/src/plugins/credits/modules/balance/index.ts @@ -24,7 +24,7 @@ export default { .setRequired(false) ); }, - execute: async (interaction: CommandInteraction) => { + execute: async (interaction: CommandInteraction, tools: any) => { // Destructure const { options, user, guild } = interaction; diff --git a/src/commands/credits/modules/gift/index.ts b/src/plugins/credits/modules/gift/index.ts similarity index 99% rename from src/commands/credits/modules/gift/index.ts rename to src/plugins/credits/modules/gift/index.ts index a680fa7..7cd8852 100644 --- a/src/commands/credits/modules/gift/index.ts +++ b/src/plugins/credits/modules/gift/index.ts @@ -37,7 +37,7 @@ export default { option.setName("reason").setDescription("Your reason.") ); }, - execute: async (interaction: CommandInteraction) => { + execute: async (interaction: CommandInteraction, tools: any) => { const { options, user, guild, client } = interaction; const optionUser = options?.getUser("user"); diff --git a/src/commands/credits/modules/top/index.ts b/src/plugins/credits/modules/top/index.ts similarity index 95% rename from src/commands/credits/modules/top/index.ts rename to src/plugins/credits/modules/top/index.ts index 968594d..3d85b7e 100644 --- a/src/commands/credits/modules/top/index.ts +++ b/src/plugins/credits/modules/top/index.ts @@ -16,7 +16,7 @@ export default { data: (command: SlashCommandSubcommandBuilder) => { return command.setName("top").setDescription("Check the top balance."); }, - execute: async (interaction: CommandInteraction) => { + execute: async (interaction: CommandInteraction, tools: any) => { // Get all users in the guild const usersDB = await userSchema.find({ guildId: interaction?.guild?.id }); diff --git a/src/commands/credits/modules/work/index.ts b/src/plugins/credits/modules/work/index.ts similarity index 98% rename from src/commands/credits/modules/work/index.ts rename to src/plugins/credits/modules/work/index.ts index 49d51ad..072ea19 100644 --- a/src/commands/credits/modules/work/index.ts +++ b/src/plugins/credits/modules/work/index.ts @@ -22,7 +22,7 @@ export default { data: (command: SlashCommandSubcommandBuilder) => { return command.setName("work").setDescription("Work for credits."); }, - execute: async (interaction: CommandInteraction) => { + execute: async (interaction: CommandInteraction, tools: any) => { // Destructure member const { guild, user } = interaction; diff --git a/src/commands/manage/groups/counters/index.ts b/src/plugins/manage/groups/counters/index.ts similarity index 100% rename from src/commands/manage/groups/counters/index.ts rename to src/plugins/manage/groups/counters/index.ts diff --git a/src/commands/manage/groups/counters/modules/create/index.ts b/src/plugins/manage/groups/counters/modules/create/index.ts similarity index 100% rename from src/commands/manage/groups/counters/modules/create/index.ts rename to src/plugins/manage/groups/counters/modules/create/index.ts diff --git a/src/commands/manage/groups/counters/modules/delete/index.ts b/src/plugins/manage/groups/counters/modules/delete/index.ts similarity index 100% rename from src/commands/manage/groups/counters/modules/delete/index.ts rename to src/plugins/manage/groups/counters/modules/delete/index.ts diff --git a/src/commands/manage/groups/credits/index.ts b/src/plugins/manage/groups/credits/index.ts similarity index 100% rename from src/commands/manage/groups/credits/index.ts rename to src/plugins/manage/groups/credits/index.ts diff --git a/src/commands/manage/groups/credits/modules/give/index.ts b/src/plugins/manage/groups/credits/modules/give/index.ts similarity index 100% rename from src/commands/manage/groups/credits/modules/give/index.ts rename to src/plugins/manage/groups/credits/modules/give/index.ts diff --git a/src/commands/manage/groups/credits/modules/set/index.ts b/src/plugins/manage/groups/credits/modules/set/index.ts similarity index 100% rename from src/commands/manage/groups/credits/modules/set/index.ts rename to src/plugins/manage/groups/credits/modules/set/index.ts diff --git a/src/commands/manage/groups/credits/modules/take/index.ts b/src/plugins/manage/groups/credits/modules/take/index.ts similarity index 100% rename from src/commands/manage/groups/credits/modules/take/index.ts rename to src/plugins/manage/groups/credits/modules/take/index.ts diff --git a/src/commands/manage/groups/credits/modules/transfer/index.ts b/src/plugins/manage/groups/credits/modules/transfer/index.ts similarity index 100% rename from src/commands/manage/groups/credits/modules/transfer/index.ts rename to src/plugins/manage/groups/credits/modules/transfer/index.ts diff --git a/src/commands/manage/index.ts b/src/plugins/manage/index.ts similarity index 97% rename from src/commands/manage/index.ts rename to src/plugins/manage/index.ts index 18f5dac..7b0eba6 100644 --- a/src/commands/manage/index.ts +++ b/src/plugins/manage/index.ts @@ -16,6 +16,7 @@ import counters from "./groups/counters"; // Function export default { + metadata: { author: "Zyner" }, data: new SlashCommandBuilder() .setName("manage") .setDescription("Manage your guild.") diff --git a/src/commands/profile/index.ts b/src/plugins/profile/index.ts similarity index 97% rename from src/commands/profile/index.ts rename to src/plugins/profile/index.ts index 08b6151..647a4f4 100644 --- a/src/commands/profile/index.ts +++ b/src/plugins/profile/index.ts @@ -10,6 +10,7 @@ import logger from "../../logger"; // Function export default { + metadata: { author: "Zyner" }, data: new SlashCommandBuilder() .setName("profile") .setDescription("Check a profile.") diff --git a/src/commands/profile/modules/view.ts b/src/plugins/profile/modules/view.ts similarity index 100% rename from src/commands/profile/modules/view.ts rename to src/plugins/profile/modules/view.ts diff --git a/src/commands/reputation/index.ts b/src/plugins/reputation/index.ts similarity index 97% rename from src/commands/reputation/index.ts rename to src/plugins/reputation/index.ts index a844fcc..61517c6 100644 --- a/src/commands/reputation/index.ts +++ b/src/plugins/reputation/index.ts @@ -10,6 +10,7 @@ import logger from "../../logger"; // Function export default { + metadata: { author: "Zyner" }, data: new SlashCommandBuilder() .setName("reputation") .setDescription("Give reputation.") diff --git a/src/commands/reputation/modules/give.ts b/src/plugins/reputation/modules/give.ts similarity index 100% rename from src/commands/reputation/modules/give.ts rename to src/plugins/reputation/modules/give.ts diff --git a/src/commands/settings/guild/addons/credits.ts b/src/plugins/settings/guild/addons/credits.ts similarity index 100% rename from src/commands/settings/guild/addons/credits.ts rename to src/plugins/settings/guild/addons/credits.ts diff --git a/src/commands/settings/guild/addons/points.ts b/src/plugins/settings/guild/addons/points.ts similarity index 100% rename from src/commands/settings/guild/addons/points.ts rename to src/plugins/settings/guild/addons/points.ts diff --git a/src/commands/settings/guild/addons/pterodactyl.ts b/src/plugins/settings/guild/addons/pterodactyl.ts similarity index 100% rename from src/commands/settings/guild/addons/pterodactyl.ts rename to src/plugins/settings/guild/addons/pterodactyl.ts diff --git a/src/commands/settings/guild/index.ts b/src/plugins/settings/guild/index.ts similarity index 100% rename from src/commands/settings/guild/index.ts rename to src/plugins/settings/guild/index.ts diff --git a/src/commands/settings/index.ts b/src/plugins/settings/index.ts similarity index 99% rename from src/commands/settings/index.ts rename to src/plugins/settings/index.ts index 437c375..8b7435b 100644 --- a/src/commands/settings/index.ts +++ b/src/plugins/settings/index.ts @@ -11,6 +11,7 @@ import logger from "../../logger"; // Function export default { + metadata: { author: "Zyner" }, data: new SlashCommandBuilder() .setName("settings") .setDescription("Manage settings.") diff --git a/src/commands/settings/user/index.ts b/src/plugins/settings/user/index.ts similarity index 100% rename from src/commands/settings/user/index.ts rename to src/plugins/settings/user/index.ts diff --git a/src/commands/settings/user/modules/appearance.ts b/src/plugins/settings/user/modules/appearance.ts similarity index 100% rename from src/commands/settings/user/modules/appearance.ts rename to src/plugins/settings/user/modules/appearance.ts diff --git a/src/commands/shop/index.ts b/src/plugins/shop/index.ts similarity index 96% rename from src/commands/shop/index.ts rename to src/plugins/shop/index.ts index 1bfb647..c601eac 100644 --- a/src/commands/shop/index.ts +++ b/src/plugins/shop/index.ts @@ -13,9 +13,10 @@ import logger from "../../logger"; // Function export default { + metadata: { author: "Zyner" }, data: new SlashCommandBuilder() .setName("shop") - .setDescription("Open our shop.") + .setDescription("Purchase some items using your credits.") .addSubcommand((subcommand) => subcommand .setName("pterodactyl") diff --git a/src/commands/shop/modules/pterodactyl.ts b/src/plugins/shop/modules/pterodactyl.ts similarity index 100% rename from src/commands/shop/modules/pterodactyl.ts rename to src/plugins/shop/modules/pterodactyl.ts diff --git a/src/commands/shop/roles/index.ts b/src/plugins/shop/roles/index.ts similarity index 100% rename from src/commands/shop/roles/index.ts rename to src/plugins/shop/roles/index.ts diff --git a/src/commands/shop/roles/modules/buy.ts b/src/plugins/shop/roles/modules/buy.ts similarity index 100% rename from src/commands/shop/roles/modules/buy.ts rename to src/plugins/shop/roles/modules/buy.ts diff --git a/src/commands/shop/roles/modules/cancel.ts b/src/plugins/shop/roles/modules/cancel.ts similarity index 100% rename from src/commands/shop/roles/modules/cancel.ts rename to src/plugins/shop/roles/modules/cancel.ts diff --git a/src/commands/utilities/index.ts b/src/plugins/utilities/index.ts similarity index 98% rename from src/commands/utilities/index.ts rename to src/plugins/utilities/index.ts index 60593f5..65fccdb 100644 --- a/src/commands/utilities/index.ts +++ b/src/plugins/utilities/index.ts @@ -12,6 +12,7 @@ import logger from "../../logger"; // Function export default { + metadata: { author: "Zyner" }, data: new SlashCommandBuilder() .setName("utilities") .setDescription("Common utilities.") diff --git a/src/commands/utilities/modules/about.ts b/src/plugins/utilities/modules/about.ts similarity index 100% rename from src/commands/utilities/modules/about.ts rename to src/plugins/utilities/modules/about.ts diff --git a/src/commands/utilities/modules/lookup.ts b/src/plugins/utilities/modules/lookup.ts similarity index 100% rename from src/commands/utilities/modules/lookup.ts rename to src/plugins/utilities/modules/lookup.ts diff --git a/src/commands/utilities/modules/stats.ts b/src/plugins/utilities/modules/stats.ts similarity index 100% rename from src/commands/utilities/modules/stats.ts rename to src/plugins/utilities/modules/stats.ts diff --git a/src/tools/index.ts b/src/tools/index.ts new file mode 100644 index 0000000..3b50cb6 --- /dev/null +++ b/src/tools/index.ts @@ -0,0 +1,5 @@ +import helpers from "../helpers"; +import config from "../../config.json"; +import schemas from "../database/schemas"; + +export default { helpers, config, schemas };