【问题标题】:EventEmitter Ignore next listeners if conditions are trueEventEmitter 如果条件为真则忽略下一个监听器
【发布时间】:2016-07-17 07:10:34
【问题描述】:

我正在创建一个使用 node-telegram-bot-api 包的 Telegram 机器人。该包使用EventEmitter3 来发出事件。

我有一个在所有其他侦听器之前执行的侦听器:

Bot.prependListener( 'message', ( msg ) =>
  if ( CHECK msg.from.id FOR AUTHORIZED USERS == false ) {
      // IGNORE ALL OTHER LISTENERS
  }
} );
‌Bot.onText( /\/start/i, ( msg ) => {
  Bot.sendMessage( msg.from.id, `You're an authorized user for sure!` );
} );

如何让 EventEmitter 忽略所有其他侦听器?

【问题讨论】:

    标签: node.js asynchronous eventemitter


    【解决方案1】:

    您需要真正的中间件支持。 例如:Telegraf

    const Telegraf = require('telegraf')
    const app = new Telegraf(process.env.BOT_TOKEN)
    
    app.use((ctx, next) => {
        if(AUTHORIZED_USERS.includes(ctx.from.id)){
            return next()
        }
    })
    
    app.command('/start', (ctx) => ctx.reply('You`re an authorized user for sure!'))
    app.startPolling()

    【讨论】:

      猜你喜欢
      • 2018-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2013-04-08
      • 2016-12-18
      • 1970-01-01
      • 2014-06-06
      相关资源
      最近更新 更多