schat2-clive/src/irc.js

207 lines
5.3 KiB
JavaScript
Raw Normal View History

2022-10-17 02:06:55 +00:00
'use strict';
const config = require('config');
const fs = require('fs').promises;
const irc = require('irc-upd');
const logger = require('simple-node-logger').createSimpleLogger();
const { argv } = require('yargs');
// const timers = require('timers/promises');
const style = require('./utils/style');
logger.setLevel(argv.level || 'info');
const points = {};
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 setPoints(defaultKey, user, value, { mode = 'add', key } = {}) {
const gameKey = key || defaultKey;
if (!user) {
logger.warn(`Failed to set ${gameKey} points for missing user`);
return;
}
const userKey = `${user.id}:${user.username}`;
if (!points[gameKey]) {
points[gameKey] = {};
}
if (mode === 'add') {
points[gameKey][userKey] = (points[gameKey][userKey] || 0) + value;
}
if (mode === 'set') {
points[gameKey][userKey] = value;
}
await fs.writeFile(`./points-${config.user.nick}.json`, JSON.stringify(points, null, 4));
}
function getPoints(game, rawUsername, { user, room, command }) {
const username = rawUsername?.replace(new RegExp(`^${config.usernamePrefix}`), '');
const gamePoints = points[command] || points[game.key];
const userPoints = username
? Object.entries(gamePoints || {}).find(([identifier]) => identifier.split(':')[1] === username)?.[1]
: gamePoints?.[`${user?.id}:${user?.username}`];
game.sendMessage(`${username ? `${style.bold(username)} has` : 'You have'} scored ${style.bold(userPoints || 0)} points in ${game.name}, ${config.usernamePrefix}${user.username}`, room.id);
}
function getLeaderboard(game, { user, room, command }) {
const leaderboard = points[command] || points[game.key];
if (!leaderboard || Object.keys(leaderboard).length === 0) {
game.sendMessage(`No points scored in ${game.name} yet!`, room.id);
return;
}
const curatedLeaderboard = Object.entries(leaderboard)
.sort(([, scoreA], [, scoreB]) => scoreB - scoreA)
.map(([userKey, score]) => {
const username = userKey.split(':')[1];
return `${style.bold(`${username === user.username ? config.usernamePrefix : ''}${username}`)} at ${style.bold(score)} points`;
})
.slice(0, 10)
.join(', ');
game.sendMessage(`The top ${Math.min(Object.keys(leaderboard).length, 10)} ${style.italic(game.name)} players are: ${curatedLeaderboard}`, room.id);
}
function getGames(bot) {
const games = config.games.reduce((acc, key) => {
const game = require(`./games/${key.game || key}`); // eslint-disable-line global-require, import/no-dynamic-require
const sendMessage = (body, roomId, options, recipient) => {
console.log(roomId || recipient, body);
client.say(roomId || recipient, body);
};
const setGamePoints = (userId, score, options) => setPoints(key, userId, score, options);
const curatedGame = {
...game,
...(key.game && key),
name: game.name || key,
key,
sendMessage,
setPoints: setGamePoints,
};
if (game.onStart) {
game.onStart({ ...curatedGame, bot });
}
return {
...acc,
[key]: curatedGame,
...game.commands?.reduce((commandAcc, command) => ({ ...commandAcc, [command]: curatedGame }), {}),
};
}, {});
return games;
}
function onMessage(message, bot, games) {
const body = message.originalBody || message.body;
const [, command, subcommand] = body?.match(new RegExp(`^${config.prefix}(\\w+)(?:\\:(\\w+))?`)) || [];
// const user = bot.users[message.userId] || message.user;
const user = { username: message.from, id: message.from };
const room = { id: message.to, name: message.to };
if (command) {
const args = body.split(/\s+/).slice(1);
const game = games[command];
if (['leaderboard', 'lead', 'leader', 'leaders', 'scoreboard', 'best'].includes(subcommand) && games[command]) {
getLeaderboard(games[command], { user, room, command });
return;
}
if (['points', 'score'].includes(subcommand) && games[command]) {
getPoints(games[command], args[0], { user, room, command });
return;
}
if (game && game.onCommand) {
if (user) {
user.points = points[game.key]?.[`${user.id}:${user.username}`] || 0;
}
game.onCommand(args, {
...game,
command,
subcommand,
bot,
message,
user,
room,
points: points[game.key] || {},
logger,
});
}
}
Object.values(games).forEach((game) => game.onMessage?.(message, {
...game,
bot,
message,
user: user && {
...user,
points: points[game.key]?.[`${user.id}:${user.username}`] || 0,
},
room,
logger,
}));
}
async function init() {
const bot = {
rooms: config.channels.map((channel) => ({
id: channel,
name: channel,
})),
};
const games = getGames(bot);
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,
}, bot, games));
client.addListener('error', (error) => {
console.error(error);
});
});
}
init();