Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions discord-bot/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
FROM node:20-alpine

WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm ci --only=production

# Copy source code
COPY . .

# Build TypeScript
RUN npm run build

# Create non-root user
RUN addgroup -g 1001 -S nodejs
RUN adduser -S discord-bot -u 1001

# Change ownership of the app directory
RUN chown -R discord-bot:nodejs /app
USER discord-bot

# Expose port (for health checks if needed)
EXPOSE 3003

# Start the bot
CMD ["npm", "start"]
FROM node:20-alpine
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies
RUN npm ci --only=production
# Copy source code
COPY . .
# Build TypeScript
RUN npm run build
# Create non-root user
RUN addgroup -g 1001 -S nodejs
RUN adduser -S discord-bot -u 1001
# Change ownership of the app directory
RUN chown -R discord-bot:nodejs /app
USER discord-bot
# Expose port (for health checks if needed)
EXPOSE 3003
# Start the bot
CMD ["npm", "start"]
190 changes: 95 additions & 95 deletions discord-bot/README.md
Original file line number Diff line number Diff line change
@@ -1,95 +1,95 @@
# Discord Bot

A Discord bot that tracks user activity and awards points for messages sent in Discord servers.

## Features

- Tracks user messages and awards activity points
- Stores activity events in PostgreSQL database
- Rate limiting to prevent spam
- Configurable guild filtering
- Structured logging with Pino

## Quick Start

### 1. Environment Setup

Create a `.env` file in the `discord-bot` directory:

```bash
# Discord Bot Configuration
DISCORD_BOT_TOKEN=your_bot_token_here
DISCORD_APP_ID=your_app_id_here
DISCORD_GUILD_ID=your_guild_id_here

# Database Configuration
DATABASE_URL=postgresql://guild_user:guild_password@localhost:5433/guild_genesis

# Bot Behavior
POINTS_PER_MESSAGE=1
LOG_LEVEL=info
MAX_MESSAGES_PER_MINUTE=10
```

### 2. Database Migration

Start PostgreSQL and run the migration:

```bash
# Start PostgreSQL (if using Docker)
docker-compose up postgres -d

# Run migration
psql postgresql://guild_user:guild_password@localhost:5433/guild_genesis -f migrations/001_create_activity_events.sql
```

### 3. Run the Bot

```bash
# Development mode
npm run dev

# Production mode
npm run build && npm start
```

### 4. Docker Deployment

```bash
# Build and run with docker-compose
docker-compose up discord-bot
```

## Database Schema

The bot creates an `activity_events` table with the following structure:

- `id`: UUID primary key
- `user_id`: Discord user ID (stable identifier)
- `user_name`: Discord username (may change)
- `amount`: Points awarded (default: 1)
- `event_type`: Type of event (currently only 'message')
- `date`: Timestamp of the event
- `processed_status`: Whether the event has been processed for minting
- `created_at`: Record creation timestamp

## Architecture

- **src/index.ts**: Main bot logic and Discord.js client
- **src/env.ts**: Environment variable validation with Zod
- **src/db.ts**: Database connection and activity event operations
- **src/logger.ts**: Structured logging configuration
- **migrations/**: SQL migration files

## Rate Limiting

The bot implements basic rate limiting to prevent spam:
- Maximum 10 messages per user per minute
- In-memory tracking (resets on bot restart)

## Future Enhancements

- Additional event types (votes, reactions, etc.)
- Persistent rate limiting with Redis
- Health check endpoint
- Metrics and monitoring
# Discord Bot
A Discord bot that tracks user activity and awards points for messages sent in Discord servers.
## Features
- Tracks user messages and awards activity points
- Stores activity events in PostgreSQL database
- Rate limiting to prevent spam
- Configurable guild filtering
- Structured logging with Pino
## Quick Start
### 1. Environment Setup
Create a `.env` file in the `discord-bot` directory:
```bash
# Discord Bot Configuration
DISCORD_BOT_TOKEN=your_bot_token_here
DISCORD_APP_ID=your_app_id_here
DISCORD_GUILD_ID=your_guild_id_here
# Database Configuration
DATABASE_URL=postgresql://guild_user:guild_password@localhost:5433/guild_genesis
# Bot Behavior
POINTS_PER_MESSAGE=1
LOG_LEVEL=info
MAX_MESSAGES_PER_MINUTE=10
```
### 2. Database Migration
Start PostgreSQL and run the migration:
```bash
# Start PostgreSQL (if using Docker)
docker-compose up postgres -d
# Run migration
psql postgresql://guild_user:guild_password@localhost:5433/guild_genesis -f migrations/001_create_activity_events.sql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this should be updated

```
### 3. Run the Bot
```bash
# Development mode
npm run dev
# Production mode
npm run build && npm start
```
### 4. Docker Deployment
```bash
# Build and run with docker-compose
docker-compose up discord-bot
```
## Database Schema
The bot creates an `activity_events` table with the following structure:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and guild_id should be added here

- `id`: UUID primary key
- `user_id`: Discord user ID (stable identifier)
- `user_name`: Discord username (may change)
- `amount`: Points awarded (default: 1)
- `event_type`: Type of event (currently only 'message')
- `date`: Timestamp of the event
- `processed_status`: Whether the event has been processed for minting
- `created_at`: Record creation timestamp
## Architecture
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add new folder here

- **src/index.ts**: Main bot logic and Discord.js client
- **src/env.ts**: Environment variable validation with Zod
- **src/db.ts**: Database connection and activity event operations
- **src/logger.ts**: Structured logging configuration
- **migrations/**: SQL migration files
## Rate Limiting
The bot implements basic rate limiting to prevent spam:
- Maximum 10 messages per user per minute
- In-memory tracking (resets on bot restart)
## Future Enhancements
- Additional event types (votes, reactions, etc.)
- Persistent rate limiting with Redis
- Health check endpoint
- Metrics and monitoring
32 changes: 16 additions & 16 deletions discord-bot/env.example
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Discord Bot Environment Variables

# Discord Bot Configuration
DISCORD_BOT_TOKEN=your_bot_token_here
DISCORD_APP_ID=your_app_id_here
DISCORD_GUILD_ID=your_guild_id_here

# Database Configuration
DATABASE_URL=postgresql://guild_user:guild_password@localhost:5433/guild_genesis

# Bot Behavior
POINTS_PER_MESSAGE=1
LOG_LEVEL=info

# Optional: Rate Limiting
MAX_MESSAGES_PER_MINUTE=10
# Discord Bot Environment Variables
# Discord Bot Configuration
DISCORD_BOT_TOKEN=your_bot_token_here
DISCORD_APP_ID=your_app_id_here
DISCORD_GUILD_ID=your_guild_id_here
# Database Configuration
DATABASE_URL=postgresql://guild_user:guild_password@localhost:5433/guild_genesis
# Bot Behavior
POINTS_PER_MESSAGE=1
LOG_LEVEL=info
# Optional: Rate Limiting
MAX_MESSAGES_PER_MINUTE=10
54 changes: 27 additions & 27 deletions discord-bot/migrations/001_create_activity_events.sql
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
-- Create activity_events table for Discord bot
-- This table stores activity points awarded to users for various events

CREATE TABLE IF NOT EXISTS activity_events (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id TEXT NOT NULL,
user_name TEXT NOT NULL,
amount INTEGER NOT NULL DEFAULT 1,
event_type TEXT NOT NULL CHECK (event_type IN ('message')),
date TIMESTAMPTZ NOT NULL DEFAULT NOW(),
processed_status BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Create indexes for better query performance
CREATE INDEX IF NOT EXISTS idx_activity_events_user_id ON activity_events(user_id);
CREATE INDEX IF NOT EXISTS idx_activity_events_processed ON activity_events(processed_status);
CREATE INDEX IF NOT EXISTS idx_activity_events_date ON activity_events(date);
CREATE INDEX IF NOT EXISTS idx_activity_events_event_type ON activity_events(event_type);

-- Add comment for documentation
COMMENT ON TABLE activity_events IS 'Stores activity points awarded to Discord users for various events';
COMMENT ON COLUMN activity_events.user_id IS 'Discord user ID (stable identifier)';
COMMENT ON COLUMN activity_events.user_name IS 'Discord username (may change over time)';
COMMENT ON COLUMN activity_events.amount IS 'Number of points awarded for this event';
COMMENT ON COLUMN activity_events.event_type IS 'Type of event that triggered the points (message, vote, etc.)';
COMMENT ON COLUMN activity_events.processed_status IS 'Whether this event has been processed for token minting';
-- Create activity_events table for Discord bot
-- This table stores activity points awarded to users for various events
CREATE TABLE IF NOT EXISTS activity_events (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id TEXT NOT NULL,
user_name TEXT NOT NULL,
amount INTEGER NOT NULL DEFAULT 1,
event_type TEXT NOT NULL CHECK (event_type IN ('message')),
date TIMESTAMPTZ NOT NULL DEFAULT NOW(),
processed_status BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- Create indexes for better query performance
CREATE INDEX IF NOT EXISTS idx_activity_events_user_id ON activity_events(user_id);
CREATE INDEX IF NOT EXISTS idx_activity_events_processed ON activity_events(processed_status);
CREATE INDEX IF NOT EXISTS idx_activity_events_date ON activity_events(date);
CREATE INDEX IF NOT EXISTS idx_activity_events_event_type ON activity_events(event_type);
-- Add comment for documentation
COMMENT ON TABLE activity_events IS 'Stores activity points awarded to Discord users for various events';
COMMENT ON COLUMN activity_events.user_id IS 'Discord user ID (stable identifier)';
COMMENT ON COLUMN activity_events.user_name IS 'Discord username (may change over time)';
COMMENT ON COLUMN activity_events.amount IS 'Number of points awarded for this event';
COMMENT ON COLUMN activity_events.event_type IS 'Type of event that triggered the points (message, vote, etc.)';
COMMENT ON COLUMN activity_events.processed_status IS 'Whether this event has been processed for token minting';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE activity_events
ADD COLUMN IF NOT EXISTS guild_id TEXT;

CREATE INDEX IF NOT EXISTS idx_activity_events_guild_id ON activity_events(guild_id);
CREATE INDEX IF NOT EXISTS idx_activity_events_guild_user ON activity_events(guild_id, user_id);

COMMENT ON COLUMN activity_events.guild_id IS 'Discord guild (server) ID where the activity occurred';
Loading
Loading