@kamuridesu/whatframework
Version:
A simple WhatsApp Bot Framework on top of Baileys
201 lines (200 loc) • 6.72 kB
JavaScript
import { downloadMediaMessage } from "@whiskeysockets/baileys";
export class Author {
constructor(jid, name, chatJid, isBotOwner, isBot, group) {
this.jid = jid;
this.name = name;
this.chatJid = chatJid;
this.isBotOwner = isBotOwner;
this.isBot = isBot;
this.jid = jid;
this.isBotOwner = isBotOwner;
this.isBot = isBot;
this.group = group;
}
get isGroupOwner() {
return (async () => {
if (this.group != undefined)
return (await this.group.members).filter(x => x.isSuperAdmin).map(x => x.id).includes(this.jid);
return false;
})();
}
get isAdmin() {
return (async () => {
if (this.group)
return (await this.group.admins).map(x => x.id).includes(this.jid);
return false;
})();
}
}
export class QuotedMessageParsed {
constructor(unparsedQuotedMessage, chatJid) {
this.stanzaId = unparsedQuotedMessage.stanzaId;
this.author = new Author(unparsedQuotedMessage.participant, "unknown", chatJid, false, false, undefined);
this.body = this.parseBody(unparsedQuotedMessage.message);
}
parseBody(body) {
if (body == undefined)
return "";
if (body.conversation) {
return body.conversation;
}
if (body.imageMessage) {
return body.imageMessage.caption;
}
if (body.videoMessage) {
return body.videoMessage.caption;
}
return "";
}
}
export class Group {
constructor(bot, originJid) {
this.bot = bot;
this.context = originJid;
}
get name() {
return (async () => {
if (this.__name != undefined) {
return this.__name;
}
const x = (await this.bot.connection.groupMetadata(this.context)).subject;
this.__name = x;
return x;
})();
}
set name(n) {
this.name = n;
}
get description() {
return (async () => {
if (this.__description != undefined) {
return this.__description;
}
const desc = (await this.bot.connection.groupMetadata(this.context)).desc;
this.__description = desc;
return desc;
})();
}
set description(d) {
this.__description = d;
}
get groupId() {
return (async () => {
if (this.__groupId != undefined)
return this.__groupId;
const id = (await this.bot.connection.groupMetadata(this.context)).id;
this.__groupId = id;
return id;
})();
}
set groupId(g) {
this.__groupId = g;
}
get members() {
return (async () => {
if (this.__members != undefined)
return this.__members;
const members = (await this.bot.connection.groupMetadata(this.context)).participants;
this.__members = members;
return members;
})();
}
set members(m) {
this.__members = m;
}
get admins() {
return (async () => {
if (this.__admins != undefined)
return this.__admins;
const a = (await this.bot.connection.groupMetadata(this.context))
.participants
.filter((element) => element.admin === "admin" || element.admin === "superadmin");
this.__admins = a;
return a;
})();
}
set admins(m) {
this.__admins = m;
}
get locked() {
return (async () => {
const announce = (await this.bot.connection.groupMetadata(this.context)).announce;
return announce !== undefined ? JSON.parse(JSON.stringify(announce).replace(/"/g, "")) : false;
})();
}
set locked(l) {
this.__locked = l;
}
get botIsAdmin() {
return (async () => {
if (this.__botIsAdmin != undefined)
return this.botIsAdmin;
const b = (await this.admins).map(x => x.id).includes(this.bot.botNumber);
this.__botIsAdmin = b;
return b;
})();
}
set botIsAdmin(l) {
this.__botIsAdmin = l;
}
}
export class Message {
constructor(bot, originalMessage, type, body, mentionedUsers, author, chatIsGroup, isMedia, hasQuotedMessage, quotedMessageType, quotedMessage, isReactionMessage, reactionMessage, group) {
this.bot = bot;
this.originalMessage = originalMessage;
this.type = type;
this.body = body;
this.mentionedUsers = mentionedUsers;
this.author = author;
this.chatIsGroup = chatIsGroup;
this.isMedia = isMedia;
this.hasQuotedMessage = hasQuotedMessage;
this.quotedMessageType = quotedMessageType;
this.quotedMessage = quotedMessage;
this.isReactionMessage = isReactionMessage;
this.reactionMessage = reactionMessage;
this.group = group;
this.bot = bot;
this.originalMessage = originalMessage;
this.type = type;
this.body = body;
this.mentionedUsers;
this.author = author;
this.group = group;
this.chatIsGroup = chatIsGroup;
this.isMedia = isMedia;
this.hasQuotedMessage = hasQuotedMessage;
this.quotedMessageType = quotedMessageType;
this.quotedMessage = quotedMessage;
this.isReactionMessage = isReactionMessage;
this.reactionMessage = reactionMessage;
}
replyText(text, options) {
return this.bot.replyText(this, text, options);
}
replyMedia(media, messageType, mimeType, mediaCaption, options) {
return this.bot.replyMedia(this, media, messageType, mimeType, mediaCaption, options);
}
react(reaction) {
const reactionMessage = {
react: {
text: reaction,
key: this.originalMessage.key
}
};
return this.bot.reactMessage(this, reactionMessage);
}
edit(text, options) {
return this.bot.sendTextMessage(this, text, Object.assign({ edit: this.originalMessage.key }, options));
}
async downloadMedia() {
const messageMedia = this.hasQuotedMessage ? JSON.parse(JSON.stringify(this.originalMessage).replace('quotedM', 'm')).message.extendedTextMessage.contextInfo : this.originalMessage;
const media = await downloadMediaMessage(messageMedia, "buffer", {});
return {
media: media,
messageType: this.type,
mimeType: "",
error: undefined
};
}
}