SafetyAI (a.k.a discord-anti-scam-bot) is a TypeScript-powered Discord bot that uses large-language-model (LLM) providers to detect and automatically moderate phishing, giveaway, Nitro and other scam messages in real-time.
The bot combines classic heuristic triggers with AI classification and a simple online-learning memory to continuously improve over time while keeping false-positives low.
- Multi-provider LLM support – OpenAI, Anthropic, Mistral, Groq, xAI or Hugging Face out of the box.
- Heuristic triggers & hard-blocks – fast regex pre-filter before spending LLM tokens.
- Few-shot memory – ships with seed examples and learns from moderator feedback.
- Configurable moderation actions – delete, DM user, post mod embeds with one-click buttons.
- Rate-limiting – global token bucket + per-channel cool-down to control API usage.
- Structured logging – JSON & pretty Pino logs for easy ingestion.
.
├─ src/ TypeScript source
│ ├─ bot.ts Discord entry-point
│ ├─ moderation.ts Core classification & actions
│ ├─ prompt.ts Few-shot prompt builder
│ ├─ memory.ts Online learning store
│ ├─ providers/ LLM client wrappers
│ └─ util.ts Helpers & logging
├─ examples/ Seed & learned JSONL examples
├─ logs/ Runtime logs (created at run-time)
├─ config.json Runtime configuration (non-secret)
├─ env.example Environment variable template (secrets)
└─ package.json NPM metadata & scripts
-
Clone & install
git clone <repo-url> cd safetyai npm install # or pnpm install / yarn
-
Configure environment
cp env.example .env # edit .env and fill in your Discord token & any provider API keys you plan to useMandatory variables:
DISCORD_TOKEN– bot token (found in the Discord developer portal)
Optional / recommended variables:
MOD_CHANNEL_ID– channel ID where moderation alerts should be sentDEBUG_CHANNEL_ID– channel ID to receive full debug tracesOPENAI_API_KEY,ANTHROPIC_API_KEY, ... – provider keys (fill at least one)
-
Update
config.jsonprovider– one of"openai" | "anthropic" | "mistral" | "groq" | "xai" | "huggingface".model– model name for the chosen provider.- Adjust trigger & hard-block regexes, rate limits and moderation actions as desired.
-
Run in dev-mode (TypeScript directly)
npm run dev
The bot will connect, log in and start monitoring messages.
-
Build & run production JS (Recommended)
npm run build # emits JS to dist/ npm start # runs node dist/bot.js
| Field | Description |
|---|---|
provider |
LLM provider key – must match an implemented client in src/providers/. |
model |
Model name passed verbatim to the provider. |
productionReady |
When true, destructive actions (deletion) are allowed; keep false while testing. |
triggerPatterns |
Array of regex strings; if none match, the LLM is not called (saves tokens). |
hardBlockRegexes |
Regexes that, when matched, immediately mark the message as scam. |
rateLimit.maxCallsPerMinute |
Shared global LLM call quota. |
rateLimit.perChannelCooldownSec |
Minimum seconds between scans in the same channel. |
moderation.minConfidenceToDelete |
Minimum classification confidence before the bot auto-deletes. |
moderation.actions.* |
Toggle deletion, DM, mod alert actions. |
fewShot.* |
Controls how many examples are loaded & priority heuristics. |
logging.level |
Pino log level (debug, info, warn, error). |
Seed examples live in examples/seed_examples.jsonl and are loaded on every start. When the bot makes a decision it also writes learned examples to examples/learned_examples.jsonl, giving each one a weight. Moderator button feedback adds labelled ground-truth examples and influences future prompts.
| Provider | Env var(s) | Notes |
|---|---|---|
| OpenAI | OPENAI_API_KEY, optional OPENAI_BASE_URL |
Works with compatible endpoints (Groq / Together etc.) |
| Anthropic | ANTHROPIC_API_KEY |
Uses /v1/messages API |
| Mistral | MISTRAL_API_KEY |
Official Mistral endpoint |
| Groq | GROQ_API_KEY, GROQ_BASE_URL |
Groq’s OpenAI-compatible proxy |
| xAI | XAI_API_KEY, XAI_BASE_URL |
Experimental |
| Hugging Face | HUGGINGFACE_API_KEY |
Uses text-generation inference API |
Provide at least one key corresponding to the provider in config.json.
Logs are written to:
logs/actions.log– adoption & moderator actions.logs/errors.log– classification errors & fallbacks.
Verbose pretty logs are printed to stdout when NODE_ENV!==production.
The bot needs at minimum:
- Guilds
- Guild Messages
- Message Content (for non-privileged intents)
and Manage Messages permission if you allow auto-deletion.
The token bucket in src/bot.ts enforces a maximum LLM call rate globally and per channel. Tune these values in config.json to keep API costs predictable.
- Fork & create a feature branch.
- Run
npm run devand add your changes (+ tests if applicable). - Open a PR describing your improvements.
All contributions, bug reports and feature requests are welcome!