irclib/src/examples/connection-test.ts

99 lines
2.0 KiB
TypeScript

import { IRCBot } from '../bot';
import {
applyCTCP,
applyTextColor,
wrapFormattedText,
} from '../utility/message-formatting';
import { IRCNickList } from '../utility/nicklist';
import { NickServValidator } from '../utility/nickserv-validator';
const bot = new IRCBot({
host: 'icynet.eu',
nick: 'MyTestBot',
channels: ['#squeebot'],
bot: true,
nickserv: {
enabled: true,
command: 'STATUS',
},
});
const nickserv = new NickServValidator(bot);
const nicklist = new IRCNickList(bot);
bot.on('authenticated', () => {
console.log('Successful connection!');
});
bot.on('server-supports', (supported) => {
console.log(supported);
});
bot.on('supported-modes', (supported) => {
console.log(supported);
});
// bot.on('line', console.log);
bot.on('message', ({ message, to, nickname }) => {
console.log(`[${to}] ${nickname}: ${wrapFormattedText(message)}`);
if (message.startsWith('!test')) {
nickserv
.getNickStatus(nickname)
.then((valid) =>
bot.send(
to,
`Hello, %s! ${
valid ? 'You are logged in.' : 'You are not logged in.'
}`,
nickname,
),
);
return;
}
if (message.startsWith('!colors')) {
bot.send(to, `${applyTextColor('blue', 'cool blue text!')}`);
return;
}
if (message.startsWith('!whois')) {
bot.whois(nickname).then(console.log);
return;
}
if (message.startsWith('!who')) {
bot.who(to).then(console.log);
return;
}
if (message.startsWith('!hug')) {
bot.send(to, applyCTCP(`hugs ${nickname}`));
return;
}
if (message.startsWith('!names')) {
bot.names(to).then(console.log);
return;
}
if (message.startsWith('!list')) {
bot.list().then(console.log);
return;
}
if (message.startsWith('!ping')) {
bot.getPing().then((res) => bot.send(to, `Pong: ${res / 1000}s`));
return;
}
});
bot.on('disconnect', console.log);
bot.on('names', console.log);
nicklist.on('update', () => console.log(JSON.stringify(nicklist.channels)));
bot.connect();