🏗️ plugin-based architecture

This commit is contained in:
Axel Olausson Holtenäs 2022-04-13 17:22:22 +02:00
parent bc6ef0c117
commit 12b970b23e
No known key found for this signature in database
GPG key ID: 9347A5E873995701
43 changed files with 61 additions and 32 deletions

View file

@ -0,0 +1,3 @@
import counter from "./counter";
export default { counter };

View file

@ -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;

View file

@ -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}`
);
});
});
};

View file

@ -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());
}

3
src/helpers/index.ts Normal file
View file

@ -0,0 +1,3 @@
import pluralize from "./pluralize";
export default { pluralize };

View file

@ -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);
}
},
};

View file

@ -0,0 +1,3 @@
import view from "./view";
export default { view };

View file

@ -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,
});

View file

@ -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);
}
},
};

View file

@ -24,7 +24,7 @@ export default {
.setRequired(false)
);
},
execute: async (interaction: CommandInteraction) => {
execute: async (interaction: CommandInteraction, tools: any) => {
// Destructure
const { options, user, guild } = interaction;

View file

@ -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");

View file

@ -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 });

View file

@ -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;

View file

@ -16,6 +16,7 @@ import counters from "./groups/counters";
// Function
export default {
metadata: { author: "Zyner" },
data: new SlashCommandBuilder()
.setName("manage")
.setDescription("Manage your guild.")

View file

@ -10,6 +10,7 @@ import logger from "../../logger";
// Function
export default {
metadata: { author: "Zyner" },
data: new SlashCommandBuilder()
.setName("profile")
.setDescription("Check a profile.")

View file

@ -10,6 +10,7 @@ import logger from "../../logger";
// Function
export default {
metadata: { author: "Zyner" },
data: new SlashCommandBuilder()
.setName("reputation")
.setDescription("Give reputation.")

View file

@ -11,6 +11,7 @@ import logger from "../../logger";
// Function
export default {
metadata: { author: "Zyner" },
data: new SlashCommandBuilder()
.setName("settings")
.setDescription("Manage settings.")

View file

@ -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")

View file

@ -12,6 +12,7 @@ import logger from "../../logger";
// Function
export default {
metadata: { author: "Zyner" },
data: new SlashCommandBuilder()
.setName("utilities")
.setDescription("Common utilities.")

5
src/tools/index.ts Normal file
View file

@ -0,0 +1,5 @@
import helpers from "../helpers";
import config from "../../config.json";
import schemas from "../database/schemas";
export default { helpers, config, schemas };