Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.77 KB | None | 0 0
  1. "use strict";
  2. const mongoClient = require('mongodb').MongoClient;
  3. const db = require('./config/db');
  4. const TelegramBot = require('node-telegram-bot-api');
  5.  
  6. const token = '';
  7.  
  8. var ObjectID = require('mongodb').ObjectID;
  9.  
  10. const bot = new TelegramBot(token, {
  11. polling: true
  12. });
  13.  
  14.  
  15. console.log('bot started');
  16.  
  17. function AmIADmin(msg) {
  18. return new Promise(function(resolve, reject) {
  19. Promise.all([bot.getChatAdministrators(msg.chat.id), bot.getMe()]).then(results => {
  20. for (let i = 0; i < results[0].length; i++) {
  21. if (results[0][i].user.id == results[1].id) {
  22. resolve('true');
  23. };
  24. };
  25. reject('false');
  26. });
  27. });
  28. }
  29.  
  30.  
  31. function isuserAdmin(msg) {
  32. if (msg.chat === undefined) {
  33. msg.chat = msg.message.chat;
  34. }
  35. return new Promise(function(resolve, reject) {
  36. bot.getChatAdministrators(msg.chat.id).then(admins => {
  37. for (let i = 0; i < admins.length; i++) {
  38. if (admins[i].user.id == msg.from.id) {
  39. resolve('true');
  40. };
  41. };
  42. reject('false');
  43. });
  44. });
  45. }
  46.  
  47.  
  48. mongoClient.connect(db.url, function(err, db) {
  49. if (err) {
  50. return console.log(err);
  51. }
  52. bot.onText(/\/start@fatfagbot/, function(msg) {
  53. AmIADmin(msg).then(
  54. confirmed => {
  55.  
  56. let chat_id = {
  57. "chat_id": msg.chat.id
  58. };
  59.  
  60. db.collection('Bots_data').findOne(chat_id, (err, item) => {
  61.  
  62. if (item == null) {
  63. let data = {
  64. "chat_id": msg.chat.id,
  65. "banned": {
  66. "packs": [
  67.  
  68. ],
  69. "stickers": [
  70.  
  71. ]
  72. }
  73. };
  74.  
  75. db.collection('Bots_data').insert(data, (err, results) => {});
  76. bot.sendMessage(msg.chat.id, 'each admin of this conversastion can send me stiicker in reply and i will ban this sticker or entire pack');
  77. } else {
  78. bot.sendMessage(msg.chat.id, 'database for your conversation already been creasted, send me sticker as a replay if you want to ban it or ban intire pack, if youy want to reset sticker list of banned sticlers and packs type /reset@fatfagbot');
  79. }
  80. });
  81. },
  82. error => {
  83. bot.sendMessage(msg.chat.id, 'i am not an amdin in this conversetion, please set me as an admin');
  84.  
  85. }
  86. );
  87. });
  88. bot.onText(/\/ban@fatfagbot/, function(msg) {
  89. if (msg.chat.type != 'private') {
  90. AmIADmin(msg).then(
  91. confirmed => {
  92. let chat_id = {
  93. "chat_id": msg.chat.id
  94. };
  95. db.collection('Bots_data').findOne(chat_id, (err, item) => {
  96. if (item != null) {
  97. bot.sendMessage(msg.chat.id, 'send me sticker which you want to ban');
  98.  
  99. }else {
  100. bot.sendMessage(msg.chat.id, 'you have to use /start@fatfagbot to activate bot in this chat');
  101. }
  102. });
  103.  
  104. },
  105. err => {
  106. bot.sendMessage(msg.chat.id, 'i am not an admin set me as admin');
  107. }
  108. );
  109. }
  110. });
  111.  
  112.  
  113. bot.on('sticker', function(msg) {
  114.  
  115. let chat_id = {
  116. "chat_id": msg.chat.id
  117. };
  118.  
  119. db.collection('Bots_data').findOne(chat_id, (err, item) => {
  120. if (item != null) {
  121.  
  122. if(item.banned.packs.some(packs => packs == msg.sticker.set_name) == true || item.banned.stickers.some(sticker => sticker == msg.sticker.file_id) == true){
  123. bot.deleteMessage(msg.chat.id, msg.message_id);
  124.  
  125. }else{
  126.  
  127. if (typeof msg.reply_to_message !== 'undefined') {
  128. bot.getMe().then(me => {
  129. if (msg.reply_to_message.from.id == me.id) {
  130.  
  131. isuserAdmin(msg).then(confirmed => {
  132. AmIADmin(msg).then(
  133. confirmed => {
  134. const banNominee = {
  135. pack_name: msg.sticker.set_name,
  136. sticker_id: msg.sticker.file_id
  137. }
  138. const opt = {
  139. parse_mode: 'markdown',
  140. disable_web_page_preview: false,
  141. reply_markup: JSON.stringify({
  142. inline_keyboard: [
  143. [{
  144. text: `entire pack`,
  145. callback_data: 'entire'
  146. },
  147. {
  148. text: `only this sticker`,
  149. callback_data: 'onlyOne'
  150. }
  151. ]
  152. ]
  153. })
  154. }
  155. bot.sendMessage(msg.chat.id, 'do you want to ban entire pack or only one sticker?', opt);
  156.  
  157. bot.once('callback_query', function(msg) {
  158. bot.answerCallbackQuery(msg.id);
  159. isuserAdmin(msg).then(confirmed => {
  160. let chat_id = {
  161. "chat_id": msg.message.chat.id
  162. };
  163. db.collection('Bots_data').findOne(chat_id, (err, item) => {
  164. if (item != null) {
  165. if (msg.data == 'entire') {
  166. item.banned.packs.push(banNominee.pack_name);
  167. const id = {
  168. '_id': new ObjectID(item._id)
  169. };
  170. db.collection('Bots_data').update(id, item);
  171. bot.sendMessage(msg.message.chat.id, 'entire pack was added');
  172.  
  173.  
  174. }
  175. if (msg.data == 'onlyOne') {
  176. item.banned.stickers.push(banNominee.sticker_id);
  177. const id = {
  178. '_id': new ObjectID(item._id)
  179. };
  180. db.collection('Bots_data').update(id, item);
  181. bot.sendMessage(msg.message.chat.id, 'one sticker was added');
  182.  
  183.  
  184. }
  185.  
  186. } else {
  187. bot.sendMessage(msg.chat.id, 'you have to start the bot with /start@fatfagbot');
  188. }
  189. });
  190. },
  191. err => {
  192. bot.answerCallbackQuery(msg.id, 'you are not allowed ti use it', true);
  193.  
  194.  
  195. });
  196.  
  197. });
  198. },
  199. err => {
  200. bot.sendMessage(msg.chat.id, 'i am not an admin set me as admin');
  201. }
  202. );
  203. },
  204. error => {
  205. bot.editMessageText("this user are not allowed to use it", {message_id: msg.reply_to_message.message_id, chat_id: msg.chat.id});
  206.  
  207. }
  208. );
  209. //
  210. };
  211. });
  212. }
  213. }
  214. }
  215. });
  216.  
  217. });
  218.  
  219.  
  220. bot.onText(/\/reset@fatfagbot/, function(msg) {
  221. isuserAdmin(msg).then(
  222. confirmed => {
  223. const chat_id = {
  224. "chat_id": msg.chat.id
  225. };
  226. db.collection('Bots_data').findOne(chat_id, (err, item) => {
  227. if (item != null) {
  228. const clearFields = {
  229. "chat_id": msg.chat.id,
  230. "banned": {
  231. "packs": [
  232.  
  233. ],
  234. "stickers": [
  235.  
  236. ]
  237. }
  238. }
  239. const id = {
  240. '_id': new ObjectID(item._id)
  241. };
  242. db.collection('Bots_data').update(id, clearFields);
  243. bot.sendMessage(msg.chat.id, 'list of banned stickers and sticker packs was cleared');
  244. }
  245. });
  246. },
  247. error => {
  248.  
  249.  
  250. }
  251. );
  252. });
  253. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement