2020-08-12 17:08:37 -04:00
|
|
|
const humanizeDuration = require("humanize-duration");
|
|
|
|
const moment = require("moment");
|
2018-03-11 17:17:14 -04:00
|
|
|
const blocked = require("../data/blocked");
|
|
|
|
const utils = require("../utils");
|
|
|
|
|
2019-08-13 13:34:46 -04:00
|
|
|
module.exports = ({ bot, knex, config, commands }) => {
|
2019-06-09 13:04:40 -04:00
|
|
|
async function removeExpiredBlocks() {
|
|
|
|
const expiredBlocks = await blocked.getExpiredBlocks();
|
|
|
|
const logChannel = utils.getLogChannel();
|
|
|
|
for (const userId of expiredBlocks) {
|
|
|
|
await blocked.unblock(userId);
|
|
|
|
logChannel.createMessage(`Block of <@!${userId}> (id \`${userId}\`) expired`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function expiredBlockLoop() {
|
|
|
|
try {
|
|
|
|
removeExpiredBlocks();
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(expiredBlockLoop, 2000);
|
|
|
|
}
|
|
|
|
|
2020-08-12 17:08:37 -04:00
|
|
|
bot.on("ready", expiredBlockLoop);
|
2019-06-09 13:04:40 -04:00
|
|
|
|
2019-06-16 15:27:30 -04:00
|
|
|
const blockCmd = async (msg, args, thread) => {
|
|
|
|
const userIdToBlock = args.userId || (thread && thread.user_id);
|
2019-06-09 13:04:40 -04:00
|
|
|
if (! userIdToBlock) return;
|
|
|
|
|
|
|
|
const isBlocked = await blocked.isBlocked(userIdToBlock);
|
|
|
|
if (isBlocked) {
|
2020-08-12 17:08:37 -04:00
|
|
|
msg.channel.createMessage("User is already blocked");
|
2019-06-09 13:04:40 -04:00
|
|
|
return;
|
2018-03-11 17:17:14 -04:00
|
|
|
}
|
|
|
|
|
2019-06-16 15:27:30 -04:00
|
|
|
const expiresAt = args.blockTime
|
2020-08-12 17:08:37 -04:00
|
|
|
? moment.utc().add(args.blockTime, "ms").format("YYYY-MM-DD HH:mm:ss")
|
2019-06-09 13:04:40 -04:00
|
|
|
: null;
|
|
|
|
|
|
|
|
const user = bot.users.get(userIdToBlock);
|
2020-08-12 17:08:37 -04:00
|
|
|
await blocked.block(userIdToBlock, (user ? `${user.username}#${user.discriminator}` : ""), msg.author.id, expiresAt);
|
2019-06-09 13:04:40 -04:00
|
|
|
|
|
|
|
if (expiresAt) {
|
2019-06-16 15:27:30 -04:00
|
|
|
const humanized = humanizeDuration(args.blockTime, { largest: 2, round: true });
|
2019-06-09 13:04:40 -04:00
|
|
|
msg.channel.createMessage(`Blocked <@${userIdToBlock}> (id \`${userIdToBlock}\`) from modmail for ${humanized}`);
|
|
|
|
} else {
|
|
|
|
msg.channel.createMessage(`Blocked <@${userIdToBlock}> (id \`${userIdToBlock}\`) from modmail indefinitely`);
|
2018-03-11 17:17:14 -04:00
|
|
|
}
|
2019-06-16 15:27:30 -04:00
|
|
|
};
|
2018-03-11 17:17:14 -04:00
|
|
|
|
2020-08-12 17:08:37 -04:00
|
|
|
commands.addInboxServerCommand("block", "<userId:userId> [blockTime:delay]", blockCmd);
|
|
|
|
commands.addInboxServerCommand("block", "[blockTime:delay]", blockCmd);
|
2019-06-09 13:04:40 -04:00
|
|
|
|
2019-06-16 15:27:30 -04:00
|
|
|
const unblockCmd = async (msg, args, thread) => {
|
|
|
|
const userIdToUnblock = args.userId || (thread && thread.user_id);
|
2019-06-09 13:04:40 -04:00
|
|
|
if (! userIdToUnblock) return;
|
|
|
|
|
2019-06-09 13:25:21 -04:00
|
|
|
const isBlocked = await blocked.isBlocked(userIdToUnblock);
|
|
|
|
if (! isBlocked) {
|
2020-08-12 17:08:37 -04:00
|
|
|
msg.channel.createMessage("User is not blocked");
|
2019-06-09 13:25:21 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-16 15:27:30 -04:00
|
|
|
const unblockAt = args.unblockDelay
|
2020-08-12 17:08:37 -04:00
|
|
|
? moment.utc().add(args.unblockDelay, "ms").format("YYYY-MM-DD HH:mm:ss")
|
2019-06-09 13:04:40 -04:00
|
|
|
: null;
|
|
|
|
|
|
|
|
const user = bot.users.get(userIdToUnblock);
|
|
|
|
if (unblockAt) {
|
2019-06-16 15:27:30 -04:00
|
|
|
const humanized = humanizeDuration(args.unblockDelay, { largest: 2, round: true });
|
2019-06-09 13:04:40 -04:00
|
|
|
await blocked.updateExpiryTime(userIdToUnblock, unblockAt);
|
|
|
|
msg.channel.createMessage(`Scheduled <@${userIdToUnblock}> (id \`${userIdToUnblock}\`) to be unblocked in ${humanized}`);
|
|
|
|
} else {
|
|
|
|
await blocked.unblock(userIdToUnblock);
|
|
|
|
msg.channel.createMessage(`Unblocked <@${userIdToUnblock}> (id ${userIdToUnblock}) from modmail`);
|
2018-03-11 17:17:14 -04:00
|
|
|
}
|
2019-06-16 15:27:30 -04:00
|
|
|
};
|
2019-06-09 13:04:40 -04:00
|
|
|
|
2020-08-12 17:08:37 -04:00
|
|
|
commands.addInboxServerCommand("unblock", "<userId:userId> [unblockDelay:delay]", unblockCmd);
|
|
|
|
commands.addInboxServerCommand("unblock", "[unblockDelay:delay]", unblockCmd);
|
2019-06-09 13:04:40 -04:00
|
|
|
|
2020-08-12 17:08:37 -04:00
|
|
|
commands.addInboxServerCommand("is_blocked", "[userId:userId]",async (msg, args, thread) => {
|
2019-06-16 15:27:30 -04:00
|
|
|
const userIdToCheck = args.userId || (thread && thread.user_id);
|
2019-06-09 13:04:40 -04:00
|
|
|
if (! userIdToCheck) return;
|
2018-03-11 17:17:14 -04:00
|
|
|
|
2019-06-09 13:04:40 -04:00
|
|
|
const blockStatus = await blocked.getBlockStatus(userIdToCheck);
|
|
|
|
if (blockStatus.isBlocked) {
|
|
|
|
if (blockStatus.expiresAt) {
|
|
|
|
msg.channel.createMessage(`<@!${userIdToCheck}> (id \`${userIdToCheck}\`) is blocked until ${blockStatus.expiresAt} (UTC)`);
|
|
|
|
} else {
|
|
|
|
msg.channel.createMessage(`<@!${userIdToCheck}> (id \`${userIdToCheck}\`) is blocked indefinitely`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msg.channel.createMessage(`<@!${userIdToCheck}> (id \`${userIdToCheck}\`) is NOT blocked`);
|
2018-03-11 17:17:14 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|