Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- const bodyParser = require('body-parser');
- const fs = require('fs')
- const SteamUser = require('steam-user');
- const SteamTotp = require('steam-totp');
- const SteamID = require('steamID');
- const config = require("./json/config.json");
- const pathUsers = './json/users.json';
- var usersData = fs.readFileSync(pathUsers);
- var users = JSON.parse(usersData);
- const app = express();
- const client = new SteamUser();
- var msg;
- var tempUser;
- var tempSID;
- const logOnOptions = {
- accountName: config.username,
- password: config.password,
- twoFactorCode: SteamTotp.generateAuthCode(config.sharedSecret)
- };
- client.logOn(logOnOptions);
- app.use(bodyParser.urlencoded({ extended: true }));
- function getAllAccs(){
- updateUsers();
- var accs = [];
- for(var i = 0; i < users.ids; i++){
- accs.push(users["user" + (i+1)]);
- }
- return accs;
- }
- function addAcc(accountid){
- updateUsers();
- //var uacid;
- //check for already existing users
- var found = false;
- for(var i = 0; i < users.ids; i++){
- if(users["user" + (i+1)] == accountid)
- found = true;
- }
- //console.log(found);
- //adding new user
- if(!found){
- users.ids++;
- var user = "user" + users.ids;
- users[user] = accountid;
- var usr = JSON.stringify(users, null, 2);
- //IMPORTANT
- fs.writeFileSync(pathUsers, usr, finished)
- function finished (err){
- if(err != null)
- console.log(err);
- else {
- console.log("all set.");
- }
- }
- console.log(">>> added user");
- }
- else{
- console.log(">>> didnt add user");
- }
- updateUsers();
- }
- function removeAcc(accountid){
- updateUsers();
- //console.log(accountid);
- var accs = getAllAccs();
- //console.log(accs);
- var found = false;
- var i;
- for(i = 0; i < accs.length; i++){
- if(accs[i] == accountid){
- accs[i] = null;
- found = true;
- break;
- }
- }
- var newUsersJSON;
- if(found){
- newUsersJSON = { "ids": users.ids-1 };
- //console.log(accs);
- for(var j = 0; j < accs.length; j++){
- if(j != i){
- newUsersJSON["user" + (j+1)] = accs[j];
- }
- }
- //console.log(newUsersJSON);
- var usr = JSON.stringify(newUsersJSON, null, 2);
- fs.writeFileSync(pathUsers, usr, finished)
- function finished (err){
- if(err != null)
- console.log(err);
- else {
- console.log("all set.");
- }
- }
- updateUsers();
- }
- else{
- var msg = "cant find your id. Couldnt remove you from the list since you dont exist on it yet / not anymore";
- client.chatMessage(createSteamIDfromAccID(accountid), msg);
- console.log(msg);
- }
- }
- function sendMsgToAccs(msg){
- updateUsers();
- var accs = getAllAccs();
- for(var i = 0; i < accs.length; i++){
- client.chatMessage(createSteamIDfromAccID(accs[i]), msg);
- }
- console.log(">>> sent \"" + msg + "\" to all users");
- }
- function updateUsers(){
- usersData = fs.readFileSync(pathUsers);
- users = JSON.parse(usersData);
- }
- app.post('/example', (req, res) => {
- msg = req.body.fname + " " + req.body.lname;
- console.log("> data " + msg);
- sendMsgToAccs(msg);
- });
- const port = 8080;
- app.listen(port, () => {
- console.log(`>>> Server running on port ${port}`);
- });
- client.on('loggedOn', () => {
- console.log(">>> Bot successfully logged on!");
- console.log();
- client.setPersona(SteamUser.EPersonaState.Online);
- client.gamesPlayed("Chat Bot Testing");
- });
- function createSteamIDfromAccID(accountID){
- return SteamID.fromIndividualAccountID(accountID);
- }
- client.on("friendMessage", function(steamID, message){
- if(message == "remove"){
- console.log(">>> accountid: " + steamid.acountid + " is removing them from the list now");
- console.log();
- client.chatMessage(createSteamIDfromAccID(steamID.accountid), "removing you from the list now");
- removeAcc(steamID.accountid);
- }
- else if (message == "register") {
- console.log(">>> accountid: " + steamID.accountid + " is registering now");
- console.log();
- addAcc(steamID.accountid);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement