🏗️ plugin-based architecture
This commit is contained in:
parent
bc6ef0c117
commit
12b970b23e
43 changed files with 61 additions and 32 deletions
3
src/database/schemas/index.ts
Normal file
3
src/database/schemas/index.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import counter from "./counter";
|
||||||
|
|
||||||
|
export default { counter };
|
|
@ -3,6 +3,8 @@ import config from "../../../config.json";
|
||||||
import logger from "../../logger";
|
import logger from "../../logger";
|
||||||
import guilds from "../../database/schemas/guild";
|
import guilds from "../../database/schemas/guild";
|
||||||
|
|
||||||
|
import tools from "../../tools";
|
||||||
|
|
||||||
export default async (interaction: CommandInteraction) => {
|
export default async (interaction: CommandInteraction) => {
|
||||||
if (!interaction.isCommand()) return;
|
if (!interaction.isCommand()) return;
|
||||||
|
|
||||||
|
@ -22,7 +24,7 @@ export default async (interaction: CommandInteraction) => {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Execute command
|
// Execute command
|
||||||
await command.execute(interaction);
|
await command.execute(interaction, tools);
|
||||||
|
|
||||||
const { commandName, user } = interaction;
|
const { commandName, user } = interaction;
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,23 @@
|
||||||
import fs from "fs"; // fs
|
import fs from "fs"; // fs
|
||||||
import { Collection } from "discord.js"; // discord.js
|
import { Collection } from "discord.js"; // discord.js
|
||||||
import { Client } from "../types/common/discord";
|
import { Client } from "../types/common/discord";
|
||||||
|
import logger from "../logger";
|
||||||
|
|
||||||
export default async (client: Client) => {
|
export default async (client: Client) => {
|
||||||
client.commands = new Collection();
|
client.commands = new Collection();
|
||||||
const commandFiles = fs.readdirSync("./src/commands");
|
|
||||||
|
|
||||||
for (const file of commandFiles) {
|
fs.readdir("./src/plugins", async (error: any, plugins: any) => {
|
||||||
const command = require(`../commands/${file}`);
|
if (error) {
|
||||||
client.commands.set(command.default.data.name, command.default);
|
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}`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,11 +6,11 @@ import { Routes } from "discord-api-types/v9";
|
||||||
|
|
||||||
export default async () => {
|
export default async () => {
|
||||||
const commands = [];
|
const commands = [];
|
||||||
const commandFiles = fs.readdirSync("./src/commands");
|
const commandFiles = fs.readdirSync("./src/plugins");
|
||||||
|
|
||||||
for (const file of commandFiles) {
|
for (const file of commandFiles) {
|
||||||
// eslint-disable-next-line global-require
|
// eslint-disable-next-line global-require
|
||||||
const command = require(`../commands/${file}`);
|
const command = require(`../plugins/${file}`);
|
||||||
commands.push(command.default.data.toJSON());
|
commands.push(command.default.data.toJSON());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
3
src/helpers/index.ts
Normal file
3
src/helpers/index.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import pluralize from "./pluralize";
|
||||||
|
|
||||||
|
export default { pluralize };
|
|
@ -3,19 +3,20 @@ import { CommandInteraction } from "discord.js";
|
||||||
import { SlashCommandBuilder } from "@discordjs/builders";
|
import { SlashCommandBuilder } from "@discordjs/builders";
|
||||||
|
|
||||||
// Modules
|
// Modules
|
||||||
import moduleView from "./modules/view";
|
import modules from "./modules";
|
||||||
|
|
||||||
// Function
|
// Function
|
||||||
export default {
|
export default {
|
||||||
|
metadata: { author: "Zyner" },
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
.setName("counters")
|
.setName("counters")
|
||||||
.setDescription("Manage counters.")
|
.setDescription("Manage counters.")
|
||||||
.addSubcommand(moduleView.data),
|
.addSubcommand(modules?.view?.data),
|
||||||
async execute(interaction: CommandInteraction) {
|
async execute(interaction: CommandInteraction, tools: any) {
|
||||||
const { options } = interaction;
|
const { options } = interaction;
|
||||||
|
|
||||||
if (options?.getSubcommand() === "view") {
|
if (options?.getSubcommand() === "view") {
|
||||||
return moduleView.execute(interaction);
|
return modules?.view?.execute(interaction, tools);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
3
src/plugins/counters/modules/index.ts
Normal file
3
src/plugins/counters/modules/index.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import view from "./view";
|
||||||
|
|
||||||
|
export default { view };
|
|
@ -1,15 +1,8 @@
|
||||||
// Dependencies
|
// Dependencies
|
||||||
|
import { CommandInteraction, ColorResolvable, MessageEmbed } from "discord.js";
|
||||||
import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
|
import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
|
||||||
import { ChannelType } from "discord-api-types/v10";
|
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 {
|
export default {
|
||||||
data: (command: SlashCommandSubcommandBuilder) => {
|
data: (command: SlashCommandSubcommandBuilder) => {
|
||||||
return command
|
return command
|
||||||
|
@ -23,12 +16,13 @@ export default {
|
||||||
.addChannelType(ChannelType.GuildText as number)
|
.addChannelType(ChannelType.GuildText as number)
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
execute: async (interaction: CommandInteraction) => {
|
execute: async (interaction: CommandInteraction, tools: any) => {
|
||||||
const { options, guild } = interaction;
|
const { options, guild } = interaction;
|
||||||
|
const { colors, footer } = tools.config;
|
||||||
|
|
||||||
const discordChannel = options?.getChannel("channel");
|
const discordChannel = options?.getChannel("channel");
|
||||||
|
|
||||||
const counter = await counterSchema?.findOne({
|
const counter = await tools.schemas.counter?.findOne({
|
||||||
guildId: guild?.id,
|
guildId: guild?.id,
|
||||||
channelId: discordChannel?.id,
|
channelId: discordChannel?.id,
|
||||||
});
|
});
|
|
@ -10,6 +10,7 @@ import moduleWork from "./modules/work";
|
||||||
|
|
||||||
// Function
|
// Function
|
||||||
export default {
|
export default {
|
||||||
|
metadata: { author: "Zyner" },
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
.setName("credits")
|
.setName("credits")
|
||||||
.setDescription("Manage your credits.")
|
.setDescription("Manage your credits.")
|
||||||
|
@ -17,23 +18,23 @@ export default {
|
||||||
.addSubcommand(moduleGift.data)
|
.addSubcommand(moduleGift.data)
|
||||||
.addSubcommand(moduleTop.data)
|
.addSubcommand(moduleTop.data)
|
||||||
.addSubcommand(moduleWork.data),
|
.addSubcommand(moduleWork.data),
|
||||||
async execute(interaction: CommandInteraction) {
|
async execute(interaction: CommandInteraction, tools: any) {
|
||||||
const { options } = interaction;
|
const { options } = interaction;
|
||||||
|
|
||||||
if (options?.getSubcommand() === "balance") {
|
if (options?.getSubcommand() === "balance") {
|
||||||
return moduleBalance.execute(interaction);
|
return moduleBalance.execute(interaction, tools);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options?.getSubcommand() === "gift") {
|
if (options?.getSubcommand() === "gift") {
|
||||||
return moduleGift.execute(interaction);
|
return moduleGift.execute(interaction, tools);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options?.getSubcommand() === "top") {
|
if (options?.getSubcommand() === "top") {
|
||||||
return moduleTop.execute(interaction);
|
return moduleTop.execute(interaction, tools);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options?.getSubcommand() === "work") {
|
if (options?.getSubcommand() === "work") {
|
||||||
return moduleWork.execute(interaction);
|
return moduleWork.execute(interaction, tools);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
|
@ -24,7 +24,7 @@ export default {
|
||||||
.setRequired(false)
|
.setRequired(false)
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
execute: async (interaction: CommandInteraction) => {
|
execute: async (interaction: CommandInteraction, tools: any) => {
|
||||||
// Destructure
|
// Destructure
|
||||||
const { options, user, guild } = interaction;
|
const { options, user, guild } = interaction;
|
||||||
|
|
|
@ -37,7 +37,7 @@ export default {
|
||||||
option.setName("reason").setDescription("Your reason.")
|
option.setName("reason").setDescription("Your reason.")
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
execute: async (interaction: CommandInteraction) => {
|
execute: async (interaction: CommandInteraction, tools: any) => {
|
||||||
const { options, user, guild, client } = interaction;
|
const { options, user, guild, client } = interaction;
|
||||||
|
|
||||||
const optionUser = options?.getUser("user");
|
const optionUser = options?.getUser("user");
|
|
@ -16,7 +16,7 @@ export default {
|
||||||
data: (command: SlashCommandSubcommandBuilder) => {
|
data: (command: SlashCommandSubcommandBuilder) => {
|
||||||
return command.setName("top").setDescription("Check the top balance.");
|
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
|
// Get all users in the guild
|
||||||
|
|
||||||
const usersDB = await userSchema.find({ guildId: interaction?.guild?.id });
|
const usersDB = await userSchema.find({ guildId: interaction?.guild?.id });
|
|
@ -22,7 +22,7 @@ export default {
|
||||||
data: (command: SlashCommandSubcommandBuilder) => {
|
data: (command: SlashCommandSubcommandBuilder) => {
|
||||||
return command.setName("work").setDescription("Work for credits.");
|
return command.setName("work").setDescription("Work for credits.");
|
||||||
},
|
},
|
||||||
execute: async (interaction: CommandInteraction) => {
|
execute: async (interaction: CommandInteraction, tools: any) => {
|
||||||
// Destructure member
|
// Destructure member
|
||||||
const { guild, user } = interaction;
|
const { guild, user } = interaction;
|
||||||
|
|
|
@ -16,6 +16,7 @@ import counters from "./groups/counters";
|
||||||
|
|
||||||
// Function
|
// Function
|
||||||
export default {
|
export default {
|
||||||
|
metadata: { author: "Zyner" },
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
.setName("manage")
|
.setName("manage")
|
||||||
.setDescription("Manage your guild.")
|
.setDescription("Manage your guild.")
|
|
@ -10,6 +10,7 @@ import logger from "../../logger";
|
||||||
|
|
||||||
// Function
|
// Function
|
||||||
export default {
|
export default {
|
||||||
|
metadata: { author: "Zyner" },
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
.setName("profile")
|
.setName("profile")
|
||||||
.setDescription("Check a profile.")
|
.setDescription("Check a profile.")
|
|
@ -10,6 +10,7 @@ import logger from "../../logger";
|
||||||
|
|
||||||
// Function
|
// Function
|
||||||
export default {
|
export default {
|
||||||
|
metadata: { author: "Zyner" },
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
.setName("reputation")
|
.setName("reputation")
|
||||||
.setDescription("Give reputation.")
|
.setDescription("Give reputation.")
|
|
@ -11,6 +11,7 @@ import logger from "../../logger";
|
||||||
|
|
||||||
// Function
|
// Function
|
||||||
export default {
|
export default {
|
||||||
|
metadata: { author: "Zyner" },
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
.setName("settings")
|
.setName("settings")
|
||||||
.setDescription("Manage settings.")
|
.setDescription("Manage settings.")
|
|
@ -13,9 +13,10 @@ import logger from "../../logger";
|
||||||
|
|
||||||
// Function
|
// Function
|
||||||
export default {
|
export default {
|
||||||
|
metadata: { author: "Zyner" },
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
.setName("shop")
|
.setName("shop")
|
||||||
.setDescription("Open our shop.")
|
.setDescription("Purchase some items using your credits.")
|
||||||
.addSubcommand((subcommand) =>
|
.addSubcommand((subcommand) =>
|
||||||
subcommand
|
subcommand
|
||||||
.setName("pterodactyl")
|
.setName("pterodactyl")
|
|
@ -12,6 +12,7 @@ import logger from "../../logger";
|
||||||
|
|
||||||
// Function
|
// Function
|
||||||
export default {
|
export default {
|
||||||
|
metadata: { author: "Zyner" },
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
.setName("utilities")
|
.setName("utilities")
|
||||||
.setDescription("Common utilities.")
|
.setDescription("Common utilities.")
|
5
src/tools/index.ts
Normal file
5
src/tools/index.ts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import helpers from "../helpers";
|
||||||
|
import config from "../../config.json";
|
||||||
|
import schemas from "../database/schemas";
|
||||||
|
|
||||||
|
export default { helpers, config, schemas };
|
Loading…
Add table
Reference in a new issue