npm install# Copy environment template
cp .env.example .env
# Edit .env and set your database credentials:
# DB_HOST=localhost
# DB_PORT=5432
# DB_USERNAME=postgres
# DB_PASSWORD=your_password
# DB_NAME=oddnet_matrix# Option A: Using psql
createdb oddnet_matrix
# Option B: Using SQL
psql -U postgres -c "CREATE DATABASE oddnet_matrix;"npm run migration:runThis creates three tables:
users- Stores Matrix user IDsroles- Stores role definitions (admin, user, moderator)user_roles- Links users to roles
# Edit scripts/setup-db.sql with your Matrix ID
# Then run:
psql -U postgres -f scripts/setup-db.sqlOr manually:
psql -U postgres 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);# Development
npm run dev
# Production
npm run build
npm start| Command | Purpose |
|---|---|
npm run migration:run |
Apply pending migrations |
npm run migration:revert |
Undo last migration |
npm run migration:show |
Show migration status |
npm run migration:generate src/database/migrations/Name |
Auto-generate migration from entity changes |
npm run migration:create src/database/migrations/Name |
Create empty migration file |
INSERT INTO users ("matrixId", "displayName", "isActive")
VALUES ('@user:matrix.org', 'Display Name', true);-- Admin role (id: 1)
INSERT INTO user_roles ("matrixId", "roleId")
VALUES ('@user:matrix.org', 1);
-- User role (id: 2)
INSERT INTO user_roles ("matrixId", "roleId")
VALUES ('@user:matrix.org', 2);
-- Moderator role (id: 3)
INSERT INTO user_roles ("matrixId", "roleId")
VALUES ('@user:matrix.org', 3);SELECT
u."matrixId",
u."displayName",
u."isActive",
r.name as role
FROM users u
LEFT JOIN user_roles ur ON u."matrixId" = ur."matrixId"
LEFT JOIN roles r ON ur."roleId" = r.id;In CommandController.ts, specify required roles:
this.commands.set('echo', {
handler: handleEcho,
requiredRoles: ['user', 'admin'], // Requires at least one role
description: 'Echo back a message'
});
this.commands.set('ping', {
handler: handlePing,
// No requiredRoles = accessible to everyone
description: 'Check bot status'
});- DATABASE.md - Detailed database schema and management
- MIGRATION_GUIDE.md - Comprehensive migration guide
- README.md - Full project documentation
- Check PostgreSQL is running:
sudo systemctl status postgresql - Verify
.envcredentials - Test connection:
psql -U postgres -d oddnet_matrix
- Run migrations:
npm run migration:run
- Verify user exists in
userstable - Check user has required role in
user_rolestable - Ensure user's
isActiveistrue
- Check status:
npm run migration:show - The migration was already applied
- Create custom commands - See examples in
src/controllers/commands/ - Add custom roles - Insert into
rolestable - Modify entities - Edit files in
src/database/entities/ - Generate migrations -
npm run migration:generate
# Pull latest code
git pull
# Install new dependencies
npm install
# Apply new migrations
npm run migration:run
# Start bot
npm run devThat's it! Your bot now has database-backed user management with role-based command authorization.