|
@@ -2,15 +2,24 @@ const { EventBuilder, EventCommon } = require('./common')
|
|
|
const { types } = require('../tl')
|
|
|
|
|
|
class NewMessage extends EventBuilder {
|
|
|
- constructor(args = {
|
|
|
- chats: null,
|
|
|
- func: null,
|
|
|
- }) {
|
|
|
- super(args)
|
|
|
-
|
|
|
- this.chats = args.chats
|
|
|
- this.func = args.func
|
|
|
- this._noCheck = true
|
|
|
+ constructor({
|
|
|
+ outgoing = true,
|
|
|
+ incoming = false,
|
|
|
+ fromUsers = [],
|
|
|
+ forwards = true,
|
|
|
+ pattern = undefined,
|
|
|
+ } = {}) {
|
|
|
+ super()
|
|
|
+
|
|
|
+ this.outgoing = outgoing
|
|
|
+ this.incoming = incoming
|
|
|
+ this.fromUsers = fromUsers
|
|
|
+ this.forwards = forwards
|
|
|
+ this.pattern = pattern
|
|
|
+
|
|
|
+ if (!this.outgoing && !this.incoming) {
|
|
|
+ throw new Error('one of incoming or outgoing must be true')
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
async _resolve(client) {
|
|
@@ -20,14 +29,14 @@ class NewMessage extends EventBuilder {
|
|
|
|
|
|
build(update, others = null, thisId = null) {
|
|
|
let event
|
|
|
+
|
|
|
+ if (!this.filter(update)) return
|
|
|
+
|
|
|
if (update instanceof types.UpdateNewMessage || update instanceof types.UpdateNewChannelMessage) {
|
|
|
- if (!(update.message instanceof types.Message)) {
|
|
|
- return
|
|
|
- }
|
|
|
event = new Event(update.message)
|
|
|
} else if (update instanceof types.UpdateShortMessage) {
|
|
|
event = new Event(new types.Message({
|
|
|
- out: update.out,
|
|
|
+ out: update.out || false,
|
|
|
mentioned: update.mentioned,
|
|
|
mediaUnread: update.mediaUnread,
|
|
|
silent: update.silent,
|
|
@@ -41,11 +50,11 @@ class NewMessage extends EventBuilder {
|
|
|
fwdFrom: update.fwdFrom,
|
|
|
viaBotId: update.viaBotId,
|
|
|
replyToMsgId: update.replyToMsgId,
|
|
|
- entities: update.entities,
|
|
|
+ entities: update.entities || [],
|
|
|
}))
|
|
|
} else if (update instanceof types.UpdateShortChatMessage) {
|
|
|
event = new this.Event(new types.Message({
|
|
|
- out: update.out,
|
|
|
+ out: update.out || false,
|
|
|
mentioned: update.mentioned,
|
|
|
mediaUnread: update.mediaUnread,
|
|
|
silent: update.silent,
|
|
@@ -57,7 +66,7 @@ class NewMessage extends EventBuilder {
|
|
|
fwdFrom: update.fwdFrom,
|
|
|
viaBotId: update.viaBotId,
|
|
|
replyToMsgId: update.replyToMsgId,
|
|
|
- entities: update.entities,
|
|
|
+ entities: update.entities || [],
|
|
|
}))
|
|
|
} else {
|
|
|
return
|
|
@@ -74,11 +83,42 @@ class NewMessage extends EventBuilder {
|
|
|
return event
|
|
|
}
|
|
|
|
|
|
- filter(event) {
|
|
|
- if (this._noCheck) {
|
|
|
- return event
|
|
|
+ filter(update) {
|
|
|
+ const message = update.message
|
|
|
+
|
|
|
+ // Make sure this is a message object in the first place
|
|
|
+ if (!(message instanceof types.Message)) {
|
|
|
+ return false
|
|
|
}
|
|
|
- return event
|
|
|
+
|
|
|
+ // Check if the message is incoming or outgoing, and if
|
|
|
+ // we want to accept whichever one it is
|
|
|
+ if (message.out) {
|
|
|
+ if (!this.outgoing) return false
|
|
|
+ } else {
|
|
|
+ if (!this.incoming) return false
|
|
|
+ }
|
|
|
+
|
|
|
+ // See if the message was sent by one of the `fromUsers`
|
|
|
+ if (this.fromUsers.length > 0) {
|
|
|
+ const valid = this.fromUsers.map((user) => {
|
|
|
+ const id = 'id' in user ? user.id : user
|
|
|
+ if (message.fromId === id) return true
|
|
|
+ else return false
|
|
|
+ })
|
|
|
+
|
|
|
+ if (!valid.includes(true)) return false
|
|
|
+ }
|
|
|
+
|
|
|
+ // Check if the message was forwarded
|
|
|
+ if (message.fwdFrom && !this.forwards) return false
|
|
|
+
|
|
|
+ // Finally check the message text against a pattern
|
|
|
+ if (this.pattern) {
|
|
|
+ if (!message.message.match(this.pattern)) return false
|
|
|
+ }
|
|
|
+
|
|
|
+ return true
|
|
|
}
|
|
|
}
|
|
|
|