Skip to content

Commit 1ac3180

Browse files
committed
refactoring(*)
1 parent c9509b6 commit 1ac3180

File tree

17 files changed

+121
-182
lines changed

17 files changed

+121
-182
lines changed

app.js

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,9 @@ import authRoutes from './api/routes/auth.routes.js'
1515
import userRoutes from './api/routes/user.routes.js'
1616
import subsRoutes from './api/routes/subs.routes.js'
1717
import {
18-
COMMAND_ACCOUNT_EN,
19-
COMMAND_ACCOUNT_FR,
20-
COMMAND_ACCOUNT_RU,
2118
COMMAND_DALL_E,
22-
COMMAND_FILE_CONVERTOR_EN,
23-
COMMAND_FILE_CONVERTOR_FR,
24-
COMMAND_FILE_CONVERTOR_RU,
2519
COMMAND_GPT,
26-
COMMAND_HELP_EN,
27-
COMMAND_HELP_FR,
28-
COMMAND_HELP_RU,
2920
COMMAND_MIDJOURNEY,
30-
COMMAND_QUIZ_EN, COMMAND_QUIZ_FR, COMMAND_QUIZ_RU,
31-
COMMAND_TEXT_TO_SPEECH_EN,
32-
COMMAND_TEXT_TO_SPEECH_FR,
33-
COMMAND_TEXT_TO_SPEECH_RU,
3421
REQUEST_TYPES
3522
} from './bot/constants/index.js'
3623
import { keyboardChatGPT } from './bot/commands/keyboard/chat_gpt.js'
@@ -115,36 +102,24 @@ bot.on('message', async (msg, match) => {
115102
})
116103

117104
switch (msg.text) {
118-
case COMMAND_ACCOUNT_RU:
119-
case COMMAND_ACCOUNT_FR:
120-
case COMMAND_ACCOUNT_EN:
105+
case t('keyboard_acc'):
121106
switchToMode('GPT', msg.chat.id, msg.from)
122107
return keyboardMyAccount(bot, msg)
123108
case COMMAND_GPT:
124109
return keyboardChatGPT(bot, msg)
125-
case COMMAND_FILE_CONVERTOR_FR:
126-
case COMMAND_FILE_CONVERTOR_EN:
127-
case COMMAND_FILE_CONVERTOR_RU:
110+
case t('keyboard_convertor'):
128111
switchToMode('GPT', msg.chat.id, msg.from)
129112
return keyboardConverter(bot, msg)
130113
case COMMAND_MIDJOURNEY:
131114
return keyboardMidjourney(bot, msg)
132-
case COMMAND_TEXT_TO_SPEECH_EN:
133-
case COMMAND_TEXT_TO_SPEECH_RU:
134-
case COMMAND_TEXT_TO_SPEECH_FR:
115+
case t('keyboard_tts'):
135116
return keyboardTextToSpeech(bot, msg)
136117
case COMMAND_DALL_E:
137-
switchToMode('GPT', msg.chat.id, msg.from)
138118
return keyboardDalle(bot, msg)
139-
break
140-
case COMMAND_HELP_RU:
141-
case COMMAND_HELP_EN:
142-
case COMMAND_HELP_FR:
119+
case t('keyboard_help'):
143120
switchToMode('GPT', msg.chat.id, msg.from)
144121
return keyboardHelp(bot, msg, t)
145-
case COMMAND_QUIZ_RU:
146-
case COMMAND_QUIZ_EN:
147-
case COMMAND_QUIZ_FR:
122+
case t('keyboard_quiz'):
148123
switchToMode('GPT', msg.chat.id, msg.from)
149124
return keyboardQuiz(bot, msg, true)
150125
default:

bot/commands/hoc/autoRemoveMessage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const autoRemoveMessage = async (content, bot, chatId, options, duration = 5000) => {
1+
export const autoRemoveMessage = async (content, bot, chatId, options = {}, duration = 5000) => {
22
const message = await bot.sendMessage(chatId, `・ \n${content}`, options)
33
const durationTemplate = '・'
44

bot/commands/keyboard/chat_gpt.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import events from 'events'
22
import { db } from '../../db/index.js'
33
import { modesChatGPT } from '../../constants/modes.js'
4-
import { saveAndSendPhoto } from '../../utils/saveAndSendPhoto.js'
5-
import { TYPE_RESPONSE_MJ } from '../../constants/index.js'
4+
import { createStartKeyboardForReplyMarkup } from '../../utils/createStartKeyboard.js'
65
import { ct } from '../../utils/createTranslate.js'
76

87
export const keyboardChatGPT = async (bot, msg) => {
@@ -99,7 +98,8 @@ export const keyboardChatGPT = async (bot, msg) => {
9998
const msgId = msg.message_id
10099
const options = {
101100
parse_mode: 'HTML',
102-
reply_to_message_id: msgId
101+
reply_to_message_id: msgId,
102+
reply_markup: await createStartKeyboardForReplyMarkup(msg)
103103
}
104104
try {
105105
db.subscriber.findOne({
@@ -124,7 +124,7 @@ export const keyboardChatGPT = async (bot, msg) => {
124124
{ where: { chat_id: chatId } }
125125
).then(res => {
126126
bot.select_mode = 'GPT'
127-
return sendChatGPT(bot, chatId, options, "assistant")
127+
return sendChatGPT(bot, chatId, options, 'assistant')
128128
})
129129
} else {
130130
db.subscriber.create({

bot/commands/keyboard/converter.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Converter } from '../../utils/converter.js'
21
import { errorMessage } from '../hoc/errorMessage.js'
3-
import { formats } from '../../constants/formatsConterter.js'
42
import { ct } from '../../utils/createTranslate.js'
3+
import { createStartKeyboardForReplyMarkup } from '../../utils/createStartKeyboard.js'
54

65
export const keyboardConverter = async (bot, msg) => {
76
const t = await ct(msg)
@@ -11,20 +10,21 @@ export const keyboardConverter = async (bot, msg) => {
1110
).catch(err => console.log(err))
1211

1312
try {
14-
const timeout = setTimeout((chatId, message_id) => {
13+
const timeout = setTimeout(async (msg, message_id) => {
1514
bot.editMessageText(
1615
t('desc_converter'),
1716
{
18-
chat_id: chatId,
17+
chat_id: msg.chat.id,
1918
message_id: message_id,
20-
parse_mode: 'HTML'
19+
parse_mode: 'HTML',
20+
reply_markup: await createStartKeyboardForReplyMarkup(msg)
2121
}).catch(() => {
2222
return true
2323
})
2424
clearTimeout(timeout)
25-
}, 1500, msg.chat.id, accountMessage?.message_id)
25+
}, 1500, msg, accountMessage?.message_id)
2626
} catch (e) {
27-
bot.deleteMessage(msg.chat.id, accountMessage.message_id)
27+
bot.deleteMessage(msg.chat.id, accountMessage.message_id)
2828
return errorMessage(bot, e.message, msg, 'keybourd/converter')
2929
}
3030
}

bot/commands/keyboard/dalle.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import events from 'events'
2-
import { PAYOK } from 'payok'
3-
import { nanoid } from 'nanoid'
42
import dotenv from 'dotenv'
53
import { keyboardChatGPT } from './chat_gpt.js'
64
import { COMMAND_GPT } from '../../constants/index.js'
75
import { db } from '../../db/index.js'
86
import { ct } from '../../utils/createTranslate.js'
97
import { keyboardMyAccount } from './my_account.js'
8+
import { createStartKeyboardForReplyMarkup } from '../../utils/createStartKeyboard.js'
109

1110
dotenv.config({ path: '../.env' })
1211

@@ -25,8 +24,8 @@ export const keyboardDalle = async (bot, msg) => {
2524
...options,
2625
reply_markup: {
2726
inline_keyboard: [
28-
[{ text: '🏞 Купить подписку', callback_data: `buy_subscription_M_${chatId}` }],
29-
[{ text: 'Выйти', callback_data: `${COMMAND_GPT}_M_${chatId}` }]
27+
[{ text: t('keyboard_buy_subscription'), callback_data: `buy_subscription_M_${chatId}` }],
28+
[{ text: t('exit_key'), callback_data: `${COMMAND_GPT}_M_${chatId}` }]
3029
]
3130
}
3231
}
@@ -65,7 +64,8 @@ export const keyboardDalle = async (bot, msg) => {
6564
const msgId = msg.message_id
6665
const options = {
6766
parse_mode: 'HTML',
68-
reply_to_message_id: msgId
67+
reply_to_message_id: msgId,
68+
reply_markup: await createStartKeyboardForReplyMarkup(msg)
6969
}
7070
try {
7171
db.subscriber.findOne({
@@ -74,20 +74,20 @@ export const keyboardDalle = async (bot, msg) => {
7474
user_id: msg.from.id
7575
}
7676
}).then(res => {
77-
if (res?.dataValues.mode?.match(/\DALL-E/))
77+
if (res?.dataValues.mode?.match(/\DALLE/))
7878
return sendDalle(bot, chatId, options)
7979
else if (res?.dataValues.mode) {
8080
db.subscriber.update(
8181
{
82-
mode: 'DALL-E',
82+
mode: 'DALLE',
8383
first_name: msg.from.first_name,
8484
last_name: msg.from.last_name,
8585
username: msg.from.username,
8686
language_code: msg.from.language_code
8787
},
8888
{ where: { chat_id: chatId } }
8989
).then(res => {
90-
bot.select_mode = 'DALL-E'
90+
bot.select_mode = 'DALLE'
9191
return sendDalle(bot, chatId, options)
9292
})
9393
} else {
@@ -98,9 +98,9 @@ export const keyboardDalle = async (bot, msg) => {
9898
last_name: msg.from.last_name,
9999
username: msg.from.username,
100100
language_code: msg.from.language_code,
101-
mode: 'DALL-E'
101+
mode: 'DALLE'
102102
}).then(res => {
103-
bot.select_mode = 'DALL-E'
103+
bot.select_mode = 'DALLE'
104104
return sendDalle(bot, chatId, options)
105105
})
106106
}

bot/commands/keyboard/my_account.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { keyboardQuiz } from './quiz.js'
1010
import Stripe from 'stripe'
1111
import { createFullName } from '../../utils/createFullName.js'
1212
import { numberWithSpaces } from '../../utils/numberWithSpaces.js'
13+
import { createStartKeyboardForReplyMarkup } from '../../utils/createStartKeyboard.js'
1314

1415
dotenv.config({ path: '../.env' })
1516

@@ -29,7 +30,8 @@ export const keyboardMyAccount = async (bot, msg, prevMessageForEdit, prevLevel,
2930
const generalOptions = {
3031
parse_mode: 'HTML',
3132
reply_to_message_id: msgId,
32-
disable_web_page_preview: true
33+
disable_web_page_preview: true,
34+
reply_markup: await createStartKeyboardForReplyMarkup(msg)
3335
}
3436
try {
3537
const inlineKeyboard = [

bot/commands/modes/chatGPT.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import { INITIAL_SESSION } from '../../constants/index.js'
1+
import { INITIAL_SESSION, REQUEST_TYPES } from '../../constants/index.js'
22
import { OpenAI } from '../../utils/openAi.js'
33
import { spinnerOff, spinnerOn } from '../../utils/spinner.js'
44
import { errorMessage } from '../hoc/errorMessage.js'
55
import { modesChatGPT } from '../../constants/modes.js'
66
import { db } from '../../db/index.js'
77
import { exceptionForHistoryLogging } from '../../utils/exceptionForHistoryLogging.js'
88
import { createFullName } from '../../utils/createFullName.js'
9-
import { calculationOfNumberOfTokens } from '../../utils/checkTokens.js'
109
import { ct } from '../../utils/createTranslate.js'
11-
import { Sequelize } from 'sequelize'
10+
import { writingOffTokens } from '../../utils/checkTokens.js'
1211

1312
export const modeChatGPT = async (bot, msg, qweryOptions) => {
1413
const t = await ct(msg)
@@ -64,12 +63,8 @@ export const modeChatGPT = async (bot, msg, qweryOptions) => {
6463
const response = await openAi.chat(msg?.ctx.messages, bot, message, chatID, x.parse_mode)
6564

6665
const textSum = (response + newMessage)
67-
const tokenCounts = await calculationOfNumberOfTokens(textSum)
6866

69-
await db.subscriber.update(
70-
{ tokens: Sequelize.literal(`tokens - ${tokenCounts}`) },
71-
{ where: { chat_id: chatID } }
72-
)
67+
await writingOffTokens(bot, msg, REQUEST_TYPES.GPT, textSum)
7368

7469
if (!response) {
7570
throw new Error('Something went wrong please try again.')

bot/commands/modes/midjourney.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { Midjourney } from 'midjourney'
2-
import { saveAndSendPhoto, saveAndSendPreloaderPhoto } from '../../utils/saveAndSendPhoto.js'
2+
import { saveAndSendPreloaderPhoto } from '../../utils/saveAndSendPhoto.js'
33
import { loaderOn } from '../../utils/loader.js'
4-
import { REQUEST_TYPES_COST, TYPE_RESPONSE_MJ } from '../../constants/index.js'
4+
import { REQUEST_TYPES } from '../../constants/index.js'
55
import { upscale } from './midjourney/upscale.js'
66
import events from 'events'
77
import { variation } from './midjourney/variation.js'
88
import dotenv from 'dotenv'
9-
import { calculationOfNumberOfTokens } from '../../utils/checkTokens.js'
10-
import { db } from '../../db/index.js'
11-
import { Sequelize } from 'sequelize'
9+
import { writingOffTokens } from '../../utils/checkTokens.js'
1210

1311
dotenv.config()
1412

@@ -85,14 +83,7 @@ export const modeMidjourney = async (bot, sudoUser, msg, match) => {
8583
const imgDir = './Imagines'
8684
const filePath = `${imgDir}/${userMessageId}.png`
8785

88-
const prevMessage = await saveAndSendPhoto(imgUrl, imgDir, filePath, chatID, bot, options, TYPE_RESPONSE_MJ.PHOTO, waiting)
89-
90-
const tokenCounts = await calculationOfNumberOfTokens(' ', REQUEST_TYPES_COST.MIDJOURNEY)
91-
92-
await db.subscriber.update(
93-
{ tokens: Sequelize.literal(`tokens - ${tokenCounts}`) },
94-
{ where: { chat_id: chatID } }
95-
)
86+
await writingOffTokens(bot, msg, REQUEST_TYPES.MIDJOURNEY)
9687

9788
for (let i = 1; i < 5; i++) {
9889
eventEmitter.on(`V${i}++${waiting.message_id}`, async function(query) {

bot/commands/modes/modeDalle.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import dotenv from 'dotenv'
22
import { saveAndSendPhoto } from '../../utils/saveAndSendPhoto.js'
33
import { sudoChecker } from '../../utils/sudoChecker.js'
44
import { spinnerOn } from '../../utils/spinner.js'
5-
import { REQUEST_TYPES_COST, TYPE_RESPONSE_MJ } from '../../constants/index.js'
5+
import { REQUEST_TYPES, TYPE_RESPONSE_MJ } from '../../constants/index.js'
66
import { loaderOn } from '../../utils/loader.js'
77
import { OpenAI } from '../../utils/openAi.js'
8-
import { calculationOfNumberOfTokens } from '../../utils/checkTokens.js'
9-
import { db } from '../../db/index.js'
10-
import { Sequelize } from 'sequelize'
8+
import { writingOffTokens } from '../../utils/checkTokens.js'
119

1210
dotenv.config()
1311

@@ -48,21 +46,15 @@ export const modeDalle = async (bot, sudoUser, msg, match) => {
4846

4947
const response = await openAi.image(prompt)
5048

51-
const tokenCounts = await calculationOfNumberOfTokens(' ', REQUEST_TYPES_COST.DALLE)
52-
53-
await db.subscriber.update(
54-
{ tokens: Sequelize.literal(`tokens - ${tokenCounts}`) },
55-
{ where: { chat_id: chatID } }
56-
)
57-
5849
const imgUrl = response
5950
const imgDir = './dalle'
6051
const filePath = `${imgDir}/${userMessageId}.png`
6152

62-
await loaderOn('42%', bot, chatID, waiting?.message_id)
53+
await loaderOn('78%', bot, chatID, waiting?.message_id)
6354

6455
await saveAndSendPhoto(imgUrl, imgDir, filePath, chatID, bot, options, TYPE_RESPONSE_MJ.PHOTO, spinner,
6556
waiting)
57+
await writingOffTokens(bot, msg, REQUEST_TYPES.DALLE)
6658
await bot.deleteMessage(chatID, waiting.message_id).catch()
6759
} catch (error) {
6860
await bot.deleteMessage(chatID, spinner)

bot/commands/onMessageDocument.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import { formats, formatsConvertor } from '../constants/formatsConterter.js'
99
import { stepperOn } from '../utils/stepper.js'
1010
import { sleep } from '../utils/sleep.js'
1111
import { db } from '../db/index.js'
12-
import { Op, Sequelize } from 'sequelize'
12+
import { Op } from 'sequelize'
1313
import { nanoid } from 'nanoid'
1414
import { ct } from '../utils/createTranslate.js'
15-
import { calculationOfNumberOfTokens } from '../utils/checkTokens.js'
16-
import { REQUEST_TYPES_COST } from '../constants/index.js'
15+
import { REQUEST_TYPES } from '../constants/index.js'
16+
import { writingOffTokens } from '../utils/checkTokens.js'
1717

1818
// TODO: определять тип файла от типа в meta telegram
1919

@@ -128,12 +128,7 @@ export const onMessageDocument = async (bot, msg) => {
128128
price_tokens: settings['cost_converter']
129129
})
130130

131-
const tokenCounts = await calculationOfNumberOfTokens(' ', REQUEST_TYPES_COST.CONVERTOR)
132-
133-
await db.subscriber.update(
134-
{ tokens: Sequelize.literal(`tokens - ${tokenCounts}`) },
135-
{ where: { chat_id: msg.from.id } }
136-
)
131+
await writingOffTokens(bot, msg, REQUEST_TYPES.CONVERTOR)
137132

138133
await bot.deleteMessage(msg.from.id, msg.message.message_id).catch((error) => console.log('error dm', error))
139134
const waiting = await stepperOn(bot, msg, 0) // верочтно логичнее будет сохранить прошлое сообщение msg.message.message_id

0 commit comments

Comments
 (0)