I wonder if there is any way to access a Discord.Client
instance from another file.
What I've tried:
posts.js
const express = require('express')
const router = express.Router()
router.post('/checkRole', function (req, res) {
const guild = client.guilds.find(guild => guild.id === '688678156374044803');
let member = guild.members.get(req.body.id); // member ID
// Here I get an error which says the 'guild' is undefined.
res.send(response);
});
module.exports = router
index.js
const Discord = require("discord.js");
global.client = new Discord.Client();
const express = require('express');
const postsRoute = require('./routes/posts')
app = express()
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use('/posts', postsRoute)
/* ... some other code ... */
client.login(config.token);
app.listen(8080);
module.exports = { client }
I've also tried a similar approach like this but it didn't work either. Lastly I tried to store the client instance as a global variable but still no luck. Is there any way to achieve this?