Update 'src/MathCaptcha.js'

This commit is contained in:
Al Azhar 2020-07-08 08:59:42 +07:00
parent b9465d8ed3
commit 8397f0b488

View File

@ -1,242 +1,242 @@
class MathCaptcha { class MathCaptcha {
constructor(){ constructor(){
this.triggers = [ this.triggers = [
'###(new_chat_members)', '###(new_chat_members)',
'/start (CAPTCHA)(\\d+)' '/start (CAPTCHA)(\\d+)'
] ]
this.callbackTriggers = [ this.callbackTriggers = [
'captcha:(\\d+):(\\d+)' 'captcha:(\\d+):(\\d+)'
] ]
this.inlineTriggers = [] this.inlineTriggers = []
this.prop = PropertiesService.getScriptProperties() this.prop = PropertiesService.getScriptProperties()
} }
kickMember(bot, chat_id, user_id){ kickMember(bot, chat_id, user_id){
bot.kickChatMember({ bot.kickChatMember({
'chat_id': chat_id, 'chat_id': chat_id,
'user_id': user_id 'user_id': user_id
}) })
bot.unbanChatMember({ bot.unbanChatMember({
'chat_id': chat_id, 'chat_id': chat_id,
'user_id': user_id 'user_id': user_id
}) })
} }
muteMember(bot,chat_id,user_id){ muteMember(bot,chat_id,user_id){
bot.restrictChatMember({ bot.restrictChatMember({
'chat_id': chat_id, 'chat_id': chat_id,
'user_id': user_id, 'user_id': user_id,
'permissions' : { 'permissions' : {
'can_send_messages' : false 'can_send_messages' : false
} }
}) })
} }
unmuteMember(bot,chat_id,user_id){ unmuteMember(bot,chat_id,user_id){
bot.restrictChatMember({ bot.restrictChatMember({
'chat_id': chat_id, 'chat_id': chat_id,
'user_id': user_id, 'user_id': user_id,
'permissions' : { 'permissions' : {
'can_send_messages' : true, 'can_send_messages' : true,
'can_send_media_messages' : true, 'can_send_media_messages' : true,
'can_send_polls' : true, 'can_send_polls' : true,
'can_send_other_messages' : true, 'can_send_other_messages' : true,
'can_add_web_page_previews' : true, 'can_add_web_page_previews' : true,
'can_change_info' : true, 'can_change_info' : true,
'can_invite_users' : true, 'can_invite_users' : true,
'can_pin_messages' : true, 'can_pin_messages' : true,
} }
}) })
} }
// Cari id captcha untuk user di grup // Cari id captcha untuk user di grup
// jika belum ada, buat yang baru dengan id terakhir // jika belum ada, buat yang baru dengan id terakhir
getCaptchaID(bot,chat_id,user_id){ getCaptchaID(bot,chat_id,user_id){
let props = bot.obj.prop.getProperties() let props = bot.obj.prop.getProperties()
let i let i
for (i in props) { for (i in props) {
if (i.startsWith("CAPTCHA#")){ if (i.startsWith("CAPTCHA#")){
let val = props[i] let val = props[i]
let data = JSON.parse(val) let data = JSON.parse(val)
if (data.chat_id == chat_id && data.user_id == user_id){ if (data.chat_id == chat_id && data.user_id == user_id){
return i return i
} }
} }
} }
let last_id = bot.obj.prop.getProperty('last_id') let last_id = bot.obj.prop.getProperty('last_id')
if (!last_id) last_id = 0 if (!last_id) last_id = 0
last_id = parseInt(last_id) + 1 last_id = parseInt(last_id) + 1
bot.obj.prop.setProperty('last_id', parseInt(last_id)) bot.obj.prop.setProperty('last_id', parseInt(last_id))
return 'CAPTCHA#' + last_id return 'CAPTCHA#' + last_id
} }
onMessage(e){ onMessage(e){
let msg = e.message let msg = e.message
let ME = e.getMe() let ME = e.getMe()
// Jika ada update member baru gabung // Jika ada update member baru gabung
if (e.match[1] == 'new_chat_members'){ if (e.match[1] == 'new_chat_members'){
// Cek privilege bot. Jika tidak ada restric privilege abaikan update // Cek privilege bot. Jika tidak ada restric privilege abaikan update
let my_permission = e.getChatMember({'chat_id':msg.chat.id,'user_id':ME.id}) let my_permission = e.getChatMember({'chat_id':msg.chat.id,'user_id':ME.id})
if (my_permission.can_restrict_members){ if (my_permission.can_restrict_members){
// Looping setiap member baru // Looping setiap member baru
let i let i
for (i of msg.new_chat_members){ for (i of msg.new_chat_members){
let user = i let user = i
// Cek jika user adalah bot // Cek jika user adalah bot
if (user.is_bot){ if (user.is_bot){
continue continue
} }
// Buat captcha // Buat captcha
let user_name = Utils.getDisplayName(user) let user_name = Utils.getDisplayName(user)
let captcha_id = this.obj.getCaptchaID(this,msg.chat.id, user.id) let captcha_id = this.obj.getCaptchaID(this,msg.chat.id, user.id)
let max = 49 let max = 49
let min = 1 let min = 1
let num1 = Math.floor(Math.random() * (max - min + 1)) + min let num1 = Math.floor(Math.random() * (max - min + 1)) + min
let num2 = Math.floor(Math.random() * (max - min + 1)) + min let num2 = Math.floor(Math.random() * (max - min + 1)) + min
let question = num1 + ' + ' + num2 let question = num1 + ' + ' + num2
let answer = num1 + num2 let answer = num1 + num2
let choice = [answer] let choice = [answer]
max = 99 max = 99
choice.push(Math.floor(Math.random() * (max - min + 1)) + min) choice.push(Math.floor(Math.random() * (max - min + 1)) + min)
choice.push(Math.floor(Math.random() * (max - min + 1)) + min) choice.push(Math.floor(Math.random() * (max - min + 1)) + min)
choice.push(Math.floor(Math.random() * (max - min + 1)) + min) choice.push(Math.floor(Math.random() * (max - min + 1)) + min)
// Kirim pesan ke grup buat meminta user baru untuk verifikasi // Kirim pesan ke grup buat meminta user baru untuk verifikasi
let res = e.respond( let res = e.respond(
'Halo [' + user_name + '](tg://user?id=' + user.id + '), mohon verifikasi dengan menekan tombol dibawah untuk dapat mengirim pesan ke grup ini ', 'Halo [' + user_name + '](tg://user?id=' + user.id + '), mohon verifikasi dengan menekan tombol dibawah untuk dapat mengirim pesan ke grup ini ',
undefined, undefined, undefined, undefined,
Utils.inlineKeyboard([[Button.url('Verifikasi','t.me/' + ME.username + '?start=' + captcha_id.replace('#',''))]]) Utils.inlineKeyboard([[Button.url('Verifikasi','t.me/' + ME.username + '?start=' + captcha_id.replace('#',''))]])
) )
let captcha = { let captcha = {
'chat_id' : msg.chat.id, 'chat_id' : msg.chat.id,
'chat_title' : msg.chat.title, 'chat_title' : msg.chat.title,
'user_id': user.id, 'user_id': user.id,
'user_name': user_name, 'user_name': user_name,
'question': question, 'question': question,
'answer': answer, 'answer': answer,
'choice': choice, 'choice': choice,
'chance': 3, 'chance': 3,
'msg_id': res.message.message_id 'msg_id': res.message.message_id
} }
// Ganti restriction member // Ganti restriction member
this.obj.muteMember(this,msg.chat.id, user.id) this.obj.muteMember(this,msg.chat.id, user.id)
// Simpan properti // Simpan properti
this.obj.prop.setProperty(captcha_id, JSON.stringify(captcha)) this.obj.prop.setProperty(captcha_id, JSON.stringify(captcha))
} }
return return
} }
} }
// Jika user menekan tombol verifikasi // Jika user menekan tombol verifikasi
if (e.match[1] == 'CAPTCHA'){ if (e.match[1] == 'CAPTCHA'){
let id_captcha = e.match[2] let id_captcha = e.match[2]
let captcha = this.obj.prop.getProperty('CAPTCHA#' + id_captcha) let captcha = this.obj.prop.getProperty('CAPTCHA#' + id_captcha)
// Cek apakah ada captcha tersimpan dengan ID tersebut // Cek apakah ada captcha tersimpan dengan ID tersebut
if (captcha){ if (captcha){
captcha = JSON.parse(captcha) captcha = JSON.parse(captcha)
// Cek apakah user yang menekan tombol adalah member baru // Cek apakah user yang menekan tombol adalah member baru
if (captcha.user_id == msg.from.id){ if (captcha.user_id == msg.from.id){
let user_name = captcha.user_name let user_name = captcha.user_name
let chat_title = captcha.chat_title let chat_title = captcha.chat_title
let question = captcha.question let question = captcha.question
let answer = captcha.answer let answer = captcha.answer
let chance = captcha.chance let chance = captcha.chance
let choice = captcha.choice let choice = captcha.choice
choice = Utils.shuffleArray(choice) choice = Utils.shuffleArray(choice)
// Kirim captcha // Kirim captcha
e.respond('Hi *' + user_name + '* mohon selesaikan perhitungan matematika dibawah untuk verifikasi anda di grup *' + chat_title + '*' + e.respond('Hi *' + user_name + '* mohon selesaikan perhitungan matematika dibawah untuk verifikasi anda di grup *' + chat_title + '*' +
'\nKamu punya `' + chance + '` kali kesempatan' + '\nKamu punya `' + chance + '` kali kesempatan' +
'\n\n*Pertanyaan* : `' + question + '`',undefined, undefined, '\n\n*Pertanyaan* : `' + question + '`',undefined, undefined,
Utils.inlineKeyboard( Utils.inlineKeyboard(
[ [
[ [
Button.inline('A. ' + choice[0], `captcha:${id_captcha}:${choice[0]}`), Button.inline('A. ' + choice[0], `captcha:${id_captcha}:${choice[0]}`),
Button.inline('B. ' + choice[1], `captcha:${id_captcha}:${choice[1]}`) Button.inline('B. ' + choice[1], `captcha:${id_captcha}:${choice[1]}`)
], ],
[ [
Button.inline('C. ' + choice[2], `captcha:${id_captcha}:${choice[2]}`), Button.inline('C. ' + choice[2], `captcha:${id_captcha}:${choice[2]}`),
Button.inline('D. ' + choice[3], `captcha:${id_captcha}:${choice[3]}`) Button.inline('D. ' + choice[3], `captcha:${id_captcha}:${choice[3]}`)
] ]
] ]
) )
) )
} }
} }
} }
} }
onCallbackQuery(e){ onCallbackQuery(e){
let id_captcha = e.match[1] let id_captcha = e.match[1]
let user_choice = e.match[2] let user_choice = e.match[2]
let captcha = this.obj.prop.getProperty('CAPTCHA#' + id_captcha) let captcha = this.obj.prop.getProperty('CAPTCHA#' + id_captcha)
if (captcha){ if (captcha){
captcha = JSON.parse(captcha) captcha = JSON.parse(captcha)
if (captcha.user_id == captcha.user_id){ if (captcha.user_id == captcha.user_id){
let user_name = captcha.user_name let user_name = captcha.user_name
let chat_title = captcha.chat_title let chat_title = captcha.chat_title
let chance = captcha.chance let chance = captcha.chance
let question = captcha.question let question = captcha.question
let answer = captcha.answer let answer = captcha.answer
// Jika jawaban salah // Jika jawaban salah
if (user_choice != answer){ if (user_choice != answer){
e.answer('Jawaban kamu salah') e.answer('Jawaban kamu salah')
// Jika kesempatan sudah abis, kick member // Jika kesempatan sudah abis, kick member
if (chance == 1){ if (chance == 1){
e.edit('Kesempatan kamu habis, kamu sudah dikeluarkan dari grup') e.edit('Kesempatan kamu habis, kamu sudah dikeluarkan dari grup')
e.deleteMessage({'chat_id':captcha.chat_id, 'message_id':captcha.msg_id}) e.deleteMessage({'chat_id':captcha.chat_id, 'message_id':captcha.msg_id})
e.sendMessage({'chat_id':captcha.chat_id, 'text': ' *' + user_name + '* telah dikeluarkan dari grup karna tidak dapat melakukan verifikasi'}) e.sendMessage({'chat_id':captcha.chat_id, 'text': ' *' + user_name + '* telah dikeluarkan dari grup karna tidak dapat melakukan verifikasi'})
this.obj.kickMember(this,captcha.chat_id, captcha.user_id) this.obj.kickMember(this,captcha.chat_id, captcha.user_id)
this.obj.prop.deleteProperty('CAPTCHA#' + id_captcha) this.obj.prop.deleteProperty('CAPTCHA#' + id_captcha)
} }
// Jika masih memiliki kesempatan, acak jawaban // Jika masih memiliki kesempatan, acak jawaban
else{ else{
chance = chance - 1 chance = chance - 1
captcha.chance = chance captcha.chance = chance
let choice = captcha.choice let choice = captcha.choice
choice = Utils.shuffleArray(choice) choice = Utils.shuffleArray(choice)
e.edit('Hi *' + user_name + '* mohon selesaikan perhitungan matematika dibawah untuk verifikasi anda di grup *' + chat_title + '*' + e.edit('Hi *' + user_name + '* mohon selesaikan perhitungan matematika dibawah untuk verifikasi anda di grup *' + chat_title + '*' +
'\nKamu punya `' + chance + '` kali kesempatan' + '\nKamu punya `' + chance + '` kali kesempatan' +
'\n\n*Pertanyaan* : `' + question + '`',undefined, undefined, '\n\n*Pertanyaan* : `' + question + '`',undefined, undefined,
Utils.inlineKeyboard( Utils.inlineKeyboard(
[ [
[ [
Button.inline('A. ' + choice[0], `captcha:${id_captcha}:${choice[0]}`), Button.inline('A. ' + choice[0], `captcha:${id_captcha}:${choice[0]}`),
Button.inline('B. ' + choice[1], `captcha:${id_captcha}:${choice[1]}`) Button.inline('B. ' + choice[1], `captcha:${id_captcha}:${choice[1]}`)
], ],
[ [
Button.inline('C. ' + choice[2], `captcha:${id_captcha}:${choice[2]}`), Button.inline('C. ' + choice[2], `captcha:${id_captcha}:${choice[2]}`),
Button.inline('D. ' + choice[3], `captcha:${id_captcha}:${choice[3]}`) Button.inline('D. ' + choice[3], `captcha:${id_captcha}:${choice[3]}`)
] ]
] ]
) )
) )
this.obj.prop.setProperty('CAPTCHA#' + id_captcha, JSON.stringify(captcha)) this.obj.prop.setProperty('CAPTCHA#' + id_captcha, JSON.stringify(captcha))
} }
} }
// Jika jawaban salah // Jika jawaban benar
else{ else{
e.edit('👍 Yapp!! Jawaban kamu benar. Kamu dapat mengirim pesan di grup. Terimakasih sudah verifikasi 😊') e.edit('👍 Yapp!! Jawaban kamu benar. Kamu dapat mengirim pesan di grup. Terimakasih sudah verifikasi 😊')
e.deleteMessage({'chat_id':captcha.chat_id, 'message_id':captcha.msg_id}) e.deleteMessage({'chat_id':captcha.chat_id, 'message_id':captcha.msg_id})
this.obj.unmuteMember(this,captcha.chat_id, captcha.user_id) this.obj.unmuteMember(this,captcha.chat_id, captcha.user_id)
this.obj.prop.deleteProperty('CAPTCHA#' + id_captcha) this.obj.prop.deleteProperty('CAPTCHA#' + id_captcha)
e.answer() e.answer()
} }
} }
} }
} }
onInlineQuery(e){ onInlineQuery(e){
} }
} }