thenorthsolution / Reciple

⚡Discord.js framework that just works
https://reciple.js.org
GNU General Public License v3.0
12 stars 1 forks source link

[TypeScript] Problem with JSON object commands #38

Closed NotM1Dev closed 11 months ago

NotM1Dev commented 11 months ago

Dependencies

typescript v5.2.2 discord.js v14.3.0 @reciple/core v8.1.7 reciple v8.1.7

Issue Description

I found a problem when attempting to create commands as objects (rather than command builders). Right now, reciple expects commands to be an array of command builders, so I can't create a command as an object.

Using command_type: CommandType.SlashCommand removed the error from the command object itself, but the command array throws an error:

Property 'commands' in type 'default' is not assignable to the same property in base type 'RecipleModuleData'.
  Type '{ command_type: CommandType; name: string; description: string; execute({ interaction }: { interaction: ChatInputCommandInteraction<CacheType>; }): void; }[]' is not assignable to type 'AnyCommandResolvable[]'.
    Type '{ command_type: CommandType; name: string; description: string; execute({ interaction }: { interaction: ChatInputCommandInteraction<CacheType>; }): void; }' is not assignable to type 'AnyCommandResolvable'.
      Type '{ command_type: CommandType; name: string; description: string; execute({ interaction }: { interaction: ChatInputCommandInteraction<CacheType>; }): void; }' is not assignable to type 'SlashCommandBuilderData'.
        Types of property 'command_type' are incompatible.
          Type 'CommandType' is not assignable to type 'CommandType.SlashCommand'.

And here's the test code:

src/example.ts

import { ChatInputCommandInteraction } from 'discord.js';
import { CommandType, type RecipleModuleData } from 'reciple';

export default class implements RecipleModuleData {
    public versions: string = '^8';

    public async onStart() {
        return true;
    }

    public commands = [
        {
            command_type: CommandType.SlashCommand,
            name: 'my_command',
            description: 'does a thing',
            execute({ interaction }: { interaction: ChatInputCommandInteraction }) {
                interaction.reply('Test.');
            },
        },
    ];
}
catplvsplus commented 11 months ago

You can use a temporary fix by adding as const keyword after your command object to fix this error while still having the type checks

image

NotM1Dev commented 11 months ago

You can use a temporary fix by adding as const keyword after your command object to fix this error while still having the type checks

image

@NotGhex That's a good idea. However, I've found another solution:

import { ChatInputCommandInteraction } from 'discord.js';
import { CommandType, type RecipleModuleData, type AnyCommandResolvable } from 'reciple';

export default class implements RecipleModuleData {
    public versions: string = '^8';

    public onStart() {
        return true;
    }

    public commands: AnyCommandResolvable[] = [
        {
            name: 'my_command',
            description: 'does a thing',
            command_type: CommandType.SlashCommand,
            execute({ interaction }: { interaction: ChatInputCommandInteraction }) {
                interaction.reply('Test.');
            },
        },
    ];
}

Specifically: public commands: AnyCommandResolvable[] Manually specifying the AnyCommandResolvable[] type fixes this, so I don't need to add as const at the end of every command.