### Install @sapphire/plugin-logger and @sapphire/framework Source: https://github.com/sapphiredev/plugins/blob/main/packages/logger/README.md Command to install the `@sapphire/plugin-logger` package and its required dependency `@sapphire/framework` using npm. Users can substitute `npm install` with their preferred package manager like `yarn add` or `pnpm install`. ```sh npm install @sapphire/plugin-logger @sapphire/framework ``` -------------------------------- ### Install @sapphire/plugin-editable-commands and @sapphire/framework Source: https://github.com/sapphiredev/plugins/blob/main/packages/editable-commands/README.md This command installs the `@sapphire/plugin-editable-commands` package along with its required dependency, `@sapphire/framework`, using npm. Users can substitute `npm install` with their preferred package manager. ```sh npm install @sapphire/plugin-editable-commands @sapphire/framework ``` -------------------------------- ### Install @sapphire/plugin-api and Dependencies Source: https://github.com/sapphiredev/plugins/blob/main/packages/api/README.md This command installs the `@sapphire/plugin-api` package along with its required dependencies: `@sapphire/framework`, `discord.js`, and `discord-api-types`. Use your preferred package manager. ```sh npm install @sapphire/plugin-api @sapphire/framework discord.js discord-api-types ``` -------------------------------- ### Install @sapphire/plugin-utilities-store Source: https://github.com/sapphiredev/plugins/blob/main/packages/utilities-store/README.md Command to install the @sapphire/plugin-utilities-store package and its required dependencies using npm. This prepares your project to use the plugin's features. ```sh npm install @sapphire/plugin-utilities-store ``` -------------------------------- ### Install Sapphire Pattern Commands Plugin and Dependencies Source: https://github.com/sapphiredev/plugins/blob/main/packages/pattern-commands/README.md This shell command installs the `@sapphire/plugin-pattern-commands` package along with its essential peer dependencies. These dependencies include `@sapphire/framework`, `@sapphire/utilities`, `@sapphire/stopwatch`, and `discord.js`, all required for the plugin's proper operation. ```sh npm install @sapphire/plugin-pattern-commands @sapphire/framework @sapphire/utilities @sapphire/stopwatch discord.js ``` -------------------------------- ### Install @sapphire/plugin-subcommands and Dependencies Source: https://github.com/sapphiredev/plugins/blob/main/packages/subcommands/README.md Command to install the `@sapphire/plugin-subcommands` package along with its required peer dependencies (`@sapphire/framework`, `@sapphire/utilities`, `discord.js`) using npm. ```sh npm install @sapphire/plugin-subcommands @sapphire/framework @sapphire/utilities discord.js ``` -------------------------------- ### Install Sapphire Scheduled Tasks Plugin Source: https://github.com/sapphiredev/plugins/blob/main/packages/scheduled-tasks/README.md Instructions to install the `@sapphire/plugin-scheduled-tasks` package along with its dependency `@sapphire/framework` using npm. This command also installs `bullmq` implicitly as a dependency of the plugin. ```sh npm install @sapphire/plugin-scheduled-tasks @sapphire/framework ``` -------------------------------- ### Install @sapphire/plugin-hmr and Dependencies Source: https://github.com/sapphiredev/plugins/blob/main/packages/hmr/README.md Command to install the @sapphire/plugin-hmr package along with its required dependency, @sapphire/framework, using npm. ```sh npm install @sapphire/plugin-hmr @sapphire/framework ``` -------------------------------- ### Install @sapphire/plugin-i18next and Dependencies Source: https://github.com/sapphiredev/plugins/blob/main/packages/i18next/README.md Installs the `@sapphire/plugin-i18next` package along with its required dependencies: `@sapphire/framework` and `discord.js`. This command uses npm, but can be replaced with your package manager of choice (e.g., yarn, pnpm). ```sh npm install @sapphire/plugin-i18next @sapphire/framework discord.js ``` -------------------------------- ### Configure npm Scripts for HMR Development Workflow Source: https://github.com/sapphiredev/plugins/blob/main/packages/hmr/README.md Defines npm scripts in `package.json` for a development workflow with HMR, including `dev` to run `watch` and `start` concurrently, `build` for TypeScript compilation, `watch` for continuous compilation, and `start` to run the compiled application. This setup facilitates automatic recompilation and application restart for HMR. ```json "scripts": { "dev": "run-p watch start", "build": "tsc", "watch": "tsc --watch", "start": "node dist/index.js" } ``` -------------------------------- ### Define a Custom Utility Class Source: https://github.com/sapphiredev/plugins/blob/main/packages/utilities-store/README.md Demonstrates how to create a custom utility class by extending `Utility` from `@sapphire/plugin-utilities-store`. This example defines `add` and `subtract` methods for basic arithmetic operations, which will be accessible via the Sapphire container after the bot logs in. ```typescript import { Utility } from '@sapphire/plugin-utilities-store'; export class SumUtility extends Utility { public constructor(context: Utility.LoaderContext, options: Utility.Options) { super(context, { ...options, name: 'sum' }); } public add(numberOne: number, numberTwo: number) { return numberOne + numberTwo; } public subtract(numberOne: number, numberTwo: number) { return numberOne - numberTwo; } } ``` -------------------------------- ### Register @sapphire/plugin-editable-commands plugin Source: https://github.com/sapphiredev/plugins/blob/main/packages/editable-commands/README.md This snippet demonstrates how to register the `@sapphire/plugin-editable-commands` plugin in an application. It imports and executes the plugin's registration file, typically done in the main or setup file of the application. ```javascript require('@sapphire/plugin-editable-commands/register'); ``` ```typescript import '@sapphire/plugin-editable-commands/register'; ``` -------------------------------- ### Run HMR Development Script Source: https://github.com/sapphiredev/plugins/blob/main/packages/hmr/README.md Command to start the application in development mode, leveraging the configured `dev` script in `package.json` for Hot Module Replacement. This command initiates the concurrent watch and start processes. ```sh npm run dev ``` -------------------------------- ### Create an editable Discord command with @sapphire/plugin-editable-commands Source: https://github.com/sapphiredev/plugins/blob/main/packages/editable-commands/README.md This example shows how to create a Discord command using `@sapphire/framework` that leverages `@sapphire/plugin-editable-commands` for editable messages. It defines a command class, sets options, and uses the `send` function from the plugin to send an embed that can be edited later. ```javascript const { Command } = require('@sapphire/framework'); const { MessageEmbed } = require('discord.js'); const { send } = require('@sapphire/plugin-editable-commands'); module.exports = class UserCommand extends Command { constructor(context, options) { super(context, { ...options, description: 'A very cool command', requiredClientPermissions: ['EMBED_LINKS'] }); } messageRun(message) { const embed = new MessageEmbed() .setURL('https://github.com/skyra-project/editable-commands') .setColor('#7586D8') .setDescription('Example description') .setTimestamp(); return send(message, { embeds: [embed] }); } }; ``` ```typescript import { ApplyOptions } from '@sapphire/decorators'; import { Command } from '@sapphire/framework'; import { Message, MessageEmbed } from 'discord.js'; import { send } from '@sapphire/plugin-editable-commands'; @ApplyOptions({ description: 'A very cool command', requiredClientPermissions: ['EMBED_LINKS'] }) export class UserCommand extends Command { public messageRun(message: Message) { const embed = new MessageEmbed() .setURL('https://github.com/sapphiredev/plugins') .setColor('#7586D8') .setDescription('Example description') .setTimestamp(); return send(message, { embeds: [embed] }); } } ``` -------------------------------- ### Define and Enforce Scheduled Task Payloads with Module Augmentation Source: https://github.com/sapphiredev/plugins/blob/main/packages/scheduled-tasks/UPGRADING-v9-v10.md This TypeScript code demonstrates how to define and enforce payload types for scheduled tasks using module augmentation on the `ScheduledTasks` interface. It illustrates examples for tasks with required payloads, no payloads, and optional payloads, showing both valid and type-error-producing `container.tasks.create` calls to highlight the new type enforcement. ```typescript declare module '@sapphire/plugin-scheduled-tasks' { interface ScheduledTasks { [ExampleTasks.One]: { data: string } | null; [ExampleTasks.Two]: never; [ExampleTasks.Three]: boolean | undefined; } } /** ExampleTasks.One */ // Good await container.tasks.create({ name: ExampleTasks.One, payload: { data: 'value' } }); await container.tasks.create({ name: ExampleTasks.One, payload: null }); // Type error await container.tasks.create({ name: ExampleTasks.One, payload: { data: true } }); await container.tasks.create({ name: ExampleTasks.One, payload: false }); /** ExampleTasks.Two */ // Good await container.tasks.create(ExampleTasks.Two); await container.tasks.create({ name: ExampleTasks.Two }); // Type error await container.tasks.create({ name: ExampleTasks.Two, payload: null }); /** ExampleTasks.Three */ // Good await container.tasks.create(ExampleTasks.Three); await container.tasks.create({ name: ExampleTasks.Three }); await container.tasks.create({ name: ExampleTasks.Three, payload: true }); ``` -------------------------------- ### Configure Sapphire Scheduled Tasks Plugin Options Source: https://github.com/sapphiredev/plugins/blob/main/packages/scheduled-tasks/README.md Demonstrates how to configure the scheduled tasks plugin within the SapphireClient options. This example shows how to customize the Redis connection settings for BullMQ, including port, password, host, and database number, to connect to a specific Redis instance. ```typescript const options = { ...otherClientOptionsGoHere, tasks: { /* You can add your Bull options here, for example we can configure custom Redis connection options: */ bull: { connection: { port: 8888, // Defaults to 6379, but if your Redis server runs on another port configure it here password: 'very-strong-password', // If your Redis server requires a password configure it here host: 'localhost', // The host at which the redis server is found db: 2 // Redis database number, defaults to 0 but can be any value between 0 and 15 } } } }; ``` -------------------------------- ### Use resolveKey to Fetch Translated Text Source: https://github.com/sapphiredev/plugins/blob/main/packages/i18next/README.md Demonstrates how to use the `resolveKey` function to retrieve translated text by its key. This function can be used anywhere in your application to get localized strings, typically within a command's `messageRun` method to send translated responses. ```typescript import { resolveKey } from '@sapphire/plugin-i18next'; import { Command } from '@sapphire/framework'; import type { Message } from 'discord.js'; export class PingCommand extends Command { public constructor(context: Command.LoaderContext, options: Command.Options) { super(context, { ...options, description: 'ping pong' }); } public async messageRun(message: Message) { await message.channel.send(await resolveKey('commands/ping:success')); } } ``` -------------------------------- ### Create Scheduled Task from a Command Source: https://github.com/sapphiredev/plugins/blob/main/packages/scheduled-tasks/README.md Illustrates how to schedule a task from within a Sapphire command. This 'mute' command example creates an 'unmute' task to run after 1 minute, demonstrating the use of `this.container.tasks` directly within a piece without explicit import. This is common for actions requiring delayed follow-ups. ```typescript // mute command import { Command, CommandOptions } from '@sapphire/framework'; import type { Message } from 'discord.js'; export class MuteCommand extends Command { public constructor(context: Command.LoaderContext) { super(context, { description: 'Mute a user' }); } public async run(message: Message) { // create a task to unmute the user in 1 minute const payload = { authorId: message.author.id }; this.container.tasks.create({ name: 'unmute', payload }, 60_000); } } ``` -------------------------------- ### Define an Interval-Based Scheduled Task Class Source: https://github.com/sapphiredev/plugins/blob/main/packages/scheduled-tasks/README.md Shows how to define a custom scheduled task class that runs at a fixed interval. This example configures the task to execute every minute (60,000 milliseconds) and includes the required module augmentation for type declaration. ```typescript import { ScheduledTask } from '@sapphire/plugin-scheduled-tasks'; export class IntervalTask extends ScheduledTask { public constructor(context: ScheduledTask.LoaderContext, options: ScheduledTask.Options) { super(context, { ...options, interval: 60_000 // 60 seconds }); } public async run() { this.container.logger.info('I run every minute'); } } declare module '@sapphire/plugin-scheduled-tasks' { interface ScheduledTasks { interval: never; } } ``` -------------------------------- ### Define a Pattern-Based Scheduled Task Class Source: https://github.com/sapphiredev/plugins/blob/main/packages/scheduled-tasks/README.md Illustrates how to define a custom scheduled task class that executes based on a cron-like pattern. This example sets the task to run every hour at the 0-minute mark and includes the necessary module augmentation for type safety. ```typescript import { ScheduledTask } from '@sapphire/plugin-scheduled-tasks'; export class PatternTask extends ScheduledTask { public constructor(context: ScheduledTask.LoaderContext, options: ScheduledTask.Options) { super(context, { ...options, pattern: '0 * * * *' }); } public async run() { this.container.logger.info('I run every hour at *:00'); } } declare module '@sapphire/plugin-scheduled-tasks' { interface ScheduledTasks { pattern: never; } } ``` -------------------------------- ### Create Scheduled Task from a Service Source: https://github.com/sapphiredev/plugins/blob/main/packages/scheduled-tasks/README.md Example of creating a scheduled task from a custom service class. It demonstrates accessing the `container.tasks` property to schedule a task named 'awesome' with a specific payload to run after a 2-second delay. This approach is suitable for scheduling tasks from non-piece classes. ```typescript import { container } from '@sapphire/framework'; export class MyAwesomeService { public createAwesomeTask() { const payload = { awesome: true }; container.tasks.create({ name: 'awesome', payload }, 2000); } } ``` -------------------------------- ### Define a Manual Scheduled Task Handler Source: https://github.com/sapphiredev/plugins/blob/main/packages/scheduled-tasks/README.md Example of defining a custom scheduled task handler. This code shows how to create a class that extends `ScheduledTask`, implement its `run` method to process the task payload, and declare the task type for TypeScript type safety. Task handlers are stored like other Sapphire pieces. ```typescript import { ScheduledTask } from '@sapphire/plugin-scheduled-tasks'; export class ManualTask extends ScheduledTask { public constructor(context: ScheduledTask.LoaderContext, options: ScheduledTask.Options) { super(context, options); } public async run(payload: unknown) { this.container.logger.info('I ran!', payload); } } declare module '@sapphire/plugin-scheduled-tasks' { interface ScheduledTasks { manual: never; } } ``` -------------------------------- ### Configure @sapphire/plugin-api Options Source: https://github.com/sapphiredev/plugins/blob/main/packages/api/README.md This configuration object defines various settings for the API plugin within your `SapphireClient` extension or constructor. It includes authentication details, API prefix, origin control, and server listening options like the port. ```js { auth: { // The application/client ID of your bot. // You can find this at https://discord.com/developers/applications id: '', // The client secret of your bot. // You can find this at https://discord.com/developers/applications secret: '', // The name of the authentication cookie. cookie: 'SAPPHIRE_AUTH', // The URL that users should be redirected to after a successful authentication redirect: '', // The scopes that should be given to the authentication. scopes: [OAuth2Scopes.Identify], // Transformers to transform the raw data from Discord to a different structure. transformers: [] }, // The prefix for all routes, e.g. / or v1/. prefix: '', // The origin header to be set on every request at 'Access-Control-Allow-Origin. origin: '*', // Any options passed to the NodeJS "net" internal server.listen function // See https://nodejs.org/api/net.html#net_server_listen_options_callback listenOptions: { // The port the API will listen on. port: 4000 } } ``` -------------------------------- ### Register @sapphire/plugin-api Plugin Source: https://github.com/sapphiredev/plugins/blob/main/packages/api/README.md To enable the API plugin, import its registration file in your project. This can be done using `require` for JavaScript or `import` for TypeScript. ```js require('@sapphire/plugin-api/register'); ``` ```typescript import '@sapphire/plugin-api/register'; ``` -------------------------------- ### Define a Basic Sapphire Pattern Command Source: https://github.com/sapphiredev/plugins/blob/main/packages/pattern-commands/README.md This TypeScript code demonstrates how to create a simple `PatternCommand` named `AwooCommand`. It utilizes the `@ApplyOptions` decorator to configure command aliases ('cat', 'postman') and a specific chance (85%) for the command to execute. The `messageRun` method defines the bot's response, which in this case is to reply 'Woof!' when a matching message is detected. ```typescript import type { Message } from 'discord.js'; import { PatternCommand } from '@sapphire/plugin-pattern-commands'; import { ApplyOptions } from '@sapphire/decorators'; @ApplyOptions({ aliases: ['cat', 'postman'], chance: 85 }) export class AwooCommand extends PatternCommand { public messageRun(message: Message) { message.reply('Woof!'); } } ``` -------------------------------- ### Available Logger Methods Source: https://github.com/sapphiredev/plugins/blob/main/packages/logger/README.md Documents the various logging methods available through the `container.logger` or `this.container.logger` object, each corresponding to a different log level. These methods allow for categorized output in the console. ```APIDOC container.logger: trace(message: string, ...args: any[]): void debug(message: string, ...args: any[]): void info(message: string, ...args: any[]): void warn(message: string, ...args: any[]): void error(message: string, ...args: any[]): void fatal(message: string, ...args: any[]): void ``` -------------------------------- ### Register @sapphire/plugin-utilities-store Source: https://github.com/sapphiredev/plugins/blob/main/packages/utilities-store/README.md Imports and registers the @sapphire/plugin-utilities-store plugin in your project, making its functionalities available. This step is crucial for the plugin to initialize and integrate with the Sapphire framework. ```javascript require('@sapphire/plugin-utilities-store/register'); ``` ```typescript import '@sapphire/plugin-utilities-store/register'; ``` -------------------------------- ### Register Sapphire Pattern Commands Plugin Source: https://github.com/sapphiredev/plugins/blob/main/packages/pattern-commands/README.md This TypeScript import statement is crucial for enabling the `@sapphire/plugin-pattern-commands` within your Sapphire-based bot. It should be placed in your bot's main entry file, typically `Bot.ts` or where your client is initialized, to ensure the plugin's functionality is loaded. ```typescript import '@sapphire/plugin-pattern-commands/register'; ``` -------------------------------- ### Define Subcommand Class Source: https://github.com/sapphiredev/plugins/blob/main/packages/subcommands/README.md Demonstrates how to create a subcommand class by extending `Subcommand` instead of `Command`. It shows how to configure subcommand names and map them to specific message and chat input run methods for handling different subcommand actions. ```typescript import { Subcommand } from '@sapphire/plugin-subcommands'; import { ApplyOptions } from '@sapphire/decorators'; import type { Args } from '@sapphire/framework'; import type { Message, CommandInteraction } from 'discord.js'; // Using ApplyOptions decorator makes it easy to configure @ApplyOptions({ subcommands: [ { name: 'add', messageRun: 'messageAdd', chatInputRun: 'chatInputAdd' }, { name: 'remove', messageRun: 'messageRemove', chatInputRun: 'chatInputRemove' }, { name: 'list', messageRun: 'messageList', chatInputRun: 'chatInputList' } ] }) // Extend `Subcommand` instead of `Command` export class UserCommand extends Subcommand { public async messageAdd(message: Message, args: Args) {} public async messageRemove(message: Message, args: Args) {} public async messageList(message: Message, args: Args) {} public async chatInputAdd(interaction: Subcommand.ChatInputInteraction) {} public async chatInputRemove(interaction: Subcommand.ChatInputInteraction) {} public async chatInputList(interaction: Subcommand.ChatInputInteraction) {} } ``` ```javascript const { Subcommand } = require('@sapphire/plugin-subcommands'); // Extend `Subcommand` instead of `Command` module.exports = class UserCommand extends Subcommand { constructor(context, options) { super(context, { ...options, subcommands: [ { name: 'add', messageRun: 'messageAdd', chatInputRun: 'chatInputAdd' }, { name: 'remove', messageRun: 'messageRemove', chatInputRun: 'chatInputRemove' }, { name: 'list', messageRun: 'messageList', chatInputRun: 'chatInputList' } ] }); } async messageAdd(message, args) {} async messageRemove(message, args) {} async messageList(message, args) {} async chatInputAdd(interaction) {} async chatInputRemove(interaction) {} async chatInputList(interaction) {} }; ``` -------------------------------- ### Register @sapphire/plugin-subcommands Source: https://github.com/sapphiredev/plugins/blob/main/packages/subcommands/README.md Imports the registration file for `@sapphire/plugin-subcommands` to enable its functionalities in your project. This step is crucial before defining any subcommands. ```js require('@sapphire/plugin-subcommands/register'); ``` ```typescript import '@sapphire/plugin-subcommands/register'; ``` -------------------------------- ### Define i18next Translation File Structure Source: https://github.com/sapphiredev/plugins/blob/main/packages/i18next/README.md Illustrates the basic JSON structure for a translation file within the `languages` directory (e.g., `languages/en-US/commands/ping.json`). Keys like 'success' and 'success_with_args' map to translated strings, supporting arguments like `{{latency}}` for dynamic text replacement. ```jsonc // languages/en-US/commands/ping.json { "success": "Pong!", "success_with_args": "Pong! Took me {{latency}}ms to reply" } ``` -------------------------------- ### Log messages from a custom service or module Source: https://github.com/sapphiredev/plugins/blob/main/packages/logger/README.md Demonstrates how to access and use the logger from any place other than a Sapphire piece (e.g., commands, arguments, preconditions). It requires importing the `container` property from `@sapphire/framework` to access `container.logger`. ```typescript import { container } from '@sapphire/framework'; export class MyAwesomeService { public printAwesomeLog() { container.logger.info('log message'); } } ``` -------------------------------- ### Dynamically Register @sapphire/plugin-hmr for Development Source: https://github.com/sapphiredev/plugins/blob/main/packages/hmr/README.md Demonstrates how to dynamically register the @sapphire/plugin-hmr plugin, enabling HMR only in development environments by configuring the SapphireClient. This ensures HMR is active during development without affecting production builds. ```javascript require('@sapphire/plugin-hmr/register'); const client = new SapphireClient({ /* your bot options */ hmr: { enabled: process.env.NODE_ENV === 'development' } }); async function main() { await client.login(); } void main(); ``` ```typescript import '@sapphire/plugin-hmr/register'; const client = new SapphireClient({ /* your bot options */ hmr: { enabled: process.env.NODE_ENV === 'development' } }); async function main() { await client.login(); } void main(); ``` -------------------------------- ### Log messages from a Sapphire Command piece Source: https://github.com/sapphiredev/plugins/blob/main/packages/logger/README.md Illustrates how to use the logger within a Sapphire command. Inside a piece, the logger is directly accessible via `this.container.logger` without needing an explicit `container` import, simplifying logging within command contexts. ```typescript // ping command import { Command } from '@sapphire/framework'; import type { Message } from 'discord.js'; export class PingCommand extends Command { public constructor(context: Command.LoaderContext, options: Command.Options) { super(context, { ...options, description: 'ping pong' }); } public async messageRun(message: Message) { this.container.logger.warn('warning message'); } } ``` -------------------------------- ### Create a Manual Scheduled Task Source: https://github.com/sapphiredev/plugins/blob/main/packages/scheduled-tasks/README.md Demonstrates how to create a manual scheduled task with a specific payload and a timeout using the Sapphire framework's task container. Manual tasks require explicit creation. ```typescript const payload = { awesome: true }; container.tasks.create({ name: 'manual', payload }, 5000); ``` -------------------------------- ### Register @sapphire/plugin-i18next Plugin Source: https://github.com/sapphiredev/plugins/blob/main/packages/i18next/README.md Registers the i18next plugin for the Sapphire client. This import statement should be placed in your main bot file, ensuring the plugin is registered before instantiating the `SapphireClient` to enable all translation methods and options. ```typescript // Main bot file // Be sure to register the plugin before instantiating the client. import '@sapphire/plugin-i18next/register'; ``` -------------------------------- ### Register Sapphire Scheduled Tasks Plugin Source: https://github.com/sapphiredev/plugins/blob/main/packages/scheduled-tasks/README.md Registers the necessary options and methods for the scheduled tasks plugin within the Sapphire client. This import should be placed in the main bot file before instantiating the SapphireClient to ensure proper plugin initialization. ```typescript // Main bot file // Be sure to register the plugin before instantiating the client. import '@sapphire/plugin-scheduled-tasks/register'; ``` -------------------------------- ### Register @sapphire/plugin-logger in main bot file Source: https://github.com/sapphiredev/plugins/blob/main/packages/logger/README.md Registers the `@sapphire/plugin-logger` plugin within the Sapphire client. This import should be placed in the main bot file before instantiating the client to ensure the necessary options and methods are available for logging. ```typescript // Main bot file // Be sure to register the plugin before instantiating the client. import '@sapphire/plugin-logger/register'; ``` -------------------------------- ### Fetch Guild-Specific Language with fetchLanguage Source: https://github.com/sapphiredev/plugins/blob/main/packages/i18next/README.md Demonstrates how to use the `fetchLanguage` function to retrieve the currently configured guild-specific language. This is useful for debugging, logging, or for displaying the active language setting to users. ```typescript import { fetchLanguage } from '@sapphire/plugin-i18next'; import { Command } from '@sapphire/framework'; import type { Message } from 'discord.js'; export class PingCommand extends Command { public constructor(context: Command.LoaderContext, options: Command.Options) { super(context, { ...options, description: 'ping pong' }); } public async messageRun(message: Message) { return message.channel.send(await fetchLanguage(message)); // ===> en-US } } ``` -------------------------------- ### Register @sapphire/plugin-hmr Plugin Source: https://github.com/sapphiredev/plugins/blob/main/packages/hmr/README.md Registers the @sapphire/plugin-hmr plugin in an application, enabling Hot Module Replacement features. This is the simplest way to enable the plugin. ```javascript require('@sapphire/plugin-hmr/register'); ``` ```typescript import '@sapphire/plugin-hmr/register'; ``` -------------------------------- ### Augment Module for Custom Utility Type Definition Source: https://github.com/sapphiredev/plugins/blob/main/packages/utilities-store/README.md Shows how to use TypeScript module augmentation to declare the type of a custom utility (e.g., `SumUtility`) within the `@sapphire/plugin-utilities-store` module. This ensures type safety and auto-completion when accessing the utility from the Sapphire container, providing a better developer experience. ```typescript declare module '@sapphire/plugin-utilities-store' { export interface Utilities { sum: SumUtility; } } ``` -------------------------------- ### Define Pattern Command with Regex Alias Source: https://github.com/sapphiredev/plugins/blob/main/packages/pattern-commands/README.md This TypeScript snippet illustrates how to use a regular expression as an alias for a `PatternCommand`. By encapsulating the regex pattern `'a{3,}'` as a string within the `aliases` array, the command will trigger for messages containing three or more consecutive 'a' characters, providing advanced pattern matching capabilities. ```typescript @ApplyOptions({ aliases: ['a{3,}'], }) ``` -------------------------------- ### Send Localized Messages with sendLocalized Source: https://github.com/sapphiredev/plugins/blob/main/packages/i18next/README.md Shows how to use the `sendLocalized` function to send a message with translated text to a specified channel. This function simplifies sending localized responses by resolving the translation key and sending the message in a single step. ```typescript import { sendLocalized } from '@sapphire/plugin-i18next'; import { Command } from '@sapphire/framework'; import type { Message } from 'discord.js'; export class PingCommand extends Command { public constructor(context: Command.LoaderContext, options: Command.Options) { super(context, { ...options, description: 'ping pong' }); } public async messageRun(message: Message) { await sendLocalized(message, 'commands/ping:success'); } } ``` -------------------------------- ### Edit Messages with editLocalized Source: https://github.com/sapphiredev/plugins/blob/main/packages/i18next/README.md Illustrates the use of the `editLocalized` function to update an existing message with translated content. This function resolves a translation key and edits the message, optionally passing arguments for dynamic text replacement within the translated string. ```typescript import { editLocalized } from '@sapphire/plugin-i18next'; import { Command } from '@sapphire/framework'; import type { Message } from 'discord.js'; export class PingCommand extends Command { public constructor(context: Command.LoaderContext, options: Command.Options) { super(context, { ...options, description: 'ping pong' }); } public async messageRun(message: Message) { await editLocalized(message, 'commands/ping:success_args', { latency: ws.ping }); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.