Advertisement
JieBaef

Untitled

Jul 18th, 2019
621
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(prop in a){
  36.     if(prop!=="ids") {
  37.       accs.push(a[prop]);
  38.     }
  39.   }
  40.   return accs;
  41. }
  42.  
  43. function addAcc(accountid){
  44.   updateUsers();
  45.   //var uacid;
  46.   //check for already existing users
  47.   var found = false;
  48.   for(var i = 1; i <= users.ids; i++){
  49.     if(users["user" + i] == accountid) {
  50.       found = true;
  51.       break;
  52.     }
  53.   }
  54.   //console.log(found);
  55.  
  56.   //adding new user
  57.   if(!found){
  58.     users.ids++;
  59.     var user = "user" + users.ids;
  60.  
  61.     users[user] = accountid;
  62.     var usr = JSON.stringify(users, null, 2);
  63.  
  64.     //IMPORTANT
  65.  
  66.     fs.writeFileSync(pathUsers, usr);
  67.     console.log(">>> added user");
  68.   }
  69.   else{
  70.     console.log(">>> didnt add user");
  71.   }
  72.   updateUsers();
  73. }
  74.  
  75. function removeAcc(accountid){
  76.   updateUsers();
  77.   //console.log(accountid);
  78.   var accs = getAllAccs();
  79.   //console.log(accs);
  80.   var found = false;
  81.   var i;
  82.   for(i = 1; i <= accs.length; i++){
  83.     if(accs[i] == accountid){
  84.       accs[i] = null;
  85.       found = true;
  86.       break;
  87.     }
  88.   }
  89.   var newUsersJSON;
  90.   if(found){
  91.     newUsersJSON = { "ids": users.ids-1 };
  92.     //console.log(accs);
  93.     for(var j = 1; j <= accs.length; j++){
  94.       if(j != i){
  95.         newUsersJSON["user" + j] = accs[j-1];
  96.       }
  97.     }
  98.     //console.log(newUsersJSON);
  99.     var usr = JSON.stringify(newUsersJSON, null, 2);
  100.     fs.writeFileSync(pathUsers, usr);
  101.     updateUsers();
  102.   }
  103.   else{
  104.     var msg = "cant find your id. Couldnt remove you from the list since you dont exist on it yet / not anymore";
  105.     client.chatMessage(createSteamIDfromAccID(accountid), msg);
  106.     console.log(msg);
  107.   }
  108.  
  109. }
  110.  
  111.  
  112. function sendMsgToAccs(msg){
  113.   updateUsers();
  114.   getAllAccs().map(
  115.     acc=>client.chatMessage(createSteamIDfromAccID(acc), msg)
  116.   )
  117.   console.log(">>> sent \"" + msg + "\" to all users");
  118. }
  119.  
  120. function updateUsers(){
  121.   usersData = fs.readFileSync(pathUsers);
  122.   users = JSON.parse(usersData);
  123. }
  124.  
  125. app.post('/example', (req, res) => {
  126.   msg = req.body.fname + " " + req.body.lname;
  127.   console.log("> data " + msg);
  128.  
  129.     sendMsgToAccs(msg);
  130.  
  131. });
  132.  
  133. const port = 8080;
  134.  
  135. app.listen(port, () => {
  136.   console.log(`>>> Server running on port ${port}`);
  137. });
  138.  
  139. client.on('loggedOn', () => {
  140.   console.log(">>> Bot successfully logged on!");
  141.   console.log();
  142.   client.setPersona(SteamUser.EPersonaState.Online);
  143.   client.gamesPlayed("Chat Bot Testing");
  144. });
  145.  
  146. function createSteamIDfromAccID(accountID){
  147.   return SteamID.fromIndividualAccountID(accountID);
  148. }
  149.  
  150. client.on("friendMessage", function(steamID, message){
  151.   if(message == "remove"){
  152.     console.log(">>> accountid: " + steamid.acountid + " is removing them from the list now");
  153.     console.log();
  154.     client.chatMessage(createSteamIDfromAccID(steamID.accountid), "removing you from the list now");
  155.     removeAcc(steamID.accountid);
  156.   }
  157.   else if (message == "register") {
  158.     console.log(">>> accountid: " + steamID.accountid + " is registering now");
  159.     console.log();
  160.     addAcc(steamID.accountid);
  161.  
  162.   }
  163. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement