-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.mjs
125 lines (100 loc) · 3.55 KB
/
main.mjs
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
import { Configuration, OpenAIApi } from "openai"
import axios from "axios"
import { Client, IntentsBitField } from "discord.js";
// https://script.google.com/macros/s/AKfycbyT5XyWpHjbAb5gZsw3fMImWtxxQTqFO6-b5w5wYiaQaplt_f443lgVh7YrE7R_Uuoa/exec
const SYS_API = process.env.GAS_URL
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
let sysprompt = ""
const client = new Client({
intents:
[IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
IntentsBitField.Flags.GuildMessageReactions,
IntentsBitField.Flags.GuildVoiceStates
]
});
client.on("ready", async (args) => {
console.log("ready")
})
client.on("messageCreate", async (msg) => {
// console.log("msg!", msg.content)
if (msg.author.bot) return
if (msg.channel.id !== process.env.DISCORD_CHANNEL) return
if (msg.content.startsWith("!") || msg.content.startsWith("!")) return
const nickname = msg.member?.nickname || msg.author.username;
if (msg.reference) {
const replyChain = await getReplyChain(msg);
// msg.content = "発言者:" + nickname + " 発言内容:" + msg.content
const retult = await generateReplyWithRef([...replyChain, msg])
msg.reply(retult)
return
}
// const result = await generateReply("発言者:" + nickname + " 発言内容:" + msg.content)
const result = await generateReply(msg.content)
msg.reply(result);
});
client.login(process.env.DISCORD_TOKEN)
async function getReplyChain(message) {
const replyChain = [];
let currentMessage = message;
while (currentMessage.reference) {
try {
const referencedMessage = await currentMessage.channel.messages.fetch(currentMessage.reference.messageId);
replyChain.unshift(referencedMessage);
currentMessage = referencedMessage;
} catch (error) {
console.error('Error fetching message:', error);
break;
}
}
return replyChain;
}
const openai = new OpenAIApi(configuration);
const getSysPrompt = async () => {
const { data } = await axios.get(SYS_API)
sysprompt = data
return data
}
const generateReply = async (user) => {
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
{ "role": "system", "content": await getSysPrompt() },
{ "role": "user", "content": user }
],
});
var reply = completion.data.choices[0].message.content;
console.log(reply.slice(0, 10), completion.data.usage.total_tokens)
return reply
}
const generateReplyWithRef = async (prompt) => {
const msgs = [
{ "role": "system", "content": await getSysPrompt() }
]
if (prompt.length >= 6) {
prompt = prompt.slice(-6)
}
prompt.forEach(p => {
if (p.author.id === client?.user?.id) {
msgs.push({
"role": "assistant",
"content": p.content
})
} else {
msgs.push({
"role": "user",
"content": p.content
})
}
})
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: msgs,
});
const reply = completion.data.choices[0].message.content;
console.log(reply.slice(0, 10), completion.data.usage.total_tokens)
return reply
}