Skip to content

Commit

Permalink
add log every receive message
Browse files Browse the repository at this point in the history
  • Loading branch information
Kry9toN committed Jan 17, 2021
1 parent 9473942 commit 092bb88
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
46 changes: 44 additions & 2 deletions krypton.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const {
const { Collection } = require('discord.js')
const { readdirSync } = require('fs')
const { join } = require('path')
const { start, success } = require('./utils/functions')
const { start, success, getGroupAdmins } = require('./utils/functions')
const { color } = require('./utils/color')
const fs = require('fs')
const moment = require('moment-timezone')

async function krypton () {
const client = new WAConnection()
Expand Down Expand Up @@ -36,7 +37,7 @@ async function krypton () {
// Server connected
await client.on('open', () => {
success('1', '[SERVER] Terhubung')
console.log('🤖',color('KryPtoN Bot Sudah siap!!', 'green'))
console.log('🤖', color('KryPtoN Bot Sudah siap!!', 'green'))
})

// Create file for sessions
Expand All @@ -51,12 +52,53 @@ async function krypton () {
if (!chat.message) return
if (chat.key.remoteJid == 'status@broadcast') return
if (chat.key.fromMe) return

// Variable
const from = chat.key.remoteJid
const type = Object.keys(chat.message)[0]
body = (type === 'conversation' && chat.message.conversation.startsWith(prefix)) ? chat.message.conversation : (type == 'imageMessage') && chat.message.imageMessage.caption.startsWith(prefix) ? chat.message.imageMessage.caption : (type == 'videoMessage') && chat.message.videoMessage.caption.startsWith(prefix) ? chat.message.videoMessage.caption : (type == 'extendedTextMessage') && chat.message.extendedTextMessage.text.startsWith(prefix) ? chat.message.extendedTextMessage.text : ''
const args = body.trim().split(/ +/).slice(1)
const isCmd = body.startsWith(prefix)
const commandName = body.slice(1).trim().split(/ +/).shift().toLowerCase()
const time = moment.tz('Asia/Jakarta').format('DD/MM HH:mm:ss')
const content = JSON.stringify(chat.message)

const botNumber = client.user.jid
const ownerNumber = ["[email protected]"] // replace this with your number
const isGroup = from.endsWith('@g.us')
const sender = isGroup ? chat.participant : chat.key.remoteJid
const groupMetadata = isGroup ? await client.groupMetadata(from) : ''
const groupName = isGroup ? groupMetadata.subject : ''
const groupId = isGroup ? groupMetadata.jid : ''
const groupMembers = isGroup ? groupMetadata.participants : ''
const groupAdmins = isGroup ? getGroupAdmins(groupMembers) : ''
const isBotGroupAdmins = groupAdmins.includes(botNumber) || false
const isGroupAdmins = groupAdmins.includes(sender) || false
const isOwner = ownerNumber.includes(sender)
const isUrl = (url) => {
return url.match(new RegExp(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)/, 'gi'))
}
const reply = (teks) => {
client.sendMessage(from, teks, text, {quoted:chat})
}
const sendMess = (hehe, teks) => {
client.sendMessage(hehe, teks, text)
}
const mentions = (teks, memberr, id) => {
(id == null || id == undefined || id == false) ? client.sendMessage(from, teks.trim(), extendedText, {contextInfo: {"mentionedJid": memberr}}) : client.sendMessage(from, teks.trim(), extendedText, {quoted: chat, contextInfo: {"mentionedJid": memberr}})
}

colors = ['red','white','black','blue','yellow','green']
const isMedia = (type === 'imageMessage' || type === 'videoMessage')
const isQuotedImage = type === 'extendedTextMessage' && content.includes('imageMessage')
const isQuotedVideo = type === 'extendedTextMessage' && content.includes('videoMessage')
const isQuotedSticker = type === 'extendedTextMessage' && content.includes('stickerMessage')

// Logging Message
if (!isGroup && isCmd) console.log('\x1b[1;31m~\x1b[1;37m>', '[\x1b[1;32mEXEC\x1b[1;37m]', time, color(commandName), 'from', color(sender.split('@')[0]), 'args :', color(args.length))
if (!isGroup && !isCmd) console.log('\x1b[1;31m~\x1b[1;37m>', '[\x1b[1;31mRECV\x1b[1;37m]', time, color('Message'), 'from', color(sender.split('@')[0]), 'args :', color(args.length))
if (isCmd && isGroup) console.log('\x1b[1;31m~\x1b[1;37m>', '[\x1b[1;32mEXEC\x1b[1;37m]', time, color(commandName), 'from', color(sender.split('@')[0]), 'in', color(groupName), 'args :', color(args.length))
if (!isCmd && isGroup) console.log('\x1b[1;31m~\x1b[1;37m>', '[\x1b[1;31mRECV\x1b[1;37m]', time, color('Message'), 'from', color(sender.split('@')[0]), 'in', color(groupName), 'args :', color(args.length))

/**
* Import all commands
Expand Down
11 changes: 10 additions & 1 deletion utils/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,17 @@ const processTime = (timestamp, now) => {
return moment.duration(now - moment(timestamp)).asSeconds()
}

const getGroupAdmins = (participants) => {
admins = []
for (let i of participants) {
i.isAdmin ? admins.push(i.jid) : ''
}
return admins
}

module.exports = {
start,
success,
processTime
processTime,
getGroupAdmins
}

0 comments on commit 092bb88

Please sign in to comment.