huge inital commit
This commit is contained in:
192
src/index.js
Normal file
192
src/index.js
Normal file
@@ -0,0 +1,192 @@
|
||||
import {
|
||||
EncryptionAlgorithm,
|
||||
MatrixClient,
|
||||
MessageEvent,
|
||||
RustSdkCryptoStorageProvider,
|
||||
SimpleFsStorageProvider,
|
||||
} from "matrix-bot-sdk";
|
||||
|
||||
import { StoreType } from "@matrix-org/matrix-sdk-crypto-nodejs";
|
||||
|
||||
import fs from 'fs';
|
||||
|
||||
import {
|
||||
logError,
|
||||
logInfo,
|
||||
readConfig,
|
||||
readMessages
|
||||
} from './utils.js';
|
||||
|
||||
import {
|
||||
getClient
|
||||
} from './initDb.js'
|
||||
|
||||
const db = await getClient()
|
||||
|
||||
const config = readConfig()
|
||||
const messages = readMessages()
|
||||
|
||||
const homeserverUrl = config.homeserverURL;
|
||||
const accessToken = config.token;
|
||||
|
||||
const crypto = new RustSdkCryptoStorageProvider("./bot_data/encryption_bot_sled", StoreType.Sled);
|
||||
const storage = new SimpleFsStorageProvider("./bot_data/bot.json");
|
||||
|
||||
const client = new MatrixClient(homeserverUrl, accessToken, storage, crypto);
|
||||
|
||||
// client.on("room.message", async (roomId, event) => {
|
||||
// let current_action = (await db.query("SELECT current_action FROM users WHERE room_id = $1", [roomId])).rows[0];
|
||||
|
||||
// console.log(state)
|
||||
|
||||
// if (body?.startsWith("!hello")) {
|
||||
// await client.sendText(roomId, messages.welcome);
|
||||
// await client.sendText(roomId, messages.setup.country);
|
||||
|
||||
|
||||
// }
|
||||
|
||||
// });
|
||||
client.on("room.message", async (roomId, event) => {
|
||||
try {
|
||||
if (event.content?.msgtype !== 'm.text' || event.content?.msgtype !== 'm.image') return;
|
||||
if (event.sender === await client.getUserId()) return;
|
||||
|
||||
let current_action = (await db.query('SELECT current_action FROM users WHERE room_id = $1', [roomId])).rows[0];
|
||||
let answer = event.content.body;
|
||||
console.log(answer)
|
||||
switch (current_action) {
|
||||
case "country":
|
||||
if (answer.length > 64) {
|
||||
await client.sendText(roomId, messages.errors.toobig);
|
||||
return;
|
||||
}
|
||||
await db.query("UPDATE users SET country = $1 WHERE room_id = $2", [answer, roomId]);
|
||||
await client.sendText(roomId, `Set your country setting to "${answer}".`);
|
||||
await client.sendText(roomId, messages.setup.city); //next question
|
||||
await db.query("UPDATE users SET current_action = 'city' WHERE room_id = $1", [roomId]);
|
||||
break;
|
||||
case "city":
|
||||
if (answer.length > 64) {
|
||||
await client.sendText(roomId, messages.errors.toobig);
|
||||
return;
|
||||
}
|
||||
await db.query("UPDATE users SET city = $1 WHERE room_id = $2", [answer, roomId]);
|
||||
await client.sendText(roomId, `Set your city setting to "${answer}".`);
|
||||
await client.sendText(roomId, messages.setup.name); //next question
|
||||
await db.query("UPDATE users SET current_action = 'name' WHERE room_id = $1", [roomId]);
|
||||
break;
|
||||
case "name":
|
||||
if (answer.length > 32) {
|
||||
await client.sendText(roomId, messages.errors.toobig);
|
||||
return;
|
||||
}
|
||||
await db.query("UPDATE users SET name = $1 WHERE room_id = $2", [answer, roomId]);
|
||||
await client.sendText(roomId, `Set your name setting to "${answer}".`);
|
||||
await client.sendText(roomId, messages.setup.age); //next question
|
||||
await db.query("UPDATE users SET current_action = 'age' WHERE room_id = $1", [roomId]);
|
||||
break;
|
||||
case "age":
|
||||
if (answer < 14) {
|
||||
await client.sendText(roomId, messages.errors.tooyoung);
|
||||
await client.leaveRoom(roomId)
|
||||
return;
|
||||
}
|
||||
await db.query("UPDATE users SET age = $1 WHERE room_id = $2", [answer, roomId]);
|
||||
await client.sendText(roomId, `Set your age setting to "${answer}".`);
|
||||
await client.sendText(roomId, messages.setup.gender); //next question
|
||||
await db.query("UPDATE users SET current_action = 'gender' WHERE room_id = $1", [roomId]);
|
||||
break;
|
||||
case "gender":
|
||||
if (answer.toLowerCase() != "male" || answer.toLowerCase() != "female") {
|
||||
await client.sendText(roomId, messages.errors.twogenders);
|
||||
return;
|
||||
}
|
||||
await db.query("UPDATE users SET gender = $1 WHERE room_id = $2", [answer.toLowerCase() == "male" ? true : false, roomId]);
|
||||
await client.sendText(roomId, `Set your gender setting to "${answer}".`);
|
||||
await client.sendText(roomId, messages.setup.interest); //next question
|
||||
await db.query("UPDATE users SET current_action = 'interest' WHERE room_id = $1", [roomId]);
|
||||
break;
|
||||
case "interest":
|
||||
if (answer.toLowerCase() != "male" || answer.toLowerCase() != "female" || answer.toLowerCase() != "both") {
|
||||
await client.sendText(roomId, messages.errors.didntunderstand);
|
||||
return;
|
||||
}
|
||||
await db.query("UPDATE users SET interest = $1 WHERE room_id = $2", [answer.toLowerCase(), roomId]);
|
||||
await client.sendText(roomId, `Set your interest setting to "${answer}".`);
|
||||
await client.sendText(roomId, messages.setup.description); //next question
|
||||
await db.query("UPDATE users SET current_action = 'description' WHERE room_id = $1", [roomId]);
|
||||
break;
|
||||
case "description":
|
||||
if (answer.length > 512) {
|
||||
await client.sendText(roomId, messages.errors.toobig);
|
||||
return;
|
||||
}
|
||||
await db.query("UPDATE users SET description = $1 WHERE room_id = $2", [answer, roomId]);
|
||||
await client.sendText(roomId, `Set your description setting to "${answer}".`);
|
||||
await client.sendText(roomId, messages.setup.pictures); //next question
|
||||
await db.query("UPDATE users SET current_action = 'pictures' WHERE room_id = $1", [roomId]);
|
||||
break;
|
||||
case "pictures":
|
||||
if (event.content?.msgtype !== 'm.image') {
|
||||
await client.sendText(roomId, messages.setup.done);
|
||||
await db.query("UPDATE users SET current_action = 'view_profiles' WHERE room_id = $1", [roomId]);
|
||||
} else {
|
||||
let pictures_count = (await db.query("SELECT array_length(pictures_urls) FROM users WHERE room_id = $1"), [roomId]).rows[0];
|
||||
if (pictures_count == 5) {
|
||||
await client.sendText(roomId, messages.error.toomuch);
|
||||
await db.query("UPDATE users SET current_action = 'view_profiles' WHERE room_id = $1", [roomId]);
|
||||
} else {
|
||||
const message = new MessageEvent(event);
|
||||
const fileEvent = new MessageEvent(message.raw);
|
||||
const decrypted = await client.crypto.decryptMedia(fileEvent.content.file);
|
||||
const encrypted = await client.crypto.encryptMedia(Buffer.from(decrypted));
|
||||
const mxc = await client.uploadContent(encrypted.buffer)
|
||||
logInfo(`New media: ${mxc}`)
|
||||
let pictures_count = (await db.query("UPDATE users SET pictures_urls = array_append(pictures_urls, $1) WHERE room_id = $2 RETURNING array_length(pictures_urls)", [mxc, roomId])).rows[0];
|
||||
if (pictures_count < 5) {
|
||||
await client.sendText(roomId, messages.setup.more);
|
||||
} else {
|
||||
await client.sendText(roomId, messages.setup.enough);
|
||||
await db.query("UPDATE users SET current_action = 'view_profiles' WHERE room_id = $1", [roomId]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
} catch (e) {
|
||||
logError(e)
|
||||
}
|
||||
});
|
||||
client.on("room.invite", async (roomId) => {
|
||||
try {
|
||||
await client.joinRoom(roomId);
|
||||
|
||||
let members = await client.getAllRoomMembers(roomId)
|
||||
let isDM = members.length == 2 ? true : false;
|
||||
|
||||
if (!isDM) {
|
||||
client.sendText(roomId, messages.errors.notadm);
|
||||
client.leaveRoom(roomId)
|
||||
}
|
||||
|
||||
logInfo(`Bot has joined a room with ID ${roomId}`)
|
||||
|
||||
let mx_id = members[0].event.sender
|
||||
console.log(`Sender is ${mx_id}`)
|
||||
|
||||
await db.query("INSERT INTO users(mx_id, room_id, current_action) VALUES ($1, $2, $3)", [mx_id, roomId, "country"])
|
||||
|
||||
await client.sendText(roomId, messages.welcome);
|
||||
await client.sendText(roomId, messages.setup.country);
|
||||
|
||||
|
||||
} catch (e) {
|
||||
logError(e)
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
client.start().then(() => logInfo("Bot started!"));
|
||||
22
src/initDb.js
Normal file
22
src/initDb.js
Normal file
@@ -0,0 +1,22 @@
|
||||
import pg from 'pg'
|
||||
import fs from 'fs'
|
||||
|
||||
const { Client } = pg
|
||||
|
||||
export const getClient = async () => {
|
||||
const client = new Client({
|
||||
user: process.env.POSTGRES_USER,
|
||||
password: process.env.POSTGRES_PASSWORD,
|
||||
host: "postgresql",
|
||||
port: 5432,
|
||||
database: process.env.POSTGRES_DB
|
||||
});
|
||||
await client.connect()
|
||||
|
||||
console.log(fs.readFileSync('./scheme.psql').toString())
|
||||
|
||||
await client.query(fs.readFileSync('./scheme.psql').toString())
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
42
src/utils.js
Normal file
42
src/utils.js
Normal file
@@ -0,0 +1,42 @@
|
||||
import fs from 'fs'
|
||||
|
||||
const configPath = './config.json'
|
||||
const messagesPath = './messages.json'
|
||||
|
||||
export const logError = (message) => {
|
||||
let time = new Date
|
||||
console.error(`[${time.toLocaleString()}] [LOG] [E] ${message}`)
|
||||
}
|
||||
|
||||
export const logInfo = (message) => {
|
||||
let time = new Date
|
||||
console.log(`[${time.toLocaleString()}] [LOG] [I] ${message}`)
|
||||
}
|
||||
|
||||
export const readConfig = () => {
|
||||
|
||||
if (!fs.existsSync(configPath)) {
|
||||
|
||||
fs.writeFileSync(configPath,
|
||||
`{
|
||||
"homeserverURL": "https://matrix.org/",
|
||||
"token": "Super secret token! Do not show to anyone! Even your mum! ;)"
|
||||
}`
|
||||
);
|
||||
|
||||
logError('[LOG] [E] Config file was not found. I have created a template, please, edit it and restart a bot.')
|
||||
|
||||
process.exit(-1)
|
||||
}
|
||||
|
||||
return JSON.parse(fs.readFileSync(configPath)
|
||||
)
|
||||
}
|
||||
|
||||
export const readMessages = () => {
|
||||
if (!fs.existsSync(messagesPath)) {
|
||||
logError("No 'messages.json' file found. Please, ensure that you are up to date by syncing using 'git pull' command.")
|
||||
process.exit(-1)
|
||||
}
|
||||
return JSON.parse(fs.readFileSync(messagesPath))
|
||||
}
|
||||
Reference in New Issue
Block a user