From 5eae935b3b0b8176f93ead710f5b6465d65a4b00 Mon Sep 17 00:00:00 2001 From: Sansekai Date: Sat, 17 Dec 2022 15:46:23 +0800 Subject: [PATCH] Add type command & auto chat --- index.js | 2 +- key.json | 7 ++--- sansekai.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 74 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index feea08d..548fe88 100644 --- a/index.js +++ b/index.js @@ -232,7 +232,7 @@ async function startHisoka() { else { console.log(`Unknown DisconnectReason: ${reason}|${connection}`); startHisoka(); } } else if(connection === 'open') { console.log('Bot conneted to server') - client.sendMessage(owner+'@s.whatsapp.net', { text: `Bot started!\n\njangan lupa supportnya ya bang :)\n${donet}` }) + client.sendMessage(owner+'@s.whatsapp.net', { text: `Bot started!\n\njangan lupa support ya bang :)\n${donet}` }) } // console.log('Connected...', update) }) diff --git a/key.json b/key.json index 2dffd6b..3174f8e 100644 --- a/key.json +++ b/key.json @@ -1,4 +1,5 @@ { - "keyopenai": "ISI_APIKEY_OPENAI_DISINI", - "donasi": "jangan lupa supportnya ya bang :) https://saweria.co/sansekai" -} + "keyopenai": "ISI_APIKEY_OPENAI_DISINI", + "autoAI": true, + "donasi": "jangan lupa support ya bang :) https://saweria.co/sansekai" +} \ No newline at end of file diff --git a/sansekai.js b/sansekai.js index 75d90a5..a7eb7ff 100644 --- a/sansekai.js +++ b/sansekai.js @@ -3,18 +3,23 @@ const fs = require('fs') const util = require('util') const chalk = require('chalk') const { Configuration, OpenAIApi } = require("openai") -const setting = require('./key.json') +let setting = require('./key.json') module.exports = sansekai = async (client, m, chatUpdate, store) => { try { var body = (m.mtype === 'conversation') ? m.message.conversation : (m.mtype == 'imageMessage') ? m.message.imageMessage.caption : (m.mtype == 'videoMessage') ? m.message.videoMessage.caption : (m.mtype == 'extendedTextMessage') ? m.message.extendedTextMessage.text : (m.mtype == 'buttonsResponseMessage') ? m.message.buttonsResponseMessage.selectedButtonId : (m.mtype == 'listResponseMessage') ? m.message.listResponseMessage.singleSelectReply.selectedRowId : (m.mtype == 'templateButtonReplyMessage') ? m.message.templateButtonReplyMessage.selectedId : (m.mtype === 'messageContextInfo') ? (m.message.buttonsResponseMessage?.selectedButtonId || m.message.listResponseMessage?.singleSelectReply.selectedRowId || m.text) : '' var budy = (typeof m.text == 'string' ? m.text : '') + // var prefix = /^[\\/!#.]/gi.test(body) ? body.match(/^[\\/!#.]/gi) : "/" + var prefix = /^[\\/!#.]/gi.test(body) ? body.match(/^[\\/!#.]/gi) : "/" + const isCmd2 = body.startsWith(prefix) + const command = body.replace(prefix, '').trim().split(/ +/).shift().toLowerCase() const args = body.trim().split(/ +/).slice(1) const pushname = m.pushName || "No Name" const botNumber = await client.decodeJid(client.user.id) const itsMe = m.sender == botNumber ? true : false let text = q = args.join(" ") const arg = budy.trim().substring(budy.indexOf(' ') + 1 ) + const arg1 = arg.trim().substring(arg.indexOf(' ') + 1 ) const from = m.chat const reply = m.reply @@ -32,15 +37,24 @@ module.exports = sansekai = async (client, m, chatUpdate, store) => { // Push Message To Console let argsLog = (budy.length > 30) ? `${q.substring(0, 30)}...` : budy - // Push Message To Console && Auto Read - if (argsLog && !m.isGroup) { - // client.sendReadReceipt(m.chat, m.sender, [m.key.id]) - console.log(chalk.black(chalk.bgWhite('[ LOGS ]')), color(argsLog, 'turquoise'), chalk.magenta('From'), chalk.green(pushname), chalk.yellow(`[ ${m.sender.replace('@s.whatsapp.net', '')} ]`)) - } else if (argsLog && m.isGroup) { - // client.sendReadReceipt(m.chat, m.sender, [m.key.id]) - console.log(chalk.black(chalk.bgWhite('[ LOGS ]')), color(argsLog, 'turquoise'), chalk.magenta('From'), chalk.green(pushname), chalk.yellow(`[ ${m.sender.replace('@s.whatsapp.net', '')} ]`), chalk.blueBright('IN'), chalk.green(groupName)) + if (setting.autoAI) { + // Push Message To Console && Auto Read + if (argsLog && !m.isGroup) { + // client.sendReadReceipt(m.chat, m.sender, [m.key.id]) + console.log(chalk.black(chalk.bgWhite('[ LOGS ]')), color(argsLog, 'turquoise'), chalk.magenta('From'), chalk.green(pushname), chalk.yellow(`[ ${m.sender.replace('@s.whatsapp.net', '')} ]`)) + } else if (argsLog && m.isGroup) { + // client.sendReadReceipt(m.chat, m.sender, [m.key.id]) + console.log(chalk.black(chalk.bgWhite('[ LOGS ]')), color(argsLog, 'turquoise'), chalk.magenta('From'), chalk.green(pushname), chalk.yellow(`[ ${m.sender.replace('@s.whatsapp.net', '')} ]`), chalk.blueBright('IN'), chalk.green(groupName)) + } + } else if (!setting.autoAI) { + if (isCmd2 && !m.isGroup) { + console.log(chalk.black(chalk.bgWhite('[ LOGS ]')), color(argsLog, 'turquoise'), chalk.magenta('From'), chalk.green(pushname), chalk.yellow(`[ ${m.sender.replace('@s.whatsapp.net', '')} ]`)) + } else if (isCmd2 && m.isGroup) { + console.log(chalk.black(chalk.bgWhite('[ LOGS ]')), color(argsLog, 'turquoise'), chalk.magenta('From'), chalk.green(pushname), chalk.yellow(`[ ${m.sender.replace('@s.whatsapp.net', '')} ]`), chalk.blueBright('IN'), chalk.green(groupName)) + } } + if (setting.autoAI) { if (budy) { try { if (setting.keyopenai === 'ISI_APIKEY_OPENAI_DISINI') return reply('Apikey belum diisi\n\nSilahkan isi terlebih dahulu apikeynya di file key.json\n\nApikeynya bisa dibuat di website: https://beta.openai.com/account/api-keys') @@ -64,6 +78,53 @@ module.exports = sansekai = async (client, m, chatUpdate, store) => { m.reply('Maaf, sepertinya ada yang error') } } + } + + if (!setting.autoAI) { + if (isCmd2) { + switch(command) { + case 'ai': + try { + if (setting.keyopenai === 'ISI_APIKEY_OPENAI_DISINI') return reply('Apikey belum diisi\n\nSilahkan isi terlebih dahulu apikeynya di file key.json\n\nApikeynya bisa dibuat di website: https://beta.openai.com/account/api-keys') + if (!text) return reply(`Chat dengan AI.\n\nContoh:\n${prefix}${command} Apa itu resesi`) + const configuration = new Configuration({ + apiKey: setting.keyopenai, + }); + const openai = new OpenAIApi(configuration); + + const response = await openai.createCompletion({ + model: "text-davinci-003", + prompt: text, + temperature: 0.3, + max_tokens: 3000, + top_p: 1.0, + frequency_penalty: 0.0, + presence_penalty: 0.0, + }); + m.reply(`${response.data.choices[0].text}\n\n`) + } catch (err) { + console.log(err) + m.reply('Maaf, sepertinya ada yang error') + } + break + default:{ + + if (isCmd2 && budy.toLowerCase() != undefined) { + if (m.chat.endsWith('broadcast')) return + if (m.isBaileys) return + if (!(budy.toLowerCase())) return + if (argsLog || isCmd2 && !m.isGroup) { + // client.sendReadReceipt(m.chat, m.sender, [m.key.id]) + console.log(chalk.black(chalk.bgRed('[ ERROR ]')), color('command', 'turquoise'), color(argsLog, 'turquoise'), color('tidak tersedia', 'turquoise')) + } else if (argsLog || isCmd2 && m.isGroup) { + // client.sendReadReceipt(m.chat, m.sender, [m.key.id]) + console.log(chalk.black(chalk.bgRed('[ ERROR ]')), color('command', 'turquoise'), color(argsLog, 'turquoise'), color('tidak tersedia', 'turquoise')) + } + } + } + } + } + } } catch (err) { m.reply(util.format(err))