irc-events/message: fix types

This commit is contained in:
Reto Brunner 2024-04-15 07:54:12 +02:00
parent 92a0affba1
commit e2b56cf16b
2 changed files with 22 additions and 22 deletions

View file

@ -7,20 +7,32 @@ import Chan from "../../models/chan";
import User from "../../models/user"; import User from "../../models/user";
import {MessageType} from "../../../shared/types/msg"; import {MessageType} from "../../../shared/types/msg";
import {ChanType} from "../../../shared/types/chan"; import {ChanType} from "../../../shared/types/chan";
import {MessageEventArgs} from "irc-framework";
const nickRegExp = /(?:\x03[0-9]{1,2}(?:,[0-9]{1,2})?)?([\w[\]\\`^{|}-]+)/g; const nickRegExp = /(?:\x03[0-9]{1,2}(?:,[0-9]{1,2})?)?([\w[\]\\`^{|}-]+)/g;
type HandleInput = {
nick: string;
hostname: string;
ident: string;
target: string;
type: MessageType;
time: number;
text?: string;
from_server?: boolean;
message: string;
group?: string;
};
function convertForHandle(type: MessageType, data: MessageEventArgs): HandleInput {
return {...data, time: data.time ? data.time : new Date().getTime(), type: type};
}
export default <IrcEventHandler>function (irc, network) { export default <IrcEventHandler>function (irc, network) {
const client = this; const client = this;
irc.on("notice", function (data) { irc.on("notice", function (data) {
data.type = MessageType.NOTICE; handleMessage(convertForHandle(MessageType.NOTICE, data));
type ModifiedData = typeof data & {
type: MessageType.NOTICE;
};
handleMessage(data as ModifiedData);
}); });
irc.on("action", function (data) { irc.on("action", function (data) {
@ -39,18 +51,7 @@ export default <IrcEventHandler>function (irc, network) {
handleMessage(data); handleMessage(data);
}); });
function handleMessage(data: { function handleMessage(data: HandleInput) {
nick: string;
hostname: string;
ident: string;
target: string;
type: MessageType;
time: number;
text?: string;
from_server?: boolean;
message: string;
group?: string;
}) {
let chan: Chan | undefined; let chan: Chan | undefined;
let from: User; let from: User;
let highlight = false; let highlight = false;
@ -128,7 +129,7 @@ export default <IrcEventHandler>function (irc, network) {
// msg is constructed down here because `from` is being copied in the constructor // msg is constructed down here because `from` is being copied in the constructor
const msg = new Msg({ const msg = new Msg({
type: data.type, type: data.type,
time: data.time as any, time: new Date(data.time),
text: data.message, text: data.message,
self: self, self: self,
from: from, from: from,

View file

@ -33,8 +33,7 @@ declare module "irc-framework" {
reply: (message: string) => void; reply: (message: string) => void;
tags: {[key: string]: string}; tags: {[key: string]: string};
target: string; target: string;
time?: any; time?: number;
type: "privmsg" | "action" | "notice" | "wallops";
} }
export interface JoinEventArgs { export interface JoinEventArgs {
account: boolean; account: boolean;