-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathsmsg.js
125 lines (115 loc) · 4.31 KB
/
smsg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const {
default: dreadedConnect,
useMultiFileAuthState,
DisconnectReason,
fetchLatestBaileysVersion,
makeInMemoryStore,
downloadContentFromMessage,
jidDecode,
proto,
getContentType,
} = require("@whiskeysockets/baileys");
function smsg(conn, m, store) {
if (!m) return m;
let M = proto.WebMessageInfo;
if (m.key) {
m.id = m.key.id;
m.isBaileys = m.id.startsWith("BAE5") && m.id.length === 16;
m.chat = m.key.remoteJid;
m.fromMe = m.key.fromMe;
m.isGroup = m.chat.endsWith("@g.us");
m.sender = conn.decodeJid((m.fromMe && conn.user.id) || m.participant || m.key.participant || m.chat || "");
if (m.isGroup) m.participant = conn.decodeJid(m.key.participant) || "";
}
if (m.message) {
m.mtype = getContentType(m.message);
m.msg = m.mtype == "viewOnceMessage" ?
(m.message[m.mtype] ? m.message[m.mtype].message[getContentType(m.message[m.mtype].message)] : undefined) :
m.message[m.mtype];
m.body =
m.message.conversation ||
(m.msg && m.msg.caption) ||
(m.msg && m.msg.text) ||
(m.mtype == "listResponseMessage" && m.msg && m.msg.singleSelectReply && m.msg.singleSelectReply.selectedRowId) ||
(m.mtype == "buttonsResponseMessage" && m.msg && m.msg.selectedButtonId) ||
(m.mtype == "viewOnceMessage" && m.msg && m.msg.caption) ||
m.text;
let quoted = (m.quoted = m.msg.contextInfo ? m.msg.contextInfo.quotedMessage : null);
m.mentionedJid = m.msg.contextInfo ? m.msg.contextInfo.mentionedJid : [];
if (m.quoted) {
let type = getContentType(quoted);
m.quoted = m.quoted[type];
if (["productMessage"].includes(type)) {
type = getContentType(m.quoted);
m.quoted = m.quoted[type];
}
if (typeof m.quoted === "string")
m.quoted = {
text: m.quoted,
};
m.quoted.mtype = type;
m.quoted.id = m.msg.contextInfo.stanzaId;
m.quoted.chat = m.msg.contextInfo.remoteJid || m.chat;
m.quoted.isBaileys = m.quoted.id ? m.quoted.id.startsWith("BAE5") && m.quoted.id.length === 16 : false;
m.quoted.sender = conn.decodeJid(m.msg.contextInfo.participant);
m.quoted.fromMe = m.quoted.sender === conn.decodeJid(conn.user.id);
m.quoted.text = m.quoted.text || m.quoted.caption || m.quoted.conversation || m.quoted.contentText || m.quoted.selectedDisplayText || m.quoted.title || "";
m.quoted.mentionedJid = m.msg.contextInfo ? m.msg.contextInfo.mentionedJid : [];
m.getQuotedObj = m.getQuotedMessage = async () => {
if (!m.quoted.id) return false;
let q = await store.loadMessage(m.chat, m.quoted.id, conn);
return exports.smsg(conn, q, store);
};
let vM = (m.quoted.fakeObj = M.fromObject({
key: {
remoteJid: m.quoted.chat,
fromMe: m.quoted.fromMe,
id: m.quoted.id,
},
message: quoted,
...(m.isGroup ? { participant: m.quoted.sender } : {}),
}));
/**
*
* @returns
*/
m.quoted.delete = () => conn.sendMessage(m.quoted.chat, { delete: vM.key });
/**
*
* @param {*} jid
* @param {*} forceForward
* @param {*} options
* @returns
*/
m.quoted.copyNForward = (jid, forceForward = false, options = {}) => conn.copyNForward(jid, vM, forceForward, options);
/**
*
* @returns
*/
m.quoted.download = () => conn.downloadMediaMessage(m.quoted);
}
}
if (m.msg.url) m.download = () => conn.downloadMediaMessage(m.msg);
m.text = m.msg.text || m.msg.caption || m.message.conversation || m.msg.contentText || m.msg.selectedDisplayText || m.msg.title || "";
/**
* Reply to this message
* @param {String|Object} text
* @param {String|false} chatId
* @param {Object} options
*/
m.reply = (text, chatId = m.chat, options = {}) => (Buffer.isBuffer(text) ? conn.sendMedia(chatId, text, "file", "", m, { ...options }) : conn.sendText(chatId, text, m, { ...options }));
/**
* Copy this message
*/
m.copy = () => exports.smsg(conn, M.fromObject(M.toObject(m)));
/**
*
* @param {*} jid
* @param {*} forceForward
* @param {*} options
* @returns
*/
m.copyNForward = (jid = m.chat, forceForward = false, options = {}) => conn.copyNForward(jid, m, forceForward, options);
return m;
}
module.exports = { smsg };