generated from array-in-a-matrix/matrix-bot-template
search filtering and html output
This commit is contained in:
parent
7bd1261f35
commit
a9928e1895
1 changed files with 78 additions and 10 deletions
88
index.js
88
index.js
|
@ -10,25 +10,46 @@ 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(" ").map(word => word.toLowerCase());
|
||||
let messageArray = event["content"]["body"].split(" ");
|
||||
messageArray = messageArray.filter((str) => str !== '') //? remove empty strings
|
||||
|
||||
if (messageArray[0].toLowerCase() == config.prefix) {
|
||||
if (messageArray[0] == config.prefix) {
|
||||
switch (messageArray[1]) {
|
||||
case 's':
|
||||
case '-Si':
|
||||
case 'pkg':
|
||||
case 'search':
|
||||
|
||||
let resultIndex = 0;
|
||||
let pkgPromise = search_pkg(messageArray[2]);
|
||||
let pkgPromise = searchPkgs(messageArray);
|
||||
|
||||
if (Number.isInteger(parseInt(messageArray[3]))) {
|
||||
resultIndex = parseInt(messageArray[3])
|
||||
//? 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 => {
|
||||
let message = `${pkgResults[resultIndex].pkgname} ${pkgResults[resultIndex].repo} ${pkgResults[resultIndex].licenses} ${pkgResults[resultIndex].pkgdesc}` //? pkgResults is an array containing all search results
|
||||
client.sendNotice(room, message);
|
||||
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;
|
||||
|
||||
|
@ -38,14 +59,61 @@ client.on("room.message", (room, event) => {
|
|||
case 'help': //TODO: help menu
|
||||
break;
|
||||
default:
|
||||
client.sendNotice(room, 'Invalid command.');
|
||||
let messageHTML = `<strong>Invalid command.</strong>`
|
||||
client.sendHtmlNotice(room, messageHTML);
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
async function search_pkg(pkg) {
|
||||
/* 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) {
|
||||
|
||||
const response = await fetch("https://archlinux.org/packages/search/json/?q=" + pkg);
|
||||
// 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) {
|
||||
|
|
Loading…
Add table
Reference in a new issue