import { Channel, db } from "../database.ts"; import { BotEmitter } from "@wackford/mod.ts"; import { AIs } from "../ai/index.ts"; import config from "../config.ts"; import blacklist from "../../blacklist.json" assert { type: "json" }; import { sendMessageLog } from "../socket.ts"; //const dirname = new URL(".", import.meta.url).pathname; export default function init() { BotEmitter.on("message", async (bot, message) => { if ( message.content && !message.content.startsWith("!") && config.discord.channels.includes(message.channelId.toString()) && !message.isFromBot && message.member ) { const channel = (await db.select(message.channelId.toString()))[0] as Channel; const user = await bot.helpers.getUser(message.member?.id); await bot.helpers.startTyping(message.channelId); let res = await AIs[message.channelId.toString()].complete(user.username, message.content); const history = channel.history ?? []; history.push({ name: user.username, content: message.content, }, { name: AIs[message.channelId.toString()].name, content: res, }); db.change(message.channelId.toString(), { history, }); sendMessageLog(user.username, message.content, message.channelId.toString()); sendMessageLog( AIs[message.channelId.toString()].name, res, message.channelId.toString(), AIs[message.channelId.toString()].description, ); blacklist.forEach((val: string) => { res = res.replaceAll(new RegExp(val, "gmi"), `${val[0]}${"\\*".repeat(val.length - 1)}`); }); await bot.helpers.sendMessage(message.channelId, { content: `[${AIs[message.channelId.toString()].name}] ${res}`, messageReference: { messageId: message.id, channelId: message.channelId, guildId: message.guildId, failIfNotExists: false, }, }); } }); }