Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // accept username from command line then initialize
- if (process.argv.length < 3) {
- console.error('username not supplied!');
- process.exit(1);
- }
- var logon_settings = { rememberPassword: true, accountName: process.argv[2] }
- , Crypto = require('crypto')
- , SteamUser = require('steam-user')
- , fs = require('fs')
- , readline = require('readline').createInterface({
- input: process.stdin, output: process.stdout })
- , account = { user: new SteamUser(), name: process.argv[2] }
- , RiveScript = require("rivescript")
- , riveScript = new RiveScript();
- account.user.setOption("dataDirectory", null);
- // load chatbot engine
- console.log("loading rivescript files...");
- riveScript.loadDirectory("./rs", () => {
- riveScript.sortReplies();
- // read json file for persistent user sessions
- if (fs.existsSync('users.json')) {
- var json = JSON.parse(fs.readFileSync('users.json'));
- console.log("restoring " + Object.keys(json).length + " sessions...");
- for (var key in json) {
- if (json.hasOwnProperty(key)) {
- riveScript.setUservars(key, json[key]);
- }
- }
- }
- // use ssfn/key files if present then start login
- if (fs.existsSync('ssfn')) {
- account.user.setSentry(Crypto.createHash('sha1').update(
- fs.readFileSync('ssfn')).digest()
- );
- }
- if (fs.existsSync('key-' + process.argv[2])) {
- logon_settings.loginKey = fs.readFileSync('key-' + process.argv[2], 'utf8');
- account.user.logOn(logon_settings);
- } else {
- readline.question('password: ', (input) => {
- logon_settings.password = input;
- account.user.logOn(logon_settings);
- });
- }
- account.user.on('sentry', (sentry) => fs.writeFileSync('ssfn', sentry));
- account.user.on('loginKey', (key) => fs.writeFileSync('key-' + process.argv[2], key, 'utf8'));
- // finish login then set persona state
- account.user.on('loggedOn', (sessionID, cookies) => {
- console.log('logged on to steam: ' + process.argv[2]);
- account.user.setPersona(SteamUser.EPersonaState.LookingToPlay);
- account.user.gamesPlayed([362960,238750,2100,475150,297000,304390,211420,236430,374320,658620,238960,24810,15370,444590,372000,438420,315810,17480,24800,307780,286260,344770,221380,379430,373420,219990,236390,222880,10270,496300,242920,14221]);
- });
- // craft, sanitize, then log a reply to a user message
- function get_reply(steamID, message, steamID64 = steamID.toString()) {
- var reply = riveScript.reply(steamID64, message).replace(
- /<oob><search>.*<\/search><\/oob>/, '').replace(
- / random/g, ' ').replace(/ /g, ' ').replace('}', '');
- if (!reply.length) {
- reply = 'Huh?';
- }
- console.log(new Date() + " | " + riveScript.getUservar(steamID64, 'chat_time') +
- "\n>> " + "[" + steamID64 + "] " + reply);
- return reply + "ㅤ";
- }
- // interpret commands and pause/resume bot replies by the operator
- account.user.on('friendMessageEcho', (recipientID, message, steamID64 = recipientID .toString()) => {
- if (message.indexOf('#!') == 0) {
- account.user.chatMessage(recipientID, get_reply(recipientID, message.substr(2)));
- } else if (message.indexOf('##') == 0) {
- riveScript.setUservar(steamID64, 'chat_time', 0);
- } else if (message.indexOf('ㅤ') == -1) {
- riveScript.setUservar(steamID64, 'chat_time', Date.now());
- }
- });
- // react to chat events, ignoring paused/active exchanges
- account.user.on('friendMessage', (steamID, message, steamID64 = steamID.toString()) => {
- if (riveScript.getUservar(steamID64, 'chat_time') == 'undefined') {
- riveScript.setUservar(steamID64, 'chat_time', 0);
- riveScript.setUservar(steamID64, 'chat_active', false);
- }
- console.log(new Date() + " | " + riveScript.getUservar(steamID64, 'chat_time') +
- "\n<< " + "[" + steamID64 + "] " + message);
- if (Date.now() - riveScript.getUservar(steamID64, 'chat_time') > 3600000
- && riveScript.getUservar(steamID64, 'chat_active') != true) {
- riveScript.setUservar(steamID64, 'chat_active', true);
- // feign typing, then respond to the user
- var reply = get_reply(steamID, message);
- setTimeout(() => {
- account.user.chatTyping(steamID);
- setTimeout(() => {
- account.user.chatMessage(steamID, reply);
- riveScript.setUservar(steamID64, 'chat_active', false);
- }, Math.max(Math.min(reply.length, 100)*50, 2000));
- }, 1000);
- }
- });
- });
- // logoff, then save user json on sigint/sigterm
- quit = () => {
- account.user.logOff();
- fs.writeFileSync('users.json', JSON.stringify(riveScript.getUservars(), null, 2));
- process.exit(0);
- }
- process.on('SIGINT', quit);
- process.on('SIGTERM', quit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement