convert js to ts

This commit is contained in:
Mylloon 2022-07-20 11:49:43 +02:00
parent 1a20ace1cc
commit 136fe81b50
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
6 changed files with 54 additions and 37 deletions

View file

@ -1,9 +1,10 @@
import { REST } from '@discordjs/rest'; import { REST } from '@discordjs/rest';
import { Routes } from 'discord-api-types/v9'; import { Routes } from 'discord-api-types/v9';
import { Client } from 'discord.js';
import { readdir } from 'fs/promises'; import { readdir } from 'fs/promises';
export default async client => { export default async (client: Client) => {
const rest = new REST({ version: '9' }).setToken(client.token); const rest = new REST({ version: '9' }).setToken(client.token!);
const command_categories = (await readdir('./src/commands')) const command_categories = (await readdir('./src/commands'))
.filter(element => !element.endsWith('.js')); .filter(element => !element.endsWith('.js'));
@ -31,7 +32,7 @@ export default async client => {
) )
).flat(2); ).flat(2);
return await rest.put(Routes.applicationCommands(client.user.id), { return await rest.put(Routes.applicationCommands(client.user!.id), {
body: commands, body: commands,
}); });
}; };

View file

@ -1,13 +1,13 @@
import { SlashCommandBuilder } from '@discordjs/builders'; import { SlashCommandBuilder } from '@discordjs/builders';
import { Client, CommandInteraction, Message } from 'discord.js';
export default { export default {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName('ping') .setName('ping')
.setDescription('Pong!'), .setDescription('Pong!'),
interaction: async (interaction, client) => { interaction: async (interaction: CommandInteraction, client: Client) => {
const sent = await interaction.reply({ content: 'Pinging...', fetchReply: true }) as Message;
const sent = await interaction.reply({ content: 'Pinging...', fetchReply: true });
interaction.editReply( interaction.editReply(
`Roundtrip latency: \ `Roundtrip latency: \

View file

@ -1,4 +1,6 @@
export default (interaction, client) => { import { Client, Interaction } from 'discord.js';
export default (interaction: Interaction, client: Client) => {
if (interaction.isCommand()) { if (interaction.isCommand()) {
const command = client.commands.get(interaction.commandName); const command = client.commands.get(interaction.commandName);
if (!command) { if (!command) {

View file

@ -1,6 +1,7 @@
import { Client } from 'discord.js';
import { readdir } from 'fs/promises'; import { readdir } from 'fs/promises';
export default async client => { export default async (client: Client) => {
const events_categories = (await readdir('./src/events')) const events_categories = (await readdir('./src/events'))
.filter(element => !element.endsWith('.js')); .filter(element => !element.endsWith('.js'));
@ -16,11 +17,11 @@ export default async client => {
); );
// Remove .js // Remove .js
let event_type = event_file.split('.'); let event_type_ext = event_file.split('.');
if (event_type.pop() !== 'js') { if (event_type_ext.pop() !== 'js') {
throw `Unknown file in ${event_category}: ${event_file}`; throw `Unknown file in ${event_category}: ${event_file}`;
} }
event_type = event_type.join('.'); const event_type = event_type_ext.join('.');
if (once) { if (once) {
return client.once(event_type, (...args) => { return client.once(event_type, (...args) => {

View file

@ -1,6 +1,6 @@
import loadClient from './utils/client.js'; import loadClient from './utils/client';
import loadEvents from './events/loader.js'; import loadEvents from './events/loader';
import loadCommands from './commands/loader.js'; import loadCommands from './commands/loader';
const run = async () => { const run = async () => {
console.log('Starting Botanique...'); console.log('Starting Botanique...');
@ -11,29 +11,28 @@ const run = async () => {
} }
// Client Discord.JS // Client Discord.JS
const client = await loadClient() await loadClient()
.catch(() => { .then(async client => {
console.log('Client ✅');
await loadEvents(client)
.then(_ => console.log('Events ✅'))
.catch(() => {
throw 'Events ❌';
});
await client.login(client.config.token_discord);
await loadCommands(client)
.then(_ => console.log('Commands ✅'))
.catch(() => {
throw 'Commands ❌';
});
console.log(`Botanique "${client.user!.username}" ${client.config.version} started!`);
})
.catch(_ => {
throw 'Client ❌'; throw 'Client ❌';
}); });
if (client) {
console.log('Client ✅');
}
await loadEvents(client)
.then(console.log('Events ✅'))
.catch(() => {
throw 'Events ❌';
});
await client.login(client.config.token_discord);
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)); run().catch(error => console.error(error));

View file

@ -1,11 +1,25 @@
import { Client, Collection, Intents } from 'discord.js'; import { Client, Collection, Intents } from 'discord.js';
import { readFileSync } from 'fs'; import { readFileSync } from 'fs';
import { SlashCommandBuilder } from '@discordjs/builders';
const { version } = JSON.parse(readFileSync('./package.json')); const { version } = JSON.parse(readFileSync('./package.json').toString());
declare module "discord.js" {
export interface Client {
config: {
version: any,
token_discord: any
},
commands: Collection<string, {
data: SlashCommandBuilder,
interaction: (interaction: CommandInteraction, client: Client) => any
}>
}
}
// Création du client et de ses propriétés // Création du client et de ses propriétés
export default async () => { export default async () => {
const client = new Client({ const client: Client = new Client({
intents: [ intents: [
Intents.FLAGS.GUILDS, Intents.FLAGS.GUILDS,
], ],