$[ Haxed.net ]> _
Cybersecurity Researcher / Software Engineer / Digital Artist
April 27, 2025 by Matt

Many people ask me about Bark, a bot I created to manage my own IRC channel. The reason people ask about Bark is mainly for it's ChatGPT integreation, which is the most commonly used feature.

Since I can't bring bark into every channel on Libera I've decided to make some code so you can roll your own bot!

For this project I made use to TypeScript and Deno. I built my own IRC module for this because I'm a nerd.

Deno!

I elect to use Deno for my TypeScript projects for it's native support of TS. This means all you need is Deno and your TS files, no need to pre-compile anything!

If you plan to build TypeScript software I highly recommend Deno over NodeJS. I won't go down the rabbithole of why Deno is better for TS, but you can Google it if you're courious.

IRC.TS

This project makes use of IRC.TS, an IRC module I'm currently working on for Deno. You can find the project here on github.

The Bot Code

Below is the code to run the bot. It doesn't do much, but it's a good place to start for building your own ChatGPT bot


import { IRC } from "./irc.ts";


const nick:string = "fu";

const token:string = "sk-proj-YOUR-OPENAI-KEY";

const systemPrompt:string = "You are an AI dog named " + nick + ". You respond in a silly light-hearted way";

const irc:object = new IRC({
		nick: nick,
		user: "",
		password: "",
		ident: "Testing"
	}, "irc.libera.chat", 6667);
	

const history:object = [];

const chatLog:object = [];


irc.on("welcome", (e)=>{
	/*
		Got welcome message. safe to join channels now.
	*/
	irc.join("##foxx");
});

irc.on("privmsg", (e)=>{
	console.log(e);
	if(e.target.toLowerCase() == nick.toLowerCase()){
		//console.log("a message to me!");
		Openai(e.message, (b)=>{
			console.log(b);
			irc.privmsg(e.source, b);
		});
	}else{
		//console.log("a message to a channel");
		if(e.message.toLowerCase().split(" ")[0] == nick.toLowerCase()){
			Openai(e.message, (b)=>{
				console.log(b);
				irc.privmsg(e.target, b);
			});
		}
	}
});


function Openai(input:string, callback:object) {
    const url = "https://api.openai.com/v1/chat/completions";
    const bearer = 'Bearer ' + token;
	
	const messages:object = [];
	messages.push({"role": "system", "content": systemPrompt});

	for(let i in history){
		messages.push(history[i]);
	}
	
	if(history.length > 10){
		history.splice(0, 1);
	}
	
	messages.push({"role": "user", "content": input});
	
    fetch(url, {
        method: 'POST',
        headers: {
            'Authorization': bearer,
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
			"model":"gpt-4o-mini",
			"messages": messages,
			"max_tokens":500
		})


    }).then(response => {
        
        return response.json()
       
    }).then(data=>{
		let output:string = data['choices'][0].message.content.replace(/\r/g, "").replace(/\n/g, "").substr(0,1024);
		
		history.push({"role": "user", "content": input});
		history.push({"role": "assistant", "content": output});
		callback(output);
		
    }).catch(error => {
		console.log('Something bad happened ' + error)
	});

}
						
About Me
Hello I'm Matt. I like to dabble in cyber security. I also like to draw cute cartoon animals, especially ducks -- and yes, I am a FURRY.
 
Something else
This is a message in the right panel