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