true-irc-logger/index.js
2024-12-16 03:06:56 -03:00

187 lines
6.3 KiB
JavaScript

const fs = require('fs')
const irc = require('irc');
const readline = require('readline')
const model = 'user,date,message'
const { dirname } = require('path');
const rootDir = dirname(require.main.filename);
const settings = JSON.parse(fs.readFileSync(rootDir + '/settings.json'));
let currentChannel = settings.channels[0]
const client = new irc.Client(settings.server, settings.username, {
channels: settings.channels,
userName: 'gordown',
realName: 'Gor Down',
autoRejoin: true,
retryCount: 100000000,
retryDelay: 2*60*1000,
port: 6697,
secure: true,
certExpired: true,
selfSigned: true
});
client.addListener('registered', () => {
//////////////LISTENERS////////////////////
client.addListener('error', (message) => {
console.log('Error: ' + message)
})
client.addListener('message', function (from, channel, message) {
//DMS SENT TO THE BOT
if (channel === client.nick){
console.log("DM from " + from + ": " + message)
const args = message.split("# ")[1]
if(args){
if(args === "GetInfo") client.say(from, "# HeresInfo: japanese nationalist")
}
return
}
logMessage(from, channel, message)
});
client.addListener('join', (channel, nick) => {
logMessage(nick, channel, "Joined the channel")
//I'M THE ANIME GIRL FAGGOT
if (nick === client.nick){
sendMessage(channel, "# Appears as Jhenna_Hina.http://www.mermeliz.com/hina/dl/Jhenna_Hina.AVB")
}else{
client.say(nick, "# Appears as Jhenna_Hina.http://www.mermeliz.com/hina/dl/Jhenna_Hina.AVB")
}
})
client.addListener('part', (channel, nick, reason, _message) => {
logMessage(nick, channel, `Left${reason ? ":" + reason : "."}` )
})
client.addListener('kick', (channel, nick, by, reason, _message) => {
logMessage(nick, channel, "Kicked by: " + by + ": " + reason)
})
////////////////////////////////////////////////////////////////
///LOGGING AND SHIT/////////////////////////////////////////////
function logMessage(from, channel, message){
const currentDate = new Date();
////////////////////////////////////////////////////////////////DATE FORMAT
const year = currentDate.getFullYear();
const month = String(currentDate.getMonth() + 1).padStart(2, '0');
const day = String(currentDate.getDate()).padStart(2, '0');
const hours = String(currentDate.getHours()).padStart(2, '0');
const minutes = String(currentDate.getMinutes()).padStart(2, '0');
const seconds = String(currentDate.getSeconds()).padStart(2, '0');
////////////////////////////////////////////////////////////////
const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
const filename = rootDir + '/logs/' + channel + '.csv'
if (!fs.existsSync(filename)){
fs.writeFileSync(filename, model)
}
fs.appendFileSync(filename, `\n${from},${formattedDate},"${message}"`)
if(from !== settings.username)console.log(`${from}: ${message}`)
}
function sendMessage(channel, message){
logMessage(settings.username, channel, message)
client.say(channel, message)
}
////////////////////////////////////////////////////////////////
//COMMAND LINE SHIT/////////////////////////////////
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function messagePrompt(){
rl.question(client.nick + ': ', (command) => {
const split = command.split("!")
const prefix = split[0]
if(prefix === "g"){
const terminalCommand = split[1].split(' ')[0]
const splitArgs = split[1].split(' ')
const args = splitArgs[1] ? splitArgs[1] : ""
switch (terminalCommand) {
case "switch_channel":
if (!args) {
console.log("Arguments required");
break
}
if(!checkIfIsInChannelAndLog(args)) break
currentChannel = args;
console.log("Current Channel set to:", currentChannel);
break;
case "get_current_channel":
console.log(currentChannel)
break
case "get_users":
let channel = currentChannel
if(args){
if(!checkIfIsInChannelAndLog(channel)) break
channel = args
}
for (user in client.chans[channel].users){
console.log(user)
}
break
case "message":
if(!args){
console.log("Arguments required")
}
const message = getMessageFromArgs(splitArgs)
if (!message) {
console.log("Message required");
break
}
client.say(args, message)
break
case "whois":
if(!args) return console.log("Arguments required")
client.whois(args, (info) => {
console.log(info)
})
break
default:
console.log("Comando inexistente")
break
}
}else{
sendMessage(currentChannel, command)
}
messagePrompt()
});
}
rl.prompt();
messagePrompt()
})
function checkIfIsInChannelAndLog(channel){
if(!client.chans[channel]){
console.log(`You're not in that channel!`);
return false
}
return true
}
function getMessageFromArgs(splitArgs){
splitArgs.shift()
splitArgs.shift()
return splitArgs.join(" ")
}