Skip to content

Commit 3d99b57

Browse files
authored
Refactor: restructure setup (#1)
* πŸ”¨ refactor: move `index.ts`, `env.ts` and `loadEnvFile.ts` from root to `src/` * 🧹 chore: add .vscode settings * πŸ”¨ refactor: update command types to use REST API types from discord.js * 🌟 feat: implement command creation utilities for slash and context menu commands * πŸ”¨ refactor: use createMessageContextMenuCommand helper for the reportMessage command * πŸ”¨ refactor: update commandsData mapping to use command data directly * 🧹 chore: add typecheck script * 🌟 feat: add generic discord event type * 🌟 feat: add helper for creating an event * 🌟 feat: add loadEvents utility * πŸ”¨ refactor: update tsconfig module settings and root directory * πŸ”¨ refactor: remove dynamic import of `./loadEnvFile.js` * πŸ”¨ refactor: remove unused command files and related utilities * πŸ”¨ refactor: add commandType to command types for better type safety * 🌟 feat: add command creation utilities for slash and context menu commands * πŸ”¨ refactor: move report message and ping commands to new structure * πŸ”¨ refactor: remove command creation utilities and streamline command loading and registration * 🌟 feat: move ready event handler from src/index.ts to a seperate file * πŸ”¨ refactor: update command imports and change commands structure to use Map * 🌟 feat: add events index file to consolidate event imports * πŸ”¨ refactor: move interaction create event handler from src/index.ts to a seperate file * πŸ”¨ refactor: update imports to use explicit file extensions * πŸ”¨ refactor: simplify index.ts by consolidating command imports and removing unused event handlers * πŸ“š docs: add README.md to document project features, installation, and usage * πŸ“š docs: remove directory structure from README * πŸ”¨ refactor: update DiscordEvent type definition * πŸ“š docs: update events declaration to use explicit type annotation * πŸ”¨ refactor: rename GUILD_ID to SERVER_ID
1 parent 4ebb83d commit 3d99b57

26 files changed

Lines changed: 639 additions & 302 deletions

β€Ž.env.local.exampleβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Discord Bot Configuration (Required)
66
DISCORD_TOKEN=your_bot_token_here
77
CLIENT_ID=your_client_id_here
8+
SERVER_ID=your_server_id_here
89

910
# Database Configuration (Optional - uncomment and add to config.ts when needed)
1011
# DATABASE_URL=postgresql://user:password@localhost:5432/botdb

β€Ž.gitignoreβ€Ž

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ dist/
132132
out/
133133

134134
# IDE files
135-
.vscode/
136135
.idea/
137136
*.swp
138137
*.swo

β€Ž.vscode/settings.jsonβ€Ž

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"editor.defaultFormatter": "biomejs.biome",
3+
"prettier.enable": false,
4+
"oxc.enable": false,
5+
"editor.formatOnSave": true,
6+
"editor.codeActionsOnSave": {
7+
"source.fixAll.biome": "explicit"
8+
},
9+
"[typescript]": {
10+
"editor.defaultFormatter": "biomejs.biome"
11+
},
12+
"[json]": {
13+
"editor.defaultFormatter": "biomejs.biome"
14+
},
15+
"typescript.tsdk": "node_modules/typescript/lib"
16+
}

β€ŽREADME.mdβ€Ž

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
# Discord Moderation Tool
2+
3+
A TypeScript Discord bot built with Discord.js v14, designed for server moderation with a clean, type-safe architecture.
4+
5+
## Features
6+
7+
- **Slash Commands**: Traditional `/command` style interactions
8+
- **Context Menu Commands**: Right-click context menus for users and messages
9+
- **Event-Driven Architecture**: Modular event handling system
10+
- **Type-Safe**: Full TypeScript support with discriminated unions
11+
- **Environment Configuration**: Secure configuration management
12+
- **Command Helpers**: Easy-to-use helpers for creating different command types
13+
14+
## Installation
15+
16+
### Prerequisites
17+
18+
- Node.js 22.20.0+
19+
- pnpm (recommended) or npm
20+
- A Discord bot token
21+
22+
### Setup
23+
24+
1. **Clone the repository**
25+
```bash
26+
git clone <repository-url>
27+
cd moderation-tool
28+
```
29+
30+
2. **Install dependencies**
31+
```bash
32+
pnpm install
33+
```
34+
35+
3. **Configure environment**
36+
```bash
37+
cp .env.local.example .env.local
38+
```
39+
Edit `.env.local` with your bot credentials:
40+
```env
41+
DISCORD_TOKEN=your_bot_token_here
42+
CLIENT_ID=your_client_id_here
43+
SERVER_ID=your_guild_id_here
44+
```
45+
46+
4. **Build and run**
47+
```bash
48+
# Development (with hot reload)
49+
pnpm dev
50+
51+
# Production build
52+
pnpm build:ci
53+
pnpm start:ci
54+
```
55+
56+
57+
## Creating Commands
58+
59+
### Slash Commands
60+
61+
Use `createSlashCommand` helper for traditional `/command` interactions:
62+
63+
```typescript
64+
import { createSlashCommand } from "../commands/helpers.js";
65+
66+
export const ping = createSlashCommand({
67+
data: {
68+
name: "ping", // Must be lowercase
69+
description: "Replies with Pong!",
70+
options: [ // Optional: command options
71+
{
72+
name: "message",
73+
description: "Optional message to include",
74+
type: ApplicationCommandOptionType.String,
75+
required: false,
76+
}
77+
]
78+
},
79+
execute: async (interaction) => {
80+
await interaction.reply("Pong!");
81+
},
82+
});
83+
```
84+
85+
### Context Menu Commands
86+
87+
#### User Context Menu Commands
88+
89+
```typescript
90+
import { createUserContextMenuCommand } from "../commands/helpers.js";
91+
92+
export const userInfo = createUserContextMenuCommand({
93+
data: {
94+
name: "Get User Info", // Can contain spaces and capitals
95+
},
96+
execute: async (interaction) => {
97+
const targetUser = interaction.targetUser;
98+
await interaction.reply(`User: ${targetUser.tag}`);
99+
},
100+
});
101+
```
102+
103+
#### Message Context Menu Commands
104+
105+
```typescript
106+
import { createMessageContextMenuCommand } from "../commands/helpers.js";
107+
108+
export const reportMessage = createMessageContextMenuCommand({
109+
data: {
110+
name: "Report to Moderators",
111+
},
112+
execute: async (interaction) => {
113+
const targetMessage = interaction.targetMessage;
114+
// Handle message reporting logic
115+
await interaction.reply("Message reported!");
116+
},
117+
});
118+
```
119+
120+
### Registering Commands
121+
122+
Add your commands to `src/commands/index.ts`:
123+
124+
```typescript
125+
import { ping } from "./ping.js";
126+
import { reportMessage } from "./reportMessage.js";
127+
import type { Command } from "./types.js";
128+
129+
export const commands = new Map<string, Command>(
130+
[ping, reportMessage].flat().map((command) => [command.data.name, command])
131+
);
132+
```
133+
134+
## Creating Events
135+
136+
Events are handled automatically through the event system. Create new events using the `createEvent` helper:
137+
138+
```typescript
139+
import { Events } from "discord.js";
140+
import { createEvent } from "../events/helpers.js";
141+
142+
export const messageCreateEvent = createEvent(
143+
{
144+
name: Events.MessageCreate,
145+
},
146+
async (message) => {
147+
if (message.author.bot) return;
148+
149+
// Handle message logic
150+
console.log(`Message from ${message.author.tag}: ${message.content}`);
151+
}
152+
);
153+
```
154+
155+
### Registering Events
156+
157+
Add your events to `src/events/index.ts`:
158+
159+
```typescript
160+
import { messageCreateEvent } from "./message-create/index.js";
161+
import { readyEvent } from "./ready/index.js";
162+
import type { DiscordEvent } from "./types.js";
163+
164+
export const events: DiscordEvent[] = [readyEvent, messageCreateEvent];
165+
```
166+
167+
## Configuration
168+
169+
The bot uses a centralized configuration system in `src/env.ts`. Environment variables are validated and logged on startup.
170+
171+
### Required Environment Variables
172+
173+
- `DISCORD_TOKEN`: Your Discord bot token
174+
- `CLIENT_ID`: Your Discord application client ID
175+
- `SERVER_ID`: Guild ID (Server ID) for guild-specific command registration
176+
177+
## Development
178+
179+
### Scripts
180+
181+
- `pnpm dev` - Start development server with hot reload
182+
- `pnpm build:ci` - Build for production
183+
- `pnpm start:ci` - Run production build
184+
- `pnpm typecheck` - Run TypeScript type checking
185+
- `pnpm lint` - Lint code with Biome
186+
- `pnpm format` - Format code with Biome
187+
188+
### Code Quality
189+
190+
This project uses:
191+
- **Biome** for linting and formatting
192+
- **TypeScript** for type safety
193+
- **Husky** + **lint-staged** for pre-commit hooks
194+
195+
### Adding New Features
196+
197+
1. **Commands**: Create in `src/commands/` and register in `src/commands/index.ts`
198+
2. **Events**: Create in `src/events/` and register in `src/events/index.ts`
199+
3. **Utilities**: Add to `src/utils/`
200+
4. **Configuration**: Update `src/env.ts` for new environment variables
201+
202+
## Deployment
203+
204+
1. Build the project:
205+
```bash
206+
pnpm build:ci
207+
```
208+
209+
2. Set environment variables in your deployment platform
210+
211+
3. Run the production build:
212+
```bash
213+
pnpm start:ci
214+
```
215+
216+
## Contributing
217+
218+
1. Fork the repository
219+
2. Create a feature branch
220+
3. Make your changes
221+
4. Run tests and linting
222+
5. Submit a pull request
223+
224+
## License
225+
226+
MIT

β€Žindex.tsβ€Ž

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
Β (0)