Skip to content

Commit

Permalink
some feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Nurutomo committed Feb 3, 2021
1 parent 9c21a67 commit d781472
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 30 deletions.
22 changes: 13 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ console.log('Starting...')
let { spawn } = require('child_process')
let path = require('path')
let fs = require('fs')
let package = require('./package.json')
const CFonts = require('cfonts')
CFonts.say('TERMUX WHATSAPP BOT', {
CFonts.say('Lightweight\nWhatsApp Bot', {
font: 'chrome',
align: 'center',
gradient: ['red', 'magenta']
})
CFonts.say('\'wabot-aq\' By @Nurutomo', {
CFonts.say(`'${package.name}' By @${package.author.name || package.author}`, {
font: 'console',
align: 'center',
gradient: ['red', 'magenta']
})

function start() {
let args = [path.join(__dirname, 'main.js'), ...process.argv.slice(2)]
function start(file) {
let args = [path.join(__dirname, file), ...process.argv.slice(2)]
CFonts.say([process.argv[0], ...args].join(' '), {
font: 'console',
align: 'center',
Expand All @@ -25,10 +26,12 @@ function start() {
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
})
.on('message', data => {
if (data == 'reset') {
console.log('RESET')
p.kill()
start()
console.log('[RECEIVED]', data)
switch (data) {
case 'reset':
p.kill()
start.apply(this, arguments)
break
}
})
.on('error', e => {
Expand All @@ -38,6 +41,7 @@ function start() {
fs.unwatchFile(args[0])
})
})
// console.log(p)
}

start()
start('main.js')
19 changes: 19 additions & 0 deletions lib/logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
let stdouts = []
module.exports = (maxLength = 200) => {
let oldWrite = process.stdout.write.bind(process.stdout)
module.exports.disable = () => {
module.exports.isModified = false
return process.stdout.write = oldWrite
}
process.stdout.write = (chunk, encoding, callback) => {
stdouts.push(Buffer.from(chunk, encoding))
oldWrite(chunk, encoding, callback)
if (stdouts.length > maxLength) stdouts.shift()
}
module.exports.isModified = true
return module.exports
}

module.exports.isModified = false
module.exports.logs = () => Buffer.concat(stdouts)

6 changes: 3 additions & 3 deletions lib/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ ${chalk.green('%s')} ${chalk.yellow('%s%s')} ${chalk.blueBright('to')} ${chalk.g
// console.log({ depth, type, formatted, text, monospace }, formatted)
return formatted
}
log = log
.replace(urlRegex, (url, i, text) => {
if (log.length < 4096)
log = log.replace(urlRegex, (url, i, text) => {
let end = url.length + i
return i === 0 || end === text.length || (/^\s$/.test(text[end]) && /^\s$/.test(text[i - 1])) ? chalk.blueBright(url) : url
})
.replace(mdRegex, mdFormat(4))
log = log.replace(mdRegex, mdFormat(4))
if (m.mentionedJid) for (let user of m.mentionedJid) log = log.replace('@' + user.split`@`[0], chalk.blueBright('@' + conn.getName(user)))
console.log(m.error ? chalk.red(log) : m.isCommand ? chalk.yellow(log) : log)
}
Expand Down
11 changes: 7 additions & 4 deletions lib/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,16 @@ exports.WAConnection = (_WAConnection) => {
mime: 'application/octet-stream',
ext: '.bin'
}
let opt = { filename, caption }
if (quoted) opt.quoted = quoted
if (!type) {
if (!options.asDocument && typeof file === 'string' && path == file) return await this.sendMessage(jid, file, MessageType.extended, options)
else options.asDocument = true
if (!options.asDocument && typeof file === 'string') {
delete opt.filename
delete opt.caption
return await this.sendMessage(jid, [file, caption].join('\n\n\n').trim(), MessageType.extended, opt)
} else options.asDocument = true
}
let mtype = ''
let opt = { filename, caption }
if (options.asSticker) {
mtype = MessageType.sticker
try { throw { json: JSON.parse(file) } }
Expand All @@ -134,7 +138,6 @@ exports.WAConnection = (_WAConnection) => {
}
delete options.asDocument
delete options.asSticker
if (quoted) opt.quoted = quoted
if (!opt.caption) delete opt.caption
return await this.sendMessage(jid, file, mtype, {...opt, ...options})
}
Expand Down
5 changes: 4 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ let { WAConnection: _WAConnection, WA_MESSAGE_STUB_TYPES } = require('@adiwajshi
let { generate } = require('qrcode-terminal')
let qrcode = require('qrcode')
let simple = require('./lib/simple')
let logs = require('./lib/logs')
let yargs = require('yargs/yargs')
let syntaxerror = require('syntax-error')
let fetch = require('node-fetch')
Expand All @@ -26,10 +27,11 @@ global.APIKeys = { // APIKey Here
}


global.API = (name, path = '/', query = {}, apikeyqueryname) => (name in global.APIs ? global.APIs[name] : name) + path + (query || apikeyqueryname ? '?' + Object.entries({...query, [apikeyqueryname]: global.APIKeys[name in global.APIs ? global.APIs[name] : name]}).map(([key, val]) => encodeURIComponent(key) + (val || val === false ? '=' + encodeURIComponent(val) : '')).join('&') : '')
global.API = (name, path = '/', query = {}, apikeyqueryname) => (name in global.APIs ? global.APIs[name] : name) + path + (query || apikeyqueryname ? '?' + Object.entries({...query, ...(apikeyqueryname ? { [apikeyqueryname]: global.APIKeys[name in global.APIs ? global.APIs[name] : name]} : {})}).map(([key, val]) => encodeURIComponent(key) + (val || val === false ? '=' + encodeURIComponent(val) : '')).join('&') : '')
global.timestamp = {
start: new Date
}
global.LOGGER = logs()
const PORT = process.env.PORT || 3000
let opts = yargs(process.argv.slice(2)).exitProcess(false).parse()
global.opts = Object.freeze({...opts})
Expand Down Expand Up @@ -120,6 +122,7 @@ conn.handler = async function (m) {
if ((usedPrefix = (_prefix.exec(m.text) || '')[0])) {
let noPrefix = m.text.replace(usedPrefix, '')
let [command, ...args] = noPrefix.trim().split` `.filter(v=>v)
args = args || []
let _args = noPrefix.trim().split` `.slice(1)
let text = _args.join` `
command = (command || '').toLowerCase()
Expand Down
2 changes: 1 addition & 1 deletion plugins/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let handler = async (m, { conn, usedPrefix, command, text, noPrefix, args, grou
if (--i < 1) return
console.log(...args)
return conn.reply(m.chat, util.format(...args), m)
}, m, handler, require, conn, CustomArray, {...process, exit: function exit() { return ':P' }}, args, groupMetadata)
}, m, handler, require, conn, CustomArray, process, args, groupMetadata)
} catch (e) {
let err = await syntaxerror(_text, 'Execution Function', {
allowReturnOutsideFunction: true,
Expand Down
9 changes: 5 additions & 4 deletions plugins/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ let fetch = require('node-fetch')
let util = require('util')
let handler = async (m, { text }) => {
let res = await fetch(text)
let txt = await res.text()
if (!/text|json/.test(res.headers.get('content-type'))) return conn.sendFile(m.chat, text, 'file', text, m)
let txt = await res.buffer()
try {
txt = util.format(JSON.parse(txt))
txt = util.format(JSON.parse(txt+''))
} catch (e) {
txt = txt
txt = txt + ''
} finally {
m.reply(txt.slice(0, 65536))
m.reply(txt.slice(0, 65536) + '')
}
}
handler.help = ['fetch', 'get'].map(v => v + ' <url>')
Expand Down
18 changes: 11 additions & 7 deletions plugins/profile.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
let PhoneNumber = require('awesome-phonenumber')
let handler = async (m, { conn }) => {
let pp = './src/avatar_contact.png'
let who = m.mentionedJid && m.mentionedJid[0] ? m.mentionedJid[0] : m.fromMe ? conn.user.jid : m.sender
try {
pp = await conn.getProfilePicture(m.sender)
pp = await conn.getProfilePicture(who)
} catch (e) {

} finally {
let name = m.fromMe ? conn.user : conn.contacts[m.sender]
let name = conn.getName(who)
let about = (await conn.getStatus(who)).status
let str = `
Name: ${name.vnmae || name.notify || name.name || ('+' + name.jid.split`@`[0])} (@${m.sender.replace(/@.+/, '')})
Number: +${m.sender.split`@`[0]}
Link: https://wa.me/${m.sender.split`@`[0]}
Name: ${name} (@${who.replace(/@.+/, '')})
About: ${about}
Number: ${PhoneNumber('+' + who.replace('@s.whatsapp.net', '')).getNumber('international')}
Link: https://wa.me/${who.split`@`[0]}
`.trim()
let mentionedJid = [m.sender]
let mentionedJid = [who]
conn.sendFile(m.chat, pp, 'pp.jpg', str, m, false, { contextInfo: { mentionedJid }})
}
}
handler.help = ['profile']
handler.help = ['profile [@user]']
handler.tags = ['tools']
handler.command = /^profile$/i
module.exports = handler
Expand Down
56 changes: 56 additions & 0 deletions plugins/runningtext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
let handler = async (m, { conn, text }) => {
m.reply('_Sedang membuat..._\n*Mohon tunggu sekitar 1 menit*')
let q
try { q = m.quoted.download() }
catch (e) { q = m.download() }
running(await q).then(vid => conn.sendFile(m.chat, vid, 'run.mp4', '*© Nurutomo*\nMade with FFmpeg', m))
}
handler.help = ['run']
handler.tags = ['tools']
handler.command = /^run$/i
handler.limit = true

module.exports = handler

let { spawn } = require('child_process')
let fs = require('fs')
let path = require('path')
let tmp = path.join(__dirname, '../tmp/')
function running(img) {
return new Promise((resolve, reject) => {
let layers = [
'color=s=512x512:d=10:r=60[bg]',
'[0:v]scale=-2:512[img]',
"[bg][img]overlay=x='(w+(2*h))*((n/60)*(-1/10))+(2*h)'"
]

let n = + new Date + 'run.jpg'
let i = path.join(tmp, n)
fs.writeFileSync(i, img)
console.log(img)
let o = path.join(tmp, n + '.mp4')
let args = [
'-y',
'-i', i,
'-t', '10',
'-filter_complex', layers.join(';'),
'-pix_fmt', 'yuv420p',
'-crf', '18',
o
]
console.log('ffmpeg', ...args)
spawn('ffmpeg', args, { stdio: 'inherit' })
.on('error', reject)
.on('close', () => {
try {
fs.unlinkSync(i)
resolve(fs.readFileSync(o))
fs.unlinkSync(o)
} catch (e) {
reject(e)
}
})
//.stderr.on('data', a => console.log(a+''))
})
}

23 changes: 23 additions & 0 deletions plugins/textpro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
let fetch = require('node-fetch')
let split = '|'
let handler = async (m, { conn, args: [effect], text: txt }) => {
let { effects } = await (await (fetch(global.API('xteam', '/textpro')))).json()
if (!effect) throw '*List Effect*\n\n' + effects.sort((a, b) => a - b).join('\n')
effect = effect.toLowerCase()
if (!effect in effects) throw `Efek *${effect}* tidak ditemukan`
let [text, ...text2] = txt.replace(effect, '').trimStart().split(split)
text2 = text2.join(split)
let url = global.API('xteam', '/textpro/' + effect, { text, text2 }, 'APIKEY')
try {
conn.sendFile(m.chat, url, 'textpro.jpg', `*TEXTPRO*\n*Effect:* ${effect}`, m)
} catch (e) {
m.reply(url)
throw e
}
}
handler.help = ['textpro'].map(v => v + ' <effect> <text>|[text2]')
handler.tags = ['tools']
handler.command = /^(textpro)$/i

module.exports = handler

2 changes: 1 addition & 1 deletion plugins/toimg.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let handler = async (m, { conn }) => {
let sticker = await conn.downloadM(q)
if (!sticker) throw sticker
let bufs = []
let im = spawn('convert', ['webp:-', 'jpeg:-'])
let im = spawn('convert', ['webp:-', 'png:-'])
im.on('error',e => conn.reply(m.chat, util.format(e), m))
im.stdout.on('data', chunk => bufs.push(chunk))
im.stdin.write(sticker)
Expand Down

0 comments on commit d781472

Please sign in to comment.