tagkeyboard
All checks were successful
fetch npm modules / f0ck the f0cker (push) Successful in 41s

This commit is contained in:
Flummi 2023-05-01 17:21:26 +02:00
parent 4e6b337d4f
commit 9ccf17ac54

View File

@ -5,6 +5,23 @@ import cfg from "../config.mjs";
import db from "../sql.mjs"; import db from "../sql.mjs";
import lib from "../lib.mjs"; import lib from "../lib.mjs";
const tagkeyboard = id => {
const tags = [{
tag: 'music',
id: 115
}, {
tag: 'german',
id: 329
}, {
tag: 'cat',
id: 217
}, {
tag: 'doggo',
id: 5
}];
return Promise.all(tags.map(async t => ({ text: `${await lib.hasTag(id, t.id) ? '✓ ' : ''}${t.tag}`, callback_data: `b_settag_${t.id}:${id}` })));
};
export default async bot => { export default async bot => {
return [{ return [{
@ -17,25 +34,49 @@ export default async bot => {
let f0ck; let f0ck;
id = +id; id = +id;
if(cmd.startsWith('b_settag_')) {
const tagid = +cmd.replace('b_settag_', '');
if(!(await lib.getTags(id)).filter(tag => tag.id == tagid).length) {
// insert
await db`
insert into "tags_assign" ${
db({
item_id: id,
tag_id: tagid,
user_id: 1
})
}
`;
}
else {
// delete
await db`
delete from "tags_assign"
where tag_id = ${tagid}
and item_id = ${id}
`;
}
const keyboard = await tagkeyboard(id);
return await e.editMessageText(e.raw.chat.id, e.raw.message_id, e.message, {
reply_markup: JSON.stringify({
inline_keyboard: [[
...keyboard
], [
{ text: 'back', callback_data: `b_back:${id}` }
]]
})
});
}
switch(cmd) { switch(cmd) {
case "b_tags": case "b_tags":
if(!id) if(!id)
return; return;
const tags = [{ const keyboard = await tagkeyboard(id);
tag: 'music',
id: 115
}, {
tag: 'german',
id: 329
}, {
tag: 'cat',
id: 217
}, {
tag: 'doggo',
id: 5
}];
const keyboard = await Promise.all(tags.map(async t => ({ text: `${await lib.hasTag(id, t.id) ? '✓ ' : ''}${t.tag}`, callback_data: `b_${t.tag}:${id}` })));
await e.editMessageText(e.raw.chat.id, e.raw.message_id, e.message, { await e.editMessageText(e.raw.chat.id, e.raw.message_id, e.message, {
reply_markup: JSON.stringify({ reply_markup: JSON.stringify({
@ -69,7 +110,7 @@ export default async bot => {
if(!id) if(!id)
return; return;
if(!(await lib.getTags(id)).filter(tag => [1,2].includes(tag.id)).length) { if(!(await lib.getTags(id)).filter(tag => tag.id == 1).length) {
// insert // insert
await db` await db`
insert into "tags_assign" ${ insert into "tags_assign" ${
@ -82,16 +123,15 @@ export default async bot => {
`; `;
} }
else { else {
// update // delete
await db` await db`
update "tags_assign" set tag_id = 1 delete from "tags_assign"
where where tag_id = 1
tag_id = 2 and and item_id = ${id}
item_id = ${id}
`; `;
} }
await e.editMessageText(e.raw.chat.id, e.raw.message_id, e.message, { return await e.editMessageText(e.raw.chat.id, e.raw.message_id, e.message, {
reply_markup: JSON.stringify({ reply_markup: JSON.stringify({
inline_keyboard: [[ inline_keyboard: [[
{ text: '✓ sfw', callback_data: `b_sfw:${id}` }, { text: '✓ sfw', callback_data: `b_sfw:${id}` },
@ -109,7 +149,7 @@ export default async bot => {
if(!id) if(!id)
return; return;
if(!(await lib.getTags(id)).filter(tag => [1,2].includes(tag.id)).length) { if(!(await lib.getTags(id)).filter(tag => tag.id == 2).length) {
// insert // insert
await db` await db`
insert into "tags_assign" ${ insert into "tags_assign" ${
@ -122,16 +162,15 @@ export default async bot => {
`; `;
} }
else { else {
// update // delete
await db` await db`
update "tags_assign" set tag_id = 2 delete from "tags_assign"
where where tag_id = 2
tag_id = 1 and and item_id = ${id}
item_id = ${id}
`; `;
} }
await e.editMessageText(e.raw.chat.id, e.raw.message_id, e.message, { return await e.editMessageText(e.raw.chat.id, e.raw.message_id, e.message, {
reply_markup: JSON.stringify({ reply_markup: JSON.stringify({
inline_keyboard: [[ inline_keyboard: [[
{ text: 'sfw', callback_data: `b_sfw:${id}` }, { text: 'sfw', callback_data: `b_sfw:${id}` },