From d7aa3024d6ea5f943629f64384d20378823acefd Mon Sep 17 00:00:00 2001 From: Sebastian Korotkiewicz Date: Sat, 26 Mar 2022 21:58:59 +0100 Subject: [PATCH] save user city --- .gitignore | 1 + components/Weather.js | 124 ++++++++++++++++++++++++++++++++++-------- components/db.js | 14 +++++ db/.gitkeep | 0 index.js | 5 +- 5 files changed, 119 insertions(+), 25 deletions(-) create mode 100644 components/db.js create mode 100644 db/.gitkeep diff --git a/.gitignore b/.gitignore index 3c3629e..65c8174 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +db/users.db \ No newline at end of file diff --git a/components/Weather.js b/components/Weather.js index 473be76..f25b700 100644 --- a/components/Weather.js +++ b/components/Weather.js @@ -1,34 +1,110 @@ import axios from "axios"; +import sqlite3 from "sqlite3"; import Color from "./Color.js"; const Weather = (event) => { - let reqCity = event.message.match(/,w ([A-Za-z0-9 ]+)/); - // let city = event.message.split(" "); - let city = encodeURIComponent(reqCity[1].trim()); - // check if user posted a city TODO + const db = new sqlite3.Database("./db/users.db"); - axios({ - method: "get", - url: `https://wttr.in/${city}?format=j1`, - }).then((r) => { - let weatherDesc = r.data.current_condition[0].weatherDesc[0].value; - let humidity = r.data.current_condition[0].humidity; - let tempC = r.data.current_condition[0].temp_C; - let tempF = r.data.current_condition[0].temp_F; - let windspeedKmph = r.data.current_condition[0].windspeedKmph; - let windspeedMiles = r.data.current_condition[0].windspeedMiles; - let areaName = r.data.nearest_area[0].areaName[0].value; - let country = r.data.nearest_area[0].country[0].value; - let region = r.data.nearest_area[0].region[0].value; + let nick = event.nick; + let msg = event.message; + let cmd = msg.match(/^,w (\w+)(.+?)?$/); - let output = `${Color("Weather")} (${ - event.nick - }) ${areaName}, ${region}, ${country} | ${tempC}C/${tempF}F/${ - Number(tempC) + 273.15 - }K | ${weatherDesc} | Humidity: ${humidity}% | Wind: ${windspeedKmph}km/h/${windspeedMiles}mi/h`; + const setUserCity = (nick, city) => { + let time = new Date().getTime(); + db.serialize(() => { + // - event.reply(output); - }); + let stmt = db.prepare("SELECT city FROM users WHERE nick = (?);"); + stmt.each( + [nick], + (err, row) => { + // console.log(err, row); + }, + (err, count) => { + if (count === 0) { + // add new user with city if not found + + let stmt = db.prepare( + "INSERT INTO users (nick, city, createdAt) VALUES (?,?,?);" + ); + stmt.run([nick, city, time]); + stmt.finalize(); + } else { + // update user city + + let stmt = db.prepare( + "UPDATE users SET city = (?) WHERE nick = (?);" + ); + stmt.run([city, nick]); + stmt.finalize(); + } + event.reply( + 'Your city is now set to "' + city + '". Call ,w to see weather.' + ); + + stmt.finalize(); + } + ); + + // + }); + }; + + const getWeather = (city) => { + axios({ + method: "get", + url: `https://wttr.in/${encodeURIComponent(city)}?format=j1`, + }).then((r) => { + let weatherDesc = r.data.current_condition[0].weatherDesc[0].value; + let humidity = r.data.current_condition[0].humidity; + let tempC = r.data.current_condition[0].temp_C; + let tempF = r.data.current_condition[0].temp_F; + let windspeedKmph = r.data.current_condition[0].windspeedKmph; + let windspeedMiles = r.data.current_condition[0].windspeedMiles; + let areaName = r.data.nearest_area[0].areaName[0].value; + let country = r.data.nearest_area[0].country[0].value; + let region = r.data.nearest_area[0].region[0].value; + + let output = `${Color("Weather")} (${ + event.nick + }) ${areaName}, ${region}, ${country} | ${tempC}C/${tempF}F/${ + Number(tempC) + 273.15 + }K | ${weatherDesc} | Humidity: ${humidity}% | Wind: ${windspeedKmph}km/h/${windspeedMiles}mi/h`; + + event.reply(output); + }); + }; + + if (cmd === null) { + // ,w + db.serialize(() => { + let stmt = db.prepare("SELECT city FROM users WHERE nick = (?);"); + stmt.each( + [nick], + (err, row) => { + // get user city from db + getWeather(row.city); + }, + (err, count) => { + if (count === 0) { + // user not found + event.reply("usage: ,w [location] OR ,w set [location]"); + } + } + ); + stmt.finalize(); + }); + } else if (cmd[1].trim().toLowerCase() === "set") { + // ,w set neustadt an der donau + + setUserCity(nick, cmd[2].trim()); + } else if (cmd[1].trim()) { + // ,w neustadt an der donau + + let cityMore = cmd[2] ? " " + cmd[2].trim() : ""; + let city = cmd[1].trim() + cityMore; + getWeather(city); + } }; export default Weather; diff --git a/components/db.js b/components/db.js new file mode 100644 index 0000000..740dc6a --- /dev/null +++ b/components/db.js @@ -0,0 +1,14 @@ +import sqlite3 from "sqlite3"; + +const InitDB = () => { + const db = new sqlite3.Database("./db/users.db"); + db.run(`CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + nick TEXT, + city TEXT, + createdAt TEXT + )`); + db.close(); +}; + +export default InitDB; diff --git a/db/.gitkeep b/db/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/index.js b/index.js index 3dad8f6..c018a45 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,9 @@ import { Client } from "irc-framework"; import Weather from "./components/Weather.js"; import UrlTitle from "./components/UrlTitle.js"; +import InitDB from "./components/db.js"; + +InitDB(); function middleware() { return function (client, raw_events, parsed_events) { @@ -35,7 +38,7 @@ bot.on("close", () => { console.log("Connection close"); }); -bot.matchMessage(/^,w /, async (event) => { +bot.matchMessage(/^,w/, async (event) => { return Weather(event); });