UNPKG

makecord-create

Version:

Create advanced Discord Bots with Makecord - Now with TypeScript support

121 lines (102 loc) 4 kB
import express from 'express'; import rateLimit from 'express-rate-limit'; import { fileURLToPath } from 'url'; import path from 'path'; import fs from 'fs'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const app = express(); const port = process.env.API_PORT || 3000; // Rate limiting const limiter = rateLimit({ windowMs: 15 * 60 * 1000, // 15 minutes max: 100 // limit each IP to 100 requests per windowMs }); app.use(limiter); app.use(express.json()); // Command management endpoints app.post('/commands/register', (req, res) => { try { const { name, description, category } = req.body; // Validate required fields if (!name || !description) { return res.status(400).json({ error: 'Name and description are required' }); } // Create command file const commandDir = path.join(__dirname, '..', 'commands', category || ''); if (!fs.existsSync(commandDir)) { fs.mkdirSync(commandDir, { recursive: true }); } const commandPath = path.join(commandDir, `${name}.js`); const commandTemplate = ` import { SlashCommandBuilder } from 'discord.js'; export default { data: new SlashCommandBuilder() .setName('${name}') .setDescription('${description}'), async execute(interaction) { await interaction.reply('Command executed successfully!'); }, };`; fs.writeFileSync(commandPath, commandTemplate); res.json({ message: 'Command registered successfully' }); } catch (error) { console.error('Error registering command:', error); res.status(500).json({ error: 'Internal server error' }); } }); app.delete('/commands/unregister/:name', (req, res) => { try { const { name } = req.params; let found = false; // Search for command file in all categories const commandsDir = path.join(__dirname, '..', 'commands'); const categories = fs.readdirSync(commandsDir); for (const category of categories) { const categoryPath = path.join(commandsDir, category); if (fs.statSync(categoryPath).isDirectory()) { const commandPath = path.join(categoryPath, `${name}.js`); if (fs.existsSync(commandPath)) { fs.unlinkSync(commandPath); found = true; break; } } } if (!found) { return res.status(404).json({ error: 'Command not found' }); } res.json({ message: 'Command unregistered successfully' }); } catch (error) { console.error('Error unregistering command:', error); res.status(500).json({ error: 'Internal server error' }); } }); app.get('/commands', (req, res) => { try { const commands = []; const commandsDir = path.join(__dirname, '..', 'commands'); const categories = fs.readdirSync(commandsDir); for (const category of categories) { const categoryPath = path.join(commandsDir, category); if (fs.statSync(categoryPath).isDirectory()) { const categoryCommands = fs.readdirSync(categoryPath) .filter(file => file.endsWith('.js')) .map(file => ({ name: path.basename(file, '.js'), category })); commands.push(...categoryCommands); } } res.json(commands); } catch (error) { console.error('Error listing commands:', error); res.status(500).json({ error: 'Internal server error' }); } }); export function startAPI() { app.listen(port, () => { console.log(`REST API server running on port ${port}`); }); }