Skip to content
This repository has been archived by the owner on Apr 2, 2023. It is now read-only.

Commit

Permalink
Add es6-compatible class, 3.0.0
Browse files Browse the repository at this point in the history
See readme for usage
  • Loading branch information
kjsmita6 committed Mar 30, 2016
1 parent cccd3e9 commit 2940f1e
Show file tree
Hide file tree
Showing 6 changed files with 379 additions and 13 deletions.
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,45 @@ Once you have node and npm installed, type this command in shell, cmd, powershel
npm install steam-parentbot
```

Once it's installed, you will need to create your config file, you may use the one in the examples folder, or you can create one anywhere. It must follow the same structure as the one in example.js.
Once it's installed, you will need to create your config file, you may use the one in the examples folder, or you can create one anywhere. It must follow the same structure as the one in example.js or example-es6.js

# ES5 vs ES6 (new!)
I have rewritten the bot to use an ES6 class instead of an ES5 function. This will not change the functionality of the bot, but it will change how the bot options are defined.

First change (affects both): Requiring
```javascript
// Old version require
var ParentBot = require('steam-parentbot').ParentBot; // .ParentBot for original version

// New version require
var ParentBot = require('steam-parentbot').ES6; // .ES6 for new version

Second (new): ChildBot and overwriting
```javascript
// Old version ChildBot
var ParentBot = require('steam-parentbot').ParentBot;
var ChildBot = function() {
ChildBot.super_.apply(this, arguments);
}
require('util').inherits(ChildBot, ParentBot);
ChildBot.prototype._onFriendMsg(steamID, message, type) { } // overridden method
var Bot = new ChildBot('username', 'password');
// New version ChildBot
var ParentBot = require('steam-parentbot').ES6;
class ChildBot extends ParentBot {
_onFriendMsg(steamID, message, type) { } //overridden method
}
var Bot = new ChildBot('username', 'password');
```

Your best bet for #2 is looking at the file `parentbot-es6.js`. It shows how classes work in JavaScript which should help you override the super ParentBot class.

# Options
To initialize the bot, you must use `var Bot = new ChildBot(username, password)`, but you may also add an optional `options` object. This object can contain the following (and any others if you are adding to the bot):
Expand Down Expand Up @@ -83,7 +121,7 @@ There are already some built in instances of libraries and things that you can u

If you need to use the any require objects directly without instantiating them first, the following are available:
```javascript
var ParentBot = require('steam-parentbot');
var ParentBot = require('steam-parentbot').ParentBot;
var Steam = ParentBot.Steam;
var SteamCommunity = ParentBot.SteamCommunity;
var SteamWebApiKey = ParentBot.SteamWebApiKey;
Expand Down
44 changes: 44 additions & 0 deletions examples/example-es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict' // must have 'use strict' at top

const ParentBot = require('../parentbot.js').ES6; // JavaScript ES6 example
const Steam = ParentBot.Steam; // Steam object

const MySQL = require('mysql'); // require your own modules

class ChildBot extends ParentBot {
_onFriendMsg(steamID, message, type) { // overwrite default event handlers
if(type === Steam.EChatEntryType.ChatMsg) {
this.logger.info(steamID + ' sent: ' + message);
if(message === '!prices') {
Bot.steamFriends.sendMessage(steamID, 'Selling for ' + Bot.options.sellPrice); //use your custom options
}
}
else {
console.log(type);
}
}
}

let Bot = new ChildBot('username', 'password', {
apikey: '1234567890987654321', // default apikey option for steam
guardCode: 'ABCDE', // put guardcode here, remove after successful login
sellPrice: '1 ref' // add your own options
});

Bot.connect(); // connect to Steam

Bot.steamTrading.on('tradeProposed', function (tradeID, steamID) { // create your own listeners
Bot.steamTrading.respondToTrade(tradeID, false);
Bot.logger.verbose('Trade request from ' + steamID);
});


Bot.connection = MySQL.createConnection({ // add properties to the bot from an external module
host: 'localhost',
user: 'root',
password: 'password'
});

Bot.connection.connect(function (e) { // call methods on your new property
if (e) Bot.logger.error('Error connecting to MySQL: ' + e)
});
14 changes: 7 additions & 7 deletions examples/example.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var util = require('util');
var ParentBot = require('../parentbot.js'); //change to 'steam-parentbot' if not running from examples directory
var Steam = ParentBot.Steam; //instance of the Steam object
var Steam = ParentBot.Steam; // Steam object

var MySQL = require('mysql'); //require your own modules

Expand All @@ -18,13 +18,13 @@ var Bot = new ChildBot('username', 'password', {

ChildBot.prototype._onFriendMsg = function (steamID, message, type) { //overwrite default event handlers
if(type === Steam.EChatEntryType.ChatMsg) {
if(message === '!prices') {
Bot.steamFriends.sendMessage(steamID, 'Selling for ' + Bot.options.sellPrice); //use your custom options
}
this.logger.info(steamID + ' sent: ' + message);
if(message === '!prices') {
this.logger.info(steamID + ' sent: ' + message);
Bot.steamFriends.sendMessage(steamID, 'Selling for ' + Bot.options.sellPrice); //use your custom options
}
}
else {
console.log(type);
console.log(type);
}
}

Expand All @@ -45,4 +45,4 @@ Bot.connection.connect(function (e) { //call methods on your new property
});


Bot.connect(); //connect to steam
Bot.connect(); //connect to Steam
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "steam-parentbot",
"version": "2.2.5",
"version": "3.0.0",
"description": "A module that provides a simple base class for a Steam bot that can be easliy overwritten and customized.",
"main": "parentbot.js",
"author": {
Expand Down
Loading

0 comments on commit 2940f1e

Please sign in to comment.