Should remove username from /command@username messages in groups

This commit is contained in:
Mahdi Dibaiee 2015-07-05 00:05:58 +04:30
parent 5fd35f89c1
commit 794ef80520
3 changed files with 10 additions and 8 deletions

View File

@ -149,7 +149,7 @@ var Bot = (function (_EventEmitter) {
*/ */
value: function command(cmd, listener) { value: function command(cmd, listener) {
this._userEvents.push({ this._userEvents.push({
pattern: new RegExp('/' + cmd), pattern: new RegExp('^/' + cmd),
listener: listener listener: listener
}); });
@ -190,9 +190,11 @@ var Bot = (function (_EventEmitter) {
var text = res.message.text; var text = res.message.text;
if (text.startsWith('/')) { if (text.startsWith('/')) {
// Commands are sent in /command@thisusername format in groups // Commands are sent in /command@thisusername format in groups
var regex = new RegExp('@' + _this2.info.username + '$'); var regex = new RegExp('(/.*)@' + _this2.info.username);
text = text.replace(regex, ''); text = text.replace(regex, '$1');
res.message.text = text; res.message.text = text;
console.log(res.message.text);
} }
var ev = _this2._userEvents.find(function (_ref) { var ev = _this2._userEvents.find(function (_ref) {
@ -201,6 +203,7 @@ var Bot = (function (_EventEmitter) {
}); });
if (!ev) { if (!ev) {
_this2.emit('command-notfound', res.message);
return; return;
} }

View File

@ -44,5 +44,5 @@ bot.command('start', message => {
const test = new Message().text('Test Command'); const test = new Message().text('Test Command');
bot.command('test', message => { bot.command('test', message => {
bot.send(test.to(message.chat.id)); bot.send(test.to(message.chat.id).text(message.text));
}); });

View File

@ -142,16 +142,15 @@ export default class Bot extends EventEmitter {
let text = res.message.text; let text = res.message.text;
if (text.startsWith('/')) { if (text.startsWith('/')) {
// Commands are sent in /command@thisusername format in groups // Commands are sent in /command@thisusername format in groups
const regex = new RegExp(`@${this.info.username}$`); const regex = new RegExp(`(/.*)@${this.info.username}`);
text = text.replace(regex, ''); text = text.replace(regex, '$1');
res.message.text = text; res.message.text = text;
console.log(res.message.text);
} }
let ev = this._userEvents.find(({pattern}) => pattern.test(text)); let ev = this._userEvents.find(({pattern}) => pattern.test(text));
if (!ev) { if (!ev) {
this.emit('command-notfound', res.message);
return; return;
} }