Ayomide Omolewa
Ayomide Omolewa

Reputation: 35

Unable to use module changes from typescript declaration file

src/index.d.ts:



    declare module "discord.js" {
        export interface Client {
            commands: import('discord.js').Collection<unknown, Command>,
            handleEvents(eventFiles: string[], path: string): any,
            handleCommands(eventFiles: string[], path: string): any,
            commandArray: any
        }

        export interface Command {
            name: string,
            description: string,
            execute: (message: import('discord.js').Message, args?: string[]) => Promise<Command> // Can be `Promise<SomeType>` if using async
        }
    }

    export {}

Whenever I use this, when I use the .handleCommands and .handleEvents, I get:

Property 'handleCommands' does not exist on type 'typeof Client'

I try and use it here src/index.ts

import DiscordJS, { Intents, Collection, Client } from 'discord.js'
import fs from 'fs'
require('dotenv').config();

export const client: Client = new DiscordJS.Client({ intents: [Intents.FLAGS.GUILDS] });
client.commands = new Collection();

const functions = fs.readdirSync('./src/functions').filter((file: string) => file.endsWith('.ts'));
const eventFiles = fs.readdirSync('./src/events').filter((file: string) => file.endsWith('.ts'));
const commandFolders = fs.readdirSync('./src/commands').filter((file: string) => file.endsWith('.ts'));

(async () => {
    for (const  file of functions) {
        require(`./functions/${file}`)(client);
    }
    client.handleEvents(eventFiles, './src/events');
    client.handleCommands(commandFolders, './src/commands');
    client.login(process.env.TOKEN);
})

Please assist me, I don't know what else I can do.

Upvotes: 0

Views: 127

Answers (1)

iamkneel
iamkneel

Reputation: 1498

You've used the declaration file correctly, however this only provides the typings for the commands, not the actual code to execute - you have to provide that yourself:

client.handleEvents = (eventFiles, path) => { /* ... */ }
client.handleCommands = (eventFiles, path) => { /* ... */ }

Edit: Also I noticed that how you typed Client#commands and Command#execute is wrong; the ESM import() function returns a promise, one you are not resolving. An easier solution would be to import them at the top of the file statically, like this:

import { Collection, Message } from "discord.js";

declare module "discord.js" {
  export interface Client {
    commands: Collection<unknown, Command>
    // ...
  }
  export interface Command {
    // ...
    execute: (message: Message, args?: string[]) => Promise<Command>
  }
}

Additionally, I don't think the export {} at the end of the file is required.

Upvotes: 1

Related Questions