core/src/types/message.ts

107 lines
2.4 KiB
TypeScript
Raw Normal View History

2020-11-28 13:34:34 +00:00
import { Protocol } from './protocol';
2020-11-21 15:41:08 +00:00
export enum EMessageType {
2021-07-03 17:50:26 +00:00
message = 'message',
roomJoin = 'room_join',
roomLeave = 'room_leave',
roomKick = 'room_kick',
nameChange = 'name_change',
edit = 'edit',
protocolStart = 'protocol_start',
protocolExit = 'protocol_exit',
custom = 'custom'
}
2020-12-05 10:01:30 +00:00
/**
* Message target specification. ID and name are mandatory,
* and they could be the same, depending on the protocol.
*/
export interface IMessageTarget {
id: string;
name: string;
2020-11-29 19:23:43 +00:00
server?: string;
}
2020-12-05 10:01:30 +00:00
/**
* Message format
*/
2020-11-21 15:41:08 +00:00
export interface IMessage {
2020-12-05 10:01:30 +00:00
/**
* Uniquely identify this message
*/
2020-11-29 19:23:43 +00:00
id?: any;
2020-12-05 10:01:30 +00:00
/**
* Type of the message. See EMessageType
*/
type: EMessageType;
2020-12-05 10:01:30 +00:00
/**
* Data included in the message. Could be an object or a string.
* Use `text` to get text value.
*/
2020-11-21 15:41:08 +00:00
data: any;
2020-12-05 10:01:30 +00:00
/**
* Text value of the message.
*/
text: string;
/**
* Source protocol of the message.
*/
2020-11-29 19:23:43 +00:00
source: Protocol;
2020-12-05 10:01:30 +00:00
/**
* If the sender has not registered themselves.
* Applicable to, for example, NickServ of IRC.
*/
guest?: boolean;
2020-12-05 10:01:30 +00:00
/**
* The room where the message was sent in.
*/
target?: IMessageTarget;
2020-12-05 10:01:30 +00:00
/**
* Information about the sender.
*/
sender?: IMessageTarget;
2020-12-05 10:01:30 +00:00
/**
* Full ID of the sender (`plugin/protocol/id`).
*/
2020-11-29 19:23:43 +00:00
fullSenderID?: string;
2020-12-05 10:01:30 +00:00
/**
* Full ID of the room (`plugin/protocol/(s:server)/id`).
*/
2020-11-29 19:23:43 +00:00
fullRoomID?: string;
2020-12-05 10:01:30 +00:00
/**
* The time the message was sent.
*/
2020-11-21 15:41:08 +00:00
time: Date;
2020-12-05 10:01:30 +00:00
/**
* True if the message is a direct message to the bot.
*/
2020-11-29 19:23:43 +00:00
direct?: boolean;
2020-12-05 10:01:30 +00:00
/**
* True when resolve has been called at least once.
*/
resolved: boolean;
2020-12-05 10:01:30 +00:00
/**
* Bot's response to this message. Will be passed to the protocol,
* which usually sends it through the formatter and then to the service
* it is connected to.
* @param args Message data
*/
2020-11-21 15:41:08 +00:00
resolve(...args: any[]): void;
2020-12-13 10:16:05 +00:00
/**
* Bot's error response to this message. Will be passed to the protocol,
* which usually sends `error.message` directly to the target of the message
* @param error Error
*/
reject(error: Error): void;
2020-12-05 10:01:30 +00:00
/**
* Insert a mention into the response
* @param target UserTarget to mention (i.e. `msg.sender`)
*/
mention(target: IMessageTarget): string;
/**
* Same as `resolve`, but replies (on supported protocols) instead.
* @param args Message data
*/
reply?(...args: any[]): void;
2020-11-21 15:41:08 +00:00
}