https://github.com/schwartzblat/baileys-minigames
https://github.com/schwartzblat/baileys-minigames
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/schwartzblat/baileys-minigames
- Owner: Schwartzblat
- Created: 2022-02-27T20:18:26.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-06-22T15:39:01.000Z (almost 3 years ago)
- Last Synced: 2025-03-11T16:51:17.238Z (3 months ago)
- Language: JavaScript
- Size: 3.91 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
wa-minigames
A module that helps you to create minigames with whatsapp-web.js.
## Installation```console
npm i @adiwajshing/baileys
npm install baileys-minigames
```
## Example Usage```js
const {default: makeWASocket, makeInMemoryStore, useSingleFileAuthState, DisconnectReason} = require('@adiwajshing/baileys');
const {Boom} = require('@hapi/boom');
const P = require('pino');
const { MiniGames, MiniGame } = require('index.js');class MyGame extends MiniGame {
constructor(message, sock){
super();
this.sock = sock;
this.chatId = message.key.remoteJid;
this.answer = Math.floor(Math.random() * 10).toString();
this.sock.sendMessage(this.chatId, {text: "Game Started! Guess the number!"});
}
async procMessage(message){
if (message.body===this.answer){
await this.sock.sendMessage(this.chatId, {text: 'You are right!'});
this.gameOver();
}else if (!message.fromMe){
await this.sock.sendMessage(this.chatId, {text: 'You are wrong.'});
}
}
gameOver(){
super.gameOver();
this.socket.sendMessage(this.chatId, {text: 'Game Over!'});
}
}const store = makeInMemoryStore({ logger: P().child({ level: 'fatal', stream: 'store'}) });
store.readFromFile('baileys_store_multi.json');
setInterval(() => {
store.writeToFile('baileys_store_multi.json');
}, 10_000)const { state, saveState } = useSingleFileAuthState('auth_info_multi.json')
const miniGames = new MiniGames();
// start a connection
const startSock = () => {
const sock = makeWASocket({
logger: P({ level: 'fatal' }),
printQRInTerminal: true,
auth: state
})
store.bind(sock.ev)sock.ev.on('messages.upsert', m => {
const message = m.messages[0]
message.body = message?.message?.conversation || message?.message?.extendedTextMessage?.text
|| message?.message?.imageMessage?.caption|| message?.message?.videoMessage?.caption;
miniGames.forwardMsg(message, sock);
})sock.ev.on('connection.update', (update) => {
const { connection, lastDisconnect } = update
if(connection === 'close') {
if((new Boom(lastDisconnect.error))?.output?.statusCode !== DisconnectReason.loggedOut) {
startSock()
}
}
})
sock.ev.on('creds.update', saveState)
return sock
}startSock()
```