From 1dac0e2a9f1b809bae40d683fbaa137101d91517 Mon Sep 17 00:00:00 2001 From: SlavyanDesu Date: Sat, 14 Jan 2023 01:36:24 +0700 Subject: [PATCH] Fix canvas deprecated --- index.js | 145 +++++++++++++++++-------------------------------------- 1 file changed, 43 insertions(+), 102 deletions(-) diff --git a/index.js b/index.js index 3913d72f..173fc22c 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,13 @@ -/* eslint-disable no-unused-vars */ const { create, Client } = require('@open-wa/wa-automate') const { color, options } = require('./tools') -const { ind, eng } = require('./message/text/lang/') +const { eng } = require('./message/text/lang/') const { loader } = require('./function') const { version, bugs } = require('./package.json') +const { ownerBot } = require('./config.json') +const { groupLimit, memberLimit } = require('./database/bot/setting.json') const msgHandler = require('./message/index.js') const figlet = require('figlet') -const canvas = require('discord-canvas') -const { ownerBot } = require('./config.json') const fs = require('fs-extra') -const { groupLimit, memberLimit } = require('./database/bot/setting.json') -const express = require('express') -const app = express() const cron = require('node-cron') const exec = require('await-exec') @@ -21,88 +17,73 @@ const start = (bocchi = new Client()) => { console.log(color('=> Source code version:', 'yellow'), color(version)) console.log(color('=> Bug? Error? Suggestion? Visit here:', 'yellow'), color(bugs.url)) console.log(color('[BOCCHI]'), color('BocchiBot is now online!', 'yellow')) - console.log(color('[DEV]', 'cyan'), color('Welcome back, Owner! Hope you are doing well~', 'magenta')) - // Creating a localhost - app.get('/', (req, res) => res.status(200).send('Bocchi Client')) - const PORT = process.env.PORT || 8080 || 5000 || 3000 - app.listen(PORT, () => { - console.log(color('Localhost is running!', 'yellow')) - }) + if (!fs.existsSync('./temp')) { + console.log(color('=> Temp folder not found! Creating temp folder...', 'yellow')) + fs.mkdirSync('./temp/audio', { recursive: true }) + fs.mkdirSync('./temp/video', { recursive: true }) + console.log(color('=> Temp folder successfully created!', 'yellow')) + } - // Uncomment code di bawah untuk mengaktifkan auto-update file changes. Tidak disarankan untuk long-time use. - // Uncomment code below to activate auto-update file changes. Not recommended for long-time use. + // Uncomment code below to activate auto-update file changes. // loader.nocache('../message/index.js', (m) => console.log(color('[WATCH]', 'orange'), color(`=> '${m}'`, 'yellow'), 'file is updated!')) - bocchi.onStateChanged((state) => { - console.log(color('[BOCCHI]'), state) - if (state === 'OPENING') return client.refresh().catch(e => { - console.log("ERROR WHEN REFRESH >>>", e) - exec('pm2 restart .') - }) - if (state === 'UNPAIRED' || state === 'CONFLICT' || state === 'UNLAUNCHED') bocchi.forceRefocus() + bocchi.onStateChanged(async (state) => { + console.log(color('[BOCCHI]'), color(state, 'yellow')) + if (state === 'OPENING') { + await bocchi.refresh() + .catch((err) => { + console.log(color('[ERROR]', 'red'), color(err, 'yellow')) + console.log(color('[BOCCHI]'), color('Initiating force restart...', 'yellow')) + exec('pm2 restart all') + }) + } + if (state === 'UNPAIRED' || state === 'CONFLICT' || state === 'UNLAUNCHED') await bocchi.forceRefocus() }) bocchi.onAddedToGroup(async (chat) => { const gc = await bocchi.getAllGroups() console.log(color('[BOCCHI]'), 'Added to a new group. Name:', color(chat.contact.name, 'yellow'), 'Total members:', color(chat.groupMetadata.participants.length, 'yellow')) if (chat.groupMetadata.participants.includes(ownerBot)) { - await bocchi.sendText(chat.id, ind.addedGroup(chat)) + await bocchi.sendText(chat.id, eng.addedGroup(chat)) } else if (gc.length > groupLimit) { await bocchi.sendText(chat.id, `Max groups reached!\n\nCurrent status: ${gc.length}/${groupLimit}`) - await bocchi.deleteChat(chat.id) + await bocchi.clearChat(chat.id) await bocchi.leaveGroup(chat.id) } else if (chat.groupMetadata.participants.length < memberLimit) { await bocchi.sendText(chat.id, `Need at least ${memberLimit} members in group!`) - await bocchi.deleteChat(chat.id) + await bocchi.clearChat(chat.id) await bocchi.leaveGroup(chat.id) } else { - await bocchi.sendText(chat.id, ind.addedGroup(chat)) + await bocchi.sendText(chat.id, eng.addedGroup(chat)) } }) - bocchi.onMessage((message) => { - // Uncomment code di bawah untuk mengaktifkan auto-delete cache pesan. - // Uncomment code below to activate auto-delete message cache. - /* - bocchi.getAmountOfLoadedMessages() - .then((msg) => { - if (msg >= 1000) { - console.log(color('[BOCCHI]'), color(`Loaded message reach ${msg}, cuting message cache...`, 'yellow')) - bocchi.cutMsgCache() - console.log(color('[BOCCHI]'), color('Cache deleted!', 'yellow')) - } - }) - */ - - // Comment code msgHandler di bawah untuk mengaktifkan auto-update. Kemudian, uncomment code require di bawah msgHandler. + bocchi.onMessage((message) => { // Comment code below to activate auto-update. Then, uncomment require code below msgHandler. msgHandler(bocchi, message) // require('./message/index.js')(bocchi, message) }) bocchi.onIncomingCall(async (callData) => { - await bocchi.sendText(callData.peerJid, ind.blocked(ownerBot)) + await bocchi.sendText(callData.peerJid, eng.blocked(ownerBot)) await bocchi.contactBlock(callData.peerJid) console.log(color('[BLOCK]', 'red'), color(`${callData.peerJid} has been blocked.`, 'yellow')) }) - // Clear Chat Every 12 Hours - cron.schedule('0 0 */12 * * *', () => { - async function start() { - const cronallChat = await bocchi.getAllChats() - for (let getdchat of cronallChat) { - if (getdchat.isGroup == true){ - console.log(color('[BOCCHI]'), 'Clear Chat Group', 'yellow') - await bocchi.clearChat(getdchat.id) - } else { - await bocchi.deleteChat(getdchat.id) - } - } - console.log(color('[BOCCHI]'), 'Success Clear All Chat!', 'yellow') + // Clear chats every 12 hour + cron.schedule('0 */12 * * *', async () => { + const allChats = await bocchi.getAllChats() + for (let chats of allChats) { + if (chats.isGroup === true) { + console.log(color('[BOCCHI]'), color('Clearing chats...', 'yellow')) + await bocchi.clearChat(chats.id) + } else { + await bocchi.deleteChat(chats.id) } - start(); - }) + } + console.log(color('[BOCCHI]'), color('Success cleared all chats!', 'yellow')) + }) bocchi.onGlobalParticipantsChanged(async (event) => { const _welcome = JSON.parse(fs.readFileSync('./database/group/welcome.json')) @@ -111,53 +92,13 @@ const start = (bocchi = new Client()) => { const pcChat = await bocchi.getContact(event.who) let { pushname, verifiedName, formattedName } = pcChat pushname = pushname || verifiedName || formattedName - const { name, groupMetadata } = gcChat + const { groupMetadata } = gcChat const botNumbers = await bocchi.getHostNumber() + '@c.us' try { if (event.action === 'add' && event.who !== botNumbers && isWelcome) { - const pic = await bocchi.getProfilePicFromServer(event.who) - if (pic === `ERROR: 401` || pic === 'ERROR: 404') { - var picx = 'https://i.ibb.co/Tq7d7TZ/age-hananta-495-photo.png' - } else { - picx = pic - } - const welcomer = await new canvas.Welcome() - .setUsername(pushname) - .setDiscriminator(event.who.substring(6, 10)) - .setMemberCount(groupMetadata.participants.length) - .setGuildName(name) - .setAvatar(picx) - .setColor('border', '#00100C') - .setColor('username-box', '#00100C') - .setColor('discriminator-box', '#00100C') - .setColor('message-box', '#00100C') - .setColor('title', '#00FFFF') - .setBackground('https://www.photohdx.com/images/2016/05/red-blurry-background.jpg') - .toAttachment() - const base64 = `data:image/png;base64,${welcomer.toBuffer().toString('base64')}` - await bocchi.sendFile(event.chat, base64, 'welcome.png', `Welcome ${pushname}!`) + await bocchi.sendText(event.chat, `Welcome, ${pushname}! You are the *${groupMetadata.participants.length}* member.`) } else if (event.action === 'remove' && event.who !== botNumbers && isWelcome) { - const pic = await bocchi.getProfilePicFromServer(event.who) - if (pic === `ERROR: 401` || pic === 'ERROR: 404') { - var picxs = 'https://i.ibb.co/Tq7d7TZ/age-hananta-495-photo.png' - } else { - picxs = pic - } - const bye = await new canvas.Goodbye() - .setUsername(pushname) - .setDiscriminator(event.who.substring(6, 10)) - .setMemberCount(groupMetadata.participants.length) - .setGuildName(name) - .setAvatar(picxs) - .setColor('border', '#00100C') - .setColor('username-box', '#00100C') - .setColor('discriminator-box', '#00100C') - .setColor('message-box', '#00100C') - .setColor('title', '#00FFFF') - .setBackground('https://www.photohdx.com/images/2016/05/red-blurry-background.jpg') - .toAttachment() - const base64 = `data:image/png;base64,${bye.toBuffer().toString('base64')}` - await bocchi.sendFile(event.chat, base64, 'welcome.png', `Bye ${pushname}, we will miss you~`) + await bocchi.sendText(event.chat, `Bye, ${pushname}. We will miss you. :(`) } } catch (err) { console.error(err) @@ -167,4 +108,4 @@ const start = (bocchi = new Client()) => { create(options(start)) .then((bocchi) => start(bocchi)) - .catch((err) => console.error(err)) + .catch((err) => console.error(err)) \ No newline at end of file