From 30a9143ac860e0c700025afb9865586bf9d30e28 Mon Sep 17 00:00:00 2001 From: James Parsons Date: Sat, 21 Mar 2020 17:29:04 -0700 Subject: [PATCH 1/7] Change package path --- .github/workflows/azure.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/azure.yml b/.github/workflows/azure.yml index 3460455..adc40f1 100644 --- a/.github/workflows/azure.yml +++ b/.github/workflows/azure.yml @@ -9,12 +9,13 @@ # For more information on GitHub Actions for Azure, refer to https://github.com/Azure/Actions # For more samples to get started with GitHub Action workflows to deploy to Azure, refer to https://github.com/Azure/actions-workflow-samples on: - release: - types: [created] + push: + branches: + - master env: AZURE_WEBAPP_NAME: MeatCoin # set this to your application's name - AZURE_WEBAPP_PACKAGE_PATH: 'https://meatcoin.azurewebsites.net' # set this to the path to your web app project, defaults to the repository root + AZURE_WEBAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root NODE_VERSION: '12.0' # set this to the node version to use jobs: From 8e8a4b0e30b74752dcfce655bdd96c2156107648 Mon Sep 17 00:00:00 2001 From: James Parsons Date: Sat, 21 Mar 2020 18:45:39 -0700 Subject: [PATCH 2/7] Run azure action on PR --- .github/workflows/azure.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/azure.yml b/.github/workflows/azure.yml index adc40f1..7fe2623 100644 --- a/.github/workflows/azure.yml +++ b/.github/workflows/azure.yml @@ -12,6 +12,9 @@ on: push: branches: - master + pull_request: + branches: + - master env: AZURE_WEBAPP_NAME: MeatCoin # set this to your application's name From aec2edf32c0d34a8a2fbcd75a431d43c3f746f8d Mon Sep 17 00:00:00 2001 From: James Parsons Date: Sat, 21 Mar 2020 20:07:05 -0700 Subject: [PATCH 3/7] test --- test.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.txt diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..e69de29 From 09b46772e3d72c611a61a4ce089ef35a8fc795f4 Mon Sep 17 00:00:00 2001 From: James Parsons Date: Sat, 21 Mar 2020 20:49:51 -0700 Subject: [PATCH 4/7] Get access token from App Settings --- index.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index e33614d..ef5ec47 100644 --- a/index.js +++ b/index.js @@ -119,24 +119,40 @@ bot.on('message', (message) => { }); function init() { - const token = parseToken(); + // Load user data and app settings + const token = getToken(); parseLedger(); parsePrice(); parseTime(); + + // Initialize app data meatState(); populateDiceTable(); + + // Schedule intervals for repetitive work setInterval(timeIncrement, 1000); setInterval(mine, 60000); setInterval(fluctuate, 5000); - // Meat Coin login + // Login bot.login(token); } +function getToken() { + // This environment variable should be set if the app is deployed to Azure + var accessToken = process.env.DISCORD_ACCESS_TOKEN; + if (accessToken ) { + return accessToken; + } + + // Parse token.txt if the app is running locally + return parseToken(); +} + function parseToken() { const path = process.cwd(); const buffer = fs.readFileSync(path + "\\data\\token.txt").toString().split('\n'); - return buffer[0] + return buffer[0]; } function parseLedger() { From 0a59f9944cebd683590009f350bd22d572ce79d9 Mon Sep 17 00:00:00 2001 From: James Parsons Date: Sun, 22 Mar 2020 03:14:04 -0700 Subject: [PATCH 5/7] Test Azure files --- index.js | 6 +- package-lock.json | 313 ++++++++++++++++++++++++++++++++++++++++++++-- package.json | 1 + 3 files changed, 311 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index ef5ec47..94deb0c 100644 --- a/index.js +++ b/index.js @@ -121,7 +121,7 @@ bot.on('message', (message) => { function init() { // Load user data and app settings const token = getToken(); - parseLedger(); + ledger = getLedger(); parsePrice(); parseTime(); @@ -155,6 +155,10 @@ function parseToken() { return buffer[0]; } +function getLedger() { + +} + function parseLedger() { var path = process.cwd(); var buffer = fs.readFileSync(path + "\\data\\ledger.txt"); diff --git a/package-lock.json b/package-lock.json index 1c96538..3c39b36 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,43 +4,313 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "@azure/abort-controller": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@azure/abort-controller/-/abort-controller-1.0.1.tgz", + "integrity": "sha512-wP2Jw6uPp8DEDy0n4KNidvwzDjyVV2xnycEIq7nPzj1rHyb/r+t3OPeNT1INZePP2wy5ZqlwyuyOMTi0ePyY1A==", + "requires": { + "tslib": "^1.9.3" + } + }, + "@azure/core-asynciterator-polyfill": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@azure/core-asynciterator-polyfill/-/core-asynciterator-polyfill-1.0.0.tgz", + "integrity": "sha512-kmv8CGrPfN9SwMwrkiBK9VTQYxdFQEGe0BmQk+M8io56P9KNzpAxcWE/1fxJj7uouwN4kXF0BHW8DNlgx+wtCg==" + }, + "@azure/core-auth": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@azure/core-auth/-/core-auth-1.0.2.tgz", + "integrity": "sha512-zhPJObdrhz2ymIqGL1x8i3meEuaLz0UPjH9mOq9RGOlJB2Pb6K6xPtkHbRsfElgoO9USR4hH2XU5pLa4/JHHIw==", + "requires": { + "@azure/abort-controller": "^1.0.0", + "@azure/core-tracing": "1.0.0-preview.7", + "@opentelemetry/types": "^0.2.0", + "tslib": "^1.9.3" + } + }, + "@azure/core-http": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@azure/core-http/-/core-http-1.0.4.tgz", + "integrity": "sha512-UhQ4Tpgv6a/7fmvleXEpyQAg8FWcemqzAPY2F75QBygEDD4Hsz2fFujTAEUBQ1an+eNQjzk7EC7TTbqXOmiwAw==", + "requires": { + "@azure/abort-controller": "^1.0.0", + "@azure/core-auth": "^1.0.0", + "@azure/core-tracing": "1.0.0-preview.7", + "@azure/logger": "^1.0.0", + "@opentelemetry/types": "^0.2.0", + "@types/node-fetch": "^2.5.0", + "@types/tunnel": "^0.0.1", + "cross-env": "^6.0.3", + "form-data": "^3.0.0", + "node-fetch": "^2.6.0", + "process": "^0.11.10", + "tough-cookie": "^3.0.1", + "tslib": "^1.10.0", + "tunnel": "^0.0.6", + "uuid": "^3.3.2", + "xml2js": "^0.4.19" + } + }, + "@azure/core-lro": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@azure/core-lro/-/core-lro-1.0.1.tgz", + "integrity": "sha512-RIrM6CTRoYoJEXpCuAr8vanXxlFBCCaHpa/PCSUfcYe6B9pAdSi1DTUZZ+2w2ysNIMHB9MyOLEAkzZ/4NCJIdQ==", + "requires": { + "@azure/abort-controller": "^1.0.0", + "@azure/core-http": "^1.0.0", + "@opentelemetry/types": "^0.2.0", + "events": "^3.0.0", + "tslib": "^1.10.0" + } + }, + "@azure/core-paging": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@azure/core-paging/-/core-paging-1.1.0.tgz", + "integrity": "sha512-Di26joIBaa5E/YAIAcis2LytbNy/LKTmgj+CUYZ8aLnzrN8h9AgVNASYOFXf+weJFD0pyaTAeW4GAt6P9NDFWw==", + "requires": { + "@azure/core-asynciterator-polyfill": "^1.0.0" + } + }, + "@azure/core-tracing": { + "version": "1.0.0-preview.7", + "resolved": "https://registry.npmjs.org/@azure/core-tracing/-/core-tracing-1.0.0-preview.7.tgz", + "integrity": "sha512-pkFCw6OiJrpR+aH1VQe6DYm3fK2KWCC5Jf3m/Pv1RxF08M1Xm08RCyQ5Qe0YyW5L16yYT2nnV48krVhYZ6SGFA==", + "requires": { + "@opencensus/web-types": "0.0.7", + "@opentelemetry/types": "^0.2.0", + "tslib": "^1.9.3" + } + }, + "@azure/logger": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@azure/logger/-/logger-1.0.0.tgz", + "integrity": "sha512-g2qLDgvmhyIxR3JVS8N67CyIOeFRKQlX/llxYJQr1OSGQqM3HTpVP8MjmjcEKbL/OIt2N9C9UFaNQuKOw1laOA==", + "requires": { + "tslib": "^1.9.3" + } + }, + "@azure/storage-blob": { + "version": "12.1.1", + "resolved": "https://registry.npmjs.org/@azure/storage-blob/-/storage-blob-12.1.1.tgz", + "integrity": "sha512-FhzXfrPe5DZE5KNbFoKXXhGqX362I+dGv0jVINCCQiToqadROZ1tRFtJ3ljnMPs75fZZzwnbq+oIB6NnpBqOzA==", + "requires": { + "@azure/abort-controller": "^1.0.0", + "@azure/core-http": "^1.0.3", + "@azure/core-lro": "^1.0.0", + "@azure/core-paging": "^1.1.0", + "@azure/core-tracing": "1.0.0-preview.7", + "@azure/logger": "^1.0.0", + "@opentelemetry/types": "^0.2.0", + "events": "^3.0.0", + "tslib": "^1.10.0" + } + }, + "@opencensus/web-types": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@opencensus/web-types/-/web-types-0.0.7.tgz", + "integrity": "sha512-xB+w7ZDAu3YBzqH44rCmG9/RlrOmFuDPt/bpf17eJr8eZSrLt7nc7LnWdxM9Mmoj/YKMHpxRg28txu3TcpiL+g==" + }, + "@opentelemetry/types": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/@opentelemetry/types/-/types-0.2.0.tgz", + "integrity": "sha512-GtwNB6BNDdsIPAYEdpp3JnOGO/3AJxjPvny53s3HERBdXSJTGQw8IRhiaTEX0b3w9P8+FwFZde4k+qkjn67aVw==" + }, + "@types/node": { + "version": "13.9.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-13.9.2.tgz", + "integrity": "sha512-bnoqK579sAYrQbp73wwglccjJ4sfRdKU7WNEZ5FW4K2U6Kc0/eZ5kvXG0JKsEKFB50zrFmfFt52/cvBbZa7eXg==" + }, + "@types/node-fetch": { + "version": "2.5.5", + "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.5.5.tgz", + "integrity": "sha512-IWwjsyYjGw+em3xTvWVQi5MgYKbRs0du57klfTaZkv/B24AEQ/p/IopNeqIYNy3EsfHOpg8ieQSDomPcsYMHpA==", + "requires": { + "@types/node": "*", + "form-data": "^3.0.0" + } + }, + "@types/tunnel": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/@types/tunnel/-/tunnel-0.0.1.tgz", + "integrity": "sha512-AOqu6bQu5MSWwYvehMXLukFHnupHrpZ8nvgae5Ggie9UwzDR1CCwoXgSSWNZJuyOlCdfdsWMA5F2LlmvyoTv8A==", + "requires": { + "@types/node": "*" + } + }, "async-limiter": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz", "integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==" }, + "asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" + }, + "combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "requires": { + "delayed-stream": "~1.0.0" + } + }, + "cross-env": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/cross-env/-/cross-env-6.0.3.tgz", + "integrity": "sha512-+KqxF6LCvfhWvADcDPqo64yVIB31gv/jQulX2NGzKS/g3GEVz6/pt4wjHFtFWsHMddebWD/sDthJemzM4MaAag==", + "requires": { + "cross-spawn": "^7.0.0" + } + }, + "cross-spawn": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.1.tgz", + "integrity": "sha512-u7v4o84SwFpD32Z8IIcPZ6z1/ie24O6RU3RbtL5Y316l3KuHVPx9ItBgWQ6VlfAFnRnTtMUrsQ9MUUTuEZjogg==", + "requires": { + "path-key": "^3.1.0", + "shebang-command": "^2.0.0", + "which": "^2.0.1" + } + }, + "delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" + }, "discord.js": { "version": "11.3.0", "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-11.3.0.tgz", "integrity": "sha512-xM3ydvb4urHjCP3N+Mgpw53a7ZGtmPwllwVgwPqdF2SHNPvTDr4So/+55VVx76s5eO9IMrOWpczsEuIr0/MAgQ==", "requires": { - "long": "3.2.0", - "prism-media": "0.0.1", - "snekfetch": "3.6.1", - "tweetnacl": "1.0.0", - "ws": "4.0.0" + "long": "^3.2.0", + "prism-media": "^0.0.1", + "snekfetch": "^3.6.1", + "tweetnacl": "^1.0.0", + "ws": "^4.0.0" } }, + "events": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/events/-/events-3.1.0.tgz", + "integrity": "sha512-Rv+u8MLHNOdMjTAFeT3nCjHn2aGlx435FP/sDHNaRhDEMwyI/aB22Kj2qIN8R0cw3z28psEQLYwxVKLsKrMgWg==" + }, + "form-data": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.0.tgz", + "integrity": "sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + }, + "ip-regex": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/ip-regex/-/ip-regex-2.1.0.tgz", + "integrity": "sha1-+ni/XS5pE8kRzp+BnuUUa7bYROk=" + }, + "isexe": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", + "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=" + }, "long": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/long/-/long-3.2.0.tgz", "integrity": "sha1-2CG3E4yhy1gcFymQ7xTbIAtcR0s=" }, + "mime-db": { + "version": "1.43.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.43.0.tgz", + "integrity": "sha512-+5dsGEEovYbT8UY9yD7eE4XTc4UwJ1jBYlgaQQF38ENsKR3wj/8q8RFZrF9WIZpB2V1ArTVFUva8sAul1NzRzQ==" + }, + "mime-types": { + "version": "2.1.26", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.26.tgz", + "integrity": "sha512-01paPWYgLrkqAyrlDorC1uDwl2p3qZT7yl806vW7DvDoxwXi46jsjFbg+WdwotBIk6/MbEhO/dh5aZ5sNj/dWQ==", + "requires": { + "mime-db": "1.43.0" + } + }, + "node-fetch": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", + "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" + }, + "path-key": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz", + "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==" + }, "prism-media": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-0.0.1.tgz", "integrity": "sha1-o0JcnKvVDRxsAuVDlBoRiVZnvRA=" }, + "process": { + "version": "0.11.10", + "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", + "integrity": "sha1-czIwDoQBYb2j5podHZGn1LwW8YI=" + }, + "psl": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/psl/-/psl-1.7.0.tgz", + "integrity": "sha512-5NsSEDv8zY70ScRnOTn7bK7eanl2MvFrOrS/R6x+dBt5g1ghnj9Zv90kO8GwT8gxcu2ANyFprnFYB85IogIJOQ==" + }, + "punycode": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", + "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" + }, "safe-buffer": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz", "integrity": "sha512-kKvNJn6Mm93gAczWVJg7wH+wGYWNrDHdWvpUmHyEsgCtIwwo3bqPtV4tR5tuPaUhTOo/kvhVwd8XwwOllGYkbg==" }, + "sax": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", + "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==" + }, + "shebang-command": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", + "integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==", + "requires": { + "shebang-regex": "^3.0.0" + } + }, + "shebang-regex": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz", + "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==" + }, "snekfetch": { "version": "3.6.1", "resolved": "https://registry.npmjs.org/snekfetch/-/snekfetch-3.6.1.tgz", "integrity": "sha512-aLEvf1YR440pINb0LEo/SL2Q2s/A26+YEqPlx09A0XpGH7qWp8iqIFFolVILHn2yudWXJne9QWyQu+lzDp+ksQ==" }, + "tough-cookie": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-3.0.1.tgz", + "integrity": "sha512-yQyJ0u4pZsv9D4clxO69OEjLWYw+jbgspjTue4lTQZLfV0c5l1VmK2y1JK8E9ahdpltPOaAThPcp5nKPUgSnsg==", + "requires": { + "ip-regex": "^2.1.0", + "psl": "^1.1.28", + "punycode": "^2.1.1" + } + }, + "tslib": { + "version": "1.11.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.11.1.tgz", + "integrity": "sha512-aZW88SY8kQbU7gpV19lN24LtXh/yD4ZZg6qieAJDDg+YBsJcSmLGK9QpnUjAKVG/xefmvJGd1WUmfpT/g6AJGA==" + }, + "tunnel": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.6.tgz", + "integrity": "sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg==" + }, "tweetnacl": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.0.tgz", @@ -51,15 +321,42 @@ "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.1.1.tgz", "integrity": "sha512-UIEXBNeYmKptWH6z8ZnqTeS8fV74zG0/eRU9VGkpzz+LIJNs8W/zM/L+7ctCkRrgbNnnR0xxw4bKOr0cW0N0Og==" }, + "uuid": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz", + "integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==" + }, + "which": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", + "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", + "requires": { + "isexe": "^2.0.0" + } + }, "ws": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/ws/-/ws-4.0.0.tgz", "integrity": "sha512-QYslsH44bH8O7/W2815u5DpnCpXWpEK44FmaHffNwgJI4JMaSZONgPBTOfrxJ29mXKbXak+LsJ2uAkDTYq2ptQ==", "requires": { - "async-limiter": "1.0.0", - "safe-buffer": "5.1.1", - "ultron": "1.1.1" + "async-limiter": "~1.0.0", + "safe-buffer": "~5.1.0", + "ultron": "~1.1.0" } + }, + "xml2js": { + "version": "0.4.23", + "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.23.tgz", + "integrity": "sha512-ySPiMjM0+pLDftHgXY4By0uswI3SPKLDw/i3UXbnO8M/p28zqexCUoPmQFrYD+/1BzhGJSs2i1ERWKJAtiLrug==", + "requires": { + "sax": ">=0.6.0", + "xmlbuilder": "~11.0.0" + } + }, + "xmlbuilder": { + "version": "11.0.1", + "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-11.0.1.tgz", + "integrity": "sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==" } } } diff --git a/package.json b/package.json index 0f3aeac..7333a00 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "license": "ISC", "repository": "https://github.com/jwparsons/MeatCoin", "dependencies": { + "@azure/storage-blob": "^12.1.1", "discord.js": "^11.3.0" } } From 16dc94a96089095a0c245d27356f232159336a34 Mon Sep 17 00:00:00 2001 From: James Parsons Date: Sun, 22 Mar 2020 12:55:52 -0700 Subject: [PATCH 6/7] Check for Azure storage --- index.js | 78 ++++++++++++++++++++++++++++++++++------------------ package.json | 1 - 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index 94deb0c..f93e10f 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,12 @@ const fs = require('fs'); const Discord = require('discord.js'); const bot = new Discord.Client(); +// storage identifiers +const TOKEN_FILE_NAME = 'token.txt'; +const LEDGER_FILE_NAME = 'ledger.txt'; +const PRICE_FILE_NAME = 'price.txt'; +const TIME_FILE_NAME = 'time.txt'; + // constants const fee = 0.05; @@ -120,10 +126,10 @@ bot.on('message', (message) => { function init() { // Load user data and app settings - const token = getToken(); - ledger = getLedger(); - parsePrice(); - parseTime(); + const token = readToken(TOKEN_FILE_NAME, parseToken); + ledger = readStorage(LEDGER_FILE_NAME, parseLedger); + price = readStorage(PRICE_FILE_NAME, parsePrice); + time = readStorage(TIME_FILE_NAME, parseTime); // Initialize app data meatState(); @@ -138,30 +144,40 @@ function init() { bot.login(token); } -function getToken() { +function readToken(fileName, parsingFunc) { // This environment variable should be set if the app is deployed to Azure var accessToken = process.env.DISCORD_ACCESS_TOKEN; - if (accessToken ) { + if (accessToken ) return accessToken; - } // Parse token.txt if the app is running locally - return parseToken(); + var tokenPath = path.join('.', 'data', fileName); + if (fileExists(tokenPath)) + return parsingFunc(tokenPath); } -function parseToken() { - const path = process.cwd(); - const buffer = fs.readFileSync(path + "\\data\\token.txt").toString().split('\n'); - return buffer[0]; +function readStorage(fileName, parsingFunc) { + // Azure mounted storage will exist if the app is deployed to Azure + // path.join() ensures cross-compat. file references on Ubuntu and Windows + var ledgerPath = path.join('~', 'data', fileName); + if (fileExists(ledgerPath)) + return parsingFunc(ledgerPath); + + // Check local storage + ledgerPath = path.join('.', 'data', fileName); + if (fileExists(ledgerPath)) + return parsingFunc(ledgerPath); } -function getLedger() { - +function parseToken(path) { + const buffer = fs.readFileSync(path).toString().split('\n'); + return buffer[0]; } -function parseLedger() { - var path = process.cwd(); - var buffer = fs.readFileSync(path + "\\data\\ledger.txt"); +function parseLedger(path) { + var localLedger = {}; + + var buffer = fs.readFileSync(path); var lines = buffer.toString().split("\n"); for (var i = 0; i < lines.length; i++) { var line = lines[i].replace('\r', ''); @@ -180,24 +196,32 @@ function parseLedger() { channel: null, history: userHistory }; - ledger[data[0]] = userData; + localLedger[data[0]] = userData; } registered += 1; } -} -function parsePrice() { - const path = process.cwd(); - const buffer = fs.readFileSync(path + "\\data\\price.txt").toString().split('\n'); + return localLedger; +} - price = parseFloat(buffer[0].toString()); +function parsePrice(path) { + const buffer = fs.readFileSync(path).toString().split('\n'); + return parseFloat(buffer[0].toString()); } -function parseTime() { - const path = process.cwd(); - const buffer = fs.readFileSync(path + "\\data\\time.txt").toString().split('\n'); +function parseTime(path) { + const buffer = fs.readFileSync(path).toString().split('\n'); + return parseFloat(buffer[0].toString()); +} - time = parseFloat(buffer[0].toString()); +function fileExists(path) { + fs.access(path, (err) => { + if (err) { + return false; + } else { + return true; + } + }); } function meatState() { diff --git a/package.json b/package.json index 7333a00..0f3aeac 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "license": "ISC", "repository": "https://github.com/jwparsons/MeatCoin", "dependencies": { - "@azure/storage-blob": "^12.1.1", "discord.js": "^11.3.0" } } From 4f8a3541c261a1882b8b62f9b46a182b1453e925 Mon Sep 17 00:00:00 2001 From: James Parsons Date: Sun, 22 Mar 2020 13:37:07 -0700 Subject: [PATCH 7/7] Fix file existence checks --- index.js | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index f93e10f..45796fe 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ const fs = require('fs'); -const Discord = require('discord.js'); -const bot = new Discord.Client(); +const path = require('path'); +const discord = require('discord.js'); +const bot = new discord.Client(); // storage identifiers const TOKEN_FILE_NAME = 'token.txt'; @@ -17,8 +18,8 @@ var betTable = {}; var diceTable = {}; // statistics -var price; -var time; +var price = 0; +var time = 0; var meatTotalLast = 0; var volume = { bought: 0.0, @@ -152,21 +153,21 @@ function readToken(fileName, parsingFunc) { // Parse token.txt if the app is running locally var tokenPath = path.join('.', 'data', fileName); - if (fileExists(tokenPath)) + if (fs.existsSync(tokenPath)) return parsingFunc(tokenPath); } function readStorage(fileName, parsingFunc) { // Azure mounted storage will exist if the app is deployed to Azure // path.join() ensures cross-compat. file references on Ubuntu and Windows - var ledgerPath = path.join('~', 'data', fileName); - if (fileExists(ledgerPath)) - return parsingFunc(ledgerPath); + var storagePath = path.join('~', 'data', fileName); + if (fs.existsSync(storagePath)) + return parsingFunc(storagePath); // Check local storage - ledgerPath = path.join('.', 'data', fileName); - if (fileExists(ledgerPath)) - return parsingFunc(ledgerPath); + storagePath = path.join('.', 'data', fileName); + if (fs.existsSync(storagePath)) + return parsingFunc(storagePath); } function parseToken(path) { @@ -214,24 +215,13 @@ function parseTime(path) { return parseFloat(buffer[0].toString()); } -function fileExists(path) { - fs.access(path, (err) => { - if (err) { - return false; - } else { - return true; - } - }); -} - function meatState() { - if (Object.keys(ledger).length > 0) { - Object.keys(ledger).forEach(function(key) { - userData = ledger[key]; - meatTotalLast += parseFloat(userData.meatCoin.toFixed(2)); - }); - } - console.log('Last meat total: ' + meatTotalLast); + Object.keys(ledger).forEach(function(key) { + userData = ledger[key]; + meatTotalLast += parseFloat(userData.meatCoin.toFixed(2)); + }); + + console.log('Last meat total: ' + meatTotalLast); } function populateDiceTable() {