refactor: typescript migration #24

Merged
Anri merged 10 commits from ts-migration into main 2022-07-20 23:01:49 +02:00
6 changed files with 54 additions and 37 deletions
Showing only changes of commit 136fe81b50 - Show all commits

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,16 +11,11 @@ const run = async () => {
} }
// Client Discord.JS // Client Discord.JS
const client = await loadClient() await loadClient()
.catch(() => { .then(async client => {
throw 'Client ❌';
});
if (client) {
console.log('Client ✅'); console.log('Client ✅');
}
await loadEvents(client) await loadEvents(client)
.then(console.log('Events ✅')) .then(_ => console.log('Events ✅'))
.catch(() => { .catch(() => {
throw 'Events ❌'; throw 'Events ❌';
}); });
@ -28,12 +23,16 @@ const run = async () => {
await client.login(client.config.token_discord); await client.login(client.config.token_discord);
await loadCommands(client) await loadCommands(client)
.then(console.log('Commands ✅')) .then(_ => console.log('Commands ✅'))
.catch(() => { .catch(() => {
throw 'Commands ❌'; throw 'Commands ❌';
}); });
console.log(`Botanique "${client.user.username}" ${client.config.version} started!`); console.log(`Botanique "${client.user!.username}" ${client.config.version} started!`);
})
.catch(_ => {
throw 'Client ❌';
});
}; };
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,
], ],