A Matrix bot with PostgreSQL-backed user management and role-based command authorization.
- 🔐 PostgreSQL-backed user management - Store user data and roles in a relational database
- 🛡️ Role-based access control - Restrict commands to users with specific roles
- 📊 Entity-based architecture - TypeORM entities that reflect database tables
- 🔄 Database migrations - Version-controlled schema changes
- ⚙️ Environment-based configuration - Easy setup with
.envfiles
- Node.js 18 or higher
- PostgreSQL server (local or remote)
- Matrix account with access token
npm installCopy the example environment file and update it with your credentials:
cp .env.example .envEdit .env with your Matrix and PostgreSQL credentials:
# Matrix Configuration
MATRIX_SERVER=https://matrix.example.com
MANAGER_MATRIX_ID=@bot:example.com
MANAGER_MATRIX_ACCESS_TOKEN=your_access_token_here
# Database Configuration
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=your_db_password
DB_NAME=oddnet_matrixCreate the database (if it doesn't exist):
CREATE DATABASE oddnet_matrix;Run migrations to create tables:
npm run migration:runEdit scripts/setup-db.sql with your Matrix ID and run:
psql -U postgres -f scripts/setup-db.sqlOr manually insert into your database:
\c oddnet_matrix
INSERT INTO users ("matrixId", "displayName", "isActive")
VALUES ('@yourname:matrix.org', 'Your Name', true);
INSERT INTO user_roles ("matrixId", "roleId")
VALUES ('@yourname:matrix.org', 1); -- 1 = admin roleDevelopment mode (with hot reload):
npm run devProduction mode:
npm run build
npm startSee DATABASE.md for comprehensive documentation on:
- Database schema and entity relationships
- Migration commands and workflow
- User and role management
- Creating custom roles
- Programmatic database access
!ping- Check if the bot is responsive (public)!echo <message>- Echo back a message (requires: user, admin)!admin users- List all registered users (requires: admin)!admin roles- List all available roles (requires: admin)!help- Show help message (public)
src/
├── controllers/
│ ├── CommandController.ts # Command routing and authorization
│ ├── InputController.ts # Message handling
│ └── commands/ # Individual command handlers
│ ├── pingCommand.ts
│ ├── echoCommand.ts
│ ├── helpCommand.ts
│ └── adminCommand.ts
├── database/
│ ├── entities/ # TypeORM entities
│ │ ├── User.ts
│ │ ├── Role.ts
│ │ └── UserRole.ts
│ ├── migrations/ # Database migrations
│ ├── dataSource.ts # TypeORM configuration
│ └── DatabaseService.ts # Database service layer
├── middleware/
│ └── AuthorizationMiddleware.ts # Role-based access control
├── services/
│ └── MatrixService.ts # Matrix SDK wrapper
├── types/
│ ├── CommandTypes.ts # Command type definitions
│ └── Message.ts # Message type definitions
└── main.ts # Application entry point
To create a new command with role-based access:
- Create a command handler in
src/controllers/commands/:
import { MatrixService } from '../../services/MatrixService';
import { GeneralMessage } from '../../types/Message';
import { DatabaseService } from '../../database/DatabaseService';
export async function handleMyCommand(
message: GeneralMessage,
args: string[],
matrixService: MatrixService,
databaseService: DatabaseService
): Promise<void> {
// Your command logic here
await matrixService.sendMessage(message.roomId, 'Response');
}- Register it in
CommandController.ts:
this.commands.set('mycommand', {
handler: handleMyCommand,
requiredRoles: ['user', 'admin'], // Optional
description: 'My command description'
});Three roles are created by the initial migration:
- admin - Full access to all commands
- user - Basic access to user-level commands
- moderator - Elevated permissions for moderation
You can create custom roles through SQL or by extending the admin commands.
Apache-2.0