mishk0 / slack-bot-api

:rocket: Simple way to control your Slack Bot
MIT License
1.35k stars 176 forks source link

Constructor cannot be invoked without new (question) #77

Open tsanborn19-zz opened 7 years ago

tsanborn19-zz commented 7 years ago

Thanks for your time,

In following this tutorial (https://scotch.io/tutorials/building-a-slack-bot-with-node-js-and-chuck-norris-super-powers), I am attempting to inherit the bot using util to create my own. I am able to do this successfully when using version 0.3.0 of this library, but if I update to the current version, I get a "Constructor cannot be invoked without new" error.

Here is some of the code:

var NorrisBot = function Constructor(settings) {
    this.settings = settings;
    this.settings.name = this.settings.name || 'norrisbot';
    this.dbPath = settings.dbPath || path.resolve(process.cwd(), 'data', 'norrisbot.db');

    this.user = null;
    this.db = null;
};

// inherits methods and properties from the Bot constructor
util.inherits(NorrisBot, Bot);

NorrisBot.prototype.run = function () {
    NorrisBot.super_.call(this, this.settings);

    this.on('start', this._onStart);
    this.on('message', this._onMessage);
};

Do you happen to know the proper way to inherit the Bot class?

felixexter commented 7 years ago

Have same problem

ale0xb commented 7 years ago

The tutorial is from some time ago, and it uses slackbots, which apparently has gone through some changes lately.

In my case I'm using ES6 so I can take advantage of its proper classes support:

class JetaBot extends Bot {
    constructor(settings) {
        super(settings);
        this.dbPath = settings.dbPath;
        this.db = null;
    }
    run() {
        super.on("start", this.onStart);
        super.on("message", this.onMessage);
    }
...

That does the trick I guess.

vijaykarthickp commented 6 years ago

Any help for the below code still getting the same error? // lib/bot.js

'use strict';
var util = require('util');
var path = require('path');
var fs = require('fs');
var SlackBot = require('slackbots');
class newBot extends SlackBot {
  constructor(settings) {
    super(settings);
  }
  run() {
    super.on("start", this.onStart);
    super.on("message", this.onMessage);
  }
}
module.exports = newBot;

// in index.js

var newBot = require('./lib/bot');
const newbot = new newBot({
  token: 'xx-xxx-xxx',
  name: 'newBot',
});

Node Version : v9.5.0