Reputation: 45
I'm trying to make a Discord bot that will automatically post a message when a new chapter of a manga is posted. I have never done JavaScript before, and I have no idea what code one would use to do that. The URL ends in "/en/0/[Chapter Number]", and nothing else in the entire URL changes, so all the bot would have to do is check one number above the one posted, then when that link doesn't lead to a 404 anymore it posts the message on Discord and updates the chapter number it's checking. Any help you can give is appreciated.
EDIT:
I am using Node.js and Visual Studio Code, if that matters
Upvotes: 1
Views: 5394
Reputation: 51
If you are looking for a fast way in most situations, and you agree 'unexist ≈ unexist or not public or HEAD request disallowed', try this.
fetch(url, {method: 'HEAD'})
.then(res => {
if (res.status === 200) {
... // exist
return;
}
... // unexist
})
.catch(err => {
... // unexist
})
Upvotes: 3
Reputation: 468
You can use Node's https built-in module to determine the status code of the page. If the status code is 200
, you would know the page exists. You could also check for other status codes (404
, like you mentioned) that you see fit.
const https = require('https')
https.get('https://discordapp.com/', res => {
if(res.statusCode === 200) {
console.log('Page exists!')
}
})
Upvotes: 2
Reputation: 772
You probably want to make a request to the url in order to check the response code. If you are working on a discord bot, you are probably using node, in that case you want to pick a request library and look up the documentation for using it:
Here are some of them:
Upvotes: 0