diff --git a/example.js b/example.js index 1729fbe..c8f8181 100644 --- a/example.js +++ b/example.js @@ -64,6 +64,15 @@ client.on('message', async msg => { } else { msg.reply('This command can only be used in a group!'); } + } else if(msg.body.startsWith('!join ')) { + const inviteCode = msg.body.split(' ')[1]; + + try { + const chat = await client.acceptInvite(inviteCode); + msg.reply(`Joined *${chat.name}*!`); + } catch(e) { + msg.reply('That invite code seems to be invalid.'); + } } else if(msg.body == '!groupinfo') { let chat = await msg.getChat(); if(chat.isGroup) { diff --git a/src/Client.js b/src/Client.js index 118cd4f..817c007 100644 --- a/src/Client.js +++ b/src/Client.js @@ -175,6 +175,19 @@ class Client extends EventEmitter { return ChatFactory.create(this, chat); } + /** + * Accepts an invite by code + * @param {string} inviteCode + */ + async acceptInvite(inviteCode) { + const chat = await this.pupPage.evaluate(async inviteCode => { + const chatId = await Store.Invite.sendJoinGroupViaInvite(inviteCode); + return WWebJS.getChat(chatId._serialized); + }, inviteCode); + + return ChatFactory.create(this.client, chat); + } + } module.exports = Client; diff --git a/src/util/Injected.js b/src/util/Injected.js index afda1bc..c4365b4 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -13,6 +13,7 @@ exports.ExposeStore = (moduleRaidStr) => { window.Store.genId = window.mR.findModule((module) => module.default && typeof module.default === 'function' && module.default.toString().match(/crypto/))[0].default; window.Store.SendMessage = window.mR.findModule("addAndSendMsgToChat")[0]; window.Store.MsgKey = window.mR.findModule((module) => module.default && module.default.fromString)[0].default; + window.Store.Invite = window.mR.findModule("sendJoinGroupViaInvite")[0]; } exports.LoadUtils = () => {