Formatting + websocket message logging server

pull/1/head
Tymon 1 year ago
parent 911485b02d
commit 44735d0230

@ -2,6 +2,9 @@
"deno.enable": true,
"deno.unstable": true,
"deno.importMap": "import_map.json",
"editor.tabSize": 4,
"editor.detectIndentation": false
"[typescript]": {
"editor.tabSize": 4,
"editor.detectIndentation": false,
"editor.defaultFormatter": "denoland.vscode-deno"
}
}

@ -11,6 +11,11 @@
"database": "DB_HERE"
}
},
"websocket": {
"enabled": true,
"debug": false,
"port": 3012
},
"discord": {
"channels": [
"CHANNEL_ID_HERE"

@ -1,5 +1,5 @@
import { configType } from "../config.ts";
import { Channel } from "../database.ts"
import { Channel } from "../database.ts";
export default class BaseAI {
name: string;

@ -1,5 +1,5 @@
import { configType } from "../config.ts";
import { Channel } from "../database.ts"
import { Channel } from "../database.ts";
class HuggingFaceAI {
name: string;
@ -48,18 +48,18 @@ class HuggingFaceAI {
},
method: "POST",
});
this.tokenNum++
this.tokenNum++;
if (this.tokenNum > this.tokens.length - 1) {
this.tokenNum = 0
this.tokenNum = 0;
}
const jsonRes = await res.json();
if (!jsonRes[0].generated_text) {
console.log(jsonRes)
console.warn("Retrying generation with new token...")
return await this.#query(prompt) // bound to go well
console.log(jsonRes);
console.warn("Retrying generation with new token...");
return await this.#query(prompt); // bound to go well
}
return jsonRes
return jsonRes;
}
reset() {

@ -1,10 +1,8 @@
import { Bot, Interaction } from "@wackford/discordeno.ts";
import { sendInteractionResponse } from "@wackford/mod.ts";
import { GoofyAhhException } from "../bot.ts";
import config from "../config.ts";
import { db, Channel } from "../database.ts";
import { Channel, db } from "../database.ts";
import BaseAI from "./BaseAI.d.ts";
import HuggingFaceAI from "./huggingface.ts";
const dirname = new URL(".", import.meta.url).pathname;
@ -26,14 +24,14 @@ export default function initAI() {
moduleName: "huggingface",
name: config.general.name,
description: config.general.desc,
history: []
})
history: [],
});
channel = (await db.select(channelId))[0] as Channel;
}
console.log(channel)
changeAI(channel.moduleName, channelId, channel.name, channel.description)
console.log(channel);
changeAI(channel.moduleName, channelId, channel.name, channel.description);
});
}

@ -1,10 +1,11 @@
import { BotEmitter, initCommands } from "@wackford/mod.ts";
import { Intents, sendMessage } from "@wackford/discordeno.ts";
import { Intents } from "@wackford/discordeno.ts";
import initLocalCommands from "./commands/index.ts";
import initOnMessage from "./events/onMessage.ts";
import initAIs from "./ai/index.ts";
import initDB from "./database.ts";
import config from "./config.ts";
import initSocket from "./socket.ts";
export class GoofyAhhException extends Error {
constructor(message: string) {
@ -14,6 +15,7 @@ export class GoofyAhhException extends Error {
}
await initDB();
if (config.websocket.enabled) initSocket(config.websocket.port);
initOnMessage();
initLocalCommands();
initCommands();

@ -30,12 +30,12 @@ export default {
name: newName,
description: newDesc,
});
db.change(channelId, {
name: newName,
description: newDesc,
history: []
})
name: newName,
description: newDesc,
history: [],
});
await sendInteractionResponse(bot, interaction, {
content: `Name: ${newName ?? "[unchanged]"}\nDescription: ${newDesc ?? "[unchanged]"}`,

@ -30,8 +30,8 @@ export default {
if (name) {
await changeAI(name, interaction.channelId?.toString() as string);
db.change(interaction?.channelId?.toString() as string, {
history: []
})
history: [],
});
}
await sendInteractionResponse(bot, interaction, {
content: `Changed AI to ${name}!`,

@ -10,8 +10,10 @@ export default {
if (!checkAI(bot, interaction, AI)) return;
await sendInteractionResponse(bot, interaction, {
content: `Name: ${AI.name}\nDescription: ${AI.description}\nAI: ${AI.constructor.name}\nHistory: ${JSON.stringify(AI.memory ?? AI.history ?? "[unused]")}`,
private: true
content: `Name: ${AI.name}\nDescription: ${AI.description}\nAI: ${AI.constructor.name}\nHistory: ${
JSON.stringify(AI.memory ?? AI.history ?? "[unused]")
}`,
private: true,
});
},
} as SlashCommandOptions;

@ -5,7 +5,8 @@ export default {
description: "proves that we are not at fault when the bot says something stupid again",
execute: async (bot, interaction) => {
await sendInteractionResponse(bot, interaction, {
content: `DISCLAIMER: We (Wackyware) do not take any responsibility for the things said by this bot, if they offend you, get the administrator of your server to remove the message.
content:
`DISCLAIMER: We (Wackyware) do not take any responsibility for the things said by this bot, if they offend you, get the administrator of your server to remove the message.
For more details see https://discord.gg/Pxt9dVDSRn and <https://www.ruthenic.com/blog>`,
});
},

@ -1,6 +1,6 @@
import { createSlashCommand, sendInteractionResponse } from "@wackford/mod.ts";
const dirname = new URL(".", import.meta.url).pathname;
const dirname = new URL(".", import.meta.url);
const helpMessage: string[] = [
"/help - helps your idiot ass", // we do this manually because fuck you too

@ -14,8 +14,8 @@ export default {
AI.reset();
db.change(channelId, {
history: []
})
history: [],
});
await sendInteractionResponse(bot, interaction, {
content: `Done!`,

@ -9,8 +9,8 @@ export interface Channel {
name: string;
description: string;
history: {
name: string,
content: string
name: string;
content: string;
}[];
}
@ -18,7 +18,7 @@ export default async function init() {
try {
await db.signin({
user: config.general.db.user,
pass: config.general.db.pass
pass: config.general.db.pass,
});
await db.use(config.general.db.namespace, config.general.db.database);

@ -1,8 +1,9 @@
import { db, Channel } from "../database.ts";
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 { sendSocketMessage } from "../socket.ts";
//const dirname = new URL(".", import.meta.url).pathname;
@ -15,30 +16,37 @@ export default function init() {
!message.isFromBot &&
message.member
) {
const channel = (await db.select(message.channelId.toString()))[0] as Channel
const channel = (await db.select(message.channelId.toString()))[0] as Channel;
const user = await bot.helpers.getUser(message.member?.id);
sendSocketMessage(user.username, message.content, message.channelId.toString());
await bot.helpers.startTyping(message.channelId);
let res = await AIs[message.channelId.toString()].complete(user.username, message.content)
let res = await AIs[message.channelId.toString()].complete(user.username, message.content);
const history = channel.history ?? []
const history = channel.history ?? [];
history.push({
name: user.username,
content: message.content
content: message.content,
}, {
name: AIs[message.channelId.toString()].name,
content: res
})
content: res,
});
db.change(message.channelId.toString(), {
history
})
history,
});
sendSocketMessage(
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)}`)
})
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}`,

@ -0,0 +1,84 @@
import { serve } from "https://deno.land/std@0.160.0/http/server.ts";
import config from "./config.ts";
interface SockClient {
ws: WebSocket;
pinger: number;
}
// TODO: Read this from config
const debugMode = config.websocket.debug;
export const connected: Record<string, SockClient> = {};
function debug(msg: string) {
if (debugMode) console.log(`debug: ${msg}`);
}
function handleConnected(_event: Event, ws: WebSocket) {
debug("Client connected!");
const pinger = setInterval(() => {
// i'm not sorry for this
debug(`Pinging client ${Object.keys(connected).length - 1}...`);
ws.send("__PING__");
}, 10000);
connected[Object.keys(connected).length] = {
ws: ws,
pinger: pinger,
};
}
function handleDisconnected(_event: CloseEvent, ws: WebSocket) {
Object.keys(connected).forEach((val) => {
if (connected[val].ws == ws) {
debug(`Client ${val} disconnected!`);
clearInterval(connected[val].pinger);
delete connected[val];
}
});
}
function handleMessage(event: MessageEvent) {
if (event.data == "__PONG__") {
debug("Recieved pong!");
}
}
function reqHandler(req: Request) {
if (req.headers.get("upgrade") != "websocket") {
return new Response(null, { status: 501 });
}
const { socket: ws, response } = Deno.upgradeWebSocket(req);
ws.onopen = (event) => handleConnected(event, ws);
ws.onclose = (event) => handleDisconnected(event, ws);
ws.onmessage = (event) => handleMessage(event);
return response;
}
export default function initialize(port: number) {
serve(reqHandler, { port: port });
}
export function sendSocketMessage(
name: string,
message: string,
channel: string,
description = "",
) {
Object.keys(connected).forEach((val) => {
if (connected[val]) {
connected[val].ws.send(
JSON.stringify({
name,
description,
message,
channel,
}),
);
}
});
}
Loading…
Cancel
Save