Skip to content

Instantly share code, notes, and snippets.

@Iucasmaia
Created August 14, 2022 17:09
Show Gist options
  • Save Iucasmaia/5a8ed63266df510cb0bc486da7b98e67 to your computer and use it in GitHub Desktop.
Save Iucasmaia/5a8ed63266df510cb0bc486da7b98e67 to your computer and use it in GitHub Desktop.
waitForMessage - made by maia - baileys
sock.waitForMessage = async (from = false, timeout, max = false) => {
return new Promise((resolve) => {
var msgs = []
let x = setTimeout(() => {
if (msgs.length < 1) {
resolve('idle')
} else {
resolve(msgs)
}
}, timeout);
sock.ev.on("messages.upsert", (chat) => {
if (chat && chat.type == 'notify') {
let mc = chat.messages[0]
if (mc.message && mc.key && mc.key.remoteJid != 'status@broadcast' && !mc.key.fromMe) {
mc.message = (getContentType(mc.message) === 'ephemeralMessage') ? mc.message.ephemeralMessage.message: mc.message;
mc.message = (getContentType(mc.message) === 'viewOnceMessage') ? mc.message.viewOnceMessage.message: mc.message;
if (max) {
if (msgs.length >= max) {
clearTimeout(x)
resolve(msgs)
} else {
if (from) {
if (mc.key.remoteJid == from) msgs.push(mc)
} else {
msgs.push(mc)
}
}
} else {
if (from) {
if (mc.key.remoteJid == from) msgs.push(mc)
} else {
msgs.push(mc)
}
}
}
}
})
})
}
@Iucasmaia
Copy link
Author

merge my fork I guess :D

you helped me with this on discord months ago 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment