UNPKG

makecord-create

Version:

Create advanced Discord Bots with Makecord - Now with TypeScript support

155 lines (123 loc) 3.69 kB
# Database Integration Guide This guide explains how to use the database integration in your Discord bot. ## MongoDB If you selected MongoDB as your database, follow these steps: 1. Make sure you have MongoDB installed or use a cloud service like MongoDB Atlas. 2. Add your MongoDB connection string to the `.env` file: ``` MONGODB_URI=mongodb://localhost:27017/your-database-name ``` 3. Import the database module in your code: ```javascript import { User, findOrCreateUser } from './database/index.js'; ``` 4. Use the User model to interact with the database: ```javascript // Find or create a user const user = await findOrCreateUser({ id: message.author.id, username: message.author.username, discriminator: message.author.discriminator, avatar: message.author.avatar }); // Update user settings user.settings.set('preferredLanguage', 'en'); await user.save(); ``` ## SQLite If you selected SQLite as your database, follow these steps: 1. The SQLite database file will be created automatically in the `data` directory. 2. You can customize the database path in the `.env` file: ``` SQLITE_PATH=./data/custom-database.sqlite ``` 3. Import the database module in your code: ```javascript import { User, findOrCreateUser } from './database/index.js'; ``` 4. Use the User model to interact with the database: ```javascript // Find or create a user const user = await findOrCreateUser({ id: message.author.id, username: message.author.username, discriminator: message.author.discriminator, avatar: message.author.avatar }); // Update user settings user.settings = { ...user.settings, preferredLanguage: 'en' }; await user.save(); ``` ## PostgreSQL If you selected PostgreSQL as your database, follow these steps: 1. Make sure you have PostgreSQL installed or use a cloud service. 2. Add your PostgreSQL connection details to the `.env` file: ``` POSTGRES_DB=discord_bot POSTGRES_USER=postgres POSTGRES_PASSWORD=your_password POSTGRES_HOST=localhost POSTGRES_PORT=5432 ``` 3. Import the database module in your code: ```javascript import { User, findOrCreateUser } from './database/index.js'; ``` 4. Use the User model to interact with the database: ```javascript // Find or create a user const user = await findOrCreateUser({ id: message.author.id, username: message.author.username, discriminator: message.author.discriminator, avatar: message.author.avatar }); // Update user settings user.settings = { ...user.settings, preferredLanguage: 'en' }; await user.save(); ``` ## Creating Additional Models You can create additional models for your bot's needs. Here's an example: ```javascript // Create a new file in the models directory // models/Guild.js import { DataTypes } from 'sequelize'; // For SQLite/PostgreSQL // OR import { mongoose } from '../connection.js'; // For MongoDB // For SQLite/PostgreSQL const Guild = sequelize.define('Guild', { guildId: { type: DataTypes.STRING, allowNull: false, unique: true, primaryKey: true }, name: { type: DataTypes.STRING, allowNull: false }, prefix: { type: DataTypes.STRING, defaultValue: '!' } }); // For MongoDB const guildSchema = new mongoose.Schema({ guildId: { type: String, required: true, unique: true }, name: { type: String, required: true }, prefix: { type: String, default: '!' } }); const Guild = mongoose.model('Guild', guildSchema); export { Guild }; ``` Then import and use your new model in your code.