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 libCore = require("./libCore"); const libUtils = require("./libUtils"); const deployCommands = require("./utilities/deployCommands"); const { DebugBuilder } = require("./utilities/debugBuilder"); const log = new DebugBuilder("server", "index"); const { Routes } = require('discord-api-types/v9'); //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; 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 */ function runHTTPServer() { var server = http.createServer(app); server.listen(port); server.on('error', libUtils.onError); server.on('listening', () => { log.INFO("Local HTTP Server Running"); try { libCore.feedArray = libCore.getFeeds(); } catch (error) { log.ERROR(error); } }) } // 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); // 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); } // Deploy commands deployCommands.deploy(client.guilds.cache.map(guild => guild.id)); client.on('ready', () => { log.DEBUG(`Discord server up and running with client: ${client.user.tag}`); log.DEBUG(`Starting HTTP Server`); runHTTPServer(); log.INFO(`Logged in as ${client.user.tag}!`); log.INFO("HTTP server started!"); }); client.on(Events.InteractionCreate, async interaction => { if (!interaction.isChatInputCommand()) return; const command = interaction.client.commands.get(interaction.commandName); log.DEBUG("Interaction: ", interaction.client.commands); if (!command) { console.error(`No command matching ${interaction.commandName} was found.`); return; } try { await command.execute(interaction); } catch (error) { console.error(error); if (interaction.replied || interaction.deferred) { await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true }); } else { await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); } } }); client.login(discordToken); //Load Client Discord Token try { log.INFO("Loading initial startup feeds"); libCore.loadFeeds(); } catch (error) { log.ERROR(error); }