Files
Emmelia-Link-Flayer-Rewrite/index.js
2023-02-24 21:27:55 -05:00

166 lines
4.2 KiB
JavaScript

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 { 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,
Collection,
Intents,
MessageActionRow,
MessageButton
} = require('discord.js');
//const client = new Discord.Client();
const client = new Client({
intents: [Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.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.name, command);
}
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('interactionCreate', async interaction => {
//if (!interaction.isCommand()) return;
if (!interaction.isSelectMenu()) return;
let aaa = interaction.values[0];
await interaction.channel.send({
content: 'You picked something',
ephemeral: true
});
try {
//await command.execute(interaction);
} catch (error) {
log.ERROR(error);
//console.error(error);
//await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
client.on('messageCreate', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (!client.commands.has(command)) return;
try {
client.commands.get(command).execute(message, args);
} catch (error) {
log.ERROR(error);
//message.reply('there was an error trying to execute that command!');
}
});
client.login(discordToken); //Load Client Discord Token
try {
log.INFO("Loading initial startup feeds");
libCore.loadFeeds();
} catch (error) {
log.ERROR(error);
}