Death
Death
DIAdiscord.js - Imagine an app
Created by Death on 8/7/2023 in #djs-questions
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?
5 replies