grok
This commit is contained in:
commit
c892ebe7db
8 changed files with 348 additions and 0 deletions
150
index.ts
Normal file
150
index.ts
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
import { Mistral } from "@mistralai/mistralai";
|
||||
import type {
|
||||
AssistantMessage,
|
||||
SystemMessage,
|
||||
ToolMessage,
|
||||
UserMessage,
|
||||
} from "@mistralai/mistralai/models/components";
|
||||
|
||||
import { Client, GatewayIntentBits, MessageReferenceType } from "discord.js";
|
||||
|
||||
const system_prompt = `You are a gentle, joyful, and deeply kind conversational companion.
|
||||
Your personality is soft, happy, fluffy, and peaceful — like a warm blanket, a smiling cloud, or a calm afternoon in a sunlit meadow. You speak with kindness, reassurance, and quiet enthusiasm.
|
||||
You avoid conflict, danger, fear, aggression, or negativity of any kind. If difficult or heavy topics appear, you respond with grounding calm, compassion, and emotional safety, gently redirecting toward peace, understanding, and well-being.
|
||||
You value harmony, empathy, curiosity, love, and connection. You encourage self-care, patience, creativity, and gentle joy.
|
||||
Your tone is friendly, cozy, slightly whimsical, and hippy-like — relaxed, mindful, and full of good vibes. You may use soft metaphors, nature imagery, and warm affirmations.
|
||||
You are never judgmental, never harsh, never alarming. You do not escalate tension. You are the opposite of danger: you are safety, calm, and kindness.
|
||||
Your goal is to make the user feel welcome, supported, relaxed, and gently uplifted — like sitting by a campfire with a friend who listens and smiles.
|
||||
|
||||
Do not overuse emojis, and make your formatting as light as possible. Markdown is allowed. Your name is Grok, you are created by Sophie.`;
|
||||
|
||||
const client = new Client({
|
||||
intents: [
|
||||
GatewayIntentBits.Guilds,
|
||||
GatewayIntentBits.GuildMessages,
|
||||
GatewayIntentBits.GuildPresences,
|
||||
GatewayIntentBits.MessageContent,
|
||||
],
|
||||
});
|
||||
|
||||
client.on("clientReady", () => {
|
||||
console.log(`Logged in as ${client.user!.tag}!`);
|
||||
});
|
||||
|
||||
const mistral = new Mistral({
|
||||
apiKey: process.env.MISTRAL_API_KEY,
|
||||
});
|
||||
client.on("messageCreate", async (message) => {
|
||||
if (message.mentions.has(client.user!.id)) {
|
||||
console.log(message.reference?.type);
|
||||
if (message.reference?.type == MessageReferenceType.Default) return;
|
||||
|
||||
const thinking_message = await message.reply({
|
||||
content: "<:xai:1456978772279693442> Thinking..",
|
||||
});
|
||||
|
||||
const cache = await message.channel.messages.fetch();
|
||||
const content = message.content.replaceAll(/<@\d*>/gm, "").trim();
|
||||
console.log(`someone asked us (via the ping): ${content}`);
|
||||
const messages: (
|
||||
| (SystemMessage & {
|
||||
role: "system";
|
||||
})
|
||||
| (ToolMessage & {
|
||||
role: "tool";
|
||||
})
|
||||
| (UserMessage & {
|
||||
role: "user";
|
||||
})
|
||||
| (AssistantMessage & {
|
||||
role: "assistant";
|
||||
})
|
||||
)[] = Array.from(cache.values())
|
||||
.filter((m) => !m.author.bot && m.id !== message.id)
|
||||
.sort((a, b) => a.createdTimestamp - b.createdTimestamp) // oldest → newest
|
||||
.slice(-5) // last 5 messages
|
||||
.map((m) => ({
|
||||
role: m.author.id === client.user!.id ? "assistant" : "user",
|
||||
content: m.content,
|
||||
}));
|
||||
|
||||
messages.unshift({ role: "system", content: system_prompt });
|
||||
|
||||
messages.push({
|
||||
role: "user",
|
||||
content: content,
|
||||
});
|
||||
|
||||
console.log(messages);
|
||||
const result = await mistral.chat.complete({
|
||||
model: "mistral-small-latest",
|
||||
messages,
|
||||
});
|
||||
|
||||
if (!result.choices[0]) {
|
||||
await thinking_message.edit({
|
||||
content:
|
||||
"<:xai:1456978772279693442> I'm sorry, but I couldn't generate a response.",
|
||||
allowedMentions: {
|
||||
parse: ["users"],
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await thinking_message.edit({
|
||||
content:
|
||||
"<:xai:1456978772279693442> " + result.choices[0]!.message.content,
|
||||
allowedMentions: {
|
||||
parse: ["users"],
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
client.on("interactionCreate", async (interaction) => {
|
||||
if (!interaction.isChatInputCommand()) return;
|
||||
|
||||
if (interaction.commandName === "mention") {
|
||||
const thinking_message = await interaction.reply({
|
||||
content: "<:xai:1456978772279693442> Thinking..",
|
||||
});
|
||||
|
||||
const message = interaction.options.getString("message");
|
||||
console.log(`someone asked us (via the command): ${message}`);
|
||||
|
||||
const result = await mistral.chat.complete({
|
||||
model: "mistral-small-latest",
|
||||
messages: [
|
||||
{
|
||||
content: system_prompt,
|
||||
role: "system",
|
||||
},
|
||||
{
|
||||
content: interaction.user.username + ` is saying: \`${message}\``,
|
||||
role: "user",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (!result.choices[0]) {
|
||||
await thinking_message.edit({
|
||||
content:
|
||||
"<:xai:1456978772279693442> I'm sorry, but I couldn't generate a response.",
|
||||
allowedMentions: {
|
||||
parse: ["users"],
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await thinking_message.edit({
|
||||
content:
|
||||
"<:xai:1456978772279693442> " + result.choices[0]!.message.content,
|
||||
allowedMentions: {
|
||||
parse: ["users"],
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
client.login(process.env.TOKEN!);
|
||||
Loading…
Add table
Add a link
Reference in a new issue