This repository has been archived by the owner on May 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
92 lines (77 loc) · 2.9 KB
/
index.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
const WebSocket = require('ws')
const Discord = require('discord.js')
const fetch = require('node-fetch')
require('dotenv').config()
const client = new Discord.Client({
intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES]
})
var bot
var body
var wsLink
var processing = false
function newError(text, error) {
return new Error(`\x1b[33m${text}: \x1b[101m${error}\x1b[0m`)
}
(function () {
const socket = new WebSocket('wss://chai-959f8-default-rtdb.firebaseio.com/.ws?v=5')
try {
socket.onopen = () => {
socket.onmessage = (event) => {
wsLink = JSON.parse(event.data).d.d.h
}
}
} catch (error) {
throw newError('Error while connecting to websocket', error)
}
}())
client.once('ready', () => {
console.log(`Loaded Discord bot: ${client.user.tag}`)
try {
const socket = new WebSocket(`wss://${wsLink}/.ws?v=5&ns=chai-959f8-default-rtdb`)
socket.onopen = () => {
socket.send(JSON.stringify({ "t": "d", "d": { "r": 2, "a": "q", "b": { "p": "/botConfigs/bots/" + process.env.CHAI_BOT_ID, "h": "" } } }))
socket.onmessage = (event) => {
if (JSON.parse(event.data).d.b?.p) {
bot = JSON.parse(event.data).d.b.d
body = {
"text": `${bot.prompt}\n${bot.botLabel}: ${bot.firstMessage}`,
"temperature": bot.temperature,
"repetition_penalty": bot.repetitionPenalty,
"top_p": bot.topP,
"top_k": bot.topK,
"response_length": bot.responseLength
}
console.log(`Fetched bot data: ${bot.name}`)
}
}
}
} catch (error) {
throw newError('Error while connecting to websocket', error)
}
})
client.on('messageCreate', message => {
if (message.author.bot || message.channel.id != process.env.DISCORD_CHANNEL_ID || !bot || processing) return
processing = true
body.text += `\n${bot.userLabel}: ${message.content.slice(0, 128)}\n${bot.botLabel}:`
message.channel.sendTyping()
fetch("https://model-api-shdxwd54ta-nw.a.run.app/generate/gptj", {
"headers": {
"content-type": "application/json",
"developer_key": process.env.CHAI_DEV_KEY,
"developer_uid": process.env.CHAI_DEV_UID,
},
"body": JSON.stringify(body),
"method": "POST"
}).then(res => res.json()).then(d => {
if (d.error) throw newError('Error while fetching response', d.error.message)
body.text += d.data
message.reply({
content: d.data
}).catch(error => {
throw newError('Error while sending message', error)
}).finally(
processing = false
)
})
})
client.login(process.env.DISCORD_BOT_TOKEN)