forked from array-in-a-matrix/counter-bot
88 lines
2.8 KiB
JavaScript
88 lines
2.8 KiB
JavaScript
import config from './config.json' assert {type: "json"};
|
|
import { MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin } from "matrix-bot-sdk";
|
|
import fs from "fs";
|
|
|
|
const storage = new SimpleFsStorageProvider("storage.json");
|
|
const client = new MatrixClient(config.homeserver, config.token, storage);
|
|
|
|
let data;
|
|
|
|
const error1 = setTimeout(function () {
|
|
console.log("\x1b[41m", "ERROR: UNABLE TO CONNECT TO MATRIX SERVER");
|
|
}, 10000);
|
|
|
|
const writeToCache = (data) => {
|
|
fs.writeFileSync("count.json", JSON.stringify(data));
|
|
};
|
|
|
|
const readFromCache = () => JSON.parse(fs.readFileSync('count.json').toString());
|
|
|
|
const lbCommand = async (roomId, message) => {
|
|
const unsortedTable = new Map();
|
|
for (const row in data.count) {
|
|
unsortedTable.set(row, data.count[row]);
|
|
}
|
|
let leaderboardString = "";
|
|
const sortedTable = new Map([...unsortedTable.entries()].sort((a, b) => b[1] - a[1]));
|
|
let i = 0;
|
|
for (let [id, count] of sortedTable) {
|
|
leaderboardString += `${id}: ${count}<br>`;
|
|
if (i++ >= 9) break;
|
|
}
|
|
client.sendHtmlNotice(roomId, `<h1>Leaderboard</h1><p>${leaderboardString}</p>`);
|
|
};
|
|
|
|
const countCommand = async (roomId, message) => {
|
|
|
|
let count = data.count[message["sender"]]
|
|
|
|
//prevent undefined/NaN
|
|
if (isNaN(count)) count = "0"
|
|
|
|
//send response
|
|
client.replyNotice(roomId, message, `You said linux ${count} times!`)
|
|
|
|
};
|
|
|
|
const addCount = async (roomId, message) => {
|
|
if (!(message["sender"] in data.count)) {
|
|
data.count[message["sender"]] = 0;
|
|
}
|
|
if (++data.count[message["sender"]] % 10 === 0) {
|
|
client.sendNotice(roomId, `${message["sender"]} said linux ${data.count[message["sender"]]} times!`);
|
|
}
|
|
if (data.count[message["sender"]] > data.max.count) {
|
|
data.max.count++;
|
|
data.max.username = message["sender"];
|
|
}
|
|
};
|
|
|
|
AutojoinRoomsMixin.setupOnClient(client);
|
|
client.start().then(() => {
|
|
clearTimeout(error1);
|
|
// console.log(`Logged into ${client.guilds.cache.size} rooms`); // ? this is possible i think idk how tho
|
|
data = readFromCache();
|
|
});
|
|
|
|
client.on("room.message", (roomId, event) => {
|
|
if (! event["content"] || event["sender"] === config.user) return;
|
|
|
|
if (event["content"]["body"].toLowerCase().startsWith(config.prefix + "lb")) {
|
|
lbCommand(roomId, event);
|
|
}
|
|
if (event["content"]["body"].toLowerCase().startsWith(config.prefix + "count")) {
|
|
countCommand(roomId, event);
|
|
}
|
|
if (event["content"]["body"].match(/l*.i.*n.*u.*x/gi)){
|
|
addCount(roomId, event);
|
|
client.sendEvent(roomId, "m.reaction" ({
|
|
"m.relates_to": {
|
|
"event_id":event["event_id"],
|
|
"key":data.count[message["sender"]],
|
|
"rel_type": "m.annotation"
|
|
}
|
|
}))
|
|
}
|
|
})
|
|
|
|
setInterval(() => writeToCache(data), 60000); |