seishun / node-steam

Interface directly with Steam servers from Node.js
MIT License
1k stars 181 forks source link

'message' or 'friendMsg' doesn't seem to emit #389

Closed ishan-marikar closed 7 years ago

ishan-marikar commented 7 years ago

Hi there. Impressive work. Been enjoying working with this library and making it do things. I wanted to make my script automatically respond to messages, but 'message' or 'friendMsg' doesn't seem to emit. 😢

The source code:

var Steam = require('steam');
var fs = require('fs');
var crypto = require('crypto');

const ACCOUNT_USERNAME = '';
const ACCOUNT_PASSWORD = '';
const AUTH_CODE = '';
const GAMES_TO_IDLE = [321040];

const SENTRY_FILE = 'sentry.dat'
const MESSAGE = "Hi there. I'm not there at the moment, please leave a message and I will get back to you.";

var steamClient = new Steam.SteamClient();
var steamUser = new Steam.SteamUser(steamClient);
var steamFriends = new Steam.SteamFriends(steamClient);

if (fs.existsSync('servers')) {
    Steam.servers = JSON.parse(fs.readFileSync('servers'));
}

steamClient.connect();

steamClient.on('connected', function() {
    if (fs.existsSync(SENTRY_FILE)) {

        console.log('[Steam] Existing sentry file found. Logging in using it.');
        var sentryData = fs.readFileSync(SENTRY_FILE);
        steamUser.logOn({
            account_name: ACCOUNT_USERNAME,
            password: ACCOUNT_PASSWORD,
            sha_sentryfile: sentryData
        });

    } else {
        if (AUTH_CODE) {
            console.log('[Steam] Logging into your account with the authentication code.');
            steamUser.logOn({
                account_name: ACCOUNT_USERNAME,
                password: ACCOUNT_PASSWORD,
                auth_code: AUTH_CODE
            });
        } else {
            console.log('[Steam] Logging in to your account.');
            steamUser.logOn({
                account_name: ACCOUNT_USERNAME,
                password: ACCOUNT_PASSWORD
            });
        }
    }

});

steamUser.on('updateMachineAuth', function(sentry, callback) {
    console.log('[Steam] Retrieved sentry information.');
    fs.writeFile(SENTRY_FILE, sentry.bytes);
    callback({
        sha_file: getSHA1(sentry.bytes)
    });
});

steamClient.on('servers', function(servers) {
    console.log('[Steam] Got server list.');
    fs.writeFile('servers', JSON.stringify(servers));
});

steamClient.on('logOnResponse', function(response) {
    if (response.eresult == Steam.EResult.OK) {
        console.log('[Steam] Successfully logged in.');

        steamFriends.setPersonaState(Steam.EPersonaState.Online); // to display your bot's status as "Online"
        steamUser.gamesPlayed({
            games_played: [{
                game_id: '321040'
            }]
        });
        console.log('[Steam] Logging game hours!');

    } else {
        steamClient.disconnect();
        if (response.eresult == Steam.EResult.InvalidLoginAuthCode) {
            console.log('[Steam] Auth Code expired. Run again with no auth code for new Auth Code email.');
        } else if (response.eresult == Steam.EResult.AccountLogonDenied) {
            console.log('[Steam] Auth Code generated. Check your email and edit the database.');
        } else {
            console.log('[Steam] Login error, ID: ' + response.eresult.toString());
        }
    }
});

steamFriends.on('message', function(steamID, message, type) {
    var user = steamFriends.requestFriendData([steamID]);
    console.log('[Steam] Message from', user, message);
    steamFriends.sendMessage(steamID, MESSAGE);
});

function getSHA1(bytes) {
    var shasum = crypto.createHash("sha1");
    shasum.end(bytes);
    return shasum.read();
}
seishun commented 7 years ago

Is the account limited? Do you get messages if you log in yourself with that account?

ishan-marikar commented 7 years ago

Fixed the issue. I was being dumb, sorry. Since I had pushed the code to a server and tried running it, I realised that 'scp' was being dumb and not copying the updated file, and I was dumb enough not to do a diff to check if the code was updated. sigh

It works now, and the code works perfectly.