irclib/src/examples/connection-test.ts

99 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-09-23 17:38:41 +00:00
import { IRCBot } from '../bot';
2022-09-24 09:16:07 +00:00
import {
2022-09-24 09:21:31 +00:00
applyCTCP,
2022-09-24 09:16:07 +00:00
applyTextColor,
wrapFormattedText,
} from '../utility/message-formatting';
2022-09-24 11:00:12 +00:00
import { IRCNickList } from '../utility/nicklist';
2022-09-23 17:38:41 +00:00
import { NickServValidator } from '../utility/nickserv-validator';
const bot = new IRCBot({
host: 'icynet.eu',
nick: 'MyTestBot',
channels: ['#squeebot'],
2022-09-24 08:16:16 +00:00
bot: true,
2022-09-23 17:38:41 +00:00
nickserv: {
enabled: true,
command: 'STATUS',
},
});
const nickserv = new NickServValidator(bot);
2022-09-24 11:00:12 +00:00
const nicklist = new IRCNickList(bot);
2022-09-23 17:38:41 +00:00
bot.on('authenticated', () => {
console.log('Successful connection!');
});
bot.on('server-supports', (supported) => {
console.log(supported);
});
2022-09-24 07:49:28 +00:00
bot.on('supported-modes', (supported) => {
console.log(supported);
});
2022-09-23 17:38:41 +00:00
// bot.on('line', console.log);
bot.on('message', ({ message, to, nickname }) => {
2022-09-24 09:16:07 +00:00
console.log(`[${to}] ${nickname}: ${wrapFormattedText(message)}`);
2022-09-23 17:38:41 +00:00
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,
),
);
2022-09-24 07:49:28 +00:00
return;
2022-09-23 17:38:41 +00:00
}
2022-09-24 09:16:07 +00:00
if (message.startsWith('!colors')) {
bot.send(to, `${applyTextColor('blue', 'cool blue text!')}`);
return;
}
2022-09-23 17:38:41 +00:00
if (message.startsWith('!whois')) {
bot.whois(nickname).then(console.log);
2022-09-24 07:49:28 +00:00
return;
}
if (message.startsWith('!who')) {
bot.who(to).then(console.log);
return;
}
2022-09-24 09:21:31 +00:00
if (message.startsWith('!hug')) {
bot.send(to, applyCTCP(`hugs ${nickname}`));
return;
}
2022-09-24 07:49:28 +00:00
if (message.startsWith('!names')) {
bot.names(to).then(console.log);
return;
}
if (message.startsWith('!list')) {
bot.list().then(console.log);
return;
2022-09-23 17:38:41 +00:00
}
if (message.startsWith('!ping')) {
bot.getPing().then((res) => bot.send(to, `Pong: ${res / 1000}s`));
2022-09-24 07:49:28 +00:00
return;
2022-09-23 17:38:41 +00:00
}
});
bot.on('disconnect', console.log);
bot.on('names', console.log);
2022-09-24 11:00:12 +00:00
nicklist.on('update', () => console.log(JSON.stringify(nicklist.channels)));
2022-09-23 17:38:41 +00:00
bot.connect();