Done basic profile setting

This commit is contained in:
leca 2024-08-03 21:10:36 +03:00
parent d8d50d1525
commit 01454892f8
3 changed files with 27 additions and 41 deletions

View File

@ -34,27 +34,14 @@ 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.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 current_action = (await db.query('SELECT current_action FROM users WHERE room_id = $1', [roomId])).rows[0].current_action;
let answer = event.content.body;
console.log(answer)
switch (current_action) {
case "country":
if (answer.length > 64) {
@ -94,21 +81,21 @@ client.on("room.message", async (roomId, event) => {
}
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]);
await client.sendText(roomId, messages.setup.sex); //next question
await db.query("UPDATE users SET current_action = 'sex' WHERE room_id = $1", [roomId]);
break;
case "gender":
if (answer.toLowerCase() != "male" || answer.toLowerCase() != "female") {
await client.sendText(roomId, messages.errors.twogenders);
case "sex":
if (answer.toLowerCase() != "male" && answer.toLowerCase() != "female") {
await client.sendText(roomId, messages.errors.twosexes);
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 db.query("UPDATE users SET sex = $1 WHERE room_id = $2", [answer.toLowerCase() == "male" ? true : false, roomId]);
await client.sendText(roomId, `Set your sex 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") {
if (answer.toLowerCase() != "male" && answer.toLowerCase() != "female" && answer.toLowerCase() != "both") {
await client.sendText(roomId, messages.errors.didntunderstand);
return;
}
@ -132,30 +119,27 @@ client.on("room.message", async (roomId, event) => {
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];
let pictures_count = (await db.query("SELECT cardinality(pictures_urls) AS length FROM users WHERE room_id = $1", [roomId])).rows[0].length;
if (pictures_count == 5) {
await client.sendText(roomId, messages.error.toomuch);
await client.sendText(roomId, messages.errors.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];
// const encrypted = await client.crypto.encryptMedia(Buffer.from(decrypted));
const mxc = await client.uploadContent(decrypted)
let pictures_count = (await db.query("UPDATE users SET pictures_urls = array_append(pictures_urls, $1) WHERE room_id = $2 RETURNING cardinality(pictures_urls) AS amount", [mxc, roomId])).rows[0].amount;
if (pictures_count < 5) {
await client.sendText(roomId, messages.setup.more);
await client.sendText(roomId, messages.setup.more + String(5 - pictures_count));
} else {
await client.sendText(roomId, messages.setup.enough);
await db.query("UPDATE users SET current_action = 'view_profiles' WHERE room_id = $1", [roomId]);
}
}
}
break;
}
} catch (e) {
logError(e)
}
@ -175,13 +159,17 @@ client.on("room.invite", async (roomId) => {
logInfo(`Bot has joined a room with ID ${roomId}`)
let mx_id = members[0].event.sender
console.log(`Sender is ${mx_id}`)
let is_profile_exists = (await db.query("SELECT * FROM users WHERE room_id = $1", [roomId])).rowCount > 0;
if (!is_profile_exists){
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);
} else {
await db.query("UPDATE users SET current_action = 'view_profiles' WHERE room_id = $1", [roomId]);
}
} catch (e) {
logError(e)

View File

@ -13,8 +13,6 @@ export const getClient = async () => {
});
await client.connect()
console.log(fs.readFileSync('./scheme.psql').toString())
await client.query(fs.readFileSync('./scheme.psql').toString())
return client