Skip to content

Commit

Permalink
Eslint format
Browse files Browse the repository at this point in the history
  • Loading branch information
mullwar committed Mar 11, 2020
1 parent c24609e commit eaeea70
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 94 deletions.
34 changes: 17 additions & 17 deletions lib/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const REGEXP_URL = /^https?:\/\/|www\./;
// Methods
const methods = {

keyboard(keyboard, opt={}) {
keyboard(keyboard, opt = {}) {
const markup = keyboard !== undefined ? {keyboard} : {};
if (opt.resize === true) markup['resize_keyboard'] = true;
if (opt.once === true) markup['one_time_keyboard'] = true;
Expand All @@ -47,7 +47,7 @@ const methods = {
return {inline_keyboard: inline_keyboard};
},

inlineButton(text, opt={}) {
inlineButton(text, opt = {}) {
const markup = {text};
if (opt.url) markup.url = opt.url;
if (opt.inline !== undefined) markup.switch_inline_query = opt.inline;
Expand Down Expand Up @@ -76,7 +76,7 @@ const methods = {
return sendFile.call(this, 'photo', photo, opt, {chat_id});
},

sendAudio(chat_id, audio, opt={}) {
sendAudio(chat_id, audio, opt = {}) {
const form = {chat_id};

if (opt.title) form.title = opt.title;
Expand All @@ -94,7 +94,7 @@ const methods = {
return sendFile.call(this, 'sticker', sticker, opt, {chat_id});
},

sendVideo(chat_id, video, opt={}) {
sendVideo(chat_id, video, opt = {}) {
const form = {chat_id};

if (Number.isInteger(opt.duration)) form.duration = opt.duration;
Expand All @@ -106,14 +106,14 @@ const methods = {

sendVideoNote: {
arguments: ['chat_id', 'video_note'],
options(form, opt={}) {
options(form, opt = {}) {
if (Number.isInteger(opt.duration)) form.duration = opt.duration;
if (Number.isInteger(opt.length)) form.length = opt.length;
return form;
}
},

sendVoice(chat_id, voice, opt={}) {
sendVoice(chat_id, voice, opt = {}) {
const form = {chat_id};

if (Number.isInteger(opt.duration)) form.duration = opt.duration;
Expand All @@ -127,14 +127,14 @@ const methods = {
chat_id, latitude: position[0], longitude: position[1]
};
},
options(form, opt={}) {
options(form, opt = {}) {
if (Number.isInteger(opt.livePeriod)) form.live_period = opt.livePeriod;
return form;
}
},

editMessageLiveLocation: {
arguments(opt={}) {
arguments(opt = {}) {
const form = {};

if (opt.latitude) form.latitude = opt.latitude;
Expand All @@ -149,7 +149,7 @@ const methods = {
},

stopMessageLiveLocation: {
arguments(opt={}) {
arguments(opt = {}) {
const form = {};

if (opt.chatId) form.chat_id = opt.chatId;
Expand All @@ -166,7 +166,7 @@ const methods = {
chat_id, latitude: position[0], longitude: position[1], title, address
};
},
options(form, opt={}) {
options(form, opt = {}) {
if (opt.foursquareId) form.foursquare_id = opt.foursquareId;
return form;
}
Expand All @@ -184,7 +184,7 @@ const methods = {
getUserProfilePhotos: {
short: 'getUserPhoto',
arguments: 'user_id',
options(form, opt={}) {
options(form, opt = {}) {
if (opt.offset) form.offset = opt.offset;
if (opt.limit) form.limit = opt.limit;
return form;
Expand All @@ -205,7 +205,7 @@ const methods = {

setGameScore: {
arguments: ['user_id', 'score'],
options(form, opt={}) {
options(form, opt = {}) {
if (opt.force) form.force = opt.force;
if (opt.chatId) form.chat_id = opt.chatId;
if (opt.messageId) form.message_id = opt.messageId;
Expand All @@ -217,7 +217,7 @@ const methods = {

getGameHighScores: {
arguments: ['user_id'],
options(form, opt={}) {
options(form, opt = {}) {
if (opt.chatId) form.chat_id = opt.chatId;
if (opt.messageId) form.message_id = opt.messageId;
if (opt.inlineMessageId) form.inline_message_id = opt.inlineMessageId;
Expand Down Expand Up @@ -274,7 +274,7 @@ const methods = {
return sendFile.call(this, 'png_sticker', sticker, opt, {user_id}, 'uploadStickerFile');
},

createNewStickerSet(user_id, name, title, sticker, emojis, opt={}) {
createNewStickerSet(user_id, name, title, sticker, emojis, opt = {}) {
const form = {user_id, name, title, emojis};
if (opt.containsMasks) form.contains_masks = opt.containsMasks;
if (opt.maskPosition) form.mask_position = opt.maskPosition;
Expand All @@ -289,7 +289,7 @@ const methods = {
arguments: ['chat_id']
},

addStickerToSet(user_id, name, sticker, emojis, opt={}) {
addStickerToSet(user_id, name, sticker, emojis, opt = {}) {
const form = {user_id, name, emojis};
if (opt.maskPosition) form.mask_position = opt.maskPosition;
return sendFile.call(this, 'png_sticker', sticker, opt, form, 'addStickerToSet');
Expand Down Expand Up @@ -506,7 +506,7 @@ function editObject(obj, form) {
function sendFile(type, file, opt = {}, methodForm = {}, methodUrl) {

const form = this.properties(methodForm, opt);
const defaultName = `file.${ DEFAULT_FILE_EXTENSIONS[type] }`;
const defaultName = `file.${DEFAULT_FILE_EXTENSIONS[type]}`;

const url = methodUrl ? methodUrl : 'send' + type.charAt(0).toUpperCase() + type.slice(1);

Expand Down Expand Up @@ -559,7 +559,7 @@ function sendFile(type, file, opt = {}, methodForm = {}, methodUrl) {
form[type] = file;
}

return this.request(`/${ url }`, null, form).then((response) => response.result);
return this.request(`/${url}`, null, form).then((response) => response.result);

}

Expand Down
128 changes: 64 additions & 64 deletions lib/telebot.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class TeleBot {
this.cfg = cfg;
this.token = cfg.token;
this.id = this.token.split(':')[0];
this.api = `https://api.telegram.org/bot${ this.token }`;
this.fileLink = `https://api.telegram.org/file/bot${ this.token }/`;
this.api = `https://api.telegram.org/bot${this.token}`;
this.fileLink = `https://api.telegram.org/file/bot${this.token}/`;

this.pluginConfig = cfg.pluginConfig || {};

Expand Down Expand Up @@ -80,6 +80,53 @@ class TeleBot {

/* Plugins */

static addMethods(methods) {

for (let id in methods) {

const method = methods[id];

// If method is a function
if (typeof method === 'function') {
this.prototype[id] = method;
continue;
}

// Set method name
const name = method.short || id;

// Argument function
let argFn = method.arguments;
if (argFn && typeof argFn !== 'function') {
if (typeof argFn === 'string') argFn = [argFn];
let args = argFn;
argFn = function () {
const form = {};
args.forEach((v, i) => form[v] = arguments[i]);
return form;
};
}

// Options function
let optFn = method.options;

// Create method
this.prototype[id] = this.prototype[name] = function () {
this.event([id, name], arguments);
let form = {}, args = [].slice.call(arguments);
let options = args[args.length - 1], fnOptions = {};
if (typeof options !== 'object') options = {};
if (argFn) form = argFn.apply(this, args);
if (optFn) fnOptions = optFn.apply(this, [].concat(form, options));
form = this.properties(form, Object.assign(options, fnOptions));
return this.request(`/${id}`, form).then(method.then || (re => re && re.result));
};

}
}

/* Connection */

plug(module) {

const {id, defaultConfig, plugin} = module;
Expand All @@ -106,8 +153,6 @@ class TeleBot {

}

/* Connection */

start() {

const f = this.flags;
Expand All @@ -116,7 +161,7 @@ class TeleBot {
if (this.webhook) {

let {url, cert} = this.webhook;
if (url) url = `${ url }/${ this.token }`;
if (url) url = `${url}/${this.token}`;

return this.setWebhook(url, cert, this.allowedUpdates, this.maxConnections).then(() => {

Expand Down Expand Up @@ -173,7 +218,7 @@ class TeleBot {
const now = Date.now();
const diff = (now - f.retry) / 1000;

console.log(`[bot.info.update] reconnected after ${ diff } seconds`);
console.log(`[bot.info.update] reconnected after ${diff} seconds`);
this.event('reconnected', {
startTime: f.retry, endTime: now, diffTime: diff
});
Expand Down Expand Up @@ -203,7 +248,7 @@ class TeleBot {
}).catch(() => {

const seconds = this.retryTimeout / 1000;
console.log(`[bot.info.update] reconnecting in ${ seconds } seconds...`);
console.log(`[bot.info.update] reconnecting in ${seconds} seconds...`);
this.event('reconnecting');

// Set reconnecting timeout
Expand All @@ -215,19 +260,21 @@ class TeleBot {

}

/* Stop looping */

connect(...args) {
return this.start(...args);
}

/* Stop looping */
/* Fetch updates */

stop(message) {
this.flags.looping = false;
console.log(`[bot.info] bot stopped ${ message ? ': ' + message : '' }`);
console.log(`[bot.info] bot stopped ${message ? ': ' + message : ''}`);
this.event('stop', message);
}

/* Fetch updates */
/* Recive updates */

getUpdates(offset = this.updateId, limit = this.limit, timeout = this.timeout, allowed_updates = this.allowedUpdates) {

Expand All @@ -240,7 +287,7 @@ class TeleBot {

}

/* Recive updates */
/* Send request to server */

receiveUpdates(updateList) {

Expand Down Expand Up @@ -294,7 +341,7 @@ class TeleBot {

}

/* Send request to server */
/* Modifications */

request(url, form, data) {

Expand Down Expand Up @@ -327,8 +374,6 @@ class TeleBot {

}

/* Modifications */

mod(names, fn) {
if (typeof names === 'string') names = [names];
const mods = this.modList;
Expand All @@ -347,6 +392,8 @@ class TeleBot {
return data;
}

/* Events */

removeMod(name, fn) {
let list = this.modList[name];
if (!list) return false;
Expand All @@ -356,8 +403,6 @@ class TeleBot {
return true;
}

/* Events */

on(types, fn, opt) {

if (!opt) opt = {};
Expand Down Expand Up @@ -472,14 +517,16 @@ class TeleBot {
return true;
}

/* Process global properties */

destroyEvent(type) {
let eventList = this.eventList;
if (!eventList.has(type)) return false;
eventList.delete(type);
return true;
}

/* Process global properties */
/* Method adder */

properties(form = {}, opt = {}) {

Expand Down Expand Up @@ -511,53 +558,6 @@ class TeleBot {
return (this.modRun('property', {form, options: opt})).form;

}

/* Method adder */

static addMethods(methods) {

for (let id in methods) {

const method = methods[id];

// If method is a function
if (typeof method === 'function') {
this.prototype[id] = method;
continue;
}

// Set method name
const name = method.short || id;

// Argument function
let argFn = method.arguments;
if (argFn && typeof argFn !== 'function') {
if (typeof argFn === 'string') argFn = [argFn];
let args = argFn;
argFn = function () {
const form = {};
args.forEach((v, i) => form[v] = arguments[i]);
return form;
};
}

// Options function
let optFn = method.options;

// Create method
this.prototype[id] = this.prototype[name] = function () {
this.event([id, name], arguments);
let form = {}, args = [].slice.call(arguments);
let options = args[args.length - 1], fnOptions = {};
if (typeof options !== 'object') options = {};
if (argFn) form = argFn.apply(this, args);
if (optFn) fnOptions = optFn.apply(this, [].concat(form, options));
form = this.properties(form, Object.assign(options, fnOptions));
return this.request(`/${id}`, form).then(method.then || (re => re && re.result));
};

}
}
}

/* Add standard methods */
Expand Down
Loading

0 comments on commit eaeea70

Please sign in to comment.