fix(bots/discord): fix get response logic

This commit is contained in:
PalmDevs
2024-09-22 04:01:09 +07:00
parent f035994f9e
commit 3261294822

View File

@@ -1,6 +1,7 @@
import { type Response, responses } from '$/database/schemas' import { type Response, responses } from '$/database/schemas'
import type { Config, ConfigMessageScanResponse, ConfigMessageScanResponseLabelConfig } from 'config.schema' import type { Config, ConfigMessageScanResponse, ConfigMessageScanResponseLabelConfig } from 'config.schema'
import type { Message, PartialUser, User } from 'discord.js' import { ButtonStyle, ComponentType } from 'discord.js'
import type { APIActionRowComponent, APIButtonComponent, Message, PartialUser, User } from 'discord.js'
import { eq } from 'drizzle-orm' import { eq } from 'drizzle-orm'
import { createMessageScanResponseEmbed } from './embeds' import { createMessageScanResponseEmbed } from './embeds'
@@ -54,6 +55,8 @@ export const getResponseFromText = async (
break break
} }
} }
}
}
// If none of the regexes match, we can search for labels immediately // If none of the regexes match, we can search for labels immediately
if (!responseConfig.triggers && !flags.textRegexesOnly) { if (!responseConfig.triggers && !flags.textRegexesOnly) {
@@ -61,15 +64,12 @@ export const getResponseFromText = async (
const scan = await api.client.parseText(content) const scan = await api.client.parseText(content)
if (scan.labels.length) { if (scan.labels.length) {
const matchedLabel = scan.labels[0]! const matchedLabel = scan.labels[0]!
logger.debug( logger.debug(`Message matched label with confidence: ${matchedLabel.name}, ${matchedLabel.confidence}`)
`Message matched label with confidence: ${matchedLabel.name}, ${matchedLabel.confidence}`,
)
let trigger: ConfigMessageScanResponseLabelConfig | undefined let trigger: ConfigMessageScanResponseLabelConfig | undefined
const response = responses.find(x => { const response = responses.find(x => {
const config = x.triggers.text!.find( const config = x.triggers.text!.find(
(x): x is ConfigMessageScanResponseLabelConfig => (x): x is ConfigMessageScanResponseLabelConfig => 'label' in x && x.label === matchedLabel.name,
'label' in x && x.label === matchedLabel.name,
) )
if (config) trigger = config if (config) trigger = config
return config return config
@@ -110,8 +110,6 @@ export const getResponseFromText = async (
} }
} }
} }
}
}
return responseConfig return responseConfig
} }
@@ -163,14 +161,41 @@ export const handleUserResponseCorrection = async (
}) })
.where(eq(responses.replyId, response.replyId)) .where(eq(responses.replyId, response.replyId))
await reply.edit({ return void (await reply.edit({
...correctLabelResponse.response, ...correctLabelResponse.response,
embeds: correctLabelResponse.response.embeds?.map(createMessageScanResponseEmbed), embeds: correctLabelResponse.response.embeds?.map(createMessageScanResponseEmbed),
}) components: [],
}))
} }
await api.client.trainMessage(response.content, label) await api.client.trainMessage(response.content, label)
logger.debug(`User ${user.id} trained message ${response.replyId} as ${label} (positive)`) logger.debug(`User ${user.id} trained message ${response.replyId} as ${label} (positive)`)
await reply.reactions.removeAll() await reply.edit({
components: [],
})
} }
export const createMessageScanResponseComponents = (reply: Message<true>) => [
{
type: ComponentType.ActionRow,
components: [
{
type: ComponentType.Button,
style: ButtonStyle.Secondary,
emoji: {
id: '👍',
},
custom_id: `train:${reply.id}`,
},
{
type: ComponentType.Button,
style: ButtonStyle.Secondary,
emoji: {
id: '🔧',
},
custom_id: `edit:${reply.id}`,
},
],
} as APIActionRowComponent<APIButtonComponent>,
]