schat2-clive/src/irc.js

66 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-10-17 02:06:55 +00:00
'use strict';
const config = require('config');
2022-10-19 22:07:48 +00:00
const irc = require('irc');
2022-10-17 02:06:55 +00:00
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');
2022-10-23 23:55:30 +00:00
const client = new irc.Client(config.server, config.user.id, {
2022-10-17 02:06:55 +00:00
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-23 23:55:30 +00:00
const games = await getGames(bot, config.user.id);
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,
2023-04-10 23:09:40 +00:00
recipient: /^#/.test(to) ? null : to,
2022-10-17 02:06:55 +00:00
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;