discli 0.1.0

A CLI tool for sending Discord notifications
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# discli - Discord Notifications CLI

A simple command-line tool for sending Discord notifications programmatically. `discli` enables you to send messages to any Discord channel via a bot with a single command, making it perfect for CI/CD pipelines, automated monitoring, and system alerts.

## Features

- 🚀 **Simple CLI Interface**: Send messages with a single command
- ⚙️ **Environment-based Configuration**: Secure token management via `.env` file
- 📡 **Async Operations**: Built on Tokio for efficient HTTP requests
- 🔒 **Secure**: No hardcoded credentials - all configuration via environment variables
- 🎯 **Lightweight**: Minimal dependencies and small binary size

## Table of Contents

- [Prerequisites]#prerequisites
- [Installation]#installation
- [Configuration]#configuration
- [Usage]#usage
  - [Basic Usage]#basic-usage
  - [Advanced Examples]#advanced-examples
  - [CI/CD Integration]#cicd-integration
  - [Monitoring & Alerts]#monitoring--alerts
- [API Reference]#api-reference
- [Dependencies]#dependencies
- [Troubleshooting]#troubleshooting
- [License]#license

## Prerequisites

Before installing `discli`, ensure you have:

- **Rust Toolchain**: Rust 1.70 or later ([Install Rust]https://www.rust-lang.org/tools/install)
- **Discord Bot**: A Discord application with bot capabilities
- **Bot Token**: A valid Discord bot token with message sending permissions
- **Channel ID**: The Discord channel ID where messages will be sent

### Creating a Discord Bot

1. Go to [Discord Developer Portal]https://discord.com/developers/applications
2. Click "New Application" and give it a name
3. Navigate to the "Bot" tab and click "Add Bot"
4. Copy the bot token (you'll need this for configuration)
5. Under "Privileged Gateway Intents", enable "Message Content Intent" if needed
6. Invite the bot to your server with `Send Messages` permission

### Finding Your Channel ID

1. Enable Developer Mode in Discord (User Settings → Advanced → Developer Mode)
2. Right-click on the target channel
3. Select "Copy ID" from the context menu

## Installation

### From Source

Clone this repository and build the project:

```bash
git clone <repository-url>
cd discord-notifications
cargo build --release
```

The compiled binary will be available at `target/release/discli`.

### Install Globally (Optional)

To install `discli` globally and use it from anywhere:

```bash
cargo install --path .
```

This installs the binary to `~/.cargo/bin/discli` (on Unix) or `%USERPROFILE%\.cargo\bin\discli.exe` (on Windows).

### Add to PATH (Linux/macOS)

If the binary isn't automatically in your PATH:

```bash
export PATH="$PATH:$HOME/.cargo/bin"
```

Add this line to your `~/.bashrc` or `~/.zshrc` to make it permanent.

### Add to PATH (Windows)

Add `C:\Users\YourUsername\.cargo\bin` to your PATH environment variable.

## Configuration

`discli` requires configuration through a `discli.env` file in the project directory (or environment variables).

### Setup Instructions

1. Copy the example environment file:

```bash
cp discli.env.example discli.env
```

2. Edit `discli.env` with your credentials:

```env
# Discord Bot Configuration
# Your Discord bot token
DISCORD_TOKEN=your_actual_discord_bot_token_here

# Discord channel ID where messages will be sent
DISCORD_CHANNEL_ID=123456789012345678
```

### Security Notes

- ⚠️ **Never commit `discli.env` to version control**
- Add `discli.env` to your `.gitignore` file
- The `.gitignore` in this repository already excludes `discli.env`
- Only share the bot token with trusted individuals

### Alternative Configuration

Instead of using `discli.env`, you can set environment variables directly:

```bash
export DISCORD_TOKEN="your_bot_token"
export DISCORD_CHANNEL_ID="123456789012345678"
discli "Hello, Discord!"
```

## Usage

### Basic Usage

The basic syntax is:

```bash
discli "<message>"
```

#### Examples

Send a simple message:

```bash
discli "Hello from the command line!"
```

Send a message with emojis:

```bash
discli "✅ Build completed successfully"
discli "❌ Build failed"
discli "⚠️ Warning: High CPU usage detected"
```

### Advanced Examples

#### Multi-line Messages

Use newline characters (`\n`) for multi-line messages:

```bash
discli "Deployment Summary
Environment: Production
Status: Success
Duration: 2m 34s
Timestamp: 2024-02-15 14:30:00"
```

#### Using Shell Variables

Incorporate dynamic content using shell variables:

```bash
PROJECT_NAME="my-awesome-app"
BUILD_NUMBER="42"
discli "🎉 ${PROJECT_NAME} build #${BUILD_NUMBER} completed successfully"
```

#### Including Command Output

Capture and send command output:

```bash
discli "Disk usage: $(df -h / | tail -1)"
discli "Current time: $(date)"
```

#### Formatting Messages

Use consistent formatting for better readability:

```bash
discli "📊 System Report
━━━━━━━━━━━━━━━━━━
CPU: 45%
Memory: 62%
Disk: 78%
Uptime: 15 days
━━━━━━━━━━━━━━━━━━
Generated at: $(date)"
```

### CI/CD Integration

#### GitHub Actions

```yaml
name: Build and Notify
on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Setup Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
      
      - name: Build project
        run: cargo build --release
      
      - name: Setup discli
        run: |
          cargo install --path .
          echo "DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }}" > discli.env
          echo "DISCORD_CHANNEL_ID=${{ secrets.DISCORD_CHANNEL_ID }}" >> discli.env
      
      - name: Send notification on success
        if: success()
        run: discli "✅ Build successful - ${{ github.event.head_commit.message }}"
      
      - name: Send notification on failure
        if: failure()
        run: discli "❌ Build failed - ${{ github.event.head_commit.message }}"
```

#### GitLab CI

```yaml
build:
  stage: build
  script:
    - cargo build --release
    - cargo install --path .
    - echo "DISCORD_TOKEN=$DISCORD_TOKEN" > discli.env
    - echo "DISCORD_CHANNEL_ID=$DISCORD_CHANNEL_ID" >> discli.env
    - discli "🚀 Pipeline $CI_PIPELINE_ID completed on $CI_COMMIT_REF_NAME"
  after_script:
    - |
      if [ $CI_JOB_STATUS == "success" ]; then
        discli "✅ Build $CI_JOB_ID succeeded"
      else
        discli "❌ Build $CI_JOB_ID failed"
      fi
```

#### Jenkins Pipeline

```groovy
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'cargo build --release'
            }
        }
        stage('Notify') {
            steps {
                sh '''
                    cargo install --path .
                    echo "DISCORD_TOKEN=$DISCORD_TOKEN" > discli.env
                    echo "DISCORD_CHANNEL_ID=$DISCORD_CHANNEL_ID" >> discli.env
                    discli "Build ${BUILD_NUMBER} completed: ${currentBuild.currentResult}"
                '''
            }
        }
    }
}
```

### Monitoring & Alerts

#### System Health Checks

```bash
#!/bin/bash
# health_check.sh

# Check if service is running
if curl -sf http://localhost:3000/health > /dev/null; then
    discli "✅ Service healthy at $(date)"
else
    discli "❌ Service down at $(date)"
fi
```

#### Disk Space Monitoring

```bash
#!/bin/bash
# disk_alert.sh

THRESHOLD=90
USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')

if [ $USAGE -gt $THRESHOLD ]; then
    discli "⚠️ Disk usage alert: ${USAGE}% used on root partition"
fi
```

#### Backup Monitoring

```bash
#!/bin/bash
# backup_notify.sh

if ./backup.sh; then
    discli "✅ Backup completed successfully at $(date)"
else
    discli "❌ Backup failed at $(date)"
fi
```

#### Scheduled Cron Jobs

```bash
# Add to crontab for daily health check
0 8 * * * /path/to/health_check.sh
```

## API Reference

### Command Syntax

```bash
discli <message>
```

### Parameters

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `message` | string | Yes | The message content to send to Discord |

### Exit Codes

| Code | Meaning |
|------|---------|
| `0` | Message sent successfully |
| `1` | Error occurred (missing args, config, or API error) |

### Standard Output

**Success:**
```
Message sent successfully to channel 123456789012345678
```

### Standard Error

**Missing Arguments:**
```
Usage: discli <message>
Environment Variables:
  DISCORD_TOKEN - Your Discord bot token (required)
  DISCORD_CHANNEL_ID - Your Discord channel ID (required)

Example:
  discli "Hello from Rust!"
```

**Missing Configuration:**
```
Error: DISCORD_TOKEN environment variable not set
Please set it in your environment or in a discli.env file
```

```
Error: DISCORD_CHANNEL_ID environment variable not set
Please set it in your environment or in a discli.env file
```

**API Error:**
```
Error sending message: Discord API returned error status 403: Missing Access
```

### Message Limitations

- Maximum message length: 2000 characters (Discord limit)
- Message type: Plain text only
- Encoding: UTF-8

## Dependencies

`discli` is built with the following Rust dependencies:

| Crate | Version | Description |
|-------|---------|-------------|
| `reqwest` | 0.12 | HTTP client for making API requests |
| `tokio` | 1.40 | Async runtime for Rust |
| `serde` | 1.0 | Serialization framework |
| `serde_json` | 1.0 | JSON serialization support |
| `dotenv` | 0.15 | Environment variable loading from `.env` files |

### System Requirements

- Operating System: Linux, macOS, or Windows
- Architecture: x86_64, ARM64, or others supported by Rust
- Network: Internet connection for Discord API access

## Troubleshooting

### Common Issues

#### Binary Not Found

**Problem:** `discli: command not found`

**Solution:**
- Ensure you've compiled the binary: `cargo build --release`
- Install globally: `cargo install --path .`
- Check your PATH includes the Cargo bin directory

#### Permission Denied

**Problem:** `Permission denied when running discli`

**Solution:**
- Make the binary executable: `chmod +x target/release/discli`
- Check file permissions on `discli.env`

#### Missing Environment Variables

**Problem:** `Error: DISCORD_TOKEN environment variable not set`

**Solution:**
- Verify `discli.env` exists and contains both `DISCORD_TOKEN` and `DISCORD_CHANNEL_ID`
- Check that `discli.env` is in the current directory when running `discli`
- Ensure there are no syntax errors in `discli.env` (no spaces around `=`)

#### Message Not Appearing

**Problem:** Command succeeds but message doesn't appear in Discord

**Solution:**
- Verify bot has `SEND_MESSAGES` permission in the channel
- Confirm `DISCORD_CHANNEL_ID` is correct
- Check bot token is valid and not revoked
- Ensure bot is actually added to the server

#### Rate Limiting Errors

**Problem:** `Discord API returned error status 429: You are being rate limited`

**Solution:**
- Discord allows bot messages at different rates per bot tier
- Implement delays between messages (minimum 1 second recommended)
- For high-volume notifications, consider a message queue

#### Network Errors

**Problem:** Connection timeout or network-related errors

**Solution:**
- Check internet connection
- Verify firewall allows connections to `discord.com`
- Check DNS resolution for `discord.com`

### Debug Mode

To troubleshoot issues, you can enable more verbose output:

```bash
# Run with shell debugging
bash -x discli "Test message"
```

### Getting Help

If you encounter issues not covered here:
- Check the [Discord API Documentation]https://discord.com/developers/docs
- Review the bot permissions in Discord Developer Portal
- Verify your token hasn't been invalidated

## Development

### Building from Source

```bash
# Development build with debug symbols
cargo build

# Release build with optimizations
cargo build --release

# Run tests
cargo test

# Run with clippy for linting
cargo clippy
```

### Project Structure

```
discord-notifications/
├── Cargo.toml           # Project configuration and dependencies
├── Cargo.lock           # Dependency lock file
├── discli.env.example   # Example environment configuration
├── src/
│   └── main.rs          # Main application code
└── README.md            # This file
```

### Key Implementation Details

- Uses Discord REST API v10 endpoint: `POST /channels/{channel_id}/messages`
- Async operations via `tokio` runtime
- HTTP client via `reqwest` library
- Environment variable management via `dotenv`
- Proper error handling and user-friendly error messages

## Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Acknowledgments

- Built with [Rust]https://www.rust-lang.org/
- Powered by [Discord API]https://discord.com/developers/docs
- Inspired by the need for simple CI/CD notifications