discord remote | discord remote control | express automation routes | Search

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.

Run example

npm run import -- "discord remote proxy server"

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,
}


What the code could have been:

/* 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

Importing Modules

The code starts by importing various modules using the importer.import() function. These modules are:

Setting up Express App

The code then sets up an Express.js app by:

Defining Routes

The code defines several routes using the app.use() and app.get() methods:

Starting and Closing the Server

The code defines two functions:

Exporting Functions

The code exports the discordExpress and closeExpress functions.