-
Notifications
You must be signed in to change notification settings - Fork 482
/
Copy pathsansekai.js
144 lines (133 loc) · 6.41 KB
/
sansekai.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const { BufferJSON, WA_DEFAULT_EPHEMERAL, generateWAMessageFromContent, proto, generateWAMessageContent, generateWAMessage, prepareWAMessageMedia, areJidsSameUser, getContentType } = require("@whiskeysockets/baileys");
const fs = require("fs");
const util = require("util");
const chalk = require("chalk");
const OpenAI = require("openai");
let setting = require("./key.json");
const openai = new OpenAI({ apiKey: setting.keyopenai });
module.exports = sansekai = async (upsert, sock, store, message) => {
try {
let budy = (typeof message.text == 'string' ? message.text : '')
// var prefix = /^[\\/!#.]/gi.test(body) ? body.match(/^[\\/!#.]/gi) : "/"
var prefix = /^[\\/!#.]/gi.test(budy) ? budy.match(/^[\\/!#.]/gi) : "/";
const isCmd = budy.startsWith(prefix);
const command = budy.replace(prefix, "").trim().split(/ +/).shift().toLowerCase();
const args = budy.trim().split(/ +/).slice(1);
const pushname = message.pushName || "No Name";
const botNumber = sock.user.id;
const itsMe = message.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 = message.chat;
const color = (text, color) => {
return !color ? chalk.green(text) : chalk.keyword(color)(text);
};
// Group
const groupMetadata = message.isGroup ? await sock.groupMetadata(message.chat).catch((e) => {}) : "";
const groupName = message.isGroup ? groupMetadata.subject : "";
// Push Message To Console
let argsLog = budy.length > 30 ? `${q.substring(0, 30)}...` : budy;
if (isCmd && !message.isGroup) {
console.log(chalk.black(chalk.bgWhite("[ LOGS ]")), color(argsLog, "turquoise"), chalk.magenta("From"), chalk.green(pushname), chalk.yellow(`[ ${message.sender.replace("@s.whatsapp.net", "")} ]`));
} else if (isCmd && message.isGroup) {
console.log(
chalk.black(chalk.bgWhite("[ LOGS ]")),
color(argsLog, "turquoise"),
chalk.magenta("From"),
chalk.green(pushname),
chalk.yellow(`[ ${message.sender.replace("@s.whatsapp.net", "")} ]`),
chalk.blueBright("IN"),
chalk.green(groupName)
);
}
if (isCmd) {
switch (command) {
case "help": case "menu": case "start": case "info":
message.reply(`*Whatsapp Bot OpenAI*
*(ChatGPT)*
Cmd: ${prefix}ai
Tanyakan apa saja kepada AI.
*(DALL-E)*
Cmd: ${prefix}img
Membuat gambar dari teks
*(Source Code Bot)*
Cmd: ${prefix}sc
Menampilkan source code bot yang dipakai`)
break;
case "ai": case "openai": case "chatgpt": case "ask":
try {
// tidak perlu diisi apikeynya disini, karena sudah diisi di file key.json
if (setting.keyopenai === "ISI_APIKEY_OPENAI_DISINI") return message.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 message.reply(`Chat dengan AI.\n\nContoh:\n${prefix}${command} Apa itu resesi`);
const chatCompletion = await openai.chat.completions.create({
messages: [{ role: 'user', content: q }],
model: 'gpt-3.5-turbo'
});
await message.reply(chatCompletion.choices[0].message.content);
} catch (error) {
if (error.response) {
console.log(error.response.status);
console.log(error.response.data);
} else {
console.log(error);
message.reply("Maaf, sepertinya ada yang error :"+ error.message);
}
}
break;
case "img": case "ai-img": case "image": case "images": case "dall-e": case "dalle":
try {
// tidak perlu diisi apikeynya disini, karena sudah diisi di file key.json
if (setting.keyopenai === "ISI_APIKEY_OPENAI_DISINI") return message.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 message.reply(`Membuat gambar dari AI.\n\nContoh:\n${prefix}${command} Wooden house on snow mountain`);
const image = await openai.images.generate({
model: "dall-e-3",
prompt: q,
n: 1,
size: '1024x1024'
});
//console.log(response.data.data[0].url) // see the response
sock.sendMessage(from,
{ image: { url: image.data[0].url }, caption: "DALE-E" },
{ quoted: message, ephemeralExpiration: message.contextInfo.expiration });
} catch (error) {
if (error.response) {
console.log(error.response.status);
console.log(error.response.data);
console.log(`${error.response.status}\n\n${error.response.data}`);
} else {
console.log(error);
message.reply("Maaf, sepertinya ada yang error :"+ error.message);
}
}
break;
case "sc": case "script": case "scbot":
message.reply("Bot ini menggunakan script dari https://github.com/Sansekai/Wa-OpenAI");
break
default: {
if (isCmd && budy.toLowerCase() != undefined) {
if (message.chat.endsWith("broadcast")) return;
if (message.isBaileys) return;
if (!budy.toLowerCase()) return;
if (argsLog || (isCmd && !message.isGroup)) {
// sock.sendReadReceipt(message.chat, message.sender, [message.key.id])
console.log(chalk.black(chalk.bgRed("[ ERROR ]")), color("command", "turquoise"), color(`${prefix}${command}`, "turquoise"), color("tidak tersedia", "turquoise"));
} else if (argsLog || (isCmd && message.isGroup)) {
// sock.sendReadReceipt(message.chat, message.sender, [message.key.id])
console.log(chalk.black(chalk.bgRed("[ ERROR ]")), color("command", "turquoise"), color(`${prefix}${command}`, "turquoise"), color("tidak tersedia", "turquoise"));
}
}
}
}
}
} catch (err) {
message.reply(util.format(err));
}
};
let file = require.resolve(__filename);
fs.watchFile(file, () => {
fs.unwatchFile(file);
console.log(chalk.redBright(`Update ${__filename}`));
delete require.cache[file];
require(file);
});