|
1 | 1 | ---
|
2 |
| -title: File naming conventions |
| 2 | +title: File Naming Conventions |
3 | 3 | ---
|
| 4 | + |
| 5 | +## app.ts |
| 6 | + |
| 7 | +The `src/app.ts` (or `app.js`) is a special file that acts as the |
| 8 | +entry point for your application. It is where you define and export |
| 9 | +your `Discord.js` client instance. |
| 10 | + |
| 11 | +```ts title="src/app.ts" |
| 12 | +import { Client } from 'discord.js'; |
| 13 | + |
| 14 | +const client = new Client({ |
| 15 | + /* options */ |
| 16 | +}); |
| 17 | + |
| 18 | +// Optional: Override the default DISCORD_TOKEN environment variable |
| 19 | +client.token = 'YOUR_BOT_TOKEN'; |
| 20 | + |
| 21 | +export default client; |
| 22 | +``` |
| 23 | + |
| 24 | +## +middleware.ts |
| 25 | + |
| 26 | +The `src/app/commands/+middleware.ts` (or `+middleware.js`) file is |
| 27 | +used to define middleware functions for your application. Middleware |
| 28 | +functions get called before and after the command execution. |
| 29 | + |
| 30 | +```ts title="src/app/commands/+middleware.ts" |
| 31 | +import { MiddlewareContext } from 'commandkit'; |
| 32 | + |
| 33 | +export function beforeExecute(context: MiddlewareContext) { |
| 34 | + // This function will be executed before the command is executed |
| 35 | + console.log('Before command execution'); |
| 36 | +} |
| 37 | + |
| 38 | +export function afterExecute(context: MiddlewareContext) { |
| 39 | + // This function will be executed after the command is executed |
| 40 | + console.log('After command execution'); |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +There are 3 types of middlewares you can create: |
| 45 | + |
| 46 | +- `+middleware.ts` (or `+middleware.js`): This file is used to define |
| 47 | + middleware functions that will be executed for all sibling commands |
| 48 | + in the application. |
| 49 | +- `+<command>.middleware.ts` (or `+<command>.middleware.js`): This |
| 50 | + file is used to define middleware functions that will be executed |
| 51 | + for a specific command in the application. The `<command>` part of |
| 52 | + the filename should match the name of the command file. This is |
| 53 | + useful for defining middleware functions that should be applied to a |
| 54 | + specific command. |
| 55 | +- `+global-middleware.ts` (or `+global-middleware.js`): This file is |
| 56 | + used to define middleware functions that will be executed for all |
| 57 | + commands in the application, regardless of their location in the |
| 58 | + file system. |
| 59 | + |
| 60 | +:::info |
| 61 | + |
| 62 | +You can learn more about middlewares |
| 63 | +[here](../02-commands/07-middlewares.mdx). |
| 64 | + |
| 65 | +::: |
| 66 | + |
| 67 | +## (category) directory |
| 68 | + |
| 69 | +In your commands directory, you can create a category using |
| 70 | +parenthesis (e.g. `(Moderation)`). This is useful for organizing your |
| 71 | +commands into logical groups, making it easier to manage and maintain |
| 72 | +your code. |
| 73 | + |
| 74 | +``` |
| 75 | +src/app/commands/ |
| 76 | +├── (Moderation) |
| 77 | +│ ├── ban.ts |
| 78 | +│ ├── kick.ts |
| 79 | +│ ├── mute.ts |
| 80 | +│ ├── unmute.ts |
| 81 | +│ ├── warn.ts |
| 82 | +│ ├── warn-list.ts |
| 83 | +│ └── warn-remove.ts |
| 84 | +``` |
| 85 | + |
| 86 | +:::info |
| 87 | + |
| 88 | +You can learn more about command categories |
| 89 | +[here](../02-commands/06-category-directory.mdx). |
| 90 | + |
| 91 | +::: |
0 commit comments