schat2-clive/src/irc.js

65 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-10-17 02:06:55 +00:00
'use strict';
const config = require('config');
const irc = require('irc-upd');
const logger = require('simple-node-logger').createSimpleLogger();
const { argv } = require('yargs');
// const timers = require('timers/promises');
2022-10-18 23:24:13 +00:00
const {
onMessage,
getGames,
} = require('./play');
2022-10-17 02:06:55 +00:00
logger.setLevel(argv.level || 'info');
const client = new irc.Client(config.server, config.user.nick, {
userName: config.user.username,
realName: config.user.realName,
password: config.user.password,
port: config.port,
secure: true,
});
async function init() {
const bot = {
2022-10-18 23:24:13 +00:00
client,
2022-10-17 02:06:55 +00:00
rooms: config.channels.map((channel) => ({
id: channel,
name: channel,
})),
};
2022-10-18 23:24:13 +00:00
const games = await getGames(bot, config.user.nick);
2022-10-17 02:06:55 +00:00
client.addListener('registered', () => {
logger.info('Connected!');
logger.info('Identifying with NickServ');
client.say('nickserv', `IDENTIFY ${config.user.username} ${config.user.password}`);
config.channels.forEach((channel) => {
logger.info(`Joining ${channel}`);
client.join(channel, () => logger.info(`Joined ${channel}`));
if (config.greeting) {
client.say(channel, config.greeting);
}
2022-10-17 02:06:55 +00:00
});
client.addListener('message', (from, to, body) => onMessage({
from,
to,
body,
2022-10-18 23:24:13 +00:00
type: 'message',
2022-10-17 02:06:55 +00:00
}, bot, games));
client.addListener('error', (error) => {
2022-10-18 23:24:13 +00:00
logger.error(error);
2022-10-17 02:06:55 +00:00
});
});
}
2022-10-18 23:24:13 +00:00
module.exports = init;