Simplifying the API request process

import { REST } from '@discordjs/rest';
import { API } from '@discordjs/core/http-only';

const rest = new REST({ version: '10' }).setToken('xxxxxxxx');
const api = new API(rest);

await api.channels.createMessage('channelId', { content: 'Hello World' });
import { REST } from '@discordjs/rest';
import { API } from '@discordjs/core/http-only';

const rest = new REST({ version: '10' }).setToken('xxxxxxxx');
const api = new API(rest);

await api.channels.createMessage('channelId', { content: 'Hello World' });
This code works well, but can we streamline it further? Currently, we specify the channelId separately from the request body. Is there a way to combine all required parameters into one object, like this?
await api.channels.createMessage({ channelId, 'xxxxxxxx', content: 'Hello World' });
await api.channels.createMessage({ channelId, 'xxxxxxxx', content: 'Hello World' });
8 Replies
d.js toolkit
d.js toolkit7mo 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! - Marked as resolved by OP
Amir
AmirOP7mo ago
$ npm list discord.js @discordjs/core @discordjs/rest && node -v [email protected] C:\Program Files\Git\projects\botstudio ├─┬ @discordjs/[email protected] │ ├── @discordjs/[email protected] deduped │ └─┬ @discordjs/[email protected] │ └── @discordjs/[email protected] deduped ├── @discordjs/[email protected] └─┬ [email protected] └── @discordjs/[email protected] deduped v22.1.0 Or maybe we can use another method to do this? Or just how to get the API method required for a route?
brncray
brncray7mo ago
i'm not experienced at all with the rest api but the most i see that you could do is
new API(new REST({ version: '10' }).setToken('xxxxxxxx'));
new API(new REST({ version: '10' }).setToken('xxxxxxxx'));
i don't see how you could do that withing the method
Amir
AmirOP7mo ago
Yes. But that's not the problem. I want to send API requests like this:
createdAPIclient[category_here][method_name_here](request_parameters_here);
createdAPIclient[category_here][method_name_here](request_parameters_here);
api.channels.createMessage({ channelId, 'xxxxxxxx', content: 'Hello World' });
api.channels.createMessage({ channelId, 'xxxxxxxx', content: 'Hello World' });
Currently I have to send them like this:
createdAPIclient[category_here][method_name_here](required_parameters_here, request_parameters_here);
createdAPIclient[category_here][method_name_here](required_parameters_here, request_parameters_here);
api.channels.createMessage('channelId', { content: 'Hello World' });
api.channels.createMessage('channelId', { content: 'Hello World' });
The difference is simple; in the first one, we can include all parameters in a single object. Thanks BTW
brncray
brncray7mo ago
docs don't take in another param maybe another function?
Amir
AmirOP7mo ago
Yes sure
brncray
brncray7mo ago
let me re-clarify build your own function that already has rest & api defined wait hmm
Amir
AmirOP7mo ago
I think this is too complicated; I'll try to find another way. Thanks BTW
Want results from more Discord servers?
Add your server