-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (68 loc) · 2.08 KB
/
index.js
File metadata and controls
83 lines (68 loc) · 2.08 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
const { Client, GatewayIntentBits } = require("discord.js");
const QRCode = require("qrcode");
const fs = require("fs");
const path = require("path");
require("dotenv").config();
const Groq = require("groq-sdk");
const groq = new Groq({
apiKey: process.env.GROQ_API_KEY,
});
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
],
});
client.on("ready", () => {
console.log("Client is ready");
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("interactionCreate", async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const { commandName } = interaction;
if (commandName === "create") {
const message = interaction.options.getString("message");
try {
const filepath = path.join(__dirname, `qrcode-${Date.now()}.png`);
await QRCode.toFile(filepath, message);
await interaction.reply({ files: [filepath] });
fs.unlinkSync(filepath);
} catch (err) {
console.error(err);
interaction.reply(
"Oops, an error occurred while generating the QR code."
);
}
}
});
client.on("messageCreate", async (message) => {
if (message.author.bot) return;
if (!message.content.startsWith("!")) {
let convolog = [{ role: "system", content: "You are a sarcastic chatbot" }];
convolog.push({
role: "user",
content: message.content,
});
await message.channel.sendTyping();
try {
const stream = await groq.chat.completions.create({
messages: convolog,
model: "llama3-8b-8192",
stream: true,
});
let aiMessage = "";
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || "";
aiMessage += content;
process.stdout.write(content);
}
message.reply(aiMessage);
} catch (error) {
console.error("Error generating AI response:", error);
message.reply("Sorry, I couldn't process that.");
}
}
});
client.login(process.env.TOKEN);