I'm trying to match the "83eec6e44ea04dee9103e845ad51c4f0" from this json, but I'm getting an error (shown below). I got the json from a httpget request btw. From reading the error myself it looks like it has something to do with the httpget but I'm getting the data from the request just fine.
username = message.content;
var request = require('request');
request(`https://api.mojang.com/users/profiles/minecraft/${username}`, function(error, response, body) {
if (!error && response.statusCode == 200) {
// body is this: body = {"name":"uhWillem","id":"83eec6e44ea04dee9103e845ad51c4f0"};
console.log(body)
message.channel.send(body);
regex = "(?<=id\":\")(.*)(?=\")";
match = body.match(regex);
message.channel.send(match)
}
})
Error:
C:\Users\wille\OneDrive\Bureaublad\discordBot\node_modules\discord.js\src\rest\RequestHandler.js:298
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Cannot send an empty message
at RequestHandler.execute (C:\Users\wille\OneDrive\Bureaublad\discordBot\node_modules\discord.js\src\rest\RequestHandler.js:298:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (C:\Users\wille\OneDrive\Bureaublad\discordBot\node_modules\discord.js\src\rest\RequestHandler.js:50:14)
at async TextChannel.send (C:\Users\wille\OneDrive\Bureaublad\discordBot\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:172:15) {
method: 'post',
path: '/channels/883457777035534417/messages',
code: 50006,
httpStatus: 400,
requestData: {
json: {
content: undefined,
tts: false,
nonce: undefined,
embeds: undefined,
components: undefined,
username: undefined,
avatar_url: undefined,
allowed_mentions: undefined,
flags: undefined,
message_reference: undefined,
attachments: undefined,
sticker_ids: undefined
},
files: []
}
}
The body contains JSON, and if you want to extract the id, just use
const match = JSON.parse(body).id
I suggest you to parse your JSON. It is simpler and probably faster for more complex JSON.
if (!error && response.statusCode == 200) {
let { id } = JSON.parse(body);
message.channel.send(id);
}
If you need to use regexp, you need to know that .match(regex) returns an array, therefor you should only choose the first element:
matches = body.match(regex); // Don't name it match... It's an array :)
message.channel.send(matches[0]);
But a much more performant code can be used! You can stringify the text before the id key (we know the name key == username) and .substring():
if (!error && response.statusCode == 200) {
let pre = `{"name":"${username}","id":"`;
// id is 32 characters long (16 bytes)
let id = body.substring(pre.length, pre.length+32);
message.channel.send(id);
}
If you want to use the id outside the request callback, you need to wrap everything in a promise and a dedicated function:
const request = require('request');
function get_username_id(username) {
return new Promise((resolve, reject) => request(
`https://api.mojang.com/users/profiles/minecraft/${username}`,
function(error, response, body) {
if (error || response.statusCode != 200) reject(error || response.statusCode)
// body is {"name":"uhWillem","id":"83eec6e44ea04dee9103e845ad51c4f0"};
resolve(JSON.response(body));
});
}
let username = message.content;
get_username_id(username)
.then(({name, id}) => {
console.log(`The id of ${name} is ${id}`); // here we should see the name == username and the id
// here we work with the data, eg we send it
message.channel.send(id);
// you can return a promise that resolves to something else and chain it with an other .then()
return Promise.resolve('This will be recived after the first promise');
})
.then(text => {
console.log(text);
// id and name are not accessible anymore, but you can pass them on...
})
.catch(err => console.error(err)); // here we print the error or status code different from 200
This is quite longer and more complex but allows being more flexible in the development for the future.
I suggest you take a look around, SO to find more information about promises.