-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
128 lines (105 loc) · 3.53 KB
/
app.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
if (process.env.NODE_ENV !== "PRODUCTION") {
require("dotenv").config({
path: __dirname + "/.env",
});
}
const { announce } = require('./commands/announce');
const { info } = require("./commands/info");
const { qrCode } = require("./commands/qrCode");
const { shorten } = require("./commands/shorten");
const client = require('./config/client');
const openai = require("./config/open_ai");
const { processPrompt } = require("./utils/processed_prompt_for_gpt");
//TODO: convert to db
const banlist = [];
var symbol = process.env.SYMBOL;
// listens to the messages of owner as well as others
client.on("message_create", async (message) => {
const splitedMessage = message.body.split(" ");
const command = splitedMessage[0];
const messageBodyArray = [];
for (var i = 1; i < splitedMessage.length; i++) {
messageBodyArray.push(splitedMessage[i]);
}
const messageBody = messageBodyArray.join(" ");
//ban
if (command === symbol + "ban" && message.fromMe) {
const chat = await message.getChat();
let id = "";
try {
id = (await message.getQuotedMessage()).author;
banlist.push(id);
await chat.sendMessage(`*User got banned!*`);
} catch (e) {
console.log(e);
}
}
//unban
if (command === symbol + "unban" && message.fromMe) {
const chat = await message.getChat();
try {
const id = (await message.getQuotedMessage()).author;
const index = banlist.indexOf(id);
if (index > -1) {
banlist.splice(index, 1);
}
await chat.sendMessage(`*User has been unbanned!*`);
} catch (e) {
console.log(e);
}
}
const id = message.author;
const isbanned = banlist.includes(id);
//announce
if (command === symbol + "announce" && !isbanned) {
announce(message, messageBody);
}
//shorten link
if (command === symbol + "shorten" && !isbanned) {
shorten(message, messageBody);
}
//info
if (command === symbol + "info" && !isbanned) {
info(message);
}
// //qr code generation
// if (command === symbol + "qr" && !isbanned) {
// qrCode(message,messageBody);
// }
});
//listens to others messages only
client.on("message", async (message) => {
const id = message.author;
const isbanned = banlist.includes(id);
const splitedMessage = message.body.split(" ");
const command = splitedMessage[0];
const messageBodyArray = [];
for (var i = 1; i < splitedMessage.length; i++) {
messageBodyArray.push(splitedMessage[i]);
}
const messageBody = messageBodyArray.join(" ");
//ping
if (command === symbol + "ping" && !isbanned) {
message.reply("pong");
}
//open ai chat bot
if (command === symbol + "chat" && !isbanned) {
const chat = await message.getChat();
const proccessedPrompt = await processPrompt(messageBody);
if (proccessedPrompt !== null) {
await chat.sendMessage(proccessedPrompt);
} else {
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: messageBody,
temperature: 0.9,
max_tokens: 150,
top_p: 1,
frequency_penalty: 0.0,
presence_penalty: 0.6,
stop: [" Human:", " AI:"],
});
await chat.sendMessage(response.data.choices[0].text);
}
}
});