arch-bot/index.js
2023-06-08 00:49:49 -04:00

125 lines
4.8 KiB
JavaScript

import config from './config.json' assert {type: "json"};
import { MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin } from "matrix-bot-sdk";
const storage = new SimpleFsStorageProvider("storage.json");
const client = new MatrixClient(config.homeserver, config.token, storage);
AutojoinRoomsMixin.setupOnClient(client);
client.start().then(() => console.log(`Client has started!`));
client.on("room.message", (room, event) => {
if (!event["content"] || event["sender"] === config.user) return;
let messageArray = event["content"]["body"].split(" ");
messageArray = messageArray.filter((str) => str !== '') //? remove empty strings
if (messageArray[0] == config.prefix) {
switch (messageArray[1]) {
case 's':
case '-Si':
case 'pkg':
case 'search':
let resultIndex = 0;
let pkgPromise = searchPkgs(messageArray);
//? last item in the command must the be the result index
if (Number.isInteger(parseInt(messageArray[messageArray.length - 1]))) {
resultIndex = parseInt(messageArray[messageArray.length - 1])
};
//? pkgResults is an array containing all search results
pkgPromise.then(pkgResults => {
if (pkgResults) {
// let messageHTML = `${pkgResults[resultIndex].pkgname} ${pkgResults[resultIndex].repo} ${pkgResults[resultIndex].licenses} ${pkgResults[resultIndex].pkgdesc}`
let messageHTML = `<strong>Package Name</strong>: ${pkgResults[resultIndex].pkgname} <br>
<strong>Repository</strong>: ${pkgResults[resultIndex].repo} <br>
<strong>Architecture</strong>: ${pkgResults[resultIndex].arch} <br>
<strong>Version</strong>: ${pkgResults[resultIndex].pkgver} <br>
<strong>Description</strong>: ${pkgResults[resultIndex].pkgdesc} <br>
<strong>URL</strong>: ${pkgResults[resultIndex].url} <br>
<strong>Build Date</strong>: ${pkgResults[resultIndex].build_date} <br>
<strong>Last Update</strong>: ${pkgResults[resultIndex].last_update} <br>
<strong>Flagged out of Date</strong>: ${pkgResults[resultIndex].flag_date} <br>
<strong>Maintainer(s)</strong>: ${pkgResults[resultIndex].maintainers} <br>
<strong>Packager</strong>: ${pkgResults[resultIndex].packager} <br>
<strong>License(s)</strong>: ${pkgResults[resultIndex].licenses}`
client.sendHtmlNotice(room, messageHTML);
} else {
let messageHTML = `<strong>No package found.</strong>`
client.sendHtmlNotice(room, messageHTML);
}
}); //TODO: bot crashes if index is too large
break;
case 'aur': //TODO: search aur
break;
case 'h':
case 'help': //TODO: help menu
break;
default:
let messageHTML = `<strong>Invalid command.</strong>`
client.sendHtmlNotice(room, messageHTML);
}
}
})
/* search parameter values:
q,
name,
desc,
repo (Core, Core-Testing, Extra, Extra-Testing, Multilib, Multilib-Testing),
arch (any, x86_64),
maintainer,
packager,
flagged (Flagged, Not+Flagged)
*/
async function searchPkgs(messageArray) {
// console.log(messageArray)
let isFilterApplied = false;
let skipElement = false;
let searchURLPath = "https://archlinux.org/packages/search/json/?"
const filterValues = ['q', 'name', 'desc', 'repo', 'arch', 'maintainer', 'packager', 'flagged'];
// ? using 'for' instead of 'foreach' to be able to use 'break' and 'continue'
for (const element of messageArray) {
// ? if filter item is being searched with it's own filter, skip the next iteration; treating it as a search parameter and not a filter
// ? !a s q q repo Core
// ? ^ ^ ^ ^
// ? 1 2 1 2
// ? 1: filter, 2: search parameter (skipped)
if (skipElement) {
skipElement = false;
continue;
};
let nextElement = messageArray[messageArray.indexOf(element) + 1];
for (const filter of filterValues) {
if (element == filter) {
isFilterApplied = true;
if (!nextElement) break;
searchURLPath += '&' + filter + '=' + nextElement;
skipElement = true;
}
};
};
if (!isFilterApplied) {
searchURLPath += '&q=' + messageArray[2]
}
console.log(searchURLPath);
let response = await fetch(searchURLPath);
const dataJSON = await response.json();
if (!dataJSON.results.length) {
return null //? package does not exist
} else {
return dataJSON.results
}
}