import config from './config.json' assert {type: "json"}; import { MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin, RichRepliesPreprocessor } 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}
`; if (i++ >= 9) break; } client.sendHtmlNotice(roomId, `

Leaderboard

${leaderboardString}

`); }; const countCommand = async (roomId, message) => { 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!`); }; }; 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" } }) } const addCount = async (roomId, message) => { if (!(message["sender"] in data.count)) { data.count[message["sender"]] = 0; } if (++data.count[message["sender"]] % 10 === 0) { addReaction(roomId, message); 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.addPreprocessor(new RichRepliesPreprocessor(false)); 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); } }) setInterval(() => writeToCache(data), 60000);