Get a guild object inside an express API route

I am trying to make my bot do some changes to our server based on a request to our API endpoint. in the index.js I am starting the server and the bot, then exporting the bot to access the client on the API route. index.js
const { Client } = require('discord.js');
const app = express();

require('./routes')(app);
...

// Log in to Discord with your client's token
client.login(process.env.DISCORD_TOKEN);

const PORT = process.env.PORT || 3001;

app.listen(PORT, () => {
console.log(`Server is listening in port ${PORT}`);
});

module.exports.client = client;
const { Client } = require('discord.js');
const app = express();

require('./routes')(app);
...

// Log in to Discord with your client's token
client.login(process.env.DISCORD_TOKEN);

const PORT = process.env.PORT || 3001;

app.listen(PORT, () => {
console.log(`Server is listening in port ${PORT}`);
});

module.exports.client = client;
In the route I want the bot to make changes in: create.js
const express = require('express');

const index = require('../index');

const router = express.Router();

router.post('/', async (req, res) => {
const { name } = req.body;

if (!name) return res.status(400).send('Name is required');

const { client } = index;

if (client.isReady()) {
console.log('Client is Ready');

const guild = await client.guilds.fetch(process.env.GUILD_ID);
console.log(guild);
const role = await guild.roles.create({
name,
color: 'Random',
mentionable: true,
permissions: [],
});
} else {
res.status(500).send('The Discord bot is not ready.');
}
}
....
const express = require('express');

const index = require('../index');

const router = express.Router();

router.post('/', async (req, res) => {
const { name } = req.body;

if (!name) return res.status(400).send('Name is required');

const { client } = index;

if (client.isReady()) {
console.log('Client is Ready');

const guild = await client.guilds.fetch(process.env.GUILD_ID);
console.log(guild);
const role = await guild.roles.create({
name,
color: 'Random',
mentionable: true,
permissions: [],
});
} else {
res.status(500).send('The Discord bot is not ready.');
}
}
....
This gives me a type error TypeError: Cannot read properties of undefined (reading 'create') The guild I fetch is an object that only has a few properties and I can't access properties like roles, channels...etc
Collection(1) [Map] {
'xxxxxxxxxxxxxxxxxxx' => OAuth2Guild {
id: 'xxxxxxxxxxxxxxxxxx',
name: 'Testing',
icon: null,
features: [ 'NEWS', 'COMMUNITY' ],
owner: false,
permissions: PermissionsBitField { bitfield: xxxxxxxxxxxxxxxxxxx}
}
}
Collection(1) [Map] {
'xxxxxxxxxxxxxxxxxxx' => OAuth2Guild {
id: 'xxxxxxxxxxxxxxxxxx',
name: 'Testing',
icon: null,
features: [ 'NEWS', 'COMMUNITY' ],
owner: false,
permissions: PermissionsBitField { bitfield: xxxxxxxxxxxxxxxxxxx}
}
}
How can I change the fetched guild into an object to access roles and other properties and methods?
4 Replies
d.js toolkit
d.js toolkit•10mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
monbrey
monbrey•10mo ago
This is a pretty bad pattern. Exporting client from your index creates circular references and causes issues
Death
Death•10mo ago
Oh thank you for this note 🙌
How can I reach the result I want in a better way? What I am trying to accomplish is that we have an application where I want people to click a button and be able to make the bot create channels, roles...etc.
monbrey
monbrey•10mo ago
you could just make raw API calls