Advertisement
Guest User

Untitled

a guest
Mar 14th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. // accept username from command line then initialize
  2. if (process.argv.length < 3) {
  3. console.error('username not supplied!');
  4. process.exit(1);
  5. }
  6. var logon_settings = { rememberPassword: true, accountName: process.argv[2] }
  7. , Crypto = require('crypto')
  8. , SteamUser = require('steam-user')
  9. , fs = require('fs')
  10. , readline = require('readline').createInterface({
  11. input: process.stdin, output: process.stdout })
  12. , account = { user: new SteamUser(), name: process.argv[2] }
  13. , RiveScript = require("rivescript")
  14. , riveScript = new RiveScript();
  15. account.user.setOption("dataDirectory", null);
  16.  
  17.  
  18. // load chatbot engine
  19. console.log("loading rivescript files...");
  20. riveScript.loadDirectory("./rs", () => {
  21. riveScript.sortReplies();
  22.  
  23. // read json file for persistent user sessions
  24. if (fs.existsSync('users.json')) {
  25. var json = JSON.parse(fs.readFileSync('users.json'));
  26. console.log("restoring " + Object.keys(json).length + " sessions...");
  27. for (var key in json) {
  28. if (json.hasOwnProperty(key)) {
  29. riveScript.setUservars(key, json[key]);
  30. }
  31. }
  32. }
  33.  
  34. // use ssfn/key files if present then start login
  35. if (fs.existsSync('ssfn')) {
  36. account.user.setSentry(Crypto.createHash('sha1').update(
  37. fs.readFileSync('ssfn')).digest()
  38. );
  39. }
  40. if (fs.existsSync('key-' + process.argv[2])) {
  41. logon_settings.loginKey = fs.readFileSync('key-' + process.argv[2], 'utf8');
  42. account.user.logOn(logon_settings);
  43. } else {
  44. readline.question('password: ', (input) => {
  45. logon_settings.password = input;
  46. account.user.logOn(logon_settings);
  47. });
  48. }
  49. account.user.on('sentry', (sentry) => fs.writeFileSync('ssfn', sentry));
  50. account.user.on('loginKey', (key) => fs.writeFileSync('key-' + process.argv[2], key, 'utf8'));
  51.  
  52. // finish login then set persona state
  53. account.user.on('loggedOn', (sessionID, cookies) => {
  54. console.log('logged on to steam: ' + process.argv[2]);
  55. account.user.setPersona(SteamUser.EPersonaState.LookingToPlay);
  56. 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]);
  57. });
  58.  
  59. // craft, sanitize, then log a reply to a user message
  60. function get_reply(steamID, message, steamID64 = steamID.toString()) {
  61. var reply = riveScript.reply(steamID64, message).replace(
  62. /<oob><search>.*<\/search><\/oob>/, '').replace(
  63. / random/g, ' ').replace(/ /g, ' ').replace('}', '');
  64. if (!reply.length) {
  65. reply = 'Huh?';
  66. }
  67. console.log(new Date() + " | " + riveScript.getUservar(steamID64, 'chat_time') +
  68. "\n>> " + "[" + steamID64 + "] " + reply);
  69. return reply + "ㅤ";
  70. }
  71.  
  72. // interpret commands and pause/resume bot replies by the operator
  73. account.user.on('friendMessageEcho', (recipientID, message, steamID64 = recipientID .toString()) => {
  74. if (message.indexOf('#!') == 0) {
  75. account.user.chatMessage(recipientID, get_reply(recipientID, message.substr(2)));
  76. } else if (message.indexOf('##') == 0) {
  77. riveScript.setUservar(steamID64, 'chat_time', 0);
  78. } else if (message.indexOf('ㅤ') == -1) {
  79. riveScript.setUservar(steamID64, 'chat_time', Date.now());
  80. }
  81. });
  82.  
  83. // react to chat events, ignoring paused/active exchanges
  84. account.user.on('friendMessage', (steamID, message, steamID64 = steamID.toString()) => {
  85. if (riveScript.getUservar(steamID64, 'chat_time') == 'undefined') {
  86. riveScript.setUservar(steamID64, 'chat_time', 0);
  87. riveScript.setUservar(steamID64, 'chat_active', false);
  88. }
  89. console.log(new Date() + " | " + riveScript.getUservar(steamID64, 'chat_time') +
  90. "\n<< " + "[" + steamID64 + "] " + message);
  91. if (Date.now() - riveScript.getUservar(steamID64, 'chat_time') > 3600000
  92. && riveScript.getUservar(steamID64, 'chat_active') != true) {
  93. riveScript.setUservar(steamID64, 'chat_active', true);
  94.  
  95. // feign typing, then respond to the user
  96. var reply = get_reply(steamID, message);
  97. setTimeout(() => {
  98. account.user.chatTyping(steamID);
  99. setTimeout(() => {
  100. account.user.chatMessage(steamID, reply);
  101. riveScript.setUservar(steamID64, 'chat_active', false);
  102. }, Math.max(Math.min(reply.length, 100)*50, 2000));
  103. }, 1000);
  104. }
  105. });
  106. });
  107.  
  108. // logoff, then save user json on sigint/sigterm
  109. quit = () => {
  110. account.user.logOff();
  111. fs.writeFileSync('users.json', JSON.stringify(riveScript.getUservars(), null, 2));
  112. process.exit(0);
  113. }
  114. process.on('SIGINT', quit);
  115. process.on('SIGTERM', quit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement