irclib/src/utility/who-parser.ts

30 lines
801 B
TypeScript

import { IIRCLine } from '../types/irc.interfaces';
export interface WhoResponse {
channel?: string;
username?: string;
hostname?: string;
server?: string;
nickname?: string;
modes?: string[];
oper?: boolean;
hops?: number;
realname?: string;
}
export const parseWho = (lines: IIRCLine[]): WhoResponse[] => {
return lines
.filter(({ command }) => command === '352')
.map((line) => ({
channel: line.arguments[1],
username: line.arguments[2],
hostname: line.arguments[3],
server: line.arguments[4],
nickname: line.arguments[5],
modes: line.arguments[6].split(''),
oper: line.arguments[6].includes('*'),
hops: parseInt(line.trailing.split(' ')[0], 10),
realname: line.trailing.split(' ').slice(1).join(' '),
}));
};