Commit c2fe7f37 by Иван Кубота

Implemented Abstractions:

Chat props: id<int> User props: id<int> chat<Chat> isAdmin<bool> - return true if user is admin of the group methods: send(text: string, [Choose]) - send PM to user. Really it is not useful due to bot can not initiate chat with user ban(duration: string) - ban user in group unban() - unban user. this would not readd user to the group (bot api does not support this action) capture(Event[]): <CaptureStop> - capture all next users messages in the messages group and begin to match them only against specified `Event list` Message props: raw - original data from API raw.message_id<int> user<User> chat<Chat> from<Message> - if message is a reply - it would contains previous message. TODO: rename it methods: send(text: string) - send new message to the messages channel reply(text: string) - send reply to the message remove() - remove message from channel Choose - abstraction for generating custom in-chat-buttons TODO: refactor code to standalone module. Example of rules: '''js // Admin can ban user by replying to his\her massage with `\ban`. Works only if user is not admin. Reply('/ban') .then(async function( message ){ if( (await message.user.isAdmin()) && !(await message.from.user.isAdmin())){ message.from.reply(L('ban', {...message.from.user})); message.from.user.ban(); } }) Answer('/say {{$kokoko}}') .then((message, match)=>{ message.send(match.kokoko) }), '''
parents
node_modules/*
.idea/*
tmp/*
^tmp/.keep
package-lock.json
\ No newline at end of file
const storage = require('./DB');
const Chat = function(chat) {
Object.assign(this, chat);
this.data = storage.load(this.id);
//chatDataStorage[this.id] = this.data = chatDataStorage[this.id] || {};
};
ChatFactory = function(cfg, cfg2) {
return new Chat(cfg, cfg2);
};
Chat.prototype = {
getUserData: function(user) {
if(!(user.id in this.data)){
this.data[ user.id ] = {};
}
return this.data[ user.id ];
},
setUserData: function(user) {
this.data[ user.id ] = user.data;
this.saveData();
},
saveData: function() {
storage.save(this.id, this.data);
console.log(JSON.stringify(this.data, null, 2))
}
};
module.exports = ChatFactory;
const waitingList = {
};
const Choose = function(list, callback) {
var data = list.map(function(item) {
if(!('url' in item)){
const randomHash = Math.random().toString(36).substr(2)+
Math.random().toString(36).substr(2)+
Math.random().toString(36).substr(2)+
Math.random().toString(36).substr(2)+
Math.random().toString(36).substr(2);
item.callback_data = randomHash;
waitingList[randomHash] = {item: item, fn: item.fn || callback};
}
const safeItem = {};
if('text' in item){
safeItem.text = item.text + '';
}
if('callback_data' in item){
safeItem.callback_data = item.callback_data + '';
}
if('url' in item){
safeItem.url = item.url + '';
}
return [safeItem];
});
return {
one_time_keyboard: true, reply_markup: JSON.stringify({inline_keyboard: data})
};
};
const resolver = function(msg) {
const cbData = msg.raw.data;
if(cbData in waitingList){
const item = waitingList[cbData];
if(item.fn){
item.fn.call(item.item, item.item, msg);
}
delete waitingList[cbData];
}
};
module.exports = {Choose, resolver};
\ No newline at end of file
var fs = require('fs');
var path = require('path');
const chatDataStorage = {};
const dir = path.join(__dirname, '../','tmp');
const getPath = function(name) {
return path.join( dir, name.toString(10) );
};
var entries = fs.readdirSync( dir );
for( var i = 0; i < entries.length; i++ ){
var entry = entries[i];
var fullPath = getPath(entry);
var stat = fs.statSync( fullPath );
if( stat.isFile() ){
try{
var result = JSON.parse( fs.readFileSync( fullPath ) );
chatDataStorage[entry] = result
}catch( e ){
console.log(e);
}
}
}
module.exports = {
save: function(id, data) {
try{
chatDataStorage[id] = data;
fs.writeFile( getPath( id ), JSON.stringify( data, null, 2 ), function(err, data) {
} )
}catch(e){
console.log(e);
}
},
load: function(id) {
if(!(id in chatDataStorage))
return {};
else
return chatDataStorage[id];
}
};
\ No newline at end of file
const Event = function(cfg, then) {
if(!cfg)
return;
this._match = cfg;
this.buildRegExp();
this._then = [];
if(then)
this.then(then);
};
const ReplyFactory = function(cfg, then) {
return new Reply(cfg, then);
};
Event.prototype = {
buildRegExp: function() {
const regTokenNames = this.regTokenNames = [];
this.matchRegExp = new RegExp(this._match.trim().replace(/\//g,'\\/').replace(/\{\{\$([^\}]*)\}\}/g, function(f,k){
regTokenNames.push(k);
return '(.*)'
}), 'i');
},
then: function(resolve, reject) {
this._then.push({resolve, reject});
return this;
},
match: function(msg) {
const matched = msg.raw.text.trim().match(this.matchRegExp)
if(!matched)
return false;
const out = {},
list = this.regTokenNames;
for(let i = 0, _i = list.length; i < _i; i++){
out[list[i]] = matched[i+1];
}
return out;
},
proceed: function(msg, match) {
for( let i = 0, _i = this._then.length; i < _i; i++ ){
const thenElement = this._then[ i ];
thenElement.resolve.call(msg, msg, match);
}
}
};
const Reply = function(cfg, then) {
Event.call(this, cfg, then);
};
Reply.prototype = new Event();
Reply.prototype.match = function(msg, then) {
if(!('reply_to_message' in msg.raw))
return false;
return Event.prototype.match.call(this, msg, then);
};
ReplyFactory.cls = Reply;
const AnswerFactory = function(cfg, then) {
return new Event(cfg, then);
};
module.exports = {Reply: ReplyFactory, Answer: AnswerFactory};
const User = require('./User'),
Chat = require('./Chat');
const Message = function(msg, bot) {
this.raw = msg;
this.bot = bot;
};
MessageFactory = function(msg, bot) {
return new Message(msg, bot);
};
Message.prototype = {
getUser: function() {
return this._user || (this._user = new User(this.raw.from, this.getChat(), this))
},
getChat: function() {
return this._chat || (this._chat = new Chat(this.raw.chat))
},
getFrom: function() {
if(!('reply_to_message' in this.raw)){
throw new Error('Message is not a reply:\n'+JSON.stringify(this.raw,null,2))
}
return this._from || (this._from = new Message(this.raw.reply_to_message, this.bot))
},
remove: function() {
return this.bot.deleteMessage(this.chat.id, this.raw.message_id);
},
send: function(text) {
this.bot.sendMessage(this.chat.id, text);
},
reply: function(text) {
this.bot.sendMessage(this.chat.id, text, {reply_to_message_id: this.raw.message_id});
}
};
Object.defineProperty(Message.prototype, 'user', {get: function() {
return this.getUser();
}});
Object.defineProperty(Message.prototype, 'chat', {get: function() {
return this.getChat();
}});
Object.defineProperty(Message.prototype, 'from', {get: function() {
return this.getFrom();
}});
module.exports = MessageFactory;
const ms = require('ms');
const User = function(userData, chat, msg) {
Object.assign(this, userData);
this.raw = userData;
this.chat = chat;
this.data = chat.getUserData(this);
this.msg = msg;
this.full_name = this.first_name+' '+this.last_name;
};
UserFactory = function(cfg, cfg2, msg) {
return new User(cfg, cfg2, msg);
};
User.prototype = {
isAdmin: async function() {
var admins = await this.msg.bot.getChatAdministrators(this.chat.id);
var admin = admins.filter((item)=>item.user.id === this.id);
return admin.length ? admin[0] : false;
},
ban: function(duration) {
this.msg.bot.kickChatMember(this.chat.id, this.id, {
until_date: Date.now() + ms(duration || '60d'),
});
},
unban: function() {
this.msg.bot.unbanChatMember(this.chat.id, this.id);
},
get: function(key) {
return this.data[key];
},
set: function(cfg) {
for(let key in cfg){
this.data[key] = cfg[key];
}
this.chat.setUserData(this);
},
send: function(text, opts) {
this.msg.bot.sendMessage(this.id, text, opts);
},
capture: function(arr) {
const capture = {
chat: this.chat.id,
user: this.id,
arr: arr
};
const chat = captures[this.chat.id] || (captures[this.chat.id] = {});
(chat[this.id] || (chat[this.id] = [])).push(capture);
return {
stop: function() {
chat[capture.user].splice(chat[capture.user].indexOf(capture), 1);
if(chat[capture.user].length === 0){
delete chat[ capture.user ];
}
if(Object.keys(chat).length === 0){
delete captures[capture.chat];
}
}
};
}
};
const captures = {};
UserFactory.captured = function(msg) {
if(msg.chat.id in captures){
const chat = captures[msg.chat.id];
if(msg.user.id in chat){
const capture = chat[msg.user.id];
const rules = capture[capture.length - 1].arr; // LIFO
for( let i = 0, _i = rules.length; i < _i; i++ ){
const rule = rules[ i ], match = rule.match(msg);
if(match){
rule.proceed(msg, match);
break;
}
}
return true;
}
}
return false;
};
module.exports = UserFactory;
const rules = require('./rules');
const Message = require('./Model/Message');
const TelegramBot = require('node-telegram-bot-api');
// replace the value below with the Telegram token you receive from @BotFather
const token = process.env.token;
// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, {
polling: true
});
const captured = require('./Model/User').captured;
// Listen for any kind of message. There are different kinds of
// messages.
bot.on('message', (msg) => {
const wrappedMessage = new Message(msg, bot);
if(captured(wrappedMessage))
return;
for( let i = 0, _i = rules.length; i < _i; i++ ){
const rule = rules[ i ], match = rule.match(wrappedMessage);
if(match){
rule.proceed(wrappedMessage, match);
break;
}
}
});
const callbackResolver = require('./Model/Choose').resolver;
bot.on('callback_query', function (msg) {
const wrappedMessage = new Message(msg, bot);
callbackResolver(wrappedMessage);
});
\ No newline at end of file
module.exports = {
"ban": "Пользователь {{$first_name}} {{$last_name}} был забанен лопатой.",
"warn": "Пользователь {{$first_name}} {{$last_name}} получает {{$count}} предупреждение из {{$maxWarns}}, осталось {{$diff}} {{plural:$diff,предупреждение,предупреждения,предупреждений}}",
"unban": "Пользователя {{$full_name}} пришлось разбанить.",
"unbanGreeting": "Ты разбанены в {{$title}}",
"pidor": "{{$first_name}}, ты — {{$condition?пацак:чатланин}}, а эта планета {{$condition?чатланская:пацакская}}.\n\nСчитай до {{$count}}.",
"pidorDoNotCount1": "Считай, кому говорят",
"pidorDoNotCount2": "Транклюкирую!",
"pidorDoNotCount3": "Последний раз предупреждаю!",
"pidorDoNotCount4": "Пожизненный эцих без гвоздей",
"pidorDoNotCount5": "Пожизненный эцих с гвоздями",
"pidorDoigralsa": "{{$full_name}} уехал в эцихе",
"pidorKletka": "Здесь можно только в клетке выступать.",
"pidorKU": "Говори «Ку»!",
"pidorOK": "Всё, хорошо посчитал. Эцилоп вернётся ночью.",
"pidorCanNotCount": "Правильно считай.",
"pidorCanNotCount2": "Эцилоп, бей его. Считай с начала давай.",
"ban2": "{{$count}} {{plural:$count,рыба,рыбы,рыб}}",
"ku": "Говори «ку»",
"permission": "Не можно",
"_plural": "plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)"
};
\ No newline at end of file
{
"name": "tg-bot-for-terkin",
"version": "0.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"ajv": {
"version": "6.10.2",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.10.2.tgz",
"integrity": "sha512-TXtUUEYHuaTEbLZWIKUr5pmBuhDLy+8KYtPYdcV8qC+pOZL+NKqYwvWSRrVXHn+ZmRRAu8vJTAznH7Oag6RVRw==",
"requires": {
"fast-deep-equal": "^2.0.1",
"fast-json-stable-stringify": "^2.0.0",
"json-schema-traverse": "^0.4.1",
"uri-js": "^4.2.2"
}
},
"array.prototype.findindex": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/array.prototype.findindex/-/array.prototype.findindex-2.0.2.tgz",
"integrity": "sha1-WAaNJYh+9QXknckssAxE3O5VsGc=",
"requires": {
"define-properties": "^1.1.2",
"es-abstract": "^1.5.0"
}
},
"asn1": {
"version": "0.2.4",
"resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.4.tgz",
"integrity": "sha512-jxwzQpLQjSmWXgwaCZE9Nz+glAG01yF1QnWgbhGwHI5A6FRIEY6IVqtHhIepHqI7/kyEyQEagBC5mBEFlIYvdg==",
"requires": {
"safer-buffer": "~2.1.0"
}
},
"assert-plus": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz",
"integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU="
},
"asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k="
},
"aws-sign2": {
"version": "0.7.0",
"resolved": "https://registry.npmjs.org/aws-sign2/-/aws-sign2-0.7.0.tgz",
"integrity": "sha1-tG6JCTSpWR8tL2+G1+ap8bP+dqg="
},
"aws4": {
"version": "1.8.0",
"resolved": "https://registry.npmjs.org/aws4/-/aws4-1.8.0.tgz",
"integrity": "sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ=="
},
"bcrypt-pbkdf": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz",
"integrity": "sha1-pDAdOJtqQ/m2f/PKEaP2Y342Dp4=",
"requires": {
"tweetnacl": "^0.14.3"
}
},
"bl": {
"version": "1.2.2",
"resolved": "https://registry.npmjs.org/bl/-/bl-1.2.2.tgz",
"integrity": "sha512-e8tQYnZodmebYDWGH7KMRvtzKXaJHx3BbilrgZCfvyLUYdKpK1t5PSPmpkny/SgiTSCnjfLW7v5rlONXVFkQEA==",
"requires": {
"readable-stream": "^2.3.5",
"safe-buffer": "^5.1.1"
}
},
"bluebird": {
"version": "3.5.5",
"resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.5.tgz",
"integrity": "sha512-5am6HnnfN+urzt4yfg7IgTbotDjIT/u8AJpEt0sIU9FtXfVeezXAPKswrG+xKUCOYAINpSdgZVDU6QFh+cuH3w=="
},
"caseless": {
"version": "0.12.0",
"resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz",
"integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw="
},
"combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"requires": {
"delayed-stream": "~1.0.0"
}
},
"core-util-is": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
},
"dashdash": {
"version": "1.14.1",
"resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz",
"integrity": "sha1-hTz6D3y+L+1d4gMmuN1YEDX24vA=",
"requires": {
"assert-plus": "^1.0.0"
}
},
"debug": {
"version": "3.2.6",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz",
"integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==",
"requires": {
"ms": "^2.1.1"
}
},
"define-properties": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz",
"integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==",
"requires": {
"object-keys": "^1.0.12"
}
},
"delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk="
},
"depd": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz",
"integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak="
},
"ecc-jsbn": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz",
"integrity": "sha1-OoOpBOVDUyh4dMVkt1SThoSamMk=",
"requires": {
"jsbn": "~0.1.0",
"safer-buffer": "^2.1.0"
}
},
"end-of-stream": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.1.tgz",
"integrity": "sha512-1MkrZNvWTKCaigbn+W15elq2BB/L22nqrSY5DKlo3X6+vclJm8Bb5djXJBmEX6fS3+zCh/F4VBK5Z2KxJt4s2Q==",
"requires": {
"once": "^1.4.0"
}
},
"es-abstract": {
"version": "1.13.0",
"resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.13.0.tgz",
"integrity": "sha512-vDZfg/ykNxQVwup/8E1BZhVzFfBxs9NqMzGcvIJrqg5k2/5Za2bWo40dK2J1pgLngZ7c+Shh8lwYtLGyrwPutg==",
"requires": {
"es-to-primitive": "^1.2.0",
"function-bind": "^1.1.1",
"has": "^1.0.3",
"is-callable": "^1.1.4",
"is-regex": "^1.0.4",
"object-keys": "^1.0.12"
}
},
"es-to-primitive": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.0.tgz",
"integrity": "sha512-qZryBOJjV//LaxLTV6UC//WewneB3LcXOL9NP++ozKVXsIIIpm/2c13UDiD9Jp2eThsecw9m3jPqDwTyobcdbg==",
"requires": {
"is-callable": "^1.1.4",
"is-date-object": "^1.0.1",
"is-symbol": "^1.0.2"
}
},
"eventemitter3": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-3.1.2.tgz",
"integrity": "sha512-tvtQIeLVHjDkJYnzf2dgVMxfuSGJeM/7UCG17TT4EumTfNtF+0nebF/4zWOIkCreAbtNqhGEboB6BWrwqNaw4Q=="
},
"extend": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz",
"integrity": "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g=="
},
"extsprintf": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/extsprintf/-/extsprintf-1.3.0.tgz",
"integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU="
},
"fast-deep-equal": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz",
"integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk="
},
"fast-json-stable-stringify": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz",
"integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I="
},
"file-type": {
"version": "3.9.0",
"resolved": "https://registry.npmjs.org/file-type/-/file-type-3.9.0.tgz",
"integrity": "sha1-JXoHg4TR24CHvESdEH1SpSZyuek="
},
"forever-agent": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz",
"integrity": "sha1-+8cfDEGt6zf5bFd60e1C2P2sypE="
},
"form-data": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-2.3.3.tgz",
"integrity": "sha512-1lLKB2Mu3aGP1Q/2eCOx0fNbRMe7XdwktwOruhfqqd0rIJWwN4Dh+E3hrPSlDCXnSR7UtZ1N38rVXm+6+MEhJQ==",
"requires": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.6",
"mime-types": "^2.1.12"
}
},
"function-bind": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz",
"integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
},
"getpass": {
"version": "0.1.7",
"resolved": "https://registry.npmjs.org/getpass/-/getpass-0.1.7.tgz",
"integrity": "sha1-Xv+OPmhNVprkyysSgmBOi6YhSfo=",
"requires": {
"assert-plus": "^1.0.0"
}
},
"har-schema": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz",
"integrity": "sha1-qUwiJOvKwEeCoNkDVSHyRzW37JI="
},
"har-validator": {
"version": "5.1.3",
"resolved": "https://registry.npmjs.org/har-validator/-/har-validator-5.1.3.tgz",
"integrity": "sha512-sNvOCzEQNr/qrvJgc3UG/kD4QtlHycrzwS+6mfTrrSq97BvaYcPZZI1ZSqGSPR73Cxn4LKTD4PttRwfU7jWq5g==",
"requires": {
"ajv": "^6.5.5",
"har-schema": "^2.0.0"
}
},
"has": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz",
"integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==",
"requires": {
"function-bind": "^1.1.1"
}
},
"has-symbols": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.0.tgz",
"integrity": "sha1-uhqPGvKg/DllD1yFA2dwQSIGO0Q="
},
"http-signature": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz",
"integrity": "sha1-muzZJRFHcvPZW2WmCruPfBj7rOE=",
"requires": {
"assert-plus": "^1.0.0",
"jsprim": "^1.2.2",
"sshpk": "^1.7.0"
}
},
"inherits": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
},
"is-callable": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.4.tgz",
"integrity": "sha512-r5p9sxJjYnArLjObpjA4xu5EKI3CuKHkJXMhT7kwbpUyIFD1n5PMAsoPvWnvtZiNz7LjkYDRZhd7FlI0eMijEA=="
},
"is-date-object": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.1.tgz",
"integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY="
},
"is-regex": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz",
"integrity": "sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=",
"requires": {
"has": "^1.0.1"
}
},
"is-symbol": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.2.tgz",
"integrity": "sha512-HS8bZ9ox60yCJLH9snBpIwv9pYUAkcuLhSA1oero1UB5y9aiQpRA8y2ex945AOtCZL1lJDeIk3G5LthswI46Lw==",
"requires": {
"has-symbols": "^1.0.0"
}
},
"is-typedarray": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz",
"integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo="
},
"isarray": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
"integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE="
},
"isstream": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz",
"integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo="
},
"jsbn": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz",
"integrity": "sha1-peZUwuWi3rXyAdls77yoDA7y9RM="
},
"json-schema": {
"version": "0.2.3",
"resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.2.3.tgz",
"integrity": "sha1-tIDIkuWaLwWVTOcnvT8qTogvnhM="
},
"json-schema-traverse": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
"integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg=="
},
"json-stringify-safe": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz",
"integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus="
},
"jsprim": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.1.tgz",
"integrity": "sha1-MT5mvB5cwG5Di8G3SZwuXFastqI=",
"requires": {
"assert-plus": "1.0.0",
"extsprintf": "1.3.0",
"json-schema": "0.2.3",
"verror": "1.10.0"
}
},
"localize.js": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/localize.js/-/localize.js-2.0.1.tgz",
"integrity": "sha512-iDhGm6FcWRq1nGFqQ8kRDssnwbs81pMgIUKHZ87WFBLzanZIwQLSzsIbnlS+Tue012j/3Cj8raFml+l3kVAXWw=="
},
"lodash": {
"version": "4.17.15",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
"integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A=="
},
"mime": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
"integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg=="
},
"mime-db": {
"version": "1.40.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.40.0.tgz",
"integrity": "sha512-jYdeOMPy9vnxEqFRRo6ZvTZ8d9oPb+k18PKoYNYUe2stVEBPPwsln/qWzdbmaIvnhZ9v2P+CuecK+fpUfsV2mA=="
},
"mime-types": {
"version": "2.1.24",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.24.tgz",
"integrity": "sha512-WaFHS3MCl5fapm3oLxU4eYDw77IQM2ACcxQ9RIxfaC3ooc6PFuBMGZZsYpvoXS5D5QTWPieo1jjLdAm3TBP3cQ==",
"requires": {
"mime-db": "1.40.0"
}
},
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
},
"node-telegram-bot-api": {
"version": "0.30.0",
"resolved": "https://registry.npmjs.org/node-telegram-bot-api/-/node-telegram-bot-api-0.30.0.tgz",
"integrity": "sha512-+EeM+fe3Xt81KIPqN3L6s6eK+FK4QaqyDcwCwkY/jqsleERXwwjGlVbf4lJCOZ0uJuF5PfqTmvVNtua7AZfBXg==",
"requires": {
"array.prototype.findindex": "^2.0.2",
"bl": "^1.2.1",
"bluebird": "^3.5.1",
"debug": "^3.1.0",
"depd": "^1.1.1",
"eventemitter3": "^3.0.0",
"file-type": "^3.9.0",
"mime": "^1.6.0",
"pump": "^2.0.0",
"request": "^2.83.0",
"request-promise": "^4.2.2"
}
},
"oauth-sign": {
"version": "0.9.0",
"resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz",
"integrity": "sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ=="
},
"object-keys": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz",
"integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA=="
},
"once": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
"integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
"requires": {
"wrappy": "1"
}
},
"performance-now": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz",
"integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns="
},
"process-nextick-args": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
"integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag=="
},
"psl": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/psl/-/psl-1.3.0.tgz",
"integrity": "sha512-avHdspHO+9rQTLbv1RO+MPYeP/SzsCoxofjVnHanETfQhTJrmB0HlDoW+EiN/R+C0BZ+gERab9NY0lPN2TxNag=="
},
"pump": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/pump/-/pump-2.0.1.tgz",
"integrity": "sha512-ruPMNRkN3MHP1cWJc9OWr+T/xDP0jhXYCLfJcBuX54hhfIBnaQmAUMfDcG4DM5UMWByBbJY69QSphm3jtDKIkA==",
"requires": {
"end-of-stream": "^1.1.0",
"once": "^1.3.1"
}
},
"punycode": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz",
"integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A=="
},
"qs": {
"version": "6.5.2",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz",
"integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA=="
},
"readable-stream": {
"version": "2.3.6",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz",
"integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==",
"requires": {
"core-util-is": "~1.0.0",
"inherits": "~2.0.3",
"isarray": "~1.0.0",
"process-nextick-args": "~2.0.0",
"safe-buffer": "~5.1.1",
"string_decoder": "~1.1.1",
"util-deprecate": "~1.0.1"
},
"dependencies": {
"safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
}
}
},
"request": {
"version": "2.88.0",
"resolved": "https://registry.npmjs.org/request/-/request-2.88.0.tgz",
"integrity": "sha512-NAqBSrijGLZdM0WZNsInLJpkJokL72XYjUpnB0iwsRgxh7dB6COrHnTBNwN0E+lHDAJzu7kLAkDeY08z2/A0hg==",
"requires": {
"aws-sign2": "~0.7.0",
"aws4": "^1.8.0",
"caseless": "~0.12.0",
"combined-stream": "~1.0.6",
"extend": "~3.0.2",
"forever-agent": "~0.6.1",
"form-data": "~2.3.2",
"har-validator": "~5.1.0",
"http-signature": "~1.2.0",
"is-typedarray": "~1.0.0",
"isstream": "~0.1.2",
"json-stringify-safe": "~5.0.1",
"mime-types": "~2.1.19",
"oauth-sign": "~0.9.0",
"performance-now": "^2.1.0",
"qs": "~6.5.2",
"safe-buffer": "^5.1.2",
"tough-cookie": "~2.4.3",
"tunnel-agent": "^0.6.0",
"uuid": "^3.3.2"
}
},
"request-promise": {
"version": "4.2.4",
"resolved": "https://registry.npmjs.org/request-promise/-/request-promise-4.2.4.tgz",
"integrity": "sha512-8wgMrvE546PzbR5WbYxUQogUnUDfM0S7QIFZMID+J73vdFARkFy+HElj4T+MWYhpXwlLp0EQ8Zoj8xUA0he4Vg==",
"requires": {
"bluebird": "^3.5.0",
"request-promise-core": "1.1.2",
"stealthy-require": "^1.1.1",
"tough-cookie": "^2.3.3"
}
},
"request-promise-core": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/request-promise-core/-/request-promise-core-1.1.2.tgz",
"integrity": "sha512-UHYyq1MO8GsefGEt7EprS8UrXsm1TxEvFUX1IMTuSLU2Rh7fTIdFtl8xD7JiEYiWU2dl+NYAjCTksTehQUxPag==",
"requires": {
"lodash": "^4.17.11"
}
},
"safe-buffer": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.0.tgz",
"integrity": "sha512-fZEwUGbVl7kouZs1jCdMLdt95hdIv0ZeHg6L7qPeciMZhZ+/gdesW4wgTARkrFWEpspjEATAzUGPG8N2jJiwbg=="
},
"safer-buffer": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
},
"sshpk": {
"version": "1.16.1",
"resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.16.1.tgz",
"integrity": "sha512-HXXqVUq7+pcKeLqqZj6mHFUMvXtOJt1uoUx09pFW6011inTMxqI8BA8PM95myrIyyKwdnzjdFjLiE6KBPVtJIg==",
"requires": {
"asn1": "~0.2.3",
"assert-plus": "^1.0.0",
"bcrypt-pbkdf": "^1.0.0",
"dashdash": "^1.12.0",
"ecc-jsbn": "~0.1.1",
"getpass": "^0.1.1",
"jsbn": "~0.1.0",
"safer-buffer": "^2.0.2",
"tweetnacl": "~0.14.0"
}
},
"stealthy-require": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/stealthy-require/-/stealthy-require-1.1.1.tgz",
"integrity": "sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks="
},
"string_decoder": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",
"integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==",
"requires": {
"safe-buffer": "~5.1.0"
},
"dependencies": {
"safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
}
}
},
"tough-cookie": {
"version": "2.4.3",
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.4.3.tgz",
"integrity": "sha512-Q5srk/4vDM54WJsJio3XNn6K2sCG+CQ8G5Wz6bZhRZoAe/+TxjWB/GlFAnYEbkYVlON9FMk/fE3h2RLpPXo4lQ==",
"requires": {
"psl": "^1.1.24",
"punycode": "^1.4.1"
},
"dependencies": {
"punycode": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz",
"integrity": "sha1-wNWmOycYgArY4esPpSachN1BhF4="
}
}
},
"tunnel-agent": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz",
"integrity": "sha1-J6XeoGs2sEoKmWZ3SykIaPD8QP0=",
"requires": {
"safe-buffer": "^5.0.1"
}
},
"tweetnacl": {
"version": "0.14.5",
"resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz",
"integrity": "sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q="
},
"uri-js": {
"version": "4.2.2",
"resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.2.2.tgz",
"integrity": "sha512-KY9Frmirql91X2Qgjry0Wd4Y+YTdrdZheS8TFwvkbLWf/G5KNJDCh6pKL5OZctEW4+0Baa5idK2ZQuELRwPznQ==",
"requires": {
"punycode": "^2.1.0"
}
},
"util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
"integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8="
},
"uuid": {
"version": "3.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz",
"integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA=="
},
"verror": {
"version": "1.10.0",
"resolved": "https://registry.npmjs.org/verror/-/verror-1.10.0.tgz",
"integrity": "sha1-OhBcoXBTr1XW4nDB+CiGguGNpAA=",
"requires": {
"assert-plus": "^1.0.0",
"core-util-is": "1.0.2",
"extsprintf": "^1.2.0"
}
},
"wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8="
}
}
}
{
"name": "tg-bot-for-terkin",
"version": "0.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"localize.js": "^2.0.1",
"ms": "^2.1.2",
"node-telegram-bot-api": "^0.30.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Zibx",
"license": "MPL-2.0"
}
const {Reply, Answer} = require('./Model/Event'),
{Choose} = require('./Model/Choose'),
L = require('localize.js')('ru', require('./locale/locale.ru'));
const maxWarns = 3;
module.exports = [
Answer('/test {{$string}}', function(msg, match) {
var tmp = L.L.variableResolveFailed;
L.L.variableResolveFailed = function(varName) {
return '${'+varName+'}';
};
msg.reply(
'Template: '+L.voc[match.string]+'\n'+
'Result: '+L(match.string, {...msg.chat, ...msg.user})
);
L.L.variableResolveFailed = tmp;
}),
Answer('/help', function(message) {
message.send(
'List of commands:\n'+
module.exports.map((item)=>' '+(item instanceof Reply.cls?'>':' ')+item._match).join('\n')
);
}),
Reply('/warn')
.then((message)=>{
let warns = message.from.user.get('warns')|0;
warns++;
message.from.user.set({warns: warns});
if(warns>=maxWarns){
message.from.user.ban('60d');
message.reply(L('ban', {...message.from.user}))
}else{
message.reply(L('warn', {
maxWarns: maxWarns,
count: warns,
diff: maxWarns-warns,
...message.from.user
}))
}
}),
Reply('/ban')
.then((message)=>{
message.from.reply(L('ban', {...message.from.user}));
message.from.user.ban();
}),
Reply('/unban')
.then((message)=>{
message.from.user.unban();
//message.from.user.send(L('unbanGreeting', {...message.chat}));
message.reply(L('unban', {...message.from.user}))
}),
Reply('/reply')
.then( (msg)=>{
msg.send('Команда /reply пожрона');
msg.from.reply('Реплай на реплай');
}),
Reply('/test1')
.then((message)=>{
message.remove();
message.reply('Ti pidor')
}),
Answer('/say {{$kokoko}}')
.then((message, match)=>{
message.send(match.kokoko)
}),
Answer('/reply {{$text}}')
.then((msg, match)=>{
msg.send('Preparing to /reply');
msg.reply(match.text);
}),
Reply('/count {{$to}}', async function( message, match ){
if( (await message.user.isAdmin()) && !(await message.from.user.isAdmin())){
message.reply(L('permission'))
var usr = message.from.user;
var isPazak = ( usr.id % 2 ) === 0;
//var chatlanPlanet = isPazak;
var countTo = (match.to|0)||3;
message.from.send(L('pidor', {count: countTo, condition: isPazak, ...usr}));
/*message.from.user.full_name + ', ты — ' + ( isPazak ? 'пацак' : 'чатланин' ) + ', а это — ' + ( chatlanPlanet ? 'чатланская' : 'пацаковская' ) + ' планета.\n\n' +
'Считай до 5'
);*/
var last = 1;
var mistakes = 0;
var debil = 0;
var capture = message.from.user.capture( [
Answer('{{$any}}', (msg, match)=>{
var matched = match.any.match(/(\d+)/);
var matchedKU = match.any.match(/ку/i) !== null || match.any.match(/ыыы/i) !== null;
if(!matched && matchedKU){
// можно говорить "КУ" и "ЫЫЫ"
Math.random()>0.5 && msg.reply( L( 'pidorKletka' ) );
}else{
if( !matched ){
mistakes++;
if( mistakes <= 5 ){
msg.reply( L( 'pidorDoNotCount' + mistakes ) )
}else{
msg.reply( L( 'pidorDoigralsa' ) );
msg.user.ban();
capture.stop();
}
}else{
var num = parseInt( matched[ 0 ], 10 );
if( num === last ){
last++;
if(last % 3 === 0){
msg.reply( L( 'pidorKU' ) );
}
if(countTo === last - 1){
msg.reply( L( 'pidorOK' ) );
msg.reply( L( 'pidorKU' ) );
capture.stop();
}
}else{
debil++;
if(debil<3 || last === 1){
msg.reply( L( 'pidorCanNotCount' ) );
}else{
msg.reply( L( 'pidorCanNotCount2' ) );
last = 1;
}
}
}
}
})
] );
}
}),
Answer('/counter', async function( message ){
console.log( message.user.full_name + ' isAdmin:' + ( await message.user.isAdmin() ? 'true' : 'false' ) );
console.log( 1 );
for( let i = 0; i < 10; i++ )
setTimeout( () => message.send( i ), i * 1000 )
}),
Answer('/newPattern',function(message) {
message.user
.send('Which pattern?', Choose([
{ text: 'Read only'},
{ text: 'Text only'},
{ text: 'Timeout kick'},
], function(result) {
message.user.send('Selected: '+result.text);
}));
})
];
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment