First working prototype, but it has alzheimer's
This commit is contained in:
14
src/index.ts
14
src/index.ts
@@ -0,0 +1,14 @@
|
||||
import * as Discord from "discord.js";
|
||||
import dotenv from "dotenv";
|
||||
import axios from "axios";
|
||||
import { GoogleGenAI } from "@google/genai";
|
||||
import { createShardManager } from "./utils.ts";
|
||||
import path from "path";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const shardManager = createShardManager("src/shard.ts", process.env.BOT_TOKEN, "auto", false);
|
||||
|
||||
shardManager.on("shardCreate", shard => console.log(`[ShardManager] Launched shard ${shard.id}`));
|
||||
|
||||
shardManager.spawn().catch(console.error);
|
||||
|
||||
98
src/shard.ts
Normal file
98
src/shard.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import * as Discord from "discord.js";
|
||||
import * as Utils from "@discordjs/util"
|
||||
import * as Formatters from "@discordjs/formatters"
|
||||
import * as Builders from "@discordjs/builders"
|
||||
import * as Types from "discord-api-types/v10"
|
||||
import * as fs from "node:fs"
|
||||
import dateFormat, { masks } from "dateformat";
|
||||
|
||||
|
||||
import { GoogleGenAI } from "@google/genai";
|
||||
import EventEmitter from "events";
|
||||
|
||||
const Client:Discord.Client = new Discord.Client({
|
||||
intents: ["MessageContent", "Guilds", "GuildMessages", "GuildMembers","GuildMessageReactions","GuildPresences","GuildVoiceStates","GuildIntegrations","GuildEmojisAndStickers","GuildInvites","GuildExpressions","GuildScheduledEvents","GuildMessageTyping","GuildMessagePolls"],
|
||||
ws: { properties: { $browser: "Discord iOS" }},
|
||||
presence: {status: "online", activities:
|
||||
[{
|
||||
name: "something",
|
||||
state: "who am i?",
|
||||
type: Discord.ActivityType.Custom
|
||||
}]
|
||||
},
|
||||
shards: "auto",
|
||||
allowedMentions: {parse: ["users", "roles"], repliedUser: true},
|
||||
})
|
||||
|
||||
const ai = new GoogleGenAI({
|
||||
apiKey: process.env.GEMINI_KEY,
|
||||
project: process.env.GCP_PROJECT_ID||undefined,
|
||||
});
|
||||
|
||||
const shardCacheMap = new Map<string,string|undefined>();
|
||||
|
||||
function logPrefix():string {return `[${dateFormat(new Date(), masks.isoTime)}] [Shard-${Client.shard?.ids[0]}]: `}
|
||||
|
||||
Client.on("ready", async () => {
|
||||
console.log(logPrefix()+"Client is ready.")
|
||||
//await createCaches();
|
||||
})
|
||||
|
||||
function sevenDays():string {
|
||||
const nextWeek = new Date();
|
||||
nextWeek.setDate(nextWeek.getDate() + 7);
|
||||
return nextWeek.toISOString();
|
||||
}
|
||||
async function createCaches() {
|
||||
console.log(logPrefix()+"Creating caches...")
|
||||
const guildList:string[] = Client.guilds.cache.map(guild => guild.id)
|
||||
var cacheList = new Set<string>();
|
||||
const caches = await ai.caches.list({config: {pageSize: 100}})
|
||||
|
||||
for await (const cache of caches) {
|
||||
if (cache.displayName) {
|
||||
cacheList.add(cache.displayName);
|
||||
shardCacheMap.set(cache.displayName, cache.name);
|
||||
}
|
||||
}
|
||||
|
||||
for (const guildId of guildList) {
|
||||
if (!cacheList.has(guildId)) {
|
||||
console.log(logPrefix()+`Guild ${guildId} does not have a cache, creating one now.`)
|
||||
const newCache = await ai.caches.create({
|
||||
model: "gemini-2.5-flash",
|
||||
config: {
|
||||
displayName: guildId,
|
||||
expireTime: sevenDays(), // im not gonna bother trying to fit all this in one value
|
||||
systemInstruction: fs.readFileSync("systemprompt.txt").toString().replace("<GUILD>",Client.guilds.cache.get(guildId)?.name||"undefined"),
|
||||
}
|
||||
})
|
||||
shardCacheMap.set(guildId, newCache.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function embedMessageInfo(message:Discord.Message) {
|
||||
return `Message Author Username: ${message.author.username}
|
||||
Message Author Name: ${message.author.displayName}
|
||||
Message Content: ${message.cleanContent}`
|
||||
}
|
||||
|
||||
|
||||
|
||||
Client.on("messageCreate", async message => {
|
||||
if (message.content.includes("<@1396892425548730601>")) {
|
||||
const genContent = await ai.models.generateContent({
|
||||
model: "gemini-2.5-flash",
|
||||
contents: embedMessageInfo(message),
|
||||
config: {
|
||||
//cachedContent: shardCacheMap.get(message.guildId || ""),
|
||||
systemInstruction: fs.readFileSync("systemprompt.txt").toString().replace("<GUILD>",Client.guilds.cache.get(message.guildId||"")?.name||"undefined"),
|
||||
}
|
||||
})
|
||||
const resContent:string = genContent.text?.toString()||" ";
|
||||
message.reply(resContent);
|
||||
}
|
||||
})
|
||||
|
||||
Client.login(process.env.TOKEN);
|
||||
13
src/utils.ts
Normal file
13
src/utils.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import * as Discord from "discord.js";
|
||||
|
||||
function createShardManager(file:string, token:string|undefined, shards:number|"auto", silent?:boolean):Discord.ShardingManager {
|
||||
const manager = new Discord.ShardingManager(file,{
|
||||
token: token,
|
||||
totalShards: shards,
|
||||
mode: "process",
|
||||
silent: silent||false
|
||||
})
|
||||
return manager;
|
||||
}
|
||||
|
||||
export {createShardManager};
|
||||
Reference in New Issue
Block a user