The code sets up an Express.js app, importing necessary modules and configuring routes for authentication, key tracking, and monitor management, as well as API endpoints for token retrieval and server monitoring. The app can be started or closed via the discordExpress
and closeExpress
functions, which manage the app's lifecycle based on the presence of participants in the activity and the server's running state.
npm run import -- "discord remote proxy server"
const {doClick, doKeys, getMonitor, serveHomepage} = importer.import("express automation routes")
const {DEFAULT_APPLICATION} = importer.import("discord configuration")
const getToken = importer.import("discord express token endpoint")
const {authenticateRoute} = importer.import("discord authenticate instances")
const express = require('express')
const cors = require('cors')
const cookieParser = require('cookie-parser');
const app = express()
const BASE_URI = `https://${DEFAULT_APPLICATION}.discordsays.com/.proxy/`
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;
app.use(cors({
credentials: true,
origin: '*'
}))
app.use(express.json())
app.use(cookieParser())
app.post('/keys', authenticateRoute, doKeys)
app.post('/click', authenticateRoute, doClick)
app.get('/monitors/*', getMonitor)
app.post('/api/token', getToken)
//app.post('/register', registerInstance)
app.get('*', serveHomepage.bind(null, BASE_URI))
let server
async function discordExpress(activity) {
if(!express) {
return false
}
//process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;
if(activity.participants.length == 0 && server) {
server.close()
server = null
}
// TODO: check if user was removed
if(activity.length > 1 || server) {
return
}
return (server = await app.listen(3000, () => {
console.log('Listening on: ', 3000)
}))
}
function closeExpress(activity) {
if(activity) {
return
}
if(server) {
server.close()
server = null
}
}
module.exports = {
discordExpress,
closeExpress,
}
/* eslint-disable no-console */
const { doClick, doKeys, getMonitor, serveHomepage } = require('express-automation-routes');
const { DEFAULT_APPLICATION } = require('discord-configuration');
const { getToken } = require('discord-express-token-endpoint');
const { authenticateRoute } = require('discord-authenticate-instances');
const express = require('express');
const cors = require('cors');
const cookieParser = require('cookie-parser');
const app = express();
const BASE_URI = `https://${DEFAULT_APPLICATION}.discordsays.com/.proxy/`;
app.use(cors({
credentials: true,
origin: '*',
}));
app.use(express.json());
app.use(cookieParser());
app.post('/keys', authenticateRoute, doKeys);
app.post('/click', authenticateRoute, doClick);
app.get('/monitors/*', getMonitor);
app.post('/api/token', getToken);
// app.post('/register', registerInstance);
app.get('*', serveHomepage.bind(null, BASE_URI));
let server;
/**
* Start Discord Express server
* @param {Object} activity - Discord activity object
* @returns {Promise}
*/
async function discordExpress(activity) {
if (!express) {
return false;
}
// Reject unauthorized certificates
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;
// Close server if there are no participants
if (activity.participants.length === 0 && server) {
await closeExpress();
}
// Check if user was removed from activity
if (activity.participants.length > 1 && activity.participants.length > 0) {
return;
}
// Start server if there is only one participant
if (activity.participants.length === 1) {
return (server = await new Promise((resolve, reject) => {
app.listen(3000, () => {
console.log('Listening on:', 3000);
resolve(server);
}).on('error', reject);
}));
}
}
/**
* Close Discord Express server
* @param {Object} activity - Discord activity object
* @returns {Promise}
*/
async function closeExpress(activity) {
if (activity) {
return;
}
if (server) {
await new Promise((resolve, reject) => {
server.close((err) => {
if (err) {
reject(err);
} else {
server = null;
resolve();
}
});
});
}
}
module.exports = {
discordExpress,
closeExpress,
};
Code Breakdown
The code starts by importing various modules using the importer.import()
function. These modules are:
express automation routes
: exports functions for Express.js routing (doClick
, doKeys
, getMonitor
, serveHomepage
)discord configuration
: exports the DEFAULT_APPLICATION
constantdiscord express token endpoint
: exports the getToken
functiondiscord authenticate instances
: exports the authenticateRoute
functionThe code then sets up an Express.js app by:
express
modulecors
and cookie-parser
modulesThe code defines several routes using the app.use()
and app.get()
methods:
/keys
: accepts POST requests, uses the authenticateRoute
middleware, and calls the doKeys
function/click
: accepts POST requests, uses the authenticateRoute
middleware, and calls the doClick
function/monitors/*
: accepts GET requests and calls the getMonitor
function/api/token
: accepts POST requests and calls the getToken
function*
: accepts GET requests and calls the serveHomepage
functionThe code defines two functions:
discordExpress(activity)
: starts the Express app listening on port 3000 if there are no participants in the activity and the server is not already runningcloseExpress(activity)
: closes the Express app if the server is runningThe code exports the discordExpress
and closeExpress
functions.