counter-bot/index.js

95 lines
3.1 KiB
JavaScript
Raw Normal View History

2023-10-25 11:12:18 -04:00
import config from './config.json' assert {type: "json"};
2023-11-06 14:14:56 -05:00
import { MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin, RichRepliesPreprocessor } from "matrix-bot-sdk";
2023-10-25 21:17:32 -04:00
import fs from "fs";
2023-10-25 11:12:18 -04:00
const storage = new SimpleFsStorageProvider("storage.json");
const client = new MatrixClient(config.homeserver, config.token, storage);
2023-10-29 14:43:38 -04:00
let data;
2023-10-25 21:17:32 -04:00
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) {
2023-10-29 13:48:31 -04:00
leaderboardString += `${id}: ${count}<br>`;
2023-10-25 21:17:32 -04:00
if (i++ >= 9) break;
}
2023-10-25 22:22:17 -04:00
client.sendHtmlNotice(roomId, `<h1>Leaderboard</h1><p>${leaderboardString}</p>`);
2023-10-25 21:17:32 -04:00
};
const countCommand = async (roomId, message) => {
2023-10-29 14:44:15 -04:00
let count = data.count[message["sender"]]
if(isNaN(count)){
client.replyNotice(roomId, message, `You did not say linux.`);
} else if(count === 1){
client.replyNotice(roomId, message, `You said linux 1 time!`);
} else {
client.replyNotice(roomId, message, `You said linux ${count} times!`);
2023-10-29 14:44:15 -04:00
};
2023-10-25 21:17:32 -04:00
};
2023-10-29 16:26:47 -04:00
const addReaction = async (roomId, message) => {
client.sendEvent(roomId, "m.reaction", {
"m.relates_to": {
"event_id":message["event_id"],
"key":String(data.count[message["sender"]]),
"rel_type": "m.annotation"
}
})
}
2023-10-25 21:17:32 -04:00
const addCount = async (roomId, message) => {
2023-10-25 21:50:21 -04:00
if (!(message["sender"] in data.count)) {
2023-10-25 21:17:32 -04:00
data.count[message["sender"]] = 0;
}
if (++data.count[message["sender"]] % 10 === 0) {
2023-10-29 16:26:47 -04:00
addReaction(roomId, message);
2023-10-25 21:17:32 -04:00
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"];
}
};
2023-10-25 11:12:18 -04:00
AutojoinRoomsMixin.setupOnClient(client);
client.addPreprocessor(new RichRepliesPreprocessor(false));
2023-10-25 21:17:32 -04:00
client.start().then(() => {
clearTimeout(error1);
// console.log(`Logged into ${client.guilds.cache.size} rooms`); // ? this is possible i think idk how tho
data = readFromCache();
});
2023-10-25 11:12:18 -04:00
client.on("room.message", (roomId, event) => {
if (! event["content"] || event["sender"] === config.user) return;
2023-10-25 21:17:32 -04:00
if (event["content"]["body"].toLowerCase().startsWith(config.prefix + "lb")) {
lbCommand(roomId, event);
}
if (event["content"]["body"].toLowerCase().startsWith(config.prefix + "count")) {
countCommand(roomId, event);
}
2023-10-25 22:03:13 -04:00
if (event["content"]["body"].match(/l*.i.*n.*u.*x/gi)){
2023-10-25 21:17:32 -04:00
addCount(roomId, event);
2023-10-25 11:12:18 -04:00
}
})
2023-10-25 21:17:32 -04:00
setInterval(() => writeToCache(data), 60000);