import { Client, Events, GatewayIntentBits } from 'discord.js'; import fs from 'fs'; import { createWriteStream, existsSync } from 'fs'; import { parse } from 'node-html-parser'; import { pipeline } from 'node:stream/promises'; import path from 'node:path'; import axios from 'axios'; const client = new Client({ intents: [GatewayIntentBits.Guilds] }); const { config } = JSON.parse(fs.readFileSync(path.resolve("config.json"))); client.once(Events.ClientReady, readyClient => { console.log(`Ready! Logged in as ${readyClient.user.tag}`); }); async function get_rain_image() { try { const radar_image = await axios.get('https://weather.bangkok.go.th/Images/Radar/radarh.jpg', { responseType: "stream" }); const radar_image_size = radar_image.headers['content-length']; console.log("Got Radar Data"); return [radar_image, radar_image_size]; } catch (error) { console.error(error); return [error]; } } async function get_radar_caption() { const response = await axios.get('https://weather.bangkok.go.th/Radar/RadarHighResolution.aspx'); let root = parse(response.data); console.log("Got Radar Caption") return root.querySelector('#repeaDaily_lblDESCRIPTION_0').childNodes[0]._rawText; } const curr_image_path = path.resolve("data", "current_radar.jpg"); client.on('ready', () => { (async () => { const channel = client.channels.cache.get(config.room); 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"); console.log("Updating on boot"); let message = await channel.send({ content: radar_caption, files: [curr_image_path] }); console.log(`Sent: ${message.content}`); /* setInterval(() => { let curr_image_size = fs.statSync(curr_image_path).size; let fetched_image_size = await get_rain_image(curr_) }, 5000); */ })(); }); /* 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);