diff --git a/lib/methods.js b/lib/methods.js index 575637c..d714fbe 100644 --- a/lib/methods.js +++ b/lib/methods.js @@ -529,9 +529,9 @@ function editObject(obj, form) { return form; } -function sendFile(type, file, opt = {}, methodForm = {}, methodUrl) { +async function sendFile(type, file, opt = {}, methodForm = {}, methodUrl) { - const form = this.properties(methodForm, opt); + const form = await this.properties(methodForm, opt); const defaultName = `file.${DEFAULT_FILE_EXTENSIONS[type]}`; const url = methodUrl ? methodUrl : 'send' + type.charAt(0).toUpperCase() + type.slice(1); diff --git a/lib/telebot.js b/lib/telebot.js index 1670ec8..2e502dd 100644 --- a/lib/telebot.js +++ b/lib/telebot.js @@ -111,14 +111,14 @@ class TeleBot { let optFn = method.options; // Create method - this.prototype[id] = this.prototype[name] = function () { + this.prototype[id] = this.prototype[name] = async 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)); + form = await this.properties(form, Object.assign(options, fnOptions)); return this.request(`/${id}`, form).then(method.then || (re => re && re.result)); }; @@ -299,10 +299,10 @@ class TeleBot { if (!updateList.length) return promise; // We have updates - return this.event('update', updateList).then(eventProps => { + return this.event('update', updateList).then(async eventProps => { // Run update list modifiers - mod = this.modRun('updateList', { + mod = await this.modRun('updateList', { updateList, props: extendProps(props, eventProps) }); @@ -317,7 +317,7 @@ class TeleBot { if (this.updateId < nextId) this.updateId = nextId; // Run update modifiers - mod = this.modRun('update', {update, props}); + mod = await this.modRun('update', {update, props}); update = mod.update; props = mod.props; @@ -385,10 +385,10 @@ class TeleBot { return fn; } - modRun(name, data) { + async modRun(name, data) { const list = this.modList[name]; if (!list || !list.length) return data; - for (let fn of list) data = fn.call(this, data); + for (let fn of list) data = await fn.call(this, data); return data; } @@ -528,7 +528,7 @@ class TeleBot { /* Method adder */ - properties(form = {}, opt = {}) { + async properties(form = {}, opt = {}) { const parseMode = opt.parseMode || opt.parse; const replyToMessage = opt.replyToMessage || opt.reply; @@ -555,7 +555,7 @@ class TeleBot { } } - return (this.modRun('property', {form, options: opt})).form; + return (await this.modRun('property', {form, options: opt})).form; } } diff --git a/lib/updates.js b/lib/updates.js index e57ffeb..60c8a99 100644 --- a/lib/updates.js +++ b/lib/updates.js @@ -34,13 +34,13 @@ const SHORTCUTS = { const updateFunctions = { // Message - message(update, props) { + async message(update, props) { // Any event status: ['*', '/*'] let anyEventFlags = [false, false]; let promise = Promise.resolve(); - let mod = this.modRun('message', {message: update, props}); + let mod = await this.modRun('message', {message: update, props}); update = mod.message; props = mod.props; @@ -58,7 +58,7 @@ const updateFunctions = { props.type = type; // Run message type mod - mod = this.modRun(type, {message: update, props}); + mod = await this.modRun(type, {message: update, props}); update = mod.message; props = mod.props; diff --git a/test/index.js b/test/index.js index dc53b55..c5f479f 100644 --- a/test/index.js +++ b/test/index.js @@ -100,7 +100,7 @@ test('events', t => { }); -test('mods', t => { +test('mods', async t => { const defModCount = bot.buildInPlugins.length + bot.usePlugins.length; @@ -109,6 +109,7 @@ test('mods', t => { } var delMe = x => x; + var delMeAsync = async x => x; t.is(all(bot.modList), defModCount); @@ -117,20 +118,23 @@ test('mods', t => { bot.mod('custom', x => ++x); bot.mod('custom', delMe); bot.mod('custom', x => ++x); + bot.mod('custom', delMeAsync); + bot.mod('custom', async x => ++x); // Count - t.is(len('custom'), 4); + t.is(len('custom'), 6); t.is(all(bot.modList), 1 + defModCount); // Run - t.is(bot.modRun('custom', 5), 8); + t.is(await bot.modRun('custom', 5), 9); // Remove t.true(bot.removeMod('custom', delMe)); + t.true(bot.removeMod('custom', delMeAsync)); t.false(bot.removeMod('custom')); t.false(bot.removeMod('not_found')); - t.is(len('custom'), 3); + t.is(len('custom'), 4); t.is(all(bot.modList), 1 + defModCount); });