Compare commits

...

2 commits

Author SHA1 Message Date
513244f203
Add ping command 2022-07-03 20:37:28 +02:00
8f33f68fe6
Load commands 2022-07-03 20:37:23 +02:00
4 changed files with 71 additions and 6 deletions

37
src/commands/loader.js Normal file
View file

@ -0,0 +1,37 @@
import { REST } from '@discordjs/rest';
import { Routes } from 'discord-api-types/v9';
import { readdir } from 'fs/promises';
export default async client => {
const rest = new REST({ version: '9' }).setToken(client.token);
const command_categories = (await readdir('./src/commands'))
.filter(element => !element.endsWith('.js'));
const commands = (
await Promise.all(
// For each categorie
command_categories.map(async command_category => {
// Retrieve all the commands
const command_files = await readdir(`./src/commands/${command_category}`);
// Add the command
return Promise.all(
command_files.map(async command_file => {
const command = (
await import(`../commands/${command_category}/${command_file}`)
).default;
client.commands.set(command.data.name, command);
return command.data.toJSON();
}),
);
}),
)
).flat(2);
return await rest.put(Routes.applicationCommands(client.user.id), {
body: commands,
});
};

17
src/commands/misc/ping.js Normal file
View file

@ -0,0 +1,17 @@
import { SlashCommandBuilder } from '@discordjs/builders';
export default {
data: new SlashCommandBuilder()
.setName('ping')
.setDescription('Pong!'),
interaction: async (interaction, client) => {
const sent = await interaction.reply({ content: 'Pinging...', fetchReply: true });
interaction.editReply(
`Roundtrip latency:
${sent.createdTimestamp - interaction.createdTimestamp}ms
Websocket heartbeat: ${client.ws.ping}ms.`);
},
};

View file

@ -1,5 +1,6 @@
import loadClient from './utils/client.js';
import loadEvents from './events/loader.js';
import loadCommands from './commands/loader.js';
const run = async () => {
console.log('Starting Botanique...');
@ -14,19 +15,25 @@ const run = async () => {
.catch(() => {
throw 'Client ❌';
});
if (client) {
console.log('Client ✅');
}
await loadEvents(client)
.then(() => console.log('Events ✅'))
.then(console.log('Events ✅'))
.catch(() => {
throw 'Events ❌';
});
await client.login(client.config.token_discord);
console.log(
`Botanique "${client.user.username}" ${client.config.version} started!`
);
await loadCommands(client)
.then(console.log('Commands ✅'))
.catch(() => {
throw 'Commands ❌';
});
console.log(`Botanique "${client.user.username}" ${client.config.version} started!`);
};
run().catch(error => console.error(error));

View file

@ -1,4 +1,4 @@
import { Client, Intents } from 'discord.js';
import { Client, Collection, Intents } from 'discord.js';
import { readFileSync } from 'fs';
const { version } = JSON.parse(readFileSync('./package.json'));
@ -11,10 +11,14 @@ export default async () => {
],
});
// Store the client configuration
client.config = {
version: version,
token_discord: process.env.TOKEN_DISCORD,
};
// Store the commands available
client.commands = new Collection();
return client;
};