Advertisement
JieBaef

Untitled

Jul 17th, 2019
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const bodyParser = require('body-parser');
  3. const fs = require('fs')
  4.  
  5. const SteamUser = require('steam-user');
  6. const SteamTotp = require('steam-totp');
  7. const SteamID = require('steamID');
  8.  
  9. const config = require("./json/config.json");
  10. const pathUsers = './json/users.json';
  11. var usersData = fs.readFileSync(pathUsers);
  12. var users = JSON.parse(usersData);
  13.  
  14. const app = express();
  15. const client = new SteamUser();
  16.  
  17. var msg;
  18. var tempUser;
  19. var tempSID;
  20.  
  21. const logOnOptions = {
  22.   accountName: config.username,
  23.   password: config.password,
  24.   twoFactorCode: SteamTotp.generateAuthCode(config.sharedSecret)
  25. };
  26.  
  27. client.logOn(logOnOptions);
  28.  
  29. app.use(bodyParser.urlencoded({ extended: true }));
  30.  
  31.  
  32. function getAllAccs(){
  33.   updateUsers();
  34.   var accs = [];
  35.   for(var i = 0; i < users.ids; i++){
  36.     accs.push(users["user" + (i+1)]);
  37.   }
  38.   return accs;
  39. }
  40.  
  41. function addAcc(accountid){
  42.   updateUsers();
  43.   //var uacid;
  44.   //check for already existing users
  45.   var found = false;
  46.   for(var i = 0; i < users.ids; i++){
  47.     if(users["user" + (i+1)] == accountid)
  48.       found = true;
  49.   }
  50.   //console.log(found);
  51.  
  52.   //adding new user
  53.   if(!found){
  54.     users.ids++;
  55.     var user = "user" + users.ids;
  56.  
  57.     users[user] = accountid;
  58.     var usr = JSON.stringify(users, null, 2);
  59.  
  60.     //IMPORTANT
  61.     fs.writeFileSync(pathUsers, usr, finished)
  62.     function finished (err){
  63.       if(err != null)
  64.         console.log(err);
  65.       else {
  66.         console.log("all set.");
  67.       }
  68.     }
  69.     console.log(">>> added user");
  70.   }
  71.   else{
  72.     console.log(">>> didnt add user");
  73.   }
  74.   updateUsers();
  75. }
  76.  
  77. function removeAcc(accountid){
  78.   updateUsers();
  79.   //console.log(accountid);
  80.   var accs = getAllAccs();
  81.   //console.log(accs);
  82.   var found = false;
  83.   var i;
  84.   for(i = 0; i < accs.length; i++){
  85.     if(accs[i] == accountid){
  86.       accs[i] = null;
  87.       found = true;
  88.       break;
  89.     }
  90.   }
  91.   var newUsersJSON;
  92.   if(found){
  93.     newUsersJSON = { "ids": users.ids-1 };
  94.     //console.log(accs);
  95.     for(var j = 0; j < accs.length; j++){
  96.       if(j != i){
  97.         newUsersJSON["user" + (j+1)] = accs[j];
  98.       }
  99.     }
  100.     //console.log(newUsersJSON);
  101.     var usr = JSON.stringify(newUsersJSON, null, 2);
  102.     fs.writeFileSync(pathUsers, usr, finished)
  103.     function finished (err){
  104.       if(err != null)
  105.         console.log(err);
  106.       else {
  107.         console.log("all set.");
  108.       }
  109.     }
  110.     updateUsers();
  111.   }
  112.   else{
  113.     var msg = "cant find your id. Couldnt remove you from the list since you dont exist on it yet / not anymore";
  114.     client.chatMessage(createSteamIDfromAccID(accountid), msg);
  115.     console.log(msg);
  116.   }
  117.  
  118. }
  119.  
  120.  
  121. function sendMsgToAccs(msg){
  122.   updateUsers();
  123.   var accs = getAllAccs();
  124.   for(var i = 0; i < accs.length; i++){
  125.     client.chatMessage(createSteamIDfromAccID(accs[i]), msg);
  126.   }
  127.   console.log(">>> sent \"" + msg + "\" to all users");
  128. }
  129.  
  130. function updateUsers(){
  131.   usersData = fs.readFileSync(pathUsers);
  132.   users = JSON.parse(usersData);
  133. }
  134.  
  135. app.post('/example', (req, res) => {
  136.   msg = req.body.fname + " " + req.body.lname;
  137.   console.log("> data " + msg);
  138.  
  139.     sendMsgToAccs(msg);
  140.  
  141. });
  142.  
  143. const port = 8080;
  144.  
  145. app.listen(port, () => {
  146.   console.log(`>>> Server running on port ${port}`);
  147. });
  148.  
  149. client.on('loggedOn', () => {
  150.   console.log(">>> Bot successfully logged on!");
  151.   console.log();
  152.   client.setPersona(SteamUser.EPersonaState.Online);
  153.   client.gamesPlayed("Chat Bot Testing");
  154. });
  155.  
  156. function createSteamIDfromAccID(accountID){
  157.   return SteamID.fromIndividualAccountID(accountID);
  158. }
  159.  
  160. client.on("friendMessage", function(steamID, message){
  161.   if(message == "remove"){
  162.     console.log(">>> accountid: " + steamid.acountid + " is removing them from the list now");
  163.     console.log();
  164.     client.chatMessage(createSteamIDfromAccID(steamID.accountid), "removing you from the list now");
  165.     removeAcc(steamID.accountid);
  166.   }
  167.   else if (message == "register") {
  168.     console.log(">>> accountid: " + steamID.accountid + " is registering now");
  169.     console.log();
  170.     addAcc(steamID.accountid);
  171.  
  172.   }
  173. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement