Progressive-Victory / crm-bot

Internal Discord bot for member utilities
https://progress.win/
MIT License
3 stars 4 forks source link

Core Improvement #54

Closed bowenjw closed 1 year ago

bowenjw commented 1 year ago

Core Updates

Moved client and other classes to a client folder that encapsulates the base functions of the discord bot.

Date

Moved discord date to sting function from prototypes to client types

Date.prototype.toDiscordString = function(format?: TimeCode) {
    const code = Math.floor(this.getTime() / 1000);
    if (!format) return `<t:${code}`;
    return `<t:${code}:${format}>`;
};

Interactions

example using the ping

export default new Interaction<ButtonInteraction>()
    .setName('ping')
    .setExecute(async (interaction) => {
        interaction.reply({
            content: `${t({
                locale: interaction.locale,
                key: 'button',
                ns:'ping',
            })} 🏓`,
            components: [getPingButton(interaction.locale)],
            ephemeral: true,
        });
    });

Event

Example ready event

export default new Event()
    .setName(Events.ClientReady)
    .setOnce(true)
    .setExecute(async (client:Client) => {
        console.log(`\nReady! Logged in as ${client.user?.tag} (${client.user?.id})\n`);
    });

Chat Command

export default new ChatInputCommand()
    .setBuilder(builder)
    .setGlobal(true)
    .setExecute(function)
    .setAutocomplete(function)

Context Command

export default new ContextMenuCommand()
    .setBuilder(builder)
    .setGlobal(false)
    .setExecute(function);

i18n

i18n moved in to its own folder ./src/i18n/

This folder contains an implementations of Project Fluent

How to use

In your main file call the init function.

import { init } from './i18n';

init(path: string, options?: { hasGlobal?: boolean, fallback: Locale });

This function sets the file path where Localization files are stored and additional options:

hasGlobal: if a gobal.ftl file in present in your Localization directory

fallback: deafalt language

Translation

translation options

interface tOptions {
    key: string
    ns?: string
    ons?: string
    locale?: Locale | LocaleString
    args?: Record<string, FluentVariable>
}

Example

key: command-name, namespace: command, locale: fallback language

t({ key: 'command-name', ns: 'command' }

key: count-reply, namespace: count, locale: locale of the interaction args: username and length

t({
    locale: interaction.locale,
    key: 'count-reply',
    ns: 'count',
    args: {
        'username': message.author.username,
        'length':length.toString(),
    }
})