### Run sern Project in Development Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v4/reference/getting-started.mdx Starts the sern Discord bot in development mode. This command is used to run the bot locally, allowing for testing and iteration. Manual restarts are required for changes to take effect. ```bash npx @sern/bot dev ``` -------------------------------- ### Create sern Project Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v4/reference/getting-started.mdx Initializes a new Discord bot project using the sern framework. This command leverages a package manager (like npm, yarn, or pnpm) to create a boilerplate project structure for `@sern/bot`. ```bash npx @sern/create@latest @sern/bot ``` -------------------------------- ### Full Dependency Injection Setup Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v3/guide/walkthrough/dependency-injection.mdx A comprehensive setup for dependency injection, including client registration and exporting the container hook. This example demonstrates how to integrate the Sern handler with a custom client and define project-specific dependencies. ```ts const client = new Client({ ...options, }); interface MyDependencies extends Dependencies { "@sern/client": Singleton; } export const useContainer = Sern.makeDependencies({ build: (root) => root.add({ "@sern/client": single(() => client), }), }); ``` -------------------------------- ### Sern Handler Initialization API Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v4/api/namespaces/Sern/functions/init.md Documentation for the `init` function used to start the sern handler. It outlines parameters, return types, and usage examples. ```APIDOC init(maybeWrapper?: Wrapper): void Parameters: • maybeWrapper: Wrapper = undefined Options to pass into sern. Function to start the handler up. Returns: void Since: 1.0.0 Example: ```ts Sern.init({ commands: 'dist/commands', events: 'dist/events', }) ``` Source: [src/sern.ts:32](https://github.com/sern-handler/handler/blob/513ac8edf4d89ef8d6a1ed18ea3d08f31adf7ddb/src/sern.ts#L32) ``` -------------------------------- ### Install sern Plugins CLI Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v3/guide/walkthrough/plugins.mdx Installs sern plugins using the command-line interface. This is the primary method for adding pre-built plugins to your project. ```sh sern plugins ``` -------------------------------- ### Create a new Sern bot project Source: https://github.com/sern-handler/website/blob/main/src/content/docs/index.mdx Use this command to initialize a new Discord bot project using the Sern framework. It guides you through the setup process, setting up your development environment. ```bash npm create @sern/bot ``` -------------------------------- ### Install Plugins with CLI Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v4/reference/plugins.mdx Demonstrates how to install plugins using the sern handler CLI. This command is the primary method for adding reusable functionality to your project. ```sh sern plugins ``` -------------------------------- ### Install @sern/localizer Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v4/tools/localizer.mdx Installs the @sern/localizer package using npm. This is the first step to integrate localization into your application. ```bash npm i @sern/localizer ``` -------------------------------- ### Ping Command Example (Sapphire Framework) Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v3/guide/walkthrough/goal.mdx Demonstrates a 'ping' command using the @sapphire/framework. This example is specifically for slash commands and requires Discord.js. It takes no specific inputs and replies with 'Pong!'. ```TypeScript import { Command } from "@sapphire/framework"; import type { CommandInteraction } from "discord.js"; export class PingCommand extends Command { public constructor(context: Command.Context) { super(context, { description: "Pong!", chatInputCommand: { register: true, }, }); } public async chatInputRun(interaction: CommandInteraction) { await interaction.reply("Pong!"); } } ``` -------------------------------- ### Prerequire Script Example Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v3/cli/publish.mdx An ES6 script example for prerequiring services at the top level of a command, demonstrating how to build dependencies and log in a Discord client using `@sern/handler`. ```ts import { makeDependencies, single, Service } from "@sern/handler"; import { Client } from "discord.js"; await makeDependencies({ build: (root) => root.add({ "@sern/client": single(() => new Client(...options)) }), }); await Service("@sern/client").login(); ``` -------------------------------- ### Prerequiring Script Example Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v4/cli/publish.mdx An example of an ES6 script used for prerequiring dependencies and services before publishing commands. It demonstrates setting up a Discord client using `@sern/handler`'s dependency injection system and logging in the client. ```ts import { makeDependencies, single, Service } from "@sern/handler"; import { Client } from "discord.js"; await makeDependencies({ build: (root) => root.add({ "@sern/client": single(() => new Client(...options)) }), }); await Service("@sern/client").login(); ``` -------------------------------- ### Publish Command Example (Ping) Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v3/cli/publish.mdx Example of a Discord command (`ping`) configured to publish to a specific guild. It demonstrates importing services and defining command metadata like description and execution logic. ```ts import { commandModule, Service, CommandType } from '@sern/handler' const client = Service('@sern/client'); export const config = { guildIds: ["889026545715400705"] } export default commandModule( { type: CommandType.Slash, description: `${client.user.username}'s ping`, execute: (ctx) => { ctx.reply('pong') } }) ``` -------------------------------- ### ChannelSelectCommand locals Property Examples Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v4/api/interfaces/ChannelSelectCommand.md Provides various usage examples for the locals object, demonstrating how to store registration dates, versions, permissions, localization data, and command metadata. ```typescript // In a plugin module.locals.registrationDate = Date.now(); module.locals.version = "1.0.0"; module.locals.permissions = ["ADMIN", "MODERATE"]; ``` ```typescript // In module execution console.log(`Command registered on: ${new Date(module.locals.registrationDate)}`); ``` ```typescript // Storing localization data module.locals.translations = { en: "Hello", es: "Hola", fr: "Bonjour" }; ``` ```typescript // Storing command metadata module.locals.metadata = { category: "admin", cooldown: 5000, requiresPermissions: true }; ``` -------------------------------- ### Container Configuration Get Method Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v3/api/interfaces/Wrapper.md Describes the `get()` method within the `containerConfig` object. This method retrieves dependencies based on provided keys. ```APIDOC containerConfig.get(...keys) Description: Retrieves dependencies from the container based on the provided keys. Parameters: • ...keys: keyof Dependencies[] - A variadic list of keys representing the dependencies to retrieve. Returns: unknown[] - An array containing the retrieved dependencies. ``` -------------------------------- ### Sern CLI: Interactive `sern extra` Usage Example Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v4/cli/extra.mdx This example demonstrates an interactive session of the `sern extra` command, showing how a user would be prompted to select a feature to add, such as a `Dockerfile` for a TypeScript project. It illustrates the command's interactive nature. ```sh sern extra # Choose which extra feature from the prompt, such as this: ✔ What extra feature do you want to add? › Dockerfile (TypeScript) ``` -------------------------------- ### Install CLI Globally Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v3/guide/walkthrough/cli.mdx Installs the sern CLI globally using a package manager. This command makes the sern CLI available system-wide for managing sern projects and plugins. ```sh npm install -g @sern/cli ``` -------------------------------- ### Install Extra Utilities Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v3/guide/walkthrough/cli.mdx Installs additional utilities into your sern project. This command is used to extend the functionality of your project with helpful tools provided by the sern ecosystem. ```sh sern extra ``` -------------------------------- ### Ping Command Example (Sern Handler) Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v3/guide/walkthrough/goal.mdx Demonstrates a 'ping' command using the @sern/handler. This example supports both slash and text commands and requires @sern/handler. It takes context and arguments, replying with 'Pong!'. ```TypeScript import { commandModule, CommandType } from "@sern/handler"; export default commandModule({ type: CommandType.Both, description: "Pong!", execute: async (ctx, args) => { await ctx.reply("Pong!"); }, }); ``` -------------------------------- ### CommandInitPlugin Usage Examples Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v4/api/functions/CommandInitPlugin.md Demonstrates how to create initialization plugins using CommandInitPlugin. The first example shows updating a command's description with validation, while the second illustrates storing registration dates in module locals. Init plugins can modify command loading, use module.locals for data, and control execution flow with controller.next() or controller.stop(). ```typescript export const updateDescription = (description: string) => { return CommandInitPlugin(({ deps, module }) => { if(description.length > 100) { deps.logger?.info({ message: "Invalid description" }); return controller.stop("From updateDescription: description is invalid"); } module.description = description; return controller.next(); }); }; ``` ```typescript export const dateRegistered = () => { return CommandInitPlugin(({ module }) => { module.locals.registered = Date.now(); return controller.next(); }); }; ``` -------------------------------- ### Get Service Instance Example Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v3/api/functions/Service.md Example demonstrating how to retrieve a service instance using the Service API in TypeScript. ```ts const client = Service('@sern/client'); ``` -------------------------------- ### Initialize Client with Dependencies Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v4/reference/dependencies.mdx Demonstrates setting up a Sern client and making dependencies available using the `makeDependencies` function. It shows how to add the client instance to the dependency root. ```js const client = new Client({ ...options, }); await makeDependencies((root) => { root.add("@sern/client", client) }); ``` -------------------------------- ### Create New @sern/bot Project Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v3/guide/walkthrough/new-project.mdx Initiates the creation of a new project using the @sern/bot CLI tool. Follow the interactive prompts for project setup. This command is the first step in setting up a new bot project. ```bash npx @sern/bot create ``` -------------------------------- ### .env Configuration Example Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v4/cli/clear.mdx Example `.env` file required for the clear command, specifying Discord token and Node environment. ```env DISCORD_TOKEN= NODE_ENV= ``` -------------------------------- ### .env Configuration Example Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v3/cli/clear.mdx Example `.env` file required for the clear command, specifying Discord token and Node environment. ```env DISCORD_TOKEN= NODE_ENV= ``` -------------------------------- ### sern build CLI Usage Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v3/cli/build.mdx Demonstrates the command-line interface for the `sern build` command, outlining available options for configuring the build process, such as module format, build mode, and project file path. It also shows how to suppress warnings and access help. ```sh Usage: sern build [options] Build your bot Options: -f --format [fmt] The module system of your application. `cjs` or `esm` (default: "esm") -m --mode [mode] the mode for sern to build in. `production` or `development` (default: "development") -W --suppress-warnings suppress experimental warning -p --project [filePath] build with this sern.build file -h, --help display help for command ``` -------------------------------- ### Example Ping Command with OwnerOnly Plugin Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v4/reference/plugins.mdx An example of a command module that utilizes a plugin to restrict execution to the owner. It imports the `ownerOnly` plugin and applies it to the module. ```ts import { commandModule, CommandType } from '@sern/handler' import { ownerOnly } from '../plugins' export default commandModule({ type: CommandType.Both, plugins: [ownerOnly(['182326315813306368'])], description: 'ping command', execute: (ctx) => { ctx.reply('hello, owner'); } }) ``` -------------------------------- ### Dependency Injection Setup with Sern.makeDependencies Source: https://github.com/sern-handler/website/blob/main/src/content/docs/blog/2.0.0.md Illustrates setting up dependency injection using `Sern.makeDependencies` and `iti`. It shows how to define custom dependencies, configure the container, and initialize Sern with the custom container. ```typescript interface MyDependencies extends Dependencies { "@sern/client": Singleton; "@sern/logger": Singleton; } export const useContainer = Sern.makeDependencies({ // exclude: new Set(['@sern/logger']), don't autofill optional dependencies build: (root) => root .add({ "@sern/client": single(client) }) .add({ "@sern/logger": single(new DefaultLogging()) }), }); Sern.init({ defaultPrefix: "!", // removing defaultPrefix will shut down text commands commands: "src/commands", // events: 'src/events' (optional), containerConfig: { get: useContainer, //pass in your dependency getter here }, }); ``` -------------------------------- ### CommandType Usage Example Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v4/api/enumerations/CommandType.md Demonstrates how to use the CommandType enum to specify the type of a command module, such as a text command. This example shows the integration within a `commandModule` configuration. ```TypeScript export default commandModule({ // highlight-next-line type : CommandType.Text, name : 'a text command' execute(message) { console.log(message.content) } }) ``` -------------------------------- ### Initializing Dependencies Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v3/guide/walkthrough/dependency-injection.mdx Shows how to implement the `Init` interface for dependencies that require initialization logic, such as connecting to a database. The `init` method is automatically called when the dependency is registered. ```ts import { Init } from "@sern/handler"; class Database implements Init { init() { await this.connect(); console.log("Connected"); } } ``` ```ts import type { Initializable } from "@sern/handler"; interface Dependencies extends CoreDependencies { database: Initializable; } ``` ```ts await makeDependencies({ build: root => root .add({ database => new Database() }) }); ``` -------------------------------- ### Custom Sern Event Example Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v4/api/functions/eventModule.md An example showing how to use `eventModule` for custom Sern events. It specifies the event type as Sern and includes a placeholder for handling sern-specific event data. ```TypeScript // Custom sern event export default eventModule({ type: EventType.Sern, execute: async (eventData) => { // Handle sern-specific event } }); ``` -------------------------------- ### Update Sern Dependency Creation Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v3/guide/walkthrough/transition.mdx Illustrates the change in how dependencies are created in sern from v2 to v3, specifically the transition from `Sern.makeDependencies` to `await makeDependencies`. ```diff - Sern.makeDependencies({ build: () => {} }) + await makeDependencies({ build: () => {} }) ``` -------------------------------- ### Add Plugins Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v3/guide/walkthrough/cli.mdx Adds plugins to your sern project. This command requires a sern.config.json file to be present in your project root. It displays an interactive menu to select installable plugins. ```sh sern plugins ``` -------------------------------- ### Initialize Sern Handler Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v4/reference/config.mdx Imports the configuration and initializes the Sern framework with the specified settings. This is the entry point for integrating Sern into your application. ```js import * as config from './config.js' Sern.init(config) ``` -------------------------------- ### Discord Event Listener Example Source: https://github.com/sern-handler/website/blob/main/src/content/docs/v4/api/functions/eventModule.md An example demonstrating how to use `eventModule` to create a Discord event listener. It specifies the event type as Discord and provides an `execute` function to handle incoming messages. ```TypeScript // Discord event listener export default eventModule({ type: EventType.Discord, execute: async (message) => { console.log(`${message.author.tag}: ${message.content}`); } }); ``` -------------------------------- ### Class-based Command Module Source: https://github.com/sern-handler/website/blob/main/src/content/docs/blog/1.2.0.mdx Example of creating a command module using the `CommandExecutable` class from @sern/handler. This demonstrates defining command properties like `type`, `description`, `onEvent`, `plugins`, and the `execute` method. ```ts import { CommandType, CommandExecutable, type Args, type Context, } from "@sern/handler"; import { publish } from "../plugins/publish.js"; import { serendipityOnly } from "../plugins/serendipityOnly.js"; export default class extends CommandExecutable { type = CommandType.Both as const; description = "What is the meaning of life?"; override onEvent = [serendipityOnly()]; override plugins = [publish()]; execute = async (ctx: Context, args: Args) => { await ctx.reply("42"); }; } ```