Last active
March 15, 2019 01:38
-
-
Save ferruzzi/c8468566fbcdbab51c744b1fcb9628d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setProtection(msg, location) { | |
/** ******************************************* | |
- check if new base is already protected | |
- if timer remaining is within reup time, then | |
- cancel old timers | |
- set new timers | |
**********************************************/ | |
const ensureLocation = new Promise((resolve, reject) => { | |
if (location) { | |
resolve(location); | |
logger.info(`${msg.author.username} invoked protection on ${location}`); | |
return; | |
} | |
/** ******************************************* | |
* The easy out is to scold them and move on: | |
* msg.channel.send("You must enter a base to protect. For example:\n\t!p Coneyre"); | |
* But if I can I want to send a DM to prompt for missing variable | |
*********************************************/ | |
// This block modified from: | |
// https://stackoverflow.com/questions/51116013/await-reply-in-private-message-discord-js | |
logger.debug(`${msg.author.username} invoked protection without a location; prompting for info`); | |
// Message the user for clarification | |
msg.author.send('Which claim would you like to protect?') | |
.then((newmsg) => { // Now newmsg is the message you sent | |
newmsg.channel.awaitMessages(response => newmsg.content, { | |
max: 1, // Take the next message | |
time: 60000, // Wait at most 1 minute for a reply | |
errors: ['time'] | |
}) | |
.then((collected) => { | |
// Set their reply as the location | |
const loc = collected.first().content; | |
// Confirm that input was received | |
msg.author.send(`${loc} confirmed.`); | |
// Advise how they can skip this step in the future | |
msg.author.send('You can prevent this extra step in the future by entering it all at once. ' + | |
`For example:\n\t!p Coneyre`); | |
// Log that we asked and received clarification for possible tracking reasons | |
logger.debug(`\t${msg.author.username} replied with "${collected.first().content}"`); | |
resolve(loc); | |
}) | |
.catch(e => { | |
// If the user did not reply to the DM, then post instructions in the main channel | |
msg.channel.send('Protection must be applied to a location, For example:' + | |
`\n\t!protect Coneyre`); | |
// Log that no reply was given for potential tracking purposes | |
logger.error(`\t${msg.author.username} did not reply with a location`); | |
reject(e || "some message"); | |
}); | |
}); | |
}); | |
logger.debug(`${location} way after`); | |
ensureLocation().then(location => { | |
if ((location === 'List') || (location === 'L')) { | |
// List bases under protection | |
if (!Array.isArray(protectedBases) || !protectedBases.length) { | |
msg.channel.send(`Currently no claims under protection`); | |
} else { | |
msg.channel.send(`Currently protecting: ${protectedBases.join(', ')}.`); | |
} | |
return; | |
} | |
if (!(protectedBases.includes(location))) { | |
// Add location to the list of protected bases | |
protectedBases.push(location); | |
msg.channel.send(location + ` not currently protected, adding new timer.`); | |
logger.info(`\tAdded ${location} to protection list`); | |
} else { | |
msg.channel.send(location + ` already has a timer.`); | |
logger.info(`\t${location} already on the protection list`); | |
return; | |
} | |
// set a timer to protect it | |
// List currently protected bases | |
msg.channel.send(`Currently protecting: ${protectedBases.join(', ')}.`); | |
// .catch(e => { ... }); | |
}) | |
.catch(e => { | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment