Skip to content

Commit

Permalink
Improved plugin loading
Browse files Browse the repository at this point in the history
  • Loading branch information
IMXNOOBX committed Oct 21, 2024
1 parent 4fca88e commit 201e05c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ChatInputCommandInteraction } from "discord.js"
import { plugins } from "@/discord"

export default {
name: 'mongodb',
Expand All @@ -18,22 +19,24 @@ export default {
},
],
run: async (interaction: ChatInputCommandInteraction) => {
const show = interaction.options.getBoolean('show')
const user = interaction.options.getString('user')
const mongodb = plugins.get('mongodb')

if (
!client.mongodb
!mongodb
)
return interaction.followUp({ content: `> 🤯 MongoDB plugin not loaded` });

const { models } = await client.mongodb
const { models } = await mongodb;

const show = interaction.options.getBoolean('show')
const user = interaction.options.getString('user')

if (
show
) {
const users = await models.User.find()

return interaction.followUp({ content: `> 😉 Users: ${users.map(u => u.username).join(', ') || 'No users in the db'}` })
return interaction.followUp({ content: `> 😉 Users: ${users.map((u: any) => u.username).join(', ') || 'No users in the db'}` })
}

if (
Expand All @@ -44,14 +47,14 @@ export default {

if (
!username
) return interaction.followUp({ content: `> 🤯 You need to provide a username` })
) return interaction.followUp({ content: `> 🩻 You need to provide a username` })

const newUser = new models.User({ username, email: email || '[email protected]' })
await newUser.save()

return interaction.followUp({ content: `> 🚀 User added: ${username}` })
}

return interaction.followUp({ content: `> 🤯 This is not how you should use the command >:(` })
return interaction.followUp({ content: `> 🤨 This is not how you should use the command >:(` })
}
}
4 changes: 2 additions & 2 deletions src/discord/commands/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import log from "@/utilities/log"
*/

export default {
name: 'complete',
name: 'options',
description: 'Show how to use many different options in a command',
options: [
{
Expand Down Expand Up @@ -74,7 +74,7 @@ export default {
const role = interaction.options.getRole('role') || 'No role provided';
const mentionable = interaction.options.getMentionable('mentionable') || 'No mentionable provided';
const number = interaction.options.getNumber('number') || 'No number provided';
const attachment = interaction.options.getAttachment('attachment') || 'No attachment provided';
const attachment = interaction.options.getAttachment('attachment')?.name || 'No attachment provided';

log.info(
'string:', string,
Expand Down
4 changes: 3 additions & 1 deletion src/discord/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import log from "@/utilities/log";

const commands = new Collection<string, any>();
const aliases = new Collection<string, string>();
const plugins = new Collection<string, any>();
var client: Client;

const init = async () => {
Expand All @@ -29,5 +30,6 @@ export {
init,

client,
commands, aliases
commands, aliases,
plugins
};
3 changes: 1 addition & 2 deletions src/handlers/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ export default async () => {
continue;
}

log.info(`commands - Loaded command: ${cmd.name}`);
commands.set(cmd.name, cmd);

if (cmd.aliases && Array.isArray(cmd.aliases))
cmd.aliases.forEach((alias: string) => aliases.set(alias, cmd.name));
}

log.info(`commands - Loaded ${commands.size} commands`);
log.info(`commands - Loaded ${commands.size} commands: ${[...commands.keys()].join(', ')}`);

/**
* @brief Register slash commands once the bot is ready
Expand Down
3 changes: 2 additions & 1 deletion src/handlers/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import log from '@/utilities/log';
import { plugins } from '@/discord';

export default async () =>{
log.info('plugins - Loading plugins...');
Expand All @@ -20,7 +21,7 @@ export default async () =>{
)
return log.info('plugins - No plugins found, skipping...');

const plugins = new Map();
// const plugins = new Map();

for (const file of files) {
let plugin;
Expand Down

0 comments on commit 201e05c

Please sign in to comment.