# HaikzBot — Discord Commerce Platform

Discord bot untuk commerce, ticket, server cloning, dan automation. Stack: Node.js + discord.js + better-sqlite3 + Express webhook.

## Features

✅ **Setup Wizard** — `/setup` auto-create kategori store / ticket / welcome / logs
✅ **Server Clone** — `/clone snapshot` di server lama, `/clone apply` di server baru. Copy roles, channels, categories, permissions. Tidak menyalin member/pesan/history.
✅ **Store + QRIS** — `/product add`, `/stock add`, `/store panel`. Auto-delivery via DM saat bayar.
✅ **Ticket System** — panel + auto-create channel, claim, transcript HTML dengan auto-redact (email/phone/token/QRIS).
✅ **Welcome + Logs** — auto-greet member baru, account-age check, audit log.
✅ **Emoji** — `/emoji add` upload via URL/attachment.
✅ **Automation** — order expiry, daily report, low-stock alert ke owner.
✅ **Web webhook** — Tripay QRIS, mock provider buat dev.

## Setup

```bash
cd /root/main/discord-bot
cp .env.example .env
# Edit .env — minimal isi: DISCORD_TOKEN, CLIENT_ID, OWNER_ID, DEV_GUILD_ID
npm install
npm run deploy   # register slash commands
npm start
```

## .env wajib

```
DISCORD_TOKEN=          # dari Discord Developer Portal → Bot
CLIENT_ID=              # Application ID
OWNER_ID=               # Discord user ID kamu
DEV_GUILD_ID=           # Server testing (instant register)
```

## .env optional

```
PAYMENT_PROVIDER=mock   # tripay | mock
TRIPAY_API_KEY=
TRIPAY_PRIVATE_KEY=
TRIPAY_MERCHANT_CODE=
WEB_BASE_URL=https://your-domain.com  # for callback URLs
```

## Cara dapet token Discord

1. Buka https://discord.com/developers/applications
2. New Application → Bot → Reset Token → copy ke `DISCORD_TOKEN`
3. OAuth2 → URL Generator → centang `bot` + `applications.commands`
4. Bot Permissions: Administrator (atau permission spesifik: Manage Channels, Manage Roles, Manage Webhooks, Manage Emojis, Send Messages, Embed Links, Attach Files, Read Message History, Manage Messages, Create Public Threads)
5. Invite link → buka di browser → pilih server kamu
6. Application ID di General Information → copy ke `CLIENT_ID`

## Discord Intents (PENTING)

Di Bot page, enable:
- ✅ **Server Members Intent** (buat welcome / member event)
- ✅ **Message Content Intent** (buat ticket auto-respond AI)
- ✅ **Presence Intent** (optional)

## Test flow

1. `/setup` → klik **Full Setup** — auto-create kategori store/ticket/welcome/logs
2. `/product add name:"VPN Premium" price:50000` — buat produk
3. `/stock add product_id:1 content:"user1:pass1 | user2:pass2"` — isi stok
4. `/store panel channel:#products` — kirim panel beli
5. Klik produk dari menu → invoice generated → klik tombol Bayar
6. Mock provider auto-success → DM otomatis dengan stok

## Server clone

1. Di server lama: `/clone snapshot` — bot bikin snapshot (return ID misalnya `5`)
2. Invite bot ke server baru
3. Di server baru: `/clone apply snapshot_id:5` → pilih `Full clone`
4. Bot create roles + categories + channels + permissions

## Architecture

```
src/
├── index.js                 # entry point
├── core/
│   ├── db.js               # better-sqlite3 schema + helpers
│   └── loader.js           # auto-load commands & events
├── commands/               # slash commands
│   ├── setup.js
│   ├── clone.js
│   ├── ticket.js
│   ├── store.js
│   ├── product.js
│   ├── stock.js
│   ├── emoji.js
│   ├── config.js
│   └── help.js
├── events/
│   ├── ready.js
│   ├── interactionCreate.js
│   └── guildMemberAdd.js
├── modules/
│   ├── server-clone/       # snapshot + apply
│   ├── setup-wizard/       # full setup auto-create
│   ├── ticket/             # buttons + transcript
│   ├── store/              # checkout flow
│   ├── payment/            # provider abstraction + delivery
│   └── automation/         # cron scheduler
└── web/
    └── server.js           # Express webhook receiver
```

## Database

SQLite di `data/bot.db`. Schema lengkap di `src/core/db.js`. Tables:

- `guilds` — config per server
- `products` + `stock` — katalog
- `orders` — transaksi
- `tickets` — sistem ticket
- `clone_snapshots` — server clone snapshots
- `audit_log` — semua action

## Production checklist

- [ ] Pasang Tripay credentials di `.env`
- [ ] Set `WEB_BASE_URL` ke domain public (untuk callback)
- [ ] Reverse proxy (nginx) ke port `WEB_PORT` (default 4002) dengan TLS
- [ ] Whitelist Tripay IP di firewall
- [ ] PM2 / systemd untuk auto-restart
- [ ] Backup `data/bot.db` daily
- [ ] Monitor `/api/stats` endpoint
