added intervals
This commit is contained in:
parent
7e1ad83e33
commit
466686db7a
|
@ -131,4 +131,4 @@ dist
|
|||
.pnp.*
|
||||
|
||||
# config
|
||||
config.json
|
||||
config/config.json
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 280 KiB After Width: | Height: | Size: 280 KiB |
77
src/index.js
77
src/index.js
|
@ -24,7 +24,7 @@ const client = new Client({
|
|||
intents: [GatewayIntentBits.Guilds]
|
||||
});
|
||||
|
||||
const { config } = JSON.parse(fs.readFileSync(path.resolve("config.json")));
|
||||
const config = JSON.parse(fs.readFileSync(path.resolve("config", "config.json")));
|
||||
|
||||
client.once(Events.ClientReady, readyClient => {
|
||||
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
|
||||
|
@ -61,6 +61,7 @@ client.on('ready', () => {
|
|||
|
||||
let radar_image = await get_rain_image();
|
||||
let radar_caption = await get_radar_caption();
|
||||
|
||||
await pipeline(
|
||||
radar_image[0].data,
|
||||
fs.createWriteStream(curr_image_path)
|
||||
|
@ -74,57 +75,33 @@ client.on('ready', () => {
|
|||
});
|
||||
console.log(`Sent: ${message.content}`);
|
||||
|
||||
/* setInterval(() => {
|
||||
setInterval(async () => {
|
||||
let curr_image_size = fs.statSync(curr_image_path).size;
|
||||
let fetched_image_size = await get_rain_image(curr_)
|
||||
}, 5000); */
|
||||
|
||||
let radar_image = await get_rain_image();
|
||||
let radar_caption = await get_radar_caption();
|
||||
|
||||
await pipeline(
|
||||
radar_image[0].data,
|
||||
fs.createWriteStream(curr_image_path)
|
||||
);
|
||||
console.log("File Saved");
|
||||
|
||||
let radar_image_size = radar_image[1];
|
||||
console.log("Current: " + curr_image_size);
|
||||
console.log("New: " + radar_image_size);
|
||||
|
||||
if(radar_image_size != curr_image_size) {
|
||||
let message = await channel.send({
|
||||
content: radar_caption,
|
||||
files: [curr_image_path]
|
||||
});
|
||||
console.log(`Sent: ${message.content}`);
|
||||
} else {
|
||||
console.log("No change in data, go on.");
|
||||
}
|
||||
}, 60000);
|
||||
})();
|
||||
});
|
||||
|
||||
/* client.on('ready', () => {
|
||||
console.log("Client: on ready");
|
||||
const curr_image_path = path.resolve("data", "current_radarh.jpg");
|
||||
|
||||
// continuously check for data change
|
||||
console.log("Checking for image changes");
|
||||
setInterval(() => {
|
||||
console.log("File Exists");
|
||||
axios.get('https://weather.bangkok.go.th/Images/Radar/radarh.jpg', {
|
||||
responseType: "stream"
|
||||
})
|
||||
.then(function(response) {
|
||||
let new_image_size = response.headers['content-length'];
|
||||
let curr_image_size = fs.statSync(curr_image_path).size;
|
||||
console.log("Current: " + curr_image_size);
|
||||
console.log("New: " + new_image_size);
|
||||
|
||||
if (new_image_size != curr_image_size) {
|
||||
console.log("A change in data!");
|
||||
|
||||
console.log("Saving File");
|
||||
response.data.pipe(fs.createWriteStream(curr_image_path));
|
||||
|
||||
console.log("Retrieving Caption");
|
||||
axios.get('https://weather.bangkok.go.th/Radar/RadarHighResolution.aspx')
|
||||
.then(function(response) {
|
||||
console.log("Got Caption!");
|
||||
let root = parse(response.data);
|
||||
let caption = root.querySelector('#repeaDaily_lblDESCRIPTION_0').childNodes[0]._rawText;
|
||||
|
||||
const channel = client.channels.cache.get('1165160812797771858');
|
||||
channel.send({
|
||||
content: caption,
|
||||
files: ["https://weather.bangkok.go.th/Images/Radar/radarh.jpg"]
|
||||
})
|
||||
.then(message => console.log(`Sent message: ${message.content}`))
|
||||
.catch(console.error);
|
||||
})
|
||||
} else {
|
||||
console.log("No change in data, go on");
|
||||
}
|
||||
});
|
||||
}, 60000);
|
||||
});
|
||||
}); */
|
||||
|
||||
client.login(config.token);
|
||||
|
|
Loading…
Reference in New Issue