Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions lib/telebot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};

Expand Down Expand Up @@ -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)
});

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -555,7 +555,7 @@ class TeleBot {
}
}

return (this.modRun('property', {form, options: opt})).form;
return (await this.modRun('property', {form, options: opt})).form;

}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
12 changes: 8 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ test('events', t => {

});

test('mods', t => {
test('mods', async t => {

const defModCount = bot.buildInPlugins.length + bot.usePlugins.length;

Expand All @@ -109,6 +109,7 @@ test('mods', t => {
}

var delMe = x => x;
var delMeAsync = async x => x;

t.is(all(bot.modList), defModCount);

Expand All @@ -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);

});
Expand Down