diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..b4af3d0 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,81 @@ +# Contributing + +## Table of Contents + +- [Getting Started](#getting-started) + - [Installation](#installation) + - [Environment Setup](#environment-setup) + - [Running the Bot](#running-the-bot) +- [Adding Commands](#adding-commands) +- [Submitting Changes](#submitting-changes) + +## Getting Started + +### Installation + +If you haven't already, install [Node.js](https://nodejs.org) with a version no less than `20.11.0`. + +Afterwards, install `pnpm` by running `corepack enable`. + +[Corepack](https://nodejs.org/api/corepack.html#corepack) is a tool that manages package managers, and comes bundled with Node.js. + +Then, install the needed dependencies with `pnpm install --frozen-lockfile`. + +### Environment Setup + +First, create an application on the [Discord Developer Portal](https://discord.com/developers/applications). + +It will be a used as a test application for development purposes of the bot. + +Then, create a `.env` file at the root of the project with the following variables obtained from the dev portal: + +```env +APPLICATION_ID= +DISCORD_TOKEN= +PUBLIC_KEY= +``` + +### Running the Bot + +Make sure you run the `deploy-commands` script before you start! It updates the command list on Discord. + +You can do that by running `pnpm deploy-commands`. + +Once that's done, you can run the bot locally with `pnpm dev`. + +You need to forward the port used, and specify it in the dev portal as the interactions endpoint URL. + +## Adding Commands + +Create a new file in the `src/commands` directory, with the following structure (you can use this as a template): + +```ts +import { SlashCommandBuilder } from '@discordjs/builders'; +import { type APIApplicationCommandInteraction, InteractionResponseType } from 'discord-api-types/v10'; +import { reply } from '../reply.js'; + +export async function onXSlashCommand(interaction: APIApplicationCommandInteraction) { + return reply(InteractionResponseType.ChannelMessageWithSource, { + content: 'you just ran the test command. good job', + }); +} + +export const slashCommandData = new SlashCommandBuilder() + .setName('replace-me') + .setDescription('replace this too'); +``` + +Rename the onXSlashCommand function to match the command name (i.e. `onTestSlashCommand` for a command named `test`), +and replace the placeholder text in the builder. + +Then, import the command in `src/commands/index.ts` and add it to the switch statement in the handleInteraction function. + +Remember to run `pnpm deploy-commands` after adding a new command, to update the command list on Discord. + +## Submitting Changes + +When you're ready to submit your changes, make sure to run `pnpm check` and `pnpm typecheck` to ensure everything is in order. + +Then, create a [pull request](https://github.com/biomejs/discord-utils-bot/pulls). + +Once the PR is approved, it will be merged and deployed to the production bot. diff --git a/README.md b/README.md index 2e3de24..80c7b16 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,6 @@ Util bot for the Biome Discord server. [WIP] -## Deploying +## Contributing -Make sure you run the `deploy-commands` script before deploying! It updates the command list on Discord. - -It needs a `.env` file at the root of the project, with `APPLICATION_ID` and `DISCORD_TOKEN` variables. - -When you're done running the script, go ahead and run `pnpm run deploy` to instantly deploy your changes to Cloudflare. +Check out [`CONTRIBUTING.md`](CONTRIBUTING.md) information on how to contribute. diff --git a/package.json b/package.json index 77dde1d..6934330 100644 --- a/package.json +++ b/package.json @@ -5,14 +5,14 @@ "type": "module", "license": "MIT OR Apache-2.0", "scripts": { - "format": "biome format --write .", - "check": "biome check .", - "check:apply": "biome check --apply-unsafe .", - "typecheck": "tsc", + "dev": "wrangler dev", + "start": "wrangler dev", "deploy": "wrangler deploy", "deploy-commands": "tsx --env-file=.env scripts/deploy-commands.ts", - "dev": "wrangler dev", - "start": "wrangler dev" + "check": "biome check .", + "check:apply": "biome check --apply-unsafe .", + "format": "biome format --write .", + "typecheck": "tsc" }, "devDependencies": { "@biomejs/biome": "^1.7.0", diff --git a/src/index.ts b/src/index.ts index 8bdcdad..f8cb4a4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ import { onTestSlashCommand } from './commands/test.js'; import { reply } from './reply.js'; import { isChatInputCommand, isMessageComponent, isPing } from './typeguards.js'; export type Env = { - publicKey: string; + PUBLIC_KEY: string; }; export default { @@ -17,7 +17,7 @@ export default { } // Check if the request is valid - const isValid = await isValidRequest(request, env.publicKey, PlatformAlgorithm.Cloudflare).catch(() => false); + const isValid = await isValidRequest(request, env.PUBLIC_KEY, PlatformAlgorithm.Cloudflare).catch(() => false); if (!isValid) { return new Response('Invalid signature', { status: 401 }); }