Initial commit

This commit is contained in:
Sebastian Korotkiewicz 2022-03-26 16:52:12 +01:00
commit 53c0532e15
Signed by: grizzly
GPG Key ID: 5BDC557B496BDB0D
7 changed files with 1451 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

5
components/Color.js Normal file
View File

@ -0,0 +1,5 @@
const Color = (name) => {
return `[${"\u0003" + "68" + name + "\u0003"}]`;
};
export default Color;

29
components/UrlTitle.js Normal file
View File

@ -0,0 +1,29 @@
import axios from "axios";
import Color from "./Color.js";
const getTitle = async (url) => {
const r = await axios({ method: "get", url });
const doc = r.data;
return doc.split("<title>")[1].split("</title>")[0];
};
const UrlTitle = (event) => {
const geturl = new RegExp(
"(^|[ \t\r\n])((ftp|http|https|gopher|gemini|mailto|news|nntp|telnet|wais|file|prospero|aim|webcal):(([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]))",
"g"
);
let msg = event.message;
if (msg && msg.match(geturl)) {
let urls = msg.match(geturl);
urls.map(async (url) => {
let title = await getTitle(url.trim());
return event.reply(Color("Title") + " " + title);
});
} else {
return false;
}
};
export default UrlTitle;

37
components/Weather.js Normal file
View File

@ -0,0 +1,37 @@
import axios from "axios";
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
return new Promise((resolve, reject) => {
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 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`;
resolve(output);
});
});
};
export default Weather;

48
index.js Normal file
View File

@ -0,0 +1,48 @@
import { Client } from "irc-framework";
import Weather from "./components/Weather.js";
import UrlTitle from "./components/UrlTitle.js";
function middleware() {
return function (client, raw_events, parsed_events) {
parsed_events.use(title);
};
function title(command, event, client, next) {
if (command === "message") {
UrlTitle(event);
}
next();
}
}
const bot = new Client();
bot.use(middleware());
bot.connect({
host: "irc.libera.chat",
// host: "localhost",
nick: "g-bot",
port: 6667,
version: "grizzlys bot",
username: "g-bot",
gecos: "g-bot",
});
bot.on("registered", () => {
console.log("Connected!");
bot.join("#grizzly");
});
bot.on("close", () => {
console.log("Connection close");
});
bot.matchMessage(/^,w /, async (event) => {
const data = await Weather(event);
event.reply(data);
});
bot.matchMessage(/^,help/, (event) => {
event.reply(",help - show this message");
event.reply(",w [location] - show weather for you city");
event.reply(",w set [location] - save your location");
event.reply(",w - after your save your location you can use ,w");
});

12
package.json Normal file
View File

@ -0,0 +1,12 @@
{
"name": "irc_bot",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"axios": "^0.26.1",
"irc-framework": "^4.12.1",
"sqlite3": "^5.0.2"
},
"type": "module"
}

1319
yarn.lock Normal file

File diff suppressed because it is too large Load Diff