generated from array-in-a-matrix/matrix-bot-template
49 lines
1.6 KiB
JavaScript
49 lines
1.6 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;
|
|
|
|
const messageArray = event["content"]["body"].split(" ").map(word => word.toLowerCase());
|
|
//console.log(messageArray)
|
|
|
|
if (messageArray[0].toLowerCase() == config.prefix){
|
|
switch(messageArray[1]){
|
|
case 's':
|
|
case 'pkg':
|
|
case 'search':
|
|
search_pkg(room, messageArray[2]);
|
|
break;
|
|
case 'aur':
|
|
break;
|
|
default:
|
|
console.warn('no matches');
|
|
}
|
|
}
|
|
})
|
|
|
|
async function search_pkg(room, pkg){
|
|
|
|
const response = await fetch("https://archlinux.org/packages/search/json/?q=" + pkg);
|
|
const dataJSON = await response.json();
|
|
|
|
if (!dataJSON.results.length){
|
|
const message = 'This package does not exist.';
|
|
console.log(message)
|
|
client.sendText(room, message);
|
|
} else {
|
|
const message =
|
|
`Package Name: ${dataJSON.results[0].pkgname}
|
|
Package Repo: ${dataJSON.results[0].repo}
|
|
Package Architecture: ${dataJSON.results[0].arch};`
|
|
|
|
console.log(message)
|
|
client.sendText(room, message);
|
|
}
|
|
}
|