Awesome
ParentBot
A module that provides a simple base class for a Steam bot that can be easily overwritten and customized.
I made this (mainly for myself) because I was getting tired of writing the same code over and over again that does the same things. This is a way to eliminate the copying and pasting of code, while still allowing you to fully customize it.
Installation
First you will need to install node.js if you haven't already. <b>This will only work with Node.js >= 4.0.0</b>.
Once you have node and npm installed, type this command in shell, cmd, powershell, etc:
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 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
// 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
// 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):
//Without options
var Bot = new ChildBot(username, password);
//With options
var Bot = new ChildBot(username, password, {
apikey: '1234567890', //steam api key, will be registered automatically if one isn't supplied
sentryfile: 'username.sentry', //sentry file that stores steamguard info, defaults to username.sentry
logfile: 'username.log', //filename to log stuff to, defaults to username.log
guardCode: 'XXXXX', //steam guard code, only needed if you get error 63 when logging in, can remove after sentry is generated
twoFactorCode: 'XXXXX', //two factor authentication code, only needed if you're using the mobile 2FA
sharedSecret: 'XXXXXXXXXXXXXXXXXXXXXXXXXXX=', //shared secret, needed to automatically generate twoFactorCode
identitySecret: 'XXXXXXXXX/XXXXXXXXXXXXXXX/X=', //identity secret, needed to automatically confirm trade offers, must be used with confirmationInterval
confirmationInterval: 10000, //how often we should check for new trades to confirm in miliseconds, must be used with identitySecret
gamePlayed: 440 //game that the bot will play, don't include for no game
service: 'Parental', //service for SteamUnifiedMessages, leave blank to not include
richPresenceID: 440 //game to use rich presence with, don't include for no rich presence
});
Default methods and handlers
The base class ParentBot comes with a lot of built in methods and listeners. You can add your own, and edit the built in ones right in your config file.
The event handlers are:
_onError() //steamClient.on('error')
_onConnected() //steamClient.on('connected')
_onLogOnResponse(response) //steamClient.on('logOnResponse')
_onLoggedOff(eresult) //steamClient.on('loggedOff')
_onDebug() //steamClient.on('debug')
_onUpdateMachineAuth(response, callback) //steamUser.on('updateMachineAuth')
_onFriendMsg(steamID, message, type, chatter) //steamFriends.on('friendMsg')
_onFriend(steamID, relationship) //steamFriends.on('friend')
The two default methods are
connect() //steamClient.connect()
logOn() //steamClient.logOn()
Overwriting built in methods and handlers
This module was designed to provide a base class, as well as allow the user full customization. The module comes with lots of listeners built in already (the only ones you probably want to overwrite are _onFriend and _onFriendMsg since they don't really contain anything useful except for telling users that the bot isn't set up correctly (unless thats what you want).
There are already some built in instances of libraries and things that you can use in your config file. You can access all of these by doing Bot.property.method
. For a list of methods, please visit the links for each property below. List of properties you can use:
- steamClient - an instance of Steam.SteamClient()
- steamUser - an instance of Steam.SteamUser(steamClient)
- steamFriends - an instance of Steam.SteamFriends(steamClient)
- steamTrading - an instance of Steam.SteamTrading(steamClient)
- steamGameCoordinator - an instance of Steam.SteamGameCoordinator(steamClient, appID)
- steamUnifiedMessages - an instance of Steam.SteamUnifiedMessages(steamClient, service)
- steamRichPresence - an instance of Steam.SteamRichPresence(steamClient, appID)
- logger - an instance of Winston.Logger
- steamWebLogon - an instance of SteamWebLogon (use this for logging into Steam web, replacement for steamClient.on('webSessionID');
- steamTrade - an instance of SteamTrade
- offers - an instance of SteamTradeOffers
- steamCommunity - and instance of SteamCommunity
If you need to use the any require objects directly without instantiating them first, the following are available:
var ParentBot = require('steam-parentbot').ParentBot;
var Steam = ParentBot.Steam;
var SteamCommunity = ParentBot.SteamCommunity;
var SteamWebApiKey = ParentBot.SteamWebApiKey;
This will allow you to use certain methods not available to any handlers, but to the steam client itself (one useful example is Steam enums).
To overwrite a default handler (the ones with a _ in front), do this in your config file, assuming that ChildBot
is the child of ParentBot
(this example will show how to change _onFriend):
var admins = ['76561198091343023'];
var Bot = new ChildBot('username', 'password');
ChildBot.prototype._onFriend = function(steamID, relationship) {
if(relationship === 2) {
if(admins.indexOf(steamID) !== -1) {
Bot.steamFriends.addFriend(steamID);
}
else {
Bot.logger.warn('Someone who isn\'t an admin tried to add me, denying...');
Bot.steamFriends.removeFriend(steamID);
}
}
}
This example overwrites the _onFriend handler to create a custom friend handler that only accepts friend requests from admins. _onFriend is the function that is passed to this.steamFriends.on('friend', function(steamID, relationship) { that._onFriend(steamID, relationship); });
, so there is no need to add a new listener, you can just modify the function.
If you want to create a listener from an internal module (such as Steam), you can do it like this. Note: do not add listeners for things that are already being listened to.
//parentbot inheritence and stuff
var Bot = new ChildBot('username', 'password');
Bot.steamUser.on('tradeOffers', function(number) {
if(number > 0) {
// do stuff with trade offers
}
});
This is how you add listeners to internal modules.
If you want to create your own listener from an external module (this works the same way for modules that are already in use since I didn't create all listeners, like tradeOffers), you would do it like this
var MySQL = require('mysql');
// inherit from parentbot, see example.js for how to do this
var Bot = new ChildBot('username', 'password');
Bot.connection = MySQL.creatConnection({
host: 'localhost',
user: 'root',
password: 'password'
});
Bot.connection.on('error', function(e) {
Bot.logger.error(e);
});
That is how you add your own listeners to your bot from external libraries.
Help
This repository is beginner friendly. If you have a problem, no matter how simple it is, PLEASE open an issue, and either I or other users will try to answer it as quickly as possible. If you need help with something that is really complex or would take a long time, you can add me on steam.
Contributors
Pull requests are welcome! If you found a bug and fixed it, send a pull request. If you think that you added something useful, send a pull request. Please try to follow the existing style though. I wrote this guide, and I think it still applies here.
Feel free to add your name and github link here if you contributed. Also add what you did to contribute.