Implement Discord CnC Server into Emmelia
This commit is contained in:
123
index.js
123
index.js
@@ -1,14 +1,22 @@
|
||||
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');
|
||||
const path = require('node:path');
|
||||
require('dotenv').config();
|
||||
prefix = process.env.PREFIX
|
||||
token = process.env.TOKEN;
|
||||
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 {
|
||||
quotes
|
||||
} = require('./quotes.json');
|
||||
|
||||
//const Discord = require('discord.js');Client, Collection, Intents
|
||||
const {
|
||||
Client,
|
||||
@@ -18,54 +26,81 @@ const {
|
||||
MessageButton
|
||||
} = require('discord.js');
|
||||
//const client = new Discord.Client();
|
||||
|
||||
const client = new Client({
|
||||
intents: [Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILDS]
|
||||
});
|
||||
const PORT = process.env.PORT || 3000;
|
||||
const express = require("express");
|
||||
const server = express();
|
||||
var libCore = require("./libCore.js");
|
||||
|
||||
let linkFlayerMap = [];
|
||||
prefix = process.env.PREFIX
|
||||
discordToken = process.env.TOKEN;
|
||||
|
||||
server.all("/", (req, res) => {
|
||||
var htmlOutput = "LinkFlayer Bot is Ready - Sources loading <br />";
|
||||
var indexRouter = require('./routes/index');
|
||||
var nodesRouter = require('./routes/nodes');
|
||||
var adminRouter = require('./routes/admin');
|
||||
|
||||
var sources = libCore.getSources();
|
||||
sources.forEach(source => {
|
||||
htmlOutput += `
|
||||
<div style='margin-bottom:15px;'>
|
||||
// HTTP Server Config
|
||||
var app = express();
|
||||
|
||||
<div> Title: ${source.title} </div>
|
||||
<div> Link: ${source.link} </div>
|
||||
<div> category: ${source.category} </div>
|
||||
// view engine setup
|
||||
app.set('views', path.join(__dirname, 'views'));
|
||||
app.set('view engine', 'ejs');
|
||||
|
||||
</div>
|
||||
<div>
|
||||
<hr />
|
||||
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')));
|
||||
|
||||
</div>
|
||||
// Web Interface
|
||||
app.use('/', indexRouter);
|
||||
|
||||
`
|
||||
});
|
||||
res.send(htmlOutput);
|
||||
// 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));
|
||||
});
|
||||
|
||||
function keepAlive() {
|
||||
server.listen(PORT, () => {
|
||||
console.log("Keep Alive Server Running");
|
||||
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.loadFeeds();
|
||||
libCore.feedArray = libCore.getFeeds();
|
||||
} catch (error) {
|
||||
console.log(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'));
|
||||
@@ -78,8 +113,13 @@ for (const file of commandFiles) {
|
||||
client.commands.set(command.name, command);
|
||||
}
|
||||
|
||||
client.on('ready', () => {
|
||||
console.log(`Logged in as ${client.user.tag}!`);
|
||||
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 => {
|
||||
@@ -95,6 +135,7 @@ client.on('interactionCreate', async interaction => {
|
||||
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 });
|
||||
}
|
||||
@@ -111,17 +152,15 @@ client.on('messageCreate', message => {
|
||||
try {
|
||||
client.commands.get(command).execute(message, args);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
log.ERROR(error);
|
||||
//message.reply('there was an error trying to execute that command!');
|
||||
}
|
||||
});
|
||||
|
||||
console.log("Link Flayer Bot Activating");
|
||||
keepAlive();
|
||||
client.login(token); //Load Client Discord Token
|
||||
client.login(discordToken); //Load Client Discord Token
|
||||
try {
|
||||
console.log("Loading initial startup feeds");
|
||||
log.INFO("Loading initial startup feeds");
|
||||
libCore.loadFeeds();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
log.ERROR(error);
|
||||
}
|
||||
Reference in New Issue
Block a user