Skip to content

Commit 2dd033e

Browse files
committed
fix(*): bugs
1 parent 1ac3180 commit 2dd033e

15 files changed

+72
-36
lines changed

bot/commands/admin/getId.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const getId = bot => {
1010
parse_mode: 'HTML',
1111
reply_to_message_id: msgId
1212
}
13-
msg['ctx'] = INITIAL_SESSION
13+
1414
try {
1515
await bot.sendMessage(
1616
chatId,

bot/commands/admin/refundTokensIfError.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const refundTokensIfError = bot => {
1414
parse_mode: 'HTML',
1515
reply_to_message_id: msgId
1616
}
17-
msg['ctx'] = INITIAL_SESSION
17+
1818
try {
1919
const errors = await db.convertor_requests.findAll({ where: { [Op.or]: [{ status: 'work' }, { status: 'error' }] } })
2020

bot/commands/admin/setQuizModeForSubs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const setQuizModeForSubs = (bot) => {
1515
const options = {
1616
parse_mode: 'HTML'
1717
}
18-
msg['ctx'] = INITIAL_SESSION
18+
1919
try {
2020
await db.subscriber.update(
2121
{ quiz_subs_available, quiz_token_available },

bot/commands/keyboard/chat_gpt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export const keyboardChatGPT = async (bot, msg) => {
9999
const options = {
100100
parse_mode: 'HTML',
101101
reply_to_message_id: msgId,
102-
reply_markup: await createStartKeyboardForReplyMarkup(msg)
102+
reply_markup: createStartKeyboardForReplyMarkup(msg)
103103
}
104104
try {
105105
db.subscriber.findOne({

bot/commands/keyboard/converter.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export const keyboardConverter = async (bot, msg) => {
1717
chat_id: msg.chat.id,
1818
message_id: message_id,
1919
parse_mode: 'HTML',
20-
reply_markup: await createStartKeyboardForReplyMarkup(msg)
2120
}).catch(() => {
2221
return true
2322
})

bot/commands/keyboard/dalle.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ export const keyboardDalle = async (bot, msg) => {
6565
const options = {
6666
parse_mode: 'HTML',
6767
reply_to_message_id: msgId,
68-
reply_markup: await createStartKeyboardForReplyMarkup(msg)
6968
}
7069
try {
7170
db.subscriber.findOne({

bot/commands/keyboard/help.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import { createStartKeyboardForReplyMarkup } from '../../utils/createStartKeyboard.js'
2+
13
export const keyboardHelp = async (bot, msg, t) => {
24
let accountMessage
35
const { id: chatId } = msg.chat
46
const msgId = msg.message_id
57
const options = {
68
parse_mode: 'HTML',
7-
reply_to_message_id: msgId
9+
reply_to_message_id: msgId,
10+
reply_markup: createStartKeyboardForReplyMarkup(msg)
811
}
912

1013
try {

bot/commands/keyboard/my_account.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const keyboardMyAccount = async (bot, msg, prevMessageForEdit, prevLevel,
3131
parse_mode: 'HTML',
3232
reply_to_message_id: msgId,
3333
disable_web_page_preview: true,
34-
reply_markup: await createStartKeyboardForReplyMarkup(msg)
34+
reply_markup: createStartKeyboardForReplyMarkup(msg)
3535
}
3636
try {
3737
const inlineKeyboard = [
@@ -42,7 +42,6 @@ export const keyboardMyAccount = async (bot, msg, prevMessageForEdit, prevLevel,
4242
const prevKeyboard = [{ text: t('prev_component'), callback_data: `prev_component_${msgId}` }] // только если prevLevel
4343
const referralLevel = await referralLevelCreator(msg, generalOptions, msgId, 'my_account')
4444
const eventEmitter = new events.EventEmitter()
45-
msg['ctx'] = INITIAL_SESSION
4645

4746
if (prevLevel)
4847
inlineKeyboard.push(prevKeyboard)

bot/commands/modes/chatGPT.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@ import { createFullName } from '../../utils/createFullName.js'
99
import { ct } from '../../utils/createTranslate.js'
1010
import { writingOffTokens } from '../../utils/checkTokens.js'
1111

12+
export async function cleanContext (chatID) {
13+
await db.subscriber.update(
14+
{ comment: null },
15+
{ where: { chat_id: chatID } }
16+
)
17+
}
18+
1219
export const modeChatGPT = async (bot, msg, qweryOptions) => {
1320
const t = await ct(msg)
1421
let res
1522
let modeGPT
1623
let newMessage
24+
let ctx
1725
const { id: userId } = msg.from
1826
const { id: chatID } = msg.chat
1927
const msgId = msg.message_id
@@ -23,17 +31,20 @@ export const modeChatGPT = async (bot, msg, qweryOptions) => {
2331
}
2432

2533
try {
26-
db.subscriber.findOne({
34+
await db.subscriber.findOne({
2735
where: {
2836
chat_id: chatID,
2937
user_id: msg.from.id
3038
}
3139
}).then(async response => {
3240
modeGPT = response.dataValues.modeGPT
41+
ctx = await JSON.parse(response.dataValues.comment)
3342
})
3443

3544
// TODO: Запоминать контекст беседы пользователя или всегда начинать новый чат
36-
msg.ctx ??= INITIAL_SESSION
45+
ctx ??= INITIAL_SESSION
46+
47+
console.log('🔺ctx', ctx.messages.length)
3748

3849
res = await spinnerOn(bot, chatID, null, 'chatGPT')
3950
let message = await bot.sendMessage(chatID, '...').catch(() => {
@@ -47,20 +58,20 @@ export const modeChatGPT = async (bot, msg, qweryOptions) => {
4758

4859
if (modeGPT === 'assistant') {
4960
newMessage = msg.text ?? msg.sticker?.emoji
50-
msg.ctx = INITIAL_SESSION
61+
await cleanContext(chatID)
5162
} else if (msg.text) {
5263
newMessage = await t(x?.prompt_start)
5364
newMessage = newMessage + '\n\n' + msg.text
5465
} else {
5566
newMessage = msg.sticker.emoji
5667
}
5768

58-
await msg?.ctx.messages.push({
69+
ctx.messages.push({
5970
role: openAi.roles.User,
6071
content: newMessage
6172
})
6273

63-
const response = await openAi.chat(msg?.ctx.messages, bot, message, chatID, x.parse_mode)
74+
const response = await openAi.chat(ctx.messages, bot, message, chatID, x.parse_mode)
6475

6576
const textSum = (response + newMessage)
6677

@@ -70,11 +81,16 @@ export const modeChatGPT = async (bot, msg, qweryOptions) => {
7081
throw new Error('Something went wrong please try again.')
7182
}
7283

73-
msg?.ctx.messages.push({
84+
ctx.messages.push({
7485
role: openAi.roles.Assistant,
7586
content: response
7687
})
7788

89+
await db.subscriber.update(
90+
{ comment: JSON.stringify(ctx) },
91+
{ where: { chat_id: chatID } }
92+
)
93+
7894
await spinnerOff(bot, chatID, res)
7995

8096
db.history.update({

bot/commands/onMessageTextDefault.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import { modeChatGPT } from './modes/chatGPT.js'
1+
import { cleanContext, modeChatGPT } from './modes/chatGPT.js'
22
import events from 'events'
33
import { db } from '../db/index.js'
44
import { removeQueryFromPrevMessage } from './hoc/removeQueryFromPrevMsg.js'
55
import { modesChatGPT } from '../constants/modes.js'
6+
import { INITIAL_SESSION } from '../constants/index.js'
7+
import { autoRemoveMessage } from './hoc/autoRemoveMessage.js'
8+
import { keyboardChatGPT } from './keyboard/chat_gpt.js'
69

710
export const onMessageTextDefault = async (bot, msg, match, sudoUser, t) => {
811
const { id: chatID } = msg.chat
@@ -24,8 +27,8 @@ export const onMessageTextDefault = async (bot, msg, match, sudoUser, t) => {
2427
...optionsGeneral,
2528
reply_markup: {
2629
inline_keyboard: [
27-
[{ text: t('btn_new_chat'), callback_data: 'create_new_chat' },
28-
{ text: t('btn_change_mode'), callback_data: 'change_chat_mode' }]
30+
[{ text: t('btn_new_chat'), callback_data: `create_new_chat${msgId}` },
31+
{ text: t('btn_change_mode'), callback_data: `change_chat_mode${msgId}` }]
2932
]
3033
}
3134
}
@@ -41,14 +44,19 @@ export const onMessageTextDefault = async (bot, msg, match, sudoUser, t) => {
4144

4245
const eventEmitter = new events.EventEmitter()
4346

44-
eventEmitter.on('change_chat_mode', async function() {
47+
eventEmitter.on(`create_new_chat${msgId}`, async function() {
48+
await autoRemoveMessage('✅ ' + t('btn_new_chat'), bot, chatID, {},10000)
49+
await cleanContext(chatID)
50+
})
51+
52+
eventEmitter.on(`change_chat_mode${msgId}`, async function() {
4553
await bot.editMessageText(
4654
firstMessage.text,
4755
{
4856
message_id: firstMessage.message_id,
4957
chat_id: chatID,
5058
reply_markup: {
51-
inline_keyboard: modesChatGPT.map((mode) => [{ text: mode.name, callback_data: mode.code }])
59+
inline_keyboard: modesChatGPT.map((mode) => [{ text: t(mode.name), callback_data: mode.code }])
5260
}
5361
}
5462
).catch((err) => {
@@ -76,7 +84,8 @@ export const onMessageTextDefault = async (bot, msg, match, sudoUser, t) => {
7684
await db.subscriber.update(
7785
{ modeGPT: modesChatGPT[i].code },
7886
{ where: { chat_id: chatID } }
79-
).then(res => {
87+
).then(async res => {
88+
await removeQueryFromPrevMessage(bot, msg.chat.id, firstMessage)
8089
// bot.deleteMessage(chatID, firstMessage.message_id).catch(err => console.error(err))
8190
firstMessage = modeChatGPT(bot, msg, {
8291
message_id: firstMessage.message_id,

0 commit comments

Comments
 (0)