Add irc bot
This commit is contained in:
@@ -9,6 +9,8 @@ import { ConfigModule } from '@nestjs/config';
|
||||
import { DomainrproxyModule } from './domainrproxy/domainrproxy.module';
|
||||
import configuration from './config/configuration';
|
||||
import { CacheModule } from '@nestjs/cache-manager';
|
||||
import { IrcbotModule } from './ircbot/ircbot.module';
|
||||
import { IrcbotService } from './ircbot/ircbot.service';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -22,8 +24,9 @@ import { CacheModule } from '@nestjs/cache-manager';
|
||||
AuthModule,
|
||||
UsersModule,
|
||||
DomainrproxyModule,
|
||||
IrcbotModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
providers: [AppService, IrcbotService],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
@@ -3,4 +3,11 @@ export default () => ({
|
||||
// UserAgent should be added to calls made to third party apis
|
||||
userAgent: 'api.us.dev/@chip@talking.dev',
|
||||
rapidApiKey: process.env.RAPID_API_KEY || '',
|
||||
irc: {
|
||||
enabled: process.env.IRC_SERVER !== undefined,
|
||||
server: process.env.IRC_SERVER,
|
||||
tls: process.env.IRC_TLS === 'true',
|
||||
port: parseInt(process.env.IRC_PORT ?? '6697'),
|
||||
channel: process.env.IRC_CHANNEL ?? '#usdev',
|
||||
},
|
||||
});
|
||||
|
@@ -4,6 +4,7 @@ import { DomainrproxyController } from './domainrproxy.controller';
|
||||
|
||||
@Module({
|
||||
providers: [DomainrproxyService],
|
||||
controllers: [DomainrproxyController]
|
||||
controllers: [DomainrproxyController],
|
||||
exports: [DomainrproxyService],
|
||||
})
|
||||
export class DomainrproxyModule {}
|
||||
|
9
src/ircbot/ircbot.module.ts
Normal file
9
src/ircbot/ircbot.module.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { IrcbotService } from './ircbot.service';
|
||||
import { DomainrproxyService } from 'src/domainrproxy/domainrproxy.service';
|
||||
|
||||
@Module({
|
||||
providers: [IrcbotService, DomainrproxyService],
|
||||
exports: [IrcbotService],
|
||||
})
|
||||
export class IrcbotModule {}
|
59
src/ircbot/ircbot.service.ts
Normal file
59
src/ircbot/ircbot.service.ts
Normal 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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user