Simplifying API Requests

Here's the code I'm using:
import { REST, Routes } from 'discord.js';

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

rest.request({
fullRoute: Routes.channelMessages('xxxxxxxxxx'),
method: 'POST',
body: {
content: 'test'
}
});
import { REST, Routes } from 'discord.js';

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

rest.request({
fullRoute: Routes.channelMessages('xxxxxxxxxx'),
method: 'POST',
body: {
content: 'test'
}
});
It works well, but I have one question: Is there a way to send requests without specifying the method? I'd like to just send requests to the endpoint without specifying the method each time.
6 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 && node -v [email protected] C:\Program Files\Git\projects\botstudio └── [email protected] v22.1.0 I just want to see if it's possible to not specify the request method each time when sending requests. Perhaps consider leveraging one of Discord.js's built-in methods to determine the required request method for each endpoint.
Syjalo
Syjalo7mo ago
If you're using discord.js, use tools it provides
await client.channels.get('id').send(options);
await client.channels.get('id').send(options);
Else you can use @discordjs/core
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', options);
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', options);
Amir
AmirOP7mo ago
Thanks! That's exactly what I meant, but can I use it without installing another package? Just using 'discord.js'
Syjalo
Syjalo7mo ago
Currently discord.js doesn't include /core
Amir
AmirOP7mo ago
Thanks; I've got my answers.
Want results from more Discord servers?
Add your server