var createError = require('http-errors'); var express = require('express'); var path = require('path'); var cookieParser = require('cookie-parser'); var logger = require('morgan'); var http = require('http'); const fs = require('fs'); require('dotenv').config(); const { RSSController } = require("./controllers/rssController"); const libUtils = require("./libUtils"); const deployCommands = require("./utilities/deployCommands"); const { DebugBuilder } = require("./utilities/debugBuilder"); const log = new DebugBuilder("server", "index"); //const Discord = require('discord.js');Client, Collection, Intents const { Client, Events, Collection, GatewayIntentBits, MessageActionRow, MessageButton } = require('discord.js'); //const client = new Discord.Client(); const client = new Client({ intents: [GatewayIntentBits.GuildMessages, GatewayIntentBits.Guilds] }); prefix = process.env.PREFIX discordToken = process.env.TOKEN; rssTimeoutValue = process.env.RSS_TIMEOUT_VALUE ?? 300000; var indexRouter = require('./routes/index'); var nodesRouter = require('./routes/nodes'); var adminRouter = require('./routes/admin'); // HTTP Server Config var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); app.use(logger('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); // Web Interface app.use('/', indexRouter); // Nodes API app.use('/nodes', nodesRouter); // Admin API app.use('/admin', adminRouter); // catch 404 and forward to error handler app.use((req, res, next) => { next(createError(404)); }); var port = libUtils.normalizePort(process.env.HTTP_PORT || '3000'); app.set('port', port); // error handler app.use((err, req, res, next) => { // set locals, only providing error in development res.locals.message = err.message; res.locals.error = req.app.get('env') === 'development' ? err : {}; // render the error page res.status(err.status || 500); res.render('error'); }); /** * Start the HTTP background server */ async function runHTTPServer() { var server = http.createServer(app); server.listen(port); server.on('error', libUtils.onError); server.on('listening', () => { log.INFO("HTTP server started!"); }) } /** * Start the RSS background process */ async function runRssService() { const rssController = new RSSController(client); rssController.start(); } // Discord bot config // Setup commands for the Discord bot client.commands = new Collection(); const commandsPath = path.join(__dirname, 'commands'); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); //const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); for (const file of commandFiles) { const filePath = path.join(commandsPath, file); const command = require(filePath); log.DEBUG("Importing command: ", command.data.name); // Set a new item in the Collection // With the key as the command name and the value as the exported module client.commands.set(command.data.name, command); } // Run when the bot is ready client.on('ready', () => { log.DEBUG(`Discord server up and running with client: ${client.user.tag}`); log.INFO(`Logged in as ${client.user.tag}!`); // Deploy slash commands log.DEBUG("Deploying slash commands"); deployCommands.deploy(client.user.id, client.guilds.cache.map(guild => guild.id)); log.DEBUG(`Starting HTTP Server`); runHTTPServer(); log.DEBUG("Starting RSS watcher"); runRssService(); }); // Setup any additional event handlers const eventsPath = path.join(__dirname, 'events'); const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js')); for (const file of eventFiles) { const filePath = path.join(eventsPath, file); const event = require(filePath); if (event.once) { client.once(event.name, (...args) => event.execute(...args)); } else { client.on(event.name, (...args) => event.execute(...args)); } } client.login(discordToken); //Load Client Discord Token