_templateResponse.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // @type {import('@whiskeysockets/baileys')}
  2. const { proto, generateWAMessage, areJidsSameUser, decryptPollVote, } = (await import('@whiskeysockets/baileys')).default;
  3. export async function before(m, chatUpdate) {
  4. if (m.isBaileys) {
  5. return
  6. }
  7. if (!m.message) {
  8. return
  9. }
  10. if (!(m.message.buttonsResponseMessage || m.message.templateButtonReplyMessage || m.message.listResponseMessage || m.message.interactiveResponseMessage)) {
  11. return
  12. }
  13. let id
  14. if (m.message.buttonsResponseMessage) {
  15. id = m.message.buttonsResponseMessage.selectedButtonId
  16. } else if (m.message.templateButtonReplyMessage) {
  17. id = m.message.templateButtonReplyMessage.selectedId
  18. } else if (m.message.listResponseMessage) {
  19. id = m.message.listResponseMessage.singleSelectReply?.selectedRowId;
  20. } else if (m.message.interactiveResponseMessage) {
  21. id = JSON.parse(m.message.interactiveResponseMessage.nativeFlowResponseMessage.paramsJson).id
  22. }
  23. const text = m.message.buttonsResponseMessage?.selectedDisplayText || m.message.templateButtonReplyMessage?.selectedDisplayText || m.message.listResponseMessage?.title
  24. let isIdMessage = false
  25. let usedPrefix
  26. for (const name in global.plugins) {
  27. const plugin = global.plugins[name]
  28. if (!plugin) {
  29. continue
  30. }
  31. if (plugin.disabled) {
  32. continue
  33. }
  34. if (!opts['restrict']) {
  35. if (plugin.tags && plugin.tags.includes('admin')) {
  36. continue
  37. }}
  38. if (typeof plugin !== 'function') {
  39. continue
  40. }
  41. if (!plugin.command) {
  42. continue
  43. }
  44. const str2Regex = (str) => str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
  45. const _prefix = plugin.customPrefix ? plugin.customPrefix : this.prefix ? this.prefix : global.prefix
  46. const match = (_prefix instanceof RegExp ? [[_prefix.exec(id), _prefix]] : Array.isArray(_prefix) ? _prefix.map((p) => {
  47. const re = p instanceof RegExp ? p : new RegExp(str2Regex(p));
  48. return [re.exec(id), re]
  49. }) :
  50. typeof _prefix === 'string' ?
  51. [[new RegExp(str2Regex(_prefix)).exec(id), new RegExp(str2Regex(_prefix))]] :
  52. [[[], new RegExp]]
  53. ).find((p) => p[1])
  54. if ((usedPrefix = (match[0] || '')[0])) {
  55. const noPrefix = id.replace(usedPrefix, '')
  56. let [command] = noPrefix.trim().split` `.filter((v) => v)
  57. command = (command || '').toLowerCase()
  58. const isId = plugin.command instanceof RegExp ?
  59. plugin.command.test(command) :
  60. Array.isArray(plugin.command) ?
  61. plugin.command.some((cmd) => cmd instanceof RegExp ?
  62. cmd.test(command) :
  63. cmd === command,
  64. ) :
  65. typeof plugin.command === 'string' ?
  66. plugin.command === command :
  67. false
  68. if (!isId) {
  69. continue
  70. }
  71. isIdMessage = true
  72. }}
  73. const messages = await generateWAMessage(m.chat, {text: isIdMessage ? id : text, mentions: m.mentionedJid}, {
  74. userJid: this.user.id,
  75. quoted: m.quoted && m.quoted.fakeObj,
  76. })
  77. messages.key.fromMe = areJidsSameUser(m.sender, this.user.id)
  78. messages.key.id = m.key.id
  79. messages.pushName = m.name
  80. if (m.isGroup) {
  81. messages.key.participant = messages.participant = m.sender
  82. }
  83. const msg = {
  84. ...chatUpdate,
  85. messages: [proto.WebMessageInfo.fromObject(messages)].map((v) => (v.conn = this, v)),
  86. type: 'append',
  87. }
  88. this.ev.emit('messages.upsert', msg)
  89. }
  90. /*const {
  91. proto,
  92. generateWAMessage,
  93. areJidsSameUser
  94. } = (await import('@whiskeysockets/baileys')).default
  95. export async function all(m, chatUpdate) {
  96. try {
  97. if (m.isBaileys) return
  98. if (!m.message) return
  99. if (!(m.message.buttonsResponseMessage || m.message.templateButtonReplyMessage || m.message.listResponseMessage ||
  100. m.message.interactiveResponseMessage)) return
  101. let id = m.message.buttonsResponseMessage?.selectedButtonId || m.message.templateButtonReplyMessage?.selectedId ||
  102. m.message.listResponseMessage?.singleSelectReply?.selectedRowId || JSON.parse(m.message
  103. .interactiveResponseMessage?.nativeFlowResponseMessage?.paramsJson)?.id
  104. let text = m.message.buttonsResponseMessage?.selectedDisplayText || m.message.templateButtonReplyMessage
  105. ?.selectedDisplayText || m.message.listResponseMessage?.title || m.message.interactiveResponseMessage?.body
  106. ?.text
  107. let isIdMessage = false,
  108. usedPrefix
  109. for (let name in plugins) {
  110. let plugin = plugins[name]
  111. if (!plugin) continue
  112. if (plugin.disabled) continue
  113. if (!opts['restrict'])
  114. if (plugin.tags && plugin.tags.includes('admin')) continue
  115. if (typeof plugin !== 'function') continue
  116. if (!plugin.command) continue
  117. const str2Regex = str => str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
  118. let _prefix = plugin.customPrefix ? plugin.customPrefix : this.prefix ? this.prefix : prefix
  119. let match = (_prefix instanceof RegExp ? // RegExp Mode?
  120. [
  121. [_prefix.exec(id), _prefix]
  122. ] : Array.isArray(_prefix) ? // Array?
  123. _prefix.map(p => {
  124. let re = p instanceof RegExp ? // RegExp in Array?
  125. p : new RegExp(str2Regex(p))
  126. return [re.exec(id), re]
  127. }) : typeof _prefix === 'string' ? // String?
  128. [
  129. [new RegExp(str2Regex(_prefix)).exec(id), new RegExp(str2Regex(_prefix))]
  130. ] : [
  131. [
  132. [], new RegExp
  133. ]
  134. ]).find(p => p[1])
  135. if ((usedPrefix = (match[0] || '')[0])) {
  136. let noPrefix = id.replace(usedPrefix, '')
  137. let [command] = noPrefix.trim().split(' ').filter(v => v)
  138. command = (command || '').toLowerCase()
  139. let isId = plugin.command instanceof RegExp ? // RegExp Mode?
  140. plugin.command.test(command) : Array.isArray(plugin.command) ? // Array?
  141. plugin.command.some(cmd => cmd instanceof RegExp ? // RegExp in Array?
  142. cmd.test(command) : cmd === command) : typeof plugin.command === 'string' ? // String?
  143. plugin.command === command : false
  144. if (!isId) continue
  145. isIdMessage = true
  146. }
  147. }
  148. let messages = await generateWAMessage(m.chat, {
  149. text: isIdMessage ? id : text,
  150. mentions: m.mentionedJid
  151. }, {
  152. userJid: this.user.id,
  153. quoted: m.quoted?.fakeObj
  154. })
  155. messages.key.fromMe = areJidsSameUser(m.sender, this.user.id)
  156. messages.key.id = m.key.id
  157. messages.pushName = m.name
  158. if (m.isGroup) messages.key.participant = messages.participant = m.sender
  159. let msg = {
  160. ...chatUpdate,
  161. messages: [proto.WebMessageInfo.fromObject(messages)].map(v => (v.conn = this, v)),
  162. type: 'append'
  163. }
  164. this.ev.emit('messages.upsert', msg)
  165. } catch (error) {
  166. console.error('Error in processing message:', error);
  167. }
  168. }
  169. */