Skip to content

Commit

Permalink
refactor variable and initial welcome image
Browse files Browse the repository at this point in the history
  • Loading branch information
Kry9toN committed Jan 17, 2021
1 parent 536f58a commit ce6c76d
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 77 deletions.
Binary file added assets/fonts/KeepCalm-Medium.ttf
Binary file not shown.
Binary file added assets/fonts/LuckiestGuy-Regular.ttf
Binary file not shown.
Binary file added assets/fonts/SketchMatch.ttf
Binary file not shown.
Binary file added assets/fonts/theboldfont.ttf
Binary file not shown.
Binary file added assets/img/1px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/img/default-avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions command/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ module.exports = {
name: 'help',
aliases: ['h'],
description: 'Menampilkan semua perintah dan deskripsi',
execute (client, from) {
execute (client) {
const commands = client.cmd.array()
let text = 'Command di bot ini\n\nPrefix: !\n'
commands.forEach((cmd) => {
text += `- *${cmd.name}* ${cmd.aliases ? `(${cmd.aliases})` : ''}\n${cmd.description}\n`
})
return client.sendMessage(from, text, MessageType.text)
return client.sendMessage(client.from, text, MessageType.text)
}
}
4 changes: 2 additions & 2 deletions command/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
name: 'ping',
cooldown: 10,
description: 'Menampilkan rata-rata bot merespon',
execute (client, from) {
client.sendMessage(from, `Pong!!\n${processTime(client.pingStart, moment())} _detik_`, MessageType.text).catch(console.error)
execute (client) {
client.sendMessage(client.from, `Pong!!\n${processTime(client.pingStart, moment())} _detik_`, MessageType.text).catch(console.error)
}
}
144 changes: 74 additions & 70 deletions krypton.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { start, success, getGroupAdmins, getBuffer } = require('./utils/functions
const { color } = require('./utils/color')
const fs = require('fs')
const moment = require('moment-timezone')
const { welcome, goodbye } = require('./utils/greeting')

async function krypton () {
const client = new WAConnection()
Expand Down Expand Up @@ -44,35 +45,37 @@ async function krypton () {
await client.connect({ timeoutMs: 30 * 1000 })
fs.writeFileSync('./sessions/krypton-sessions.json', JSON.stringify(client.base64EncodedAuthInfo(), null, '\t'))

await client.on('group-participants-update', async (greeting) => {
try {
const mdata = await client.groupMetadata(greeting.jid)
console.log(greeting)
if (greeting.action == 'add') {
num = greeting.participants[0]
try {
ppimg = await client.getProfilePicture(`${greeting.participants[0].split('@')[0]}@c.us`)
} catch {
ppimg = 'https://i0.wp.com/www.gambarunik.id/wp-content/uploads/2019/06/Top-Gambar-Foto-Profil-Kosong-Lucu-Tergokil-.jpg'
}
teks = `Halo @${num.split('@')[0]}\nSelamat datang di group *${mdata.subject}*`
let buff = await getBuffer(ppimg)
client.sendMessage(mdata.id, buff, MessageType.image, {caption: teks, contextInfo: {"mentionedJid": [num]}})
} else if (greeting.action == 'remove') {
num = greeting.participants[0]
try {
ppimg = await client.getProfilePicture(`${num.split('@')[0]}@c.us`)
} catch {
ppimg = 'https://i0.wp.com/www.gambarunik.id/wp-content/uploads/2019/06/Top-Gambar-Foto-Profil-Kosong-Lucu-Tergokil-.jpg'
}
teks = `Sayonara @${num.split('@')[0]}👋`
let buff = await getBuffer(ppimg)
client.sendMessage(mdata.id, buff, MessageType.image, {caption: teks, contextInfo: {"mentionedJid": [num]}})
}
} catch (e) {
console.log('Error : %s', color(e, 'red'))
}
})
await client.on('group-participants-update', async (greeting) => {
try {
const mdata = await client.groupMetadata(greeting.jid)
console.log(greeting)
if (greeting.action == 'add') {
num = greeting.participants[0]
try {
ppimg = await client.getProfilePicture(`${greeting.participants[0].split('@')[0]}@c.us`)
} catch {
ppimg = 'https://i0.wp.com/www.gambarunik.id/wp-content/uploads/2019/06/Top-Gambar-Foto-Profil-Kosong-Lucu-Tergokil-.jpg'
}
// teks = `Halo @${num.split('@')[0]}\nSelamat datang di group *${mdata.subject}*`
const buff = await getBuffer(ppimg)
await welcome('uwu', mdata.subject, buff).then(async (hasil) => {
await client.sendMessage(mdata.id, hasil, MessageType.image)
})
} else if (greeting.action == 'remove') {
num = greeting.participants[0]
try {
ppimg = await client.getProfilePicture(`${num.split('@')[0]}@c.us`)
} catch {
ppimg = 'https://i0.wp.com/www.gambarunik.id/wp-content/uploads/2019/06/Top-Gambar-Foto-Profil-Kosong-Lucu-Tergokil-.jpg'
}
teks = `Sayonara @${num.split('@')[0]}👋`
const buff = await getBuffer(ppimg)
client.sendMessage(mdata.id, buff, MessageType.image, { caption: teks, contextInfo: { mentionedJid: [num] } })
}
} catch (e) {
console.log('Error : %s', color(e, 'red'))
}
})

await client.on('chat-update', async (chat) => {
client.pingStart = Date.now()
Expand All @@ -84,7 +87,6 @@ async function krypton () {
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)
Expand All @@ -94,41 +96,43 @@ async function krypton () {
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')
const ownerNumber = ['[email protected]'] // replace this with your number
const sender = client.isGroup ? chat.participant : chat.key.remoteJid
const groupMetadata = client.isGroup ? await client.groupMetadata(client.from) : ''
const groupName = client.isGroup ? groupMetadata.subject : ''
const groupMembers = client.isGroup ? groupMetadata.participants : ''
const groupAdmins = client.isGroup ? getGroupAdmins(groupMembers) : ''
client.from = chat.key.remoteJid
client.isGroup = client.from.endsWith('@g.us')
client.groupId = client.isGroup ? groupMetadata.jid : ''
client.isBotGroupAdmins = groupAdmins.includes(botNumber) || false
client.isGroupAdmins = groupAdmins.includes(sender) || false
client.isOwner = ownerNumber.includes(sender)
client.isUrl = (url) => {
// eslint-disable-next-line prefer-regex-literals
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'))
}
client.reply = (teks) => {
client.sendMessage(client.from, teks, text, { quoted: chat })
}
client.sendMess = (hehe, teks) => {
client.sendMessage(hehe, teks, text)
}
client.mentions = (teks, memberr, id) => {
(id == null || id == undefined || id == false) ? client.sendMessage(client.from, teks.trim(), extendedText, { contextInfo: { mentionedJid: memberr } }) : client.sendMessage(client.from, teks.trim(), extendedText, { quoted: chat, contextInfo: { mentionedJid: memberr } })
}

colors = ['red', 'white', 'black', 'blue', 'yellow', 'green']
client.isMedia = (type === 'imageMessage' || type === 'videoMessage')
client.isQuotedImage = type === 'extendedTextMessage' && content.includes('imageMessage')
client.isQuotedVideo = type === 'extendedTextMessage' && content.includes('videoMessage')
client.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))
if (!client.isGroup && isCmd) console.log('\x1b[1;31m~\x1b[1;37m>', '[\x1b[1;32mEXEC\x1b[1;37m]', time, color(commandName), 'client.from', color(sender.split('@')[0]), 'args :', color(args.length))
if (!client.isGroup && !isCmd) console.log('\x1b[1;31m~\x1b[1;37m>', '[\x1b[1;31mRECV\x1b[1;37m]', time, color('Message'), 'client.from', color(sender.split('@')[0]), 'args :', color(args.length))
if (isCmd && client.isGroup) console.log('\x1b[1;31m~\x1b[1;37m>', '[\x1b[1;32mEXEC\x1b[1;37m]', time, color(commandName), 'client.from', color(sender.split('@')[0]), 'in', color(groupName), 'args :', color(args.length))
if (!isCmd && client.isGroup) console.log('\x1b[1;31m~\x1b[1;37m>', '[\x1b[1;31mRECV\x1b[1;37m]', time, color('Message'), 'client.from', color(sender.split('@')[0]), 'in', color(groupName), 'args :', color(args.length))

/**
* Import all commands
Expand All @@ -155,26 +159,26 @@ async function krypton () {
const timestamps = cooldowns.get(command.name)
const cooldownAmount = (command.cooldown || 1) * 1000

if (timestamps.has(from)) {
const expirationTime = timestamps.get(from) + cooldownAmount
if (timestamps.has(client.from)) {
const expirationTime = timestamps.get(client.from) + cooldownAmount

if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000
return client.sendMessage(from,
return client.sendMessage(client.from,
`Mohon tunggu lebih dari ${timeLeft.toFixed(1)} detik sebelum menggunakan perintah ini *${command.name}*.`,
MessageType.text
)
}
}

timestamps.set(from, now)
setTimeout(() => timestamps.delete(from), cooldownAmount)
timestamps.set(client.from, now)
setTimeout(() => timestamps.delete(client.from), cooldownAmount)

try {
command.execute(client, from, args)
command.execute(client, args)
} catch (error) {
console.error(error)
client.sendMessage(from, 'Telah terjadi error setelah menggunakan command ini.', MessageType.text).catch(console.error)
client.sendMessage(client.from, 'Telah terjadi error setelah menggunakan command ini.', MessageType.text).catch(console.error)
}
})
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"discord.js": "^12.5.1",
"auto-changelog": "^2.2.1",
"moment-timezone": "^0.5.32",
"axios": "^0.21.1"
"axios": "^0.21.1",
"canvas": "^2.6.1"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
Expand Down
4 changes: 2 additions & 2 deletions sessions/krypton-sessions.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"clientID": "GKLkqJPLYevrdohE3aqyLw==",
"serverToken": "1@WxZP0VHxcykw4RJn8n0t2Uwcon5mN+geQYFh7afidl6NbYTPPg8oiWpcivEGRutU09R3y1SuaX2RHA==",
"clientToken": "jpvlOk7NZFZavIKJW8dce8fnKX8tsxFpOaqUikeXRxE=",
"serverToken": "1@7qS+bLikkKOuJNzAq80o1HeDbKbKVtFmGNaJQiiXh1FsP3tl0GRkD1vU7PXw03szDdVEXJ8RZJRBAw==",
"clientToken": "hN8cvVxXGTv4s6OGhvQ0HN1NgSY/qngMrrNXL1kdVvw=",
"encKey": "XmgE05UerNZpLDE2RFvE8/E4jnAkN+DGhib2P1R/gQY=",
"macKey": "ruFSFvGXjPPEfH00mvd6sYSJIEuAz1OPB5kK0x1c/Vo="
}
136 changes: 136 additions & 0 deletions utils/greeting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
const Canvas = require('canvas')
const path = require('path')
const { applyText } = require('../utils')

// Font
// Register Bold font
Canvas.registerFont(path.join(__dirname, '/../assets/fonts/theboldfont.ttf'), { family: 'Bold' })
// Register SketchMatch font
Canvas.registerFont(path.join(__dirname, '/../assets/fonts/SketchMatch.ttf'), { family: 'SketchMatch' })
// Register SketchMatch font
Canvas.registerFont(path.join(__dirname, '/../assets/fonts/LuckiestGuy-Regular.ttf'), { family: 'luckiest guy' })
// Register KeepCalm font
Canvas.registerFont(path.join(__dirname, '/../assets/fonts/KeepCalm-Medium.ttf'), { family: 'KeepCalm' })

const welcome = (pushname, gcname, picprofil) => new Promise((resolve, reject) => {
// Create canvas
const canvas = Canvas.createCanvas(1024, 450)
const ctx = canvas.getContext('2d')

const gcName = `Welcome in ${gcname}`

// Draw background
ctx.fillStyle = '000000'
ctx.fillRect(0, 0, canvas.width, canvas.height)
const background = Canvas.loadImage(path.join(__dirname, '/../assets/img/1px.png'))
ctx.drawImage(background, 0, 0, canvas.width, canvas.height)

// Draw layer
ctx.fillStyle = '#000000'
ctx.globalAlpha = '0.4'
ctx.fillRect(0, 0, 25, canvas.height)
ctx.fillRect(canvas.width - 25, 0, 25, canvas.height)
ctx.fillRect(25, 0, canvas.width - 50, 25)
ctx.fillRect(25, canvas.height - 25, canvas.width - 50, 25)
ctx.fillStyle = '#000000'
ctx.globalAlpha = '0.4'
ctx.fillRect(344, canvas.height - 296, 625, 65)
ctx.fillRect(308, canvas.height - 160, 672, 65)

// Draw username
ctx.globalAlpha = 1
ctx.fillStyle = '#ffffff'
ctx.font = applyText(canvas, pushname, 48, 600, 'Bold')
ctx.fillText(pushname, canvas.width - 660, canvas.height - 248)

// Draw guild name
ctx.fillStyle = '#ffffff'
ctx.font = applyText(canvas, gcName, 53, 600, 'Bold')
ctx.fillText(gcName, canvas.width - 690, canvas.height - 110)

// Draw title
ctx.font = '90px Bold'
ctx.strokeStyle = '#000000'
ctx.lineWidth = 15
ctx.strokeText('WELCOME', canvas.width - 620, canvas.height - 330)
ctx.fillStyle = '#df0909'
ctx.fillText('WELCOME', canvas.width - 620, canvas.height - 330)

// Draw avatar circle
ctx.beginPath()
ctx.lineWidth = 10
ctx.arc(180, 225, 135, 0, Math.PI * 2, true)
ctx.stroke()
ctx.closePath()
ctx.clip()
const avatar = Canvas.loadImage(picprofil)
ctx.drawImage(avatar, 45, 90, 270, 270)

const buff = canvas.toBuffer()
const base64 = `data:image/png;base64,${buff.toString('base64')}`
resolve(base64)
})

const goodbye = (pushname, gcname, picprofil) => new Promise((resolve, reject) => {
// Create canvas
const canvas = Canvas.createCanvas(1024, 450)
const ctx = canvas.getContext('2d')

const gcName = `Leaving from ${gcname}`

// Draw background
ctx.fillStyle = '000000'
ctx.fillRect(0, 0, canvas.width, canvas.height)
const background = Canvas.loadImage(path.join(__dirname, '/../assets/img/1px.png'))
ctx.drawImage(background, 0, 0, canvas.width, canvas.height)

// Draw layer
ctx.fillStyle = '#000000'
ctx.globalAlpha = '0.4'
ctx.fillRect(0, 0, 25, canvas.height)
ctx.fillRect(canvas.width - 25, 0, 25, canvas.height)
ctx.fillRect(25, 0, canvas.width - 50, 25)
ctx.fillRect(25, canvas.height - 25, canvas.width - 50, 25)
ctx.fillStyle = '#000000'
ctx.globalAlpha = '0.4'
ctx.fillRect(344, canvas.height - 296, 625, 65)
ctx.fillRect(308, canvas.height - 160, 672, 65)

// Draw username
ctx.globalAlpha = 1
ctx.fillStyle = '#ffffff'
ctx.font = applyText(canvas, pushname, 48, 600, 'Bold')
ctx.fillText(pushname, canvas.width - 660, canvas.height - 248)

// Draw guild name
ctx.fillStyle = '#ffffff'
ctx.font = applyText(canvas, gcName, 53, 600, 'Bold')
ctx.fillText(gcName, canvas.width - 690, canvas.height - 110)

// Draw title
ctx.font = '90px Bold'
ctx.strokeStyle = '#000000'
ctx.lineWidth = 15
ctx.strokeText('GOODBYE', canvas.width - 620, canvas.height - 330)
ctx.fillStyle = '#df0909'
ctx.fillText('GOODBYE', canvas.width - 620, canvas.height - 330)

// Draw avatar circle
ctx.beginPath()
ctx.lineWidth = 10
ctx.arc(180, 225, 135, 0, Math.PI * 2, true)
ctx.stroke()
ctx.closePath()
ctx.clip()
const avatar = Canvas.loadImage(picprofil)
ctx.drawImage(avatar, 45, 90, 270, 270)

const buff = canvas.toBuffer()
const base64 = `data:image/png;base64,${buff.toString('base64')}`
resolve(base64)
})

module.exports = {
welcome,
goodbye
}

0 comments on commit ce6c76d

Please sign in to comment.