media-bot/index.js

31 lines
1.3 KiB
JavaScript

// ? import bot sdk and configuration
import config from './config.json' assert {type: "json"};
import { MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin } from "matrix-bot-sdk";
// ? save state of bot between restarts
const storage = new SimpleFsStorageProvider("storage.json");
// ? create a new matrix client
const client = new MatrixClient(config.baseUrl, config.token, storage);
// ? auto join rooms invited to
AutojoinRoomsMixin.setupOnClient(client);
// ? start the client
client.start().then(() => console.log(`Client has started!`));
// ? event listener for when a message is sent
client.on("room.message", (roomId, event) => {
// ? check if the message's text is not empty and isn't sent by the bot itself
if (! event["content"] || event["sender"] === config.userId) return;
// ? if message starts with the bot's <prefix + command> then execute the command
if (event["content"]["body"].toLowerCase().startsWith(config.prefix + "show")){
// ? send image from the command's first argument, i.e. create mxc url from the argument
client.sendMessage(roomId, {
"body": "Image from Matrix",
"msgtype": "m.image",
"url": `${config.baseUrl.replace("https://matrix.", "mxc://")}/${event["content"]["body"].split(" ")[1]}`
});
}
})