165 lines
4.5 KiB
JavaScript
165 lines
4.5 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');
|
|
require('dotenv').config();
|
|
const fs = require('fs');
|
|
const { DebugBuilder } = require("./utilities/debugBuilder");
|
|
const deployCommands = require('./utilities/deployCommands');
|
|
|
|
var indexRouter = require('./routes/index');
|
|
var botRouter = require('./routes/bot');
|
|
var clientRouter = require('./routes/client');
|
|
var radioRouter = require('./routes/radio');
|
|
|
|
const log = new DebugBuilder("client", "app");
|
|
const {
|
|
Client,
|
|
Events,
|
|
Collection,
|
|
GatewayIntentBits,
|
|
MessageActionRow,
|
|
MessageButton
|
|
} = require('discord.js');
|
|
|
|
var app = express();
|
|
var discordToken = process.env.TOKEN;
|
|
var port = process.env.HTTP_PORT || '3010';
|
|
|
|
const discordClient = new Client({
|
|
intents: [
|
|
GatewayIntentBits.Guilds,
|
|
GatewayIntentBits.GuildMessages,
|
|
GatewayIntentBits.MessageContent,
|
|
GatewayIntentBits.GuildVoiceStates
|
|
]
|
|
});
|
|
|
|
// view engine setup
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
app.set('view engine', 'ejs');
|
|
app.set('port', port);
|
|
|
|
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')));
|
|
|
|
// Default route
|
|
app.use('/', indexRouter);
|
|
|
|
// Discord bot control route
|
|
app.use('/bot', (res, req, next) => {
|
|
req.discordClient = discordClient; // Add the discord client to bot requests to be used downstream
|
|
next()
|
|
}, botRouter);
|
|
|
|
// Local client control route
|
|
app.use("/client", clientRouter);
|
|
|
|
// Local radio controller route
|
|
app.use("/radio", radioRouter);
|
|
|
|
// catch 404 and forward to error handler
|
|
app.use((req, res, next) => {
|
|
next(createError(404));
|
|
});
|
|
|
|
// 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', (error) => {
|
|
if (error.syscall !== 'listen') {
|
|
throw error;
|
|
}
|
|
|
|
var bind = typeof port === 'string'
|
|
? 'Pipe ' + port
|
|
: 'Port ' + port;
|
|
|
|
// handle specific listen errors with friendly messages
|
|
switch (error.code) {
|
|
case 'EACCES':
|
|
log.ERROR(bind + ' requires elevated privileges');
|
|
process.exit(1);
|
|
break;
|
|
case 'EADDRINUSE':
|
|
log.ERROR(bind + ' is already in use');
|
|
process.exit(1);
|
|
break;
|
|
default:
|
|
throw error;
|
|
}
|
|
});
|
|
|
|
server.on('listening', () => {
|
|
log.INFO("HTTP server started!");
|
|
})
|
|
}
|
|
|
|
|
|
// Discord bot config
|
|
|
|
// Setup commands for the Discord bot
|
|
discordClient.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
|
|
discordClient.commands.set(command.data.name, command);
|
|
}
|
|
|
|
// Run when the bot is ready
|
|
discordClient.on('ready', () => {
|
|
log.DEBUG(`Discord server up and running with client: ${discordClient.user.tag}`);
|
|
log.INFO(`Logged in as ${discordClient.user.tag}!`);
|
|
|
|
// Deploy slash commands
|
|
log.DEBUG("Deploying slash commands");
|
|
deployCommands.deploy(discordClient.user.id, discordClient.guilds.cache.map(guild => guild.id));
|
|
|
|
log.DEBUG(`Starting HTTP Server`);
|
|
runHTTPServer();
|
|
});
|
|
|
|
// Setup any additional event handlers
|
|
const eventsPath = path.join(__dirname, 'events');
|
|
if (fs.existsSync(eventsPath)) {
|
|
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
|
|
if (eventFiles.length > 0) {
|
|
for (const file of eventFiles) {
|
|
const filePath = path.join(eventsPath, file);
|
|
const event = require(filePath);
|
|
if (event.once) {
|
|
discordClient.once(event.name, (...args) => event.execute(...args));
|
|
} else {
|
|
discordClient.on(event.name, (...args) => event.execute(...args));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
discordClient.login(discordToken); //Load Client Discord Token
|