Fix !block/unblock time arg without explicit user id

master
Dragory 2019-06-09 20:25:21 +03:00
parent 1259669c9d
commit a60515bcc7
2 changed files with 20 additions and 8 deletions

View File

@ -29,8 +29,9 @@ module.exports = bot => {
bot.on('ready', expiredBlockLoop); bot.on('ready', expiredBlockLoop);
addInboxServerCommand('block', async (msg, args, thread) => { addInboxServerCommand('block', async (msg, args, thread) => {
const userIdToBlock = args[0] const firstArgUserId = utils.getUserMention(args[0]);
? utils.getUserMention(args[0]) const userIdToBlock = firstArgUserId
? firstArgUserId
: thread && thread.user_id; : thread && thread.user_id;
if (! userIdToBlock) return; if (! userIdToBlock) return;
@ -41,7 +42,8 @@ module.exports = bot => {
return; return;
} }
const expiryTime = args[1] ? utils.convertDelayStringToMS(args[1]) : null; const inputExpiryTime = firstArgUserId ? args[1] : args[0];
const expiryTime = inputExpiryTime ? utils.convertDelayStringToMS(inputExpiryTime) : null;
const expiresAt = expiryTime const expiresAt = expiryTime
? moment.utc().add(expiryTime, 'ms').format('YYYY-MM-DD HH:mm:ss') ? moment.utc().add(expiryTime, 'ms').format('YYYY-MM-DD HH:mm:ss')
: null; : null;
@ -58,13 +60,21 @@ module.exports = bot => {
}); });
addInboxServerCommand('unblock', async (msg, args, thread) => { addInboxServerCommand('unblock', async (msg, args, thread) => {
const userIdToUnblock = args[0] const firstArgUserId = utils.getUserMention(args[0]);
? utils.getUserMention(args[0]) const userIdToUnblock = firstArgUserId
? firstArgUserId
: thread && thread.user_id; : thread && thread.user_id;
if (! userIdToUnblock) return; if (! userIdToUnblock) return;
const unblockDelay = args[1] ? utils.convertDelayStringToMS(args[1]) : null; const isBlocked = await blocked.isBlocked(userIdToUnblock);
if (! isBlocked) {
msg.channel.createMessage('User is not blocked');
return;
}
const inputUnblockDelay = firstArgUserId ? args[1] : args[0];
const unblockDelay = inputUnblockDelay ? utils.convertDelayStringToMS(inputUnblockDelay) : null;
const unblockAt = unblockDelay const unblockAt = unblockDelay
? moment.utc().add(unblockDelay, 'ms').format('YYYY-MM-DD HH:mm:ss') ? moment.utc().add(unblockDelay, 'ms').format('YYYY-MM-DD HH:mm:ss')
: null; : null;

View File

@ -135,9 +135,11 @@ async function formatAttachment(attachment, attachmentUrl) {
* @returns {String|null} * @returns {String|null}
*/ */
function getUserMention(str) { function getUserMention(str) {
if (! str) return null;
str = str.trim(); str = str.trim();
if (str.match(/^[0-9]+$/)) { if (isSnowflake(str)) {
// User ID // User ID
return str; return str;
} else { } else {
@ -294,7 +296,7 @@ function setDataModelProps(target, props) {
const snowflakeRegex = /^[0-9]{17,}$/; const snowflakeRegex = /^[0-9]{17,}$/;
function isSnowflake(str) { function isSnowflake(str) {
return snowflakeRegex.test(str); return str && snowflakeRegex.test(str);
} }
const humanizeDelay = (delay, opts = {}) => humanizeDuration(delay, Object.assign({conjunction: ' and '}, opts)); const humanizeDelay = (delay, opts = {}) => humanizeDuration(delay, Object.assign({conjunction: ' and '}, opts));