Browse Source

Refactored use of file names to be specified in config

shorten
benji7425 4 years ago
parent
commit
12c02185b6
  1. 4
      config.json
  2. 40
      feed-bot.js
  3. 13
      log.js

4
config.json

@ -6,5 +6,7 @@
"youtubeMode": true,
"logRequestMessage": "!logsplease",
"subscribeRequestMessage": "!subscribe",
"unsubscribeRequestMessage": "!unsubscribe"
"unsubscribeRequestMessage": "!unsubscribe",
"logFile": "./log",
"subscribersFile": "./subscribers.json"
}

40
feed-bot.js

@ -60,22 +60,25 @@ var DiscordClient = {
return url;
});
}
else if (message === Config.subscribeRequestMessage) {
Subscriptions.subscribe(userID, user);
}
else if (message === Config.unsubscribeRequestMessage) {
Subscriptions.unsubscribe(userID, user);
else {
switch (message) {
case Config.subscribeRequestMessage:
Subscriptions.subscribe(userID, user);
break;
case Config.unsubscribeRequestMessage:
Subscriptions.unsubscribe(userID, user);
break;
case Config.logRequestMessage:
DiscordClient.bot.uploadFile({
to: channelID,
file: Config.logFileName
}, (err, message) => {
if (err) Log.error("Failed to upload log file: " + message, err);
else Log.event("Uploaded log file for user " + user + "(" + userID + ")");
});
}
}
}
else if (message === Config.logRequestMessage) {
DiscordClient.bot.uploadFile({
to: channelID,
file: "./log"
}, (err, message) => {
if (err) Log.error("Failed to upload log file: " + message, err);
else Log.event("Uploaded log file for user " + user + "(" + userID + ")");
});
}
},
checkPastMessagesForLinks: function () {
var limit = 100;
@ -127,20 +130,23 @@ var DiscordClient = {
var Subscriptions = {
subscribers: [],
parse: function () {
JsonFile.readFile("./subscribers.json", (err, obj) => {
JsonFile.readFile(Config.subscribersFile, (err, obj) => {
if (err) Log.error("Unable to parse json subscribers file", err);
this.subscribers = obj || [];
});
},
subscribe: function (userID, user) {
this.subscribers.push(userID);
JsonFile.writeFile("./subscribers.json", this.subscribers, (err) => { if (err) Log.error("Unable to write subscribers to json file", err); });
this.writeToFile();
Log.event("Subscribed user " + (user ? user + "(" + userID + ")" : userID));
},
unsubscribe: function (userID, user) {
this.subscribers.splice(this.subscribers.indexOf(userID));
JsonFile.writeFile("./subscribers.json", this.subscribers, (err) => { if (err) Log.error("Unable to write subscribers to json file", err); });
this.writeToFile();
Log.event("Unsubscribed user " + (user ? user + "(" + userID + ")" : userID));
},
writeToFile: function () {
JsonFile.writeFile(Config.subscribersFile, this.subscribers, (err) => { if (err) Log.error("Unable to write subscribers to json file", err); });
}
};

13
log.js

@ -1,18 +1,21 @@
var console = require("console");
var FileWriter = require("simple-file-writer");
//external library imports
var Console = require("console"); //access to debug console
var FileWriter = require("simple-file-writer"); //file writer for logging
var logWriter = new FileWriter("./log");
//my imports
var Config = require("./config.json"); //config file containing other settings
var logWriter = new FileWriter(Config.logFileName);
var latestLog = "";
function log(message) {
if (message && message != latestLog) {
if (message && message !== latestLog) {
latestLog = message; //spam reduction
//attach a formatted date string to the beginning of everything we log
var dateMessage = new Date().toLocaleString() + " " + message;
console.log(dateMessage);
Console.log(dateMessage);
logWriter.write(dateMessage + "\n");
}
}

Loading…
Cancel
Save