Skip to content

Commit

Permalink
use node 22 features
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperchupuDev committed Jul 28, 2024
1 parent bd447d0 commit 6883f24
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 61 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ jobs:
- name: Install pnpm
uses: pnpm/action-setup@fe02b34f77f8bc703788d5817da081398fad5dd2 # v4.0.0

- name: Install Node 20
- name: Install Node 22
uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3
with:
node-version: 20
node-version: 22
cache: 'pnpm'

- name: Install dependencies
Expand All @@ -35,7 +35,7 @@ jobs:
run: touch .dev.vars

- name: Deploy commands to Discord
run: pnpm run deploy-commands
run: node --run deploy-commands
env:
APPLICATION_ID: ${{ secrets.APPLICATION_ID }}
DISCORD_TOKEN: ${{ secrets.DISCORD_TOKEN }}
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
uses: biomejs/setup-biome@1cbe33ead22c7a2fded3b52fa2893611c815c9b5 # v2.2.1

- name: Run Biome
run: biome ci .
run: biome ci

typescript:
name: TypeScript
Expand All @@ -28,14 +28,14 @@ jobs:
- name: Install pnpm
uses: pnpm/action-setup@fe02b34f77f8bc703788d5817da081398fad5dd2 # v4.0.0

- name: Install Node 20
- name: Install Node 22
uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3
with:
node-version: 20
node-version: 22
cache: 'pnpm'

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Run TypeScript compiler
run: pnpm run typecheck
run: node --run typecheck
10 changes: 5 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

### Installation

If you haven't already, install [Node.js](https://nodejs.org) with a version no less than `20.11.0`.
If you haven't already, install [Node.js](https://nodejs.org) `22.0.0` or later.

Afterwards, install `pnpm` by running `corepack enable`.

Expand Down Expand Up @@ -41,9 +41,9 @@ WEBHOOK_SECRET=

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`.
You can do that by running `node --run deploy-commands`.

Once that's done, you can run the bot locally with `pnpm dev`.
Once that's done, you can run the bot locally with `node --run dev`.

You need to forward the port used, and specify it in the dev portal as the interactions endpoint URL.

Expand Down Expand Up @@ -72,11 +72,11 @@ 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.
Remember to run `node --run 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.
When you're ready to submit your changes, make sure to run `node --run check` and `node --run typecheck` to ensure everything is in order.

Then, create a [pull request](https://github.com/biomejs/discord-utils-bot/pulls).

Expand Down
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
"scripts": {
"dev": "wrangler dev",
"start": "wrangler dev",
"deploy": "wrangler deploy",
"deploy-commands": "tsx --env-file=.dev.vars scripts/deploy-commands.ts",
"check": "biome check .",
"check:apply": "biome check --apply-unsafe .",
"format": "biome format --write .",
"lint": "biome lint .",
"check": "biome check",
"check:apply": "biome check --fix --unsafe",
"format": "biome format --write",
"lint": "biome lint",
"typecheck": "tsc"
},
"devDependencies": {
Expand All @@ -30,7 +29,7 @@
"discord-verify": "^1.2.0"
},
"engines": {
"node": "^20.11.0 || >=21.2.0"
"node": ">=22.0.0"
},
"packageManager": "[email protected]"
}
26 changes: 13 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 8 additions & 30 deletions scripts/deploy-commands.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { readdir } from 'node:fs/promises';
import { glob } from 'node:fs/promises';
import { join } from 'node:path';
import process from 'node:process';
import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders';
import { REST } from '@discordjs/rest';
import {
type RESTPostAPIChatInputApplicationCommandsJSONBody,
type RESTPostAPIContextMenuApplicationCommandsJSONBody,
Routes,
} from 'discord-api-types/v10';
import { Routes } from 'discord-api-types/v10';

if (!process.env.DISCORD_TOKEN || !process.env.APPLICATION_ID) {
console.error('Missing CLIENT_SECRET or APPLICATION_ID environment variables');
console.error('Missing DISCORD_TOKEN or APPLICATION_ID environment variables');
process.exit(1);
}

Expand All @@ -21,33 +17,15 @@ interface CommandFile {
slashCommandData: SlashCommandBuilder;
}

const directory = join(import.meta.dirname, '../src/commands');
const files = await readdir(directory);
const files = glob(join(import.meta.dirname, '../src/commands/*.ts'));
const commands = await Array.fromAsync(files, async (file) => {
const loadedFile = (await import(`file:${file}`)) as CommandFile;

const commands: (
| RESTPostAPIContextMenuApplicationCommandsJSONBody
| RESTPostAPIChatInputApplicationCommandsJSONBody
)[] = [];

for (const file of files) {
const loadedFile = await loadFile<CommandFile>(directory, file);

if (loadedFile) {
commands.push(loadedFile.slashCommandData?.toJSON() ?? loadedFile.contextMenuCommandData?.toJSON());
}
}
return loadedFile.slashCommandData?.toJSON() ?? loadedFile.contextMenuCommandData?.toJSON();
});

const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);

await rest.put(Routes.applicationCommands(process.env.APPLICATION_ID), { body: commands });

console.log(`Deployed ${commands.length} commands`);

async function loadFile<T>(directory: string, file: string) {
if (!file.endsWith('.ts')) {
return;
}

const data = (await import(`file://${join(directory, file)}`)) as T;
return data;
}

0 comments on commit 6883f24

Please sign in to comment.