Skip to content

Instantly share code, notes, and snippets.

@kamuridesu
Last active April 1, 2023 02:56
Show Gist options
  • Select an option

  • Save kamuridesu/ed1e78c47f992c457fb0dbd9917361e4 to your computer and use it in GitHub Desktop.

Select an option

Save kamuridesu/ed1e78c47f992c457fb0dbd9917361e4 to your computer and use it in GitHub Desktop.
import Pino from 'pino';
import { default as makeWASocket, DisconnectReason, makeInMemoryStore, useMultiFileAuthState, WAMessage } from '@adiwajshing/baileys'
// also tried `import makeWASocket from ..., same issue
import { Boom } from '@hapi/boom'
import { Bot } from "../types/bot.js";
interface Media {
media: Buffer | string;
messageType: string;
mimeType: string;
error: Error;
}
const logger = Pino().child({
level: 'error',
stream: 'store',
});
const storage = makeInMemoryStore({ logger });
const {
state,
saveCreds,
} = await useMultiFileAuthState('./states');
class WABot implements Bot {
public connection?: ReturnType<typeof makeWASocket>;
reconnectOnClose: boolean;
public readonly botName: string;
public readonly prefix: string;
public readonly botNumber: string;
public readonly ownerNumber: string;
public readonly commandsFilename: string;
public readonly language: string;
constructor() {
this.connection = undefined;
this.reconnectOnClose = true;
}
async init(messageHandler: { handle: (message: WAMessage, bot: Bot) => void }): Promise<void> {
this.connection = makeWASocket({
printQRInTerminal: true,
auth: state,
});
this.connection.ev.on('creds.update', saveCreds);
this.connection.ev.on('connection.update', (update) => {
const { connection, lastDisconnect } = update
if (connection === 'close') {
const shouldReconnect = (lastDisconnect?.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut
console.log('connection closed due to ', lastDisconnect?.error, ', reconnecting ', shouldReconnect)
if (shouldReconnect) {
this.init(messageHandler);
}
} else if (connection === 'open') {
console.log('opened connection')
}
})
storage.bind(this.connection.ev);
}
}
import Pino from 'pino';
import { default as makeWASocket, DisconnectReason, makeInMemoryStore, useMultiFileAuthState } from '@adiwajshing/baileys';
const logger = Pino().child({
level: 'error',
stream: 'store',
});
const storage = makeInMemoryStore({ logger });
const { state, saveCreds, } = await useMultiFileAuthState('./states');
class WABot {
constructor(botName = 'bot') {
this.connection = undefined;
this.reconnectOnClose = true;
}
async init(messageHandler) {
this.connection = makeWASocket({
printQRInTerminal: true,
auth: state,
});
this.connection.ev.on('creds.update', saveCreds);
this.connection.ev.on('connection.update', (update) => {
var _a, _b;
const { connection, lastDisconnect } = update;
if (connection === 'close') {
const shouldReconnect = ((_b = (_a = lastDisconnect === null || lastDisconnect === void 0 ? void 0 : lastDisconnect.error) === null || _a === void 0 ? void 0 : _a.output) === null || _b === void 0 ? void 0 : _b.statusCode) !== DisconnectReason.loggedOut;
console.log('connection closed due to ', lastDisconnect === null || lastDisconnect === void 0 ? void 0 : lastDisconnect.error, ', reconnecting ', shouldReconnect);
// reconnect if not logged out
if (shouldReconnect) {
this.init(messageHandler);
}
}
else if (connection === 'open') {
console.log('opened connection');
}
});
storage.bind(this.connection.ev);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment