File size: 1,878 Bytes
5e641bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const BotManager = require('./botManager');
const axios = require('axios');
const https = require('https');

const { APP_URL } = process.env;

// Create an express app
const app = express();
app.use(bodyParser.json());

const port = process.env.PORT || 7860;
const botManager = new BotManager();

// API status
app.get('/api', (req, res) => {
    res.json({ status: 'ok', message: 'Discord Bot Manager is running' });
});

// Bots status
app.get('/api/status', (req, res) => {
    const status = botManager.getBotsStatus();
    res.json(status);
});

// Endpoint para iniciar um bot
app.post('/api/start', (req, res) => {
    const { token } = req.body;
    if (!token) return res.status(400).json({ error: 'Token is required' });

    const response = botManager.startBot(token);
    res.json({ message: response });
});

// Endpoint para parar um bot
app.post('/api/stop', (req, res) => {
    const { token } = req.body;
    if (!token) return res.status(400).json({ error: 'Token is required' });

    const response = botManager.stopBot(token);
    res.json({ message: response });
});

// Fetch active bots and start them
axios.get(`${APP_URL}/api/assistants/active`, {
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    },
    httpsAgent: new https.Agent({  
        rejectUnauthorized: false  
    }),
    headers: {
        'Authorization': `Bearer ${process.env.API_TOKEN}`
    }
})
.then(response => {
    const bots = response.data;
    bots.forEach(bot => {
        botManager.startBot(bot.discord_bot_token);
    });
})
.catch(error => {
    console.error('Error fetching active bots:', error.message);
});

// Start the server
app.listen(port, () => {
    console.log(`Server running on port ${port}`);
});