Skip to content

Commit

Permalink
🆙 Upgrade ask command
Browse files Browse the repository at this point in the history
  • Loading branch information
Maseshi committed May 18, 2024
1 parent ccf896d commit 2ed4c6a
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions source/commands/me/ask.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js')
const { chatPage } = require('../../utils/browserUtils')
const { getDatabase, ref, get } = require('firebase/database')

module.exports = {
permissions: [PermissionFlagsBits.SendMessages],
Expand All @@ -26,15 +26,40 @@ module.exports = {

await interaction.deferReply()

const response = await chatPage(inputPrompt, 'ask')
const token = interaction.client.configs.openai.apiKey

if (response.status !== 200)
return interaction.editReply(
'❎ เอิ่มม...ตอนนี้ฉันยังไม่ว่าง ไว้รอฉันว่างแล้วถามฉันใหม่อีกรอบในครั้งหน้าละกันนะ'
if (!token)
return await interaction.editReply(
interaction.client.i18n.t('commands.ask.key_not_exsist')
)

const result = response.result
const chatRef = ref(getDatabase(), 'chat')
const chatSnapshot = await get(chatRef)
const clientUsername = interaction.client.user.username
const userData = JSON.stringify(interaction.user.toJSON())
const systemMessage = [
chatSnapshot.val().system || `Your are ${clientUsername}`,
`Here is the user's information on Discord: ${userData}.`,
].join(' ')

await interaction.editReply(`\`\`\`${result}\`\`\``)
try {
const chatCompletion =
await interaction.client.ai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [
{
role: 'system',
content: systemMessage,
},
{ role: 'user', content: inputPrompt },
],
})

await interaction.editReply(chatCompletion.choices[0].message.content)
} catch (error) {
await interaction.editReply(
interaction.client.i18n.t('commands.ask.can_not_answer_at_this_time')
)
}
},
}

0 comments on commit 2ed4c6a

Please sign in to comment.