Automatically set the bot's profile info

This commit is contained in:
2023-06-23 23:38:51 +02:00
parent a0887106d6
commit fede589b0c
3 changed files with 24 additions and 2 deletions

View File

@ -12,4 +12,7 @@ MATRIX_USER=
# You can get the access token by for example logging into
# the bot's matrix account in element and copying the
# Access Token under Settings > Help & About > Advanced
MATRIX_TOKEN=
MATRIX_TOKEN=
# The bot set this as its display name (Default: Timetable V2)
MATRIX_DISPLAYNAME=

BIN
images/profile-picture.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

View File

@ -1,6 +1,7 @@
import * as sdk from "matrix-js-sdk";
import fetch from "node-fetch";
import process from "process";
import fs from "fs";
import "dotenv/config";
import { JsonDB, Config } from "node-json-db";
import { TimetableClient } from "./timetable.js";
@ -22,6 +23,24 @@ const client = sdk.createClient({
fetchFn: fetch,
});
const displayName = process.env.MATRIX_DISPLAYNAME || "Timetable V2";
(async () => {
const profileInfo = await client.getProfileInfo(userId);
if (profileInfo.displayname != displayName) {
await client.setProfileInfo("displayname", {
displayname: displayName,
});
const file = fs.readFileSync("./images/profile-picture.png");
const content = await client.uploadContent(file, {
name: "profile-picture.png",
type: "image/png",
});
await client.setProfileInfo("avatar_url", {
avatar_url: content.content_uri,
});
}
})();
client.once("sync", function (state) {
if (state === "PREPARED") {
console.log("sync finished");
@ -105,7 +124,7 @@ setInterval(async () => {
client.sendHtmlMessage(roomId, plainText(message), message);
}
}
}, 1000 * 60);
}, 1000);
// Handle events
client.on("Room.timeline", async function (event, room) {