Skip to content

Commit 4cc091b

Browse files
committed
feat(*): added autoremove quiz
1 parent a83794e commit 4cc091b

File tree

5 files changed

+80
-13
lines changed

5 files changed

+80
-13
lines changed

bot/commands/admin/resetQuiz.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { INITIAL_SESSION } from '../../constants/index.js'
2+
3+
export const resetQuiz = bot => {
4+
bot.onText(/\/rq/, async msg => {
5+
if (msg?.chat?.id == process.env.NOTIF_GROUP) {
6+
const { id: chatId } = msg.chat
7+
const msgId = msg.message_id
8+
const { id } = msg.from
9+
const options = {
10+
parse_mode: 'HTML',
11+
reply_to_message_id: msgId
12+
}
13+
14+
try {
15+
лучше созданять дату и время, когда у человека будет доступна следубщая игра
16+
позже при открытии этим пользователем викторины я должен буду проверить эту дату
17+
если эта дата будет
18+
} catch (error) {
19+
await bot.sendMessage(chatId, `${error.message}`, options)
20+
}
21+
}
22+
})
23+
}

bot/commands/keyboard/quiz.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ import { getStringOrDist } from '../../utils/quiz/getStringOrDist.js'
44
import dotenv from 'dotenv'
55
import { ct } from '../../utils/createTranslate.js'
66
import { createNewQuizKeyboard } from '../../utils/quiz/createNewQuizKeyboard.js'
7+
import { Sequelize } from 'sequelize'
78

89
dotenv.config()
910

1011
const miniGames = ['🏀', '🏀', '🏀', '⚽', '⚽', '⚽', '🎳', '🎲', '🎯']
1112

13+
const getRebootDateForUser = (count) => {
14+
if (count !== 0) return null;
15+
return Sequelize.literal('CURRENT_TIMESTAMP')
16+
}
17+
1218
export const keyboardQuiz = async (bot, msg, isDescription) => {
1319
const t = await ct(msg)
1420
let accountMessage
@@ -30,10 +36,9 @@ export const keyboardQuiz = async (bot, msg, isDescription) => {
3036
where: {
3137
chat_id: chatId
3238
}
33-
}).then(res => {
34-
const keyboard = createNewQuizKeyboard(res, msgId, t)
39+
}).then(async res => {
40+
const keyboard = await createNewQuizKeyboard(res, msgId, t, chatId)
3541
const timeout = setTimeout(async () => {
36-
// TODO: Сделать подсчет колличества бесплатных запросов в сутки на бесплатном режиме
3742
await bot.deleteMessage(chatId, accountMessage.message_id)
3843
accountMessage = await bot.sendMessage(
3944
chatId,
@@ -101,7 +106,8 @@ export const keyboardQuiz = async (bot, msg, isDescription) => {
101106

102107
await db.subscriber.update(
103108
{
104-
quiz_token_available: available
109+
quiz_token_available: available,
110+
quiz_reboot_date: getRebootDateForUser(available)
105111
},
106112
{ where: { chat_id: chatId } }
107113
).then(res => {
@@ -202,7 +208,8 @@ export const keyboardQuiz = async (bot, msg, isDescription) => {
202208

203209
await db.subscriber.update(
204210
{
205-
quiz_subs_available: available
211+
quiz_subs_available: available,
212+
quiz_reboot_date: getRebootDateForUser(available)
206213
},
207214
{ where: { chat_id: chatId } }
208215
).then(res => {

bot/db/models/settings.model.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export default (sequelize, DataTypes) => {
3333
type: DataTypes.DOUBLE,
3434
required: true
3535
},
36+
days_freez: {
37+
type: DataTypes.DOUBLE,
38+
defaultValue: 7,
39+
required: true
40+
},
3641
})
3742

3843
return SettingsSchema

bot/db/models/subscriber.model.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ export default (sequelize, DataTypes) => {
5454
type: DataTypes.DOUBLE,
5555
defaultValue: 0
5656
},
57+
quiz_reboot_date: {
58+
type: DataTypes.DATE,
59+
},
5760
MI_count: {
5861
type: DataTypes.DOUBLE,
5962
defaultValue: 0
Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,53 @@
1-
export const createNewQuizKeyboard = (res, chatId, t) => {
1+
import { db } from '../../db/index.js'
2+
3+
export const createNewQuizKeyboard = async (res, msgId, t, chatId) => {
24
const keyboard = []
35
const keyboard2 = []
6+
// проверить, если с даты блокировки прошло ровно 7 дней - обнулить
7+
const dateFreezeQuiz = res.dataValues?.quiz_reboot_date
8+
const today = new Date()
9+
const daysHavePassed = (today - dateFreezeQuiz) / 1000 / 60 / 60 / 24
10+
const VALUES = 5;
11+
const { dataValues } = await db.settings.findOne({ where: { user_id: 0 } })
12+
13+
if ((res?.dataValues?.quiz_subs_available === 0 && res.dataValues?.quiz_token_available === 0)
14+
&& (dataValues['days_freez'] - daysHavePassed) <= 0) {
15+
db.subscriber.findOne(
16+
{ where: { chat_id: chatId } }
17+
).then(() => {
418

5-
if (res?.dataValues?.quiz_subs_available === 0 && res.dataValues?.quiz_token_available === 0) {
19+
db.subscriber.update(
20+
{
21+
quiz_reboot_date: null,
22+
quiz_subs_available: VALUES,
23+
quiz_token_available: VALUES
24+
},
25+
{ where: { chat_id: chatId } }
26+
).then(() => {
27+
keyboard.push({
28+
text: `${t('btn_win_subs')} (${VALUES})`,
29+
callback_data: `WIN_SUBS_${msgId}`
30+
})
31+
keyboard2.push({
32+
text: `${t('btn_win_tokens')} (${VALUES})`,
33+
callback_data: `WIN_REQ_${msgId}`
34+
})
35+
})
36+
})
37+
} else if ((dataValues['days_freez'] - daysHavePassed) >= 0) {
638
keyboard.push({ text: t('new_quiz_after_week'), callback_data: `NO_ATTEMPTS` })
739
} else {
840
if (res?.dataValues?.quiz_subs_available !== 0)
941
keyboard.push({
1042
text: `${t('btn_win_subs')} (${res.dataValues.quiz_subs_available})`,
11-
callback_data: `WIN_SUBS_${chatId}`
43+
callback_data: `WIN_SUBS_${msgId}`
1244
})
1345
if (res?.dataValues?.quiz_token_available !== 0)
1446
keyboard2.push({
1547
text: `${t('btn_win_tokens')} (${res.dataValues.quiz_token_available})`,
16-
callback_data: `WIN_REQ_${chatId}`
48+
callback_data: `WIN_REQ_${msgId}`
1749
})
1850
}
1951

20-
return [
21-
keyboard,
22-
keyboard2
23-
]
52+
return [keyboard, keyboard2]
2453
}

0 commit comments

Comments
 (0)