Created
February 18, 2019 21:06
-
-
Save Swiftnesses/a281a092d255504fb8eca3d4328944d7 to your computer and use it in GitHub Desktop.
Modified code...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
`use strict`; | |
const telegraf = require(`telegraf`); | |
const { Markup } = telegraf; | |
const bot = new telegraf(`secret`); | |
const botId = bot.token.split(`:`)[0]; | |
var map = new Map(); | |
bot.catch(console.log); | |
bot.context.keyboard = function () { | |
const { id } = this.message.new_chat_member; | |
return Markup.inlineKeyboard([//btn[Math.round(Math.random()*btn.length)] | |
Markup.callbackButton(`${'I\'m not a bot 🤖'}`, `unmute.${id}`) | |
]).extra(); | |
} | |
bot.command([`start`, `help`], async (ctx) => { | |
if (ctx.chat.type === `private`) { | |
await ctx.reply( | |
`Hi, I can help you prevent spam attacks in your group. Just add me to your group with permission to ban and I will mute all new users until they prove their humanity. If you give also give me permission to delete messages, I can delete join messages after the user is unmuted.`, | |
{ parse_mode: `markdown` } | |
); | |
} | |
}); | |
bot.on(`new_chat_members`, async (ctx) => { | |
const { message_id } = ctx.message; | |
const { first_name, id } = ctx.message.new_chat_member; | |
const { title, id: chatId } = ctx.chat | |
if (id == botId) { | |
const { user, status } = await ctx.getChatMember(ctx.from.id); | |
const statuses = [`creator`, `administrator`]; | |
if (!statuses.includes(status)) { | |
await ctx.reply( | |
`Hi [${user.first_name}](tg://user?id=${user.id}). Thanks for adding me but you don't seem to have admin rights here so I will have to leave - please ask an admin to add me back :)`, | |
{ parse_mode: `markdown` } | |
); | |
await ctx.leaveChat(); | |
} | |
return; | |
} | |
try { | |
await ctx.restrictChatMember(id, { | |
can_send_messages: false | |
}); | |
const reply = await ctx.reply( | |
`Hi ${first_name}, for the safety of this chat, please also confirm your humanity by clicking the button below within 48 hours.`, // Can include ${title} | |
{ | |
...ctx.keyboard(), | |
reply_to_message_id: message_id, | |
} | |
); | |
const timeout = setTimeout(() => { | |
// delete reply or edit with "message expired. message an admin to be unmuted" | |
ctx.deleteMessage(reply.message_id); | |
bot.telegram.unbanChatMember(chatId, id); | |
}, 10000); | |
map.set(`${chatId}:${id}`, timeout); | |
} catch (err) { | |
switch (err.description) { | |
case `Bad Request: can't demote chat creator`: | |
ctx.reply(`Why would you leave if you're the creator?`); | |
break; | |
case `Bad Request: user is an administrator of the chat`: | |
break; | |
default: | |
await ctx.reply(err.description); | |
await ctx.leaveChat(); | |
} | |
} | |
}); | |
bot.action(/unmute\.(\d+)/, async (ctx) => { | |
const clickedId = ctx.callbackQuery.from.id; | |
const unmuteId = ctx.match[1]; | |
const chatId = ctx.callbackQuery.message.chat.id; | |
if (clickedId == unmuteId) { | |
try { | |
await ctx.restrictChatMember(clickedId, { | |
// until_date: ((Date.now() + 86400000) / 1000)*2, // 48 hours | |
can_send_messages: true | |
}); | |
} catch (err) { | |
console.log(err); | |
} | |
ctx.deleteMessage(); | |
const key = `${chatId}:${clickedId}`; | |
clearTimeout(map.get(key)); | |
map.delete(key); | |
if (ctx.callbackQuery.message) { | |
const { reply_to_message: reply } = ctx.callbackQuery.message; | |
ctx.deleteMessage(reply.message_id) | |
.catch(() => { /* Do nothing */ }); | |
} | |
} else { | |
ctx.answerCbQuery(`⛔ Sorry, this button doesn't belong to you! ⛔`); | |
} | |
}); | |
bot.startPolling(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment