2023-02-22 17:30:56 -05:00
|
|
|
import {
|
|
|
|
MatrixClient,
|
|
|
|
SimpleFsStorageProvider,
|
|
|
|
AutojoinRoomsMixin,
|
|
|
|
RustSdkCryptoStorageProvider
|
|
|
|
} from "matrix-bot-sdk";
|
|
|
|
|
|
|
|
//import config data
|
2023-02-22 18:00:25 -05:00
|
|
|
import settings from "./config/config.Dev.json";
|
2023-02-22 17:30:56 -05:00
|
|
|
|
2023-02-22 18:00:25 -05:00
|
|
|
const homeserverUrl = settings.matrixAddress;
|
|
|
|
const accessToken = settings.accessToken;
|
2023-02-22 17:30:56 -05:00
|
|
|
|
|
|
|
const storage = new SimpleFsStorageProvider("./config/bot.json");
|
|
|
|
const cryptoProvider = new RustSdkCryptoStorageProvider("./config/crypto");
|
|
|
|
|
|
|
|
const client = new MatrixClient(homeserverUrl, accessToken, storage, cryptoProvider);
|
|
|
|
AutojoinRoomsMixin.setupOnClient(client);
|
|
|
|
|
|
|
|
|
|
|
|
client.on("room.message", handleCommand);
|
|
|
|
|
|
|
|
client.start().then(() => console.log("Now listening for TikTok URLs..."));
|
|
|
|
|
|
|
|
async function handleCommand(roomId: string, event: any) {
|
|
|
|
// Don't handle unhelpful events (ones that aren't text messages, are redacted, or sent by us)
|
|
|
|
if (event['content']?.['msgtype'] !== 'm.text') return;
|
|
|
|
if (event['sender'] === await client.getUserId()) return;
|
|
|
|
|
|
|
|
// Check to ensure that the `!hello` command is being run
|
|
|
|
const body = event['content']['body'];
|
|
|
|
if (!body?.startsWith("!hello")) return;
|
|
|
|
|
|
|
|
// Now that we've passed all the checks, we can actually act upon the command
|
|
|
|
await client.replyNotice(roomId, event, "Hello world!");
|
|
|
|
}
|