-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
325 lines (294 loc) · 13.8 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
var Winston = require('winston'); // For logging
var SteamUser = require('steam-user'); // The heart of the bot. We'll write the soul ourselves.
var TradeOfferManager = require('steam-tradeoffer-manager'); // Only required if you're using trade offers
var config = require('./config.js');
var fs = require('fs'); // For writing a dope-ass file for TradeOfferManager
// We have to use application IDs in our requests--this is just a helper
var appid = {
TF2: 440,
DOTA2: 570,
CSGO: 730,
Steam: 753
};
// We also have to know context IDs which are a bit tricker since they're undocumented.
// For Steam, ID 1 is gifts and 6 is trading cards/emoticons/backgrounds
// For all current Valve games the context ID is 2.
var contextid = {
TF2: 2,
DOTA2: 2,
CSGO: 2,
Steam: 6
}
// Setup logging to file and console
var logger = new (Winston.Logger)({
transports: [
new (Winston.transports.Console)({
colorize: true,
level: 'debug'
}),
new (Winston.transports.File)({
level: 'info',
timestamp: true,
filename: 'cratedump.log',
json: false
})
]
});
// Initialize the Steam client and our trading library
var client = new SteamUser();
var offers = new TradeOfferManager({
steam: client,
domain: config.domain,
language: "en", // English item descriptions
pollInterval: 10000, // (Poll every 10 seconds (10,000 ms)
cancelTime: 300000 // Expire any outgoing trade offers that have been up for 5+ minutes (300,000 ms)
});
// If we've run this before, we should have a saved copy of our poll data.
// We can load this up to gracefully resume polling as if we never crashed/quit
fs.readFile('polldata.json', function (err, data) {
if (err) {
logger.warn('Error reading polldata.json. If this is the first run, this is expected behavior: '+err);
} else {
logger.debug("Found previous trade offer poll data. Importing it to keep things running smoothly.");
offers.pollData = JSON.parse(data);
}
});
// Sign into Steam
client.logOn({
accountName: config.username,
password: config.password
});
client.on('loggedOn', function (details) {
logger.info("Logged into Steam as " + client.steamID.getSteam3RenderedID());
// If you wanted to go in-game after logging in (for crafting or whatever), you can do the following
// client.gamesPlayed(appid.TF2);
});
client.on('error', function (e) {
// Some error occurred during logon. ENums found here:
// https://github.com/SteamRE/SteamKit/blob/SteamKit_1.6.3/Resources/SteamLanguage/eresult.steamd
logger.error(e);
process.exit(1);
});
client.on('webSession', function (sessionID, cookies) {
logger.debug("Got web session");
// Set our status to "Online" (otherwise we always appear offline)
client.friends.setPersonaState(SteamUser.Steam.EPersonaState.Online);
offers.setCookies(cookies, function (err){
if (err) {
logger.error('Unable to set trade offer cookies: '+err);
process.exit(1); // No point in staying up if we can't use trade offers
}
logger.debug("Trade offer cookies set. Got API Key: "+offers.apiKey);
});
});
// Emitted when Steam sends a notification of new items.
// Not important in our case, but kind of neat.
client.on('newItems', function (count) {
logger.info(count + " new items in our inventory");
});
// Emitted on login and when email info changes
// Not important in our case, but kind of neat.
client.on('emailInfo', function (address, validated) {
logger.info("Our email address is " + address + " and it's " + (validated ? "validated" : "not validated"));
});
// Emitted on login and when wallet balance changes
// Not important in our case, but kind of neat.
client.on('wallet', function (hasWallet, currency, balance) {
if (hasWallet) {
logger.info("We have "+ SteamUser.formatCurrency(balance, currency) +" Steam wallet credit remaining");
} else {
logger.info("We do not have a Steam wallet.");
}
});
// Looking at your account limitations can be very useful depending on what you're doing
client.on('accountLimitations', function (limited, communityBanned, locked, canInviteFriends) {
if (limited) {
// More info: https://support.steampowered.com/kb_article.php?ref=3330-IAGK-7663
logger.warn("Our account is limited. We cannot send friend invites, use the market, open group chat, or access the web API.");
}
if (communityBanned){
// More info: https://support.steampowered.com/kb_article.php?ref=4312-UOJL-0835
// http://forums.steampowered.com/forums/showpost.php?p=17054612&postcount=3
logger.warn("Our account is banned from Steam Community");
// I don't know if this alone means you can't trade or not.
}
if (locked){
// Either self-locked or locked by a Valve employee: http://forums.steampowered.com/forums/showpost.php?p=17054612&postcount=3
logger.error("Our account is locked. We cannot trade/gift/purchase items, play on VAC servers, or access Steam Community. Shutting down.");
process.exit(1);
}
if (!canInviteFriends){
// This could be important if you need to add users. In our case, they add us or just use a direct tradeoffer link.
logger.warn("Our account is unable to send friend requests.");
}
});
// Steam Friends documentation: https://github.com/seishun/node-steam/tree/master/lib/handlers/friends
// Note: Steam-User initializes this for us by default as of v1.2.0
// On startup check our friends list
client.friends.on('relationships', function(){
var friendcount = 0;
// For every friend we have...
for (steamID in client.friends.friends) {
friendcount++;
// If the status is a new friend request...
if (client.friends.friends[steamID] === SteamUser.Steam.EFriendRelationship.RequestRecipient) {
logger.info("Friend request while offline from: "+steamID);
// Accept friend requests from when we were offline
client.friends.addFriend(steamID);
}
}
logger.debug("We have "+friendcount+" friends.");
if (friendcount > 200) {
// We might be able to find old friends after using client.friends.requestFriendData([steamids])
// but seishun will have to add support for it. Right now you can't see how long you've been friends through SteamFriends.
// This is the only data available using requestFriendData function:
// https://github.com/SteamRE/SteamKit/blob/master/Resources/Protobufs/steamclient/steammessages_clientserver.proto#L446-L469
logger.warn("We're approaching the default friends limit. Maybe we need to purge old friends?");
}
});
// Friend requests while we're online
client.friends.on('friend', function (steamID, relationship) {
// If it's a new friend request...
if (relationship == SteamUser.Steam.EFriendRelationship.RequestRecipient) {
logger.info('[' + steamID + '] Accepted friend request');
// Accept!
client.friends.addFriend(steamID);
} // If they removed us, just log it.
else if (relationship == SteamUser.Steam.EFriendRelationship.None) {
logger.info('[' + steamID + '] Un-friended');
}
});
// When they message us...
client.friends.on('friendMsg', function (steamID, message, type) {
// Only on complete regular messages
if (type === SteamUser.Steam.EChatEntryType.ChatMsg) {
logger.debug('[' + steamID + '] MSG: ' + message);
// If the message starts with !trade followed by two numbers...
var req;
if (req = message.match(/^!trade (\d+) (\d+)/i)) {
var series = req[1];
var amount = req[2];
// Send them some crates!
sendCrates(steamID, series, amount);
} else {
// Otherwise just send them the greet message again
client.friends.sendMessage(steamID, config.greetMsg);
}
}
});
// If you wanted to use regular trading, you can respond to requests via: client.trading.on('tradeProposed', function(tradeID, steamID){...});
// Docs here: https://github.com/seishun/node-steam/tree/master/lib/handlers/trading
// But we're not going to do that!
// When we get a new offer...
offers.on('newOffer', function (offer) {
logger.info("New offer #"+ offer.id +" from "+ offer.partner.getSteam3RenderedID());
// Accept any trade offer from the bot administrator, or where we're getting free stuff.
if (offer.partner.getSteamID64() === config.admin || offer.itemsToGive.length === 0) {
logger.info("User "+ offer.partner.getSteam3RenderedID() +" offered a valid trade. Trying to accept offer.");
offer.accept(function (err) {
if (err) {
logger.error("Unable to accept offer "+ offer.id +": " + err.message);
} else {
logger.info("Offer accepted");
}
});
} else { // Otherwise deny it and message the user
logger.info("User "+ offer.partner.getSteam3RenderedID() +" offered an invalid trade. Declining offer.");
offer.decline(function (err) {
if (err) {
logger.error("Unable to decline offer "+ offer.id +": " + err.message);
} else {
logger.debug("Offer declined");
// Message the user
client.friends.sendMessage(offer.partner.getSteamID64(), "Invalid offer. Please use the chat interface to request items. Trade offers sent to me must only include items you're giving to me.");
}
});
}
});
// When an offer sent by someone else changes states
offers.on('receivedOfferChanged', function (offer, oldState) {
logger.info(offer.partner.getSteam3RenderedID() +" Offer #" + offer.id + " changed: " + TradeOfferManager.getStateName(oldState) + " -> " + TradeOfferManager.getStateName(offer.state));
// Alert us when we accept an offer
if (offer.state == TradeOfferManager.ETradeOfferState.Accepted) {
offer.getReceivedItems(function (err, items) {
if (err) {
logger.error("Couldn't get received items: " + err);
} else {
var names = items.map(function(item) {
return item.name;
});
// Log a comma-separated list of items received
logger.info("Received: " + names.join(', '));
}
});
}
});
// When one of our offers changes states
offers.on('sentOfferChanged', function (offer, oldState) {
// Alert us when one of our offers is accepted
if (offer.state == TradeOfferManager.ETradeOfferState.Accepted) {
logger.info("Our sent offer #"+ offer.id + " has been accepted.");
}
});
// Steam is down or the API is having issues
offers.on('pollFailure', function (err) {
logger.error("Error polling for trade offers: "+err);
});
// When we receive new trade offer data, save it so we can use it after a crash/quit
offers.on('pollData', function (pollData) {
fs.writeFile('polldata.json', JSON.stringify(pollData));
});
function sendCrates(steamID, series, amount) {
// If no series requested, give up.
if (!series) {
return true;
}
// If not amount requested, assume 1.
if (!amount) {
var amount = 1;
}
offers.loadInventory(appid.TF2, contextid.TF2, true, function (err, inventory){
if (err) {
logger.error(err);
} else {
// Filter out all the crates
var pool = inventory.filter(function (item) {
return item.tags.some(function(element, index, array) {
return element.internal_name == 'Supply Crate';
});
});
// Filter out the series
var re = new RegExp('#' + series, 'i'); // ex: #82
pool = pool.filter(function (item) {
return item.name.match(re);
});
// Let the user know we don't have any
if (pool.length === 0) {
client.friends.sendMessage(steamID, 'I don\'t have any crates of series '+series+' available. Sorry!');
return true; // Give up
} // Let the user know we don't have enough
else if (amount > pool.length) {
logger.debug('User requested '+amount+' of series '+series+'. I only have '+pool.length+' available.');
client.friends.sendMessage(steamID, 'I only have '+pool.length+' crates of series '+series+' available. Sending a trade offer with all crates of this series.');
}
// Start a new trade offer
var trade = offers.createOffer(steamID);
// Add what we should to the current trade
logger.debug('Adding '+pool.length+' crates of series '+series);
trade.addMyItems(pool);
// Send the offer off to Steam with a cute message
trade.send('Here are the free crates you requested! <3', function (err, status){
if (err) {
logger.error(err);
client.friends.sendMessage(steamID, 'Something went wrong when trying to send the trade offer. Steam message: '+err);
} else if (status == 'pending'){
logger.warn('Trade offer sent but awaiting email confirmation. You should probably turn off email confirmation here: http://steamcommunity.com/my/edit/settings/');
client.friends.sendMessage(steamID, 'Awaiting email confirmation');
} else {
logger.info('Trade offer sent successfully');
client.friends.sendMessage(steamID, 'Trade offer sent successfully. You can find the offer here: http://steamcommunity.com/tradeoffer/'+trade.id);
}
});
}
});
}