node-steam-guide icon indicating copy to clipboard operation
node-steam-guide copied to clipboard

Add chapter on chat messages

Open andrewda opened this issue 8 years ago • 15 comments

Both handling and sending

andrewda avatar Oct 20 '17 06:10 andrewda

I was actually curious about this and figured it out myself. If you like I can give the code for it with some examples of commands, etc.

Zendrex avatar Oct 20 '17 18:10 Zendrex

Here are some snippets. My chat log formatting is a bit different however you can change it.

Added 2 functions, 1 for giving all your items of a specific game to the bot and the other being to take all the item from the bot in a specific game. The chat listener at least in this code block ignores everything that isnt considered admin of the bot. Still need to do an array for the admin list but for now it works.

// Variables
var admin_steamID = 'your_steam_64_id_here'

// Chat listener
client.on('friendMessage', (steamID, message) => {
    if (steamID == admin_steamID) {
        // Print to console that the chat message was detected for an admin.
        console.log(' Chat From ' + steamID + ': ' + message);

        if (message.match('!help')) {
            client.chatMessage(steamID, `
--------[ Bot Commands ]--------
●  !help - Get a list of help commands.
●  !take-all (option) - Withdraw all items from the bot.
     ↪  steamcard, csgo, tf2
●  !give-all (option) - Deposite all items to the bot.
    ↪  steamcard, csgo, tf2
●  !relog - Relog the bot into steam.
●  !shutdown - Shut down the bot.`);
        } else if (message.includes("!take-all")) {
            if (message.match('!take-all steamcards')) {
                client.chatMessage(steamID, '\nTransfering all STEAMCARDS to you. Please Wait.');
                takeAll(steamID, 753, 6, false);
            } else if (message.match('!take-all csgo')) {
                client.chatMessage(steamID, '\nTransfering all CSGO Items to you. Please Wait.');
                takeAll(steamID, 730, 2, false);
            } else if (message.match('!take-all tf2')) {
                client.chatMessage(steamID, '\nTransfering all TF2 Items to you. Please Wait.');
                takeAll(steamID, 440, 2, false);
            } else {
                client.chatMessage(steamID, '\nInvalid !take-all option. Please try again.');
            }
        } else if (message.includes("!give-all")) {
            if (message.match('!give-all steamcards')) {
                client.chatMessage(steamID, '\nTransfering all STEAMCARDS from you. Please Wait.');
                giveAll(steamID, 753, 6, false);
            } else if (message.match('!give-all csgo')) {
                client.chatMessage(steamID, '\nTransfering all CSGO Items from you. Please Wait.');
                giveAll(steamID, 730, 2, false);
            } else if (message.match('!give-all tf2')) {
                client.chatMessage(steamID, '\nTransfering all TF2 Items from you. Please Wait.');
                giveAll(steamID, 440, 2, false);
            } else {
                client.chatMessage(steamID, '\nInvalid !give-all option. Please try again.');
            }
        }
    } else if (message.match('!relog')) {
        console.log(' Relog command executed.')
        client.chatMessage(steamID, '\nRe-Logging the bot into steam. brb ;)');
        client.relog();
    } else {
        console.log(' Shutdown command executed.')
        client.chatMessage(steamID, '\nShutting down the bot. Goodbye.');

        // This is the most ghetto ass way of doing it. Actually cause an error for the js to break. xD
        client.shutdown();
    }
});


// Functions

// GiveAll items
function giveAll(steamID, appid, contextid, key_only) {
    manager.loadInventory(appid, contextid, true, (err, inventory) => {
        if (err) {
            console.log(err);
            client.chatMessage(steamID, '\nThere was an error, check the console.');
        } else {
            const offer = manager.createOffer(steamID);

            manager.loadUserInventory(steamID, appid, contextid, true, (err, theirInv) => {
                const theirItem = theirInv;

                offer.addTheirItems(theirItem);
                offer.send((err, status) => {
                    if (err) {
                        console.log(err);
                        console.log(' ');
                    } else {
                        console.log(` [SENT TRADE] Offer ID: (` + offer.id + `)` + ` | SteamID: ` + steamID);
                        client.chatMessage(steamID, '\n Trade offer sent. (' + offer.id + ')\n It make take up to 30 seconds for you to recieve it.');
                    }
                });
            });
        }
    });
};

// TakeAll items
function takeAll(steamID, appid, contextid, key_only) {
    manager.loadInventory(appid, contextid, true, (err, inventory) => {
        if (err) {
            console.log(err);
            client.chatMessage(steamID, '\nThere was an error, check the console.');
        } else {
            const offer = manager.createOffer(steamID);
            const items = inventory;
            offer.addMyItems(items);
            offer.send((err, status) => {
                if (err) {
                    console.log(err);
                    console.log(' ');
                } else {
                    console.log(` [SENT TRADE] Offer ID: (` + offer.id + `)` + ` | SteamID: ` + steamID);
                    client.chatMessage(steamID, '\n Trade offer sent. (' + offer.id + ')\n It make take up to 30 seconds for you to recieve it.');
                }
            });
        }
    });
};

Zendrex avatar Oct 20 '17 18:10 Zendrex

Thanks @Zendrex! If you feel like it, you can write the chapter around your code. No worries if you don't have time, though, I'll definitely get around to this in the next couple weeks.

andrewda avatar Oct 20 '17 18:10 andrewda

If I get some extra time on my hands I will go ahead and fork the repo and work on some docs for it.

Zendrex avatar Oct 20 '17 18:10 Zendrex

@DentFuse has added a chapter covering chat messages, & his pull request is pending #78.

| David, your friend |

xmwx38 avatar Apr 03 '18 22:04 xmwx38

hello this is not working, i define the manager but it says loadinventory is not a function

kebaburmum avatar Nov 23 '18 16:11 kebaburmum

@kebaburmum Try manager.getInventoryContents, check it out on the wiki

DentFuse avatar Nov 23 '18 18:11 DentFuse

I solved it already but I have a new problem. Whenever i type !relog it says this,can you help me pls? I understand this but where should I specify it? Error: To use relog(), you must specify rememberPassword=true when logging on and wait for loginKey to be emitted at SteamUser.relog (C:\Users\herni\Desktop\sandvich\Nová složka\node_modules\steam-user\components\logon.js:255:9) at SteamUser.client.on (C:\Users\herni\Desktop\sandvich\Nová složka\project2.js:269:16) at SteamUser.emit (events.js:187:15) at SteamUser._emitIdEvent (C:\Users\herni\Desktop\sandvich\Nová složka\node_modules\steam-user\components\utility.js:29:12) at SteamUser._handlers.(anonymous function) (C:\Users\herni\Desktop\sandvich\Nová složka\node_modules\steam-user\components\chat.js:281:9) at SteamUser._handleMessage (C:\Users\herni\Desktop\sandvich\Nová složka\node_modules\steam-user\components\messages.js:249:30) at CMClient.emit (events.js:187:15) at CMClient._netMsgReceived (C:\Users\herni\Desktop\sandvich\Nová složka\node_modules\steam-client\lib\cm_client.js:323:8) at CMClient.handlers.(anonymous function) (C:\Users\herni\Desktop\sandvich\Nová složka\node_modules\steam-client\lib\cm_client.js:609:8) at CMClient._netMsgReceived (C:\Users\herni\Desktop\sandvich\Nová složka\node_modules\steam-client\lib\cm_client.js:305:24)

kebaburmum avatar Nov 23 '18 18:11 kebaburmum

@DentFuse

kebaburmum avatar Nov 23 '18 18:11 kebaburmum

@kebaburmum Specify rememberPassword: true in your logOnOptions

const logOnOptions = {
  accountName: config.login.username,
  password : config.login.password,
  twoFactorCode: SteamTotp.generateAuthCode(config.login.shared_secret),
  rememberPassword: true
}

DentFuse avatar Nov 24 '18 03:11 DentFuse

Alright got it, it! Thank you @DentFuse

kebaburmum avatar Nov 24 '18 10:11 kebaburmum

@DentFuse I dont know how good you are, but you seems like a experienced guy with node.js. at the chapter 2.3 - Sending trade offers, is there any way to send a specific item for their keys? And maybe how to create a chat command to process the offer?

kebaburmum avatar Nov 24 '18 14:11 kebaburmum

@kebaburmum Refer to this to understand chat messages and commands. Read the API docs and think of how you can achieve what you want.

DentFuse avatar Nov 25 '18 09:11 DentFuse

@DentFuse Hello I havent figured it out, but I saw a quite interesting things there. Is it somehow possible at this chapter https://github.com/Steam-Bot-Basics/node-steam-guide/blob/master/Chapter%203%20-%20User%20Interaction/Chapter%203.4%20-%20Commenting%20on%20Profile%20%26%20Removing%20Friends/README.md for example when you write !remove, the bot will remove you, but is it possible somehow that you can tell me how to for example remove other users? like !remove and here ill put steam id. It would be really cool

kebaburmum avatar Nov 25 '18 11:11 kebaburmum

As I said im really new so pls take it easy on me

kebaburmum avatar Nov 25 '18 11:11 kebaburmum