-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
152 lines (134 loc) · 3.5 KB
/
index.js
File metadata and controls
152 lines (134 loc) · 3.5 KB
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
145
146
147
148
149
150
151
152
const {
Client,
GatewayIntentBits,
PermissionFlagsBits,
} = require("discord.js");
const {
GoogleGenerativeAI,
HarmCategory,
HarmBlockThreshold,
} = require("@google/generative-ai");
const fs = require("fs");
const config = require("./config.js");
const MODEL_NAME = config.model;
const noisy = [
"哇嗚!這裡要爆炸啦!",
"讓我休息一下!",
];
const safetySettings = [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
{
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
{
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
];
const generationConfig = {
temperature: config.temperature,
topK: config.topK,
topP: config.topP,
maxOutputTokens: config.maxOutputTokens,
};
let used = 0;
config.bots.map((bot) => {
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
const history = [];
client.on("messageCreate", async (message) => {
if(
config.owners.includes(message.author.id) &&
message.content.startsWith(`${bot.prefix}reset`)
) {
fs.rmSync(`./chats/${bot.name}/${message.channel.id}.json`, {
force: true,
});
history[message.channel.id] = [];
return message.reply("已重置");
}
if (message.author.bot) return;
if (
message.author.id === client.user.id ||
(!bot.channels.includes(message.channel.id) &&
((!message.mentions.users.has(client.user.id) &&
Math.random() > 0.05) ||
!message.channel
.permissionsFor(message.guild.members.me)
.has(PermissionFlagsBits.SendMessages)))
)
return;
message.channel.sendTyping();
if (!history[message.channel.id]) history[message.channel.id] = [];
const genAI = new GoogleGenerativeAI(
config.apikeys[used % config.apikeys.length]
);
used++;
const model = genAI.getGenerativeModel({
model: MODEL_NAME,
});
const chat = model.startChat({
generationConfig,
safetySettings,
history: [bot.prompt, ...history[message.channel.id]][0],
});
const result = await chat
.sendMessage(`[${message.author.username}]: ${message.content}`)
.catch((error) => {
console.error(error);
return message.reply(noisy[Math.floor(Math.random() * noisy.length)]);
});
if (!result) return;
const responseText = result.response?.text?.() || "無法生成回覆,請查看 Console。";
history[message.channel.id].push(
{
role: "user",
parts: [
{
text: `[${message.author.username}]: ${message.content}`,
},
],
},
{
role: "model",
parts: [
{
text: responseText,
},
],
}
);
if (!(history[message.channel.id].length % 10))
fs.writeFileSync(
`./chats/${bot.name}/${message.channel.id}.json`,
JSON.stringify(history[message.channel.id])
);
new Promise((resolve) => setTimeout(resolve, 1000));
return message.reply(responseText);
});
client.login(bot.token);
});
console.log("Bots are online.");
process.on("unhandledRejection", async (error) => {
console.error(error.stack || error);
});
process.on("uncaughtException", async (error) => {
console.error(error.stack);
});
process.on("uncaughtExceptionMonitor", async (error) => {
console.error(error.stack);
});