Add irc bot

This commit is contained in:
2023-09-18 22:51:41 -06:00
parent 6078010607
commit f22627e8cf
7 changed files with 114 additions and 4 deletions

View File

@@ -0,0 +1,59 @@
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { TLSSocket, connect } from 'tls';
import * as irc from 'slate-irc';
import { DomainrproxyService } from 'src/domainrproxy/domainrproxy.service';
@Injectable()
export class IrcbotService {
private readonly socket: TLSSocket;
private readonly client: irc.Client;
private readonly logger: Logger = new Logger(IrcbotService.name);
constructor(
public readonly configService: ConfigService,
public readonly domainrProxy: DomainrproxyService,
) {
if (!this.configService.get<boolean>('irc.enabled')) return;
this.socket = connect({
port: this.configService.get<number>('irc.port'),
host: this.configService.get<string>('irc.server'),
rejectUnauthorized: false,
});
this.client = irc(this.socket);
this.client.nick(process.env.NODE_ENV === 'production' ? 'usbot' : 'usdev');
this.client.user('usbot', 'usbot');
const channel: string = this.configService.get<string>(
'irc.channel',
) as string;
this.client.join(channel);
this.client.names(channel, (err, names) => {
this.logger.verbose(`${channel} contains ${JSON.stringify(names)}`);
});
this.client.on('errors', this.logger.error);
this.client.on('message', async (message) => {
console.log(message);
if (message.to !== channel) return;
if (message.message[0] !== '.') return;
this.logger.verbose(`Handling ${message.message}`);
const [command, ...args] = message.message.substring(1).split(' ');
switch (command) {
case 'domain':
const domain = args[0];
const queryResult = await this.domainrProxy.queryForDomain(domain);
this.client.send(
channel,
`${queryResult[0].domain} ${queryResult[0].status}`,
);
return;
default:
this.client.send(channel, `Dunno what ${command} means`);
return;
}
});
}
}