-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (50 loc) · 1.5 KB
/
index.js
File metadata and controls
62 lines (50 loc) · 1.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
require("dotenv").config();
const { Client, GatewayIntentBits } = require("discord.js");
const { GoogleGenerativeAI } = require("@google/generative-ai");
const MODEL_NAME = "gemini-pro";
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
client.on("ready", () => {
console.log("Bot is ready!");
});
client.on("messageCreate", async (message) => {
if (message.author.bot) return;
if (message.mentions.has(client.user)) {
const userMessage = message.content
.replace(`<@!${client.user.id}>`, "")
.trim();
const model = genAI.getGenerativeModel({ model: MODEL_NAME });
const generationConfig = {
temperature: 0.9,
topK: 1,
topP: 1,
maxOutputTokens: 2048,
};
const parts = [
{
text: `input: ${userMessage}`,
},
];
const result = await model.generateContent({
contents: [{ role: "user", parts }],
generationConfig,
});
const reply = await result.response.text();
// due to Discord limitations, we can only send 2000 characters at a time, so we need to split the message
if (reply.length > 2000) {
const replyArray = reply.match(/[\s\S]{1,2000}/g);
replyArray.forEach(async (msg) => {
await message.reply(msg);
});
return;
}
message.reply(reply);
}
});
client.login(process.env.DISCORD_API_KEY);