Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automagically keep track of restricted users + add on rejoin #30

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.env
.env.local
.env.local
*.sqlite3
6 changes: 6 additions & 0 deletions db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const knex = require("knex");

const environment = process.env.NODE_ENV || "development";
const config = require("../knexfile.js")[environment];

module.exports = knex(config);
12 changes: 12 additions & 0 deletions db/migrations/20191226200514_restrictions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
exports.up = function(knex) {
return knex.schema.createTable("restrictions", function(table) {
table.increments("id");
table.string("user_id").notNullable();
table.datetime("start_time");
table.datetime("end_time");
});
};

exports.down = function(knex) {
return knex.schema.dropTable("restrictions");
};
159 changes: 137 additions & 22 deletions features/emojiMod.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const knex = require("../db");
const staffRoles = ["mvp", "moderator", "admin", "admins"];

const isStaff = member =>
Expand All @@ -11,7 +12,10 @@ const config = {
// This is how many ️️warning reactions a post must get until mods are alerted
thumbsDownThreshold: 2,
// This is how many ️️warning reactions a post must get the message is deleted
deletionThreshold: Infinity
deletionThreshold: Infinity,

// The number of days that the restriction role should last
RESTRICTION_DAYS: 3
};

const warningMessages = {};
Expand Down Expand Up @@ -129,36 +133,147 @@ const reactionHandlers = {
});
}
}
},
"⛔": (bot, reaction, message, member) => {
if (!isStaff(member)) {
return;
}

const restrictedRole = message.guild.roles.find(
role => role.name === "restricted"
);
const targetUser = message.guild.members.get(message.author.id);

const modLogChannel = bot.channels.find(
channel => channel.name === "mod-log"
);

const startDate = new Date();
const endDate = new Date();
endDate.setDate(startDate.getDate() + config.RESTRICTION_DAYS);

let logMessage = `<@${message.author.id}> has been restricted for ${config.RESTRICTION_DAYS} days by <@${member.user.id}>.`;
logMessage += "\n\n";
logMessage += "```";
logMessage += `Start Date: ${startDate.toLocaleString()}`;
logMessage += "\n";
logMessage += `End Date: ${endDate.toLocaleString()}`;
logMessage += "```";

targetUser.roles.add(restrictedRole);
modLogChannel.send(logMessage);

knex
.table("restrictions")
.insert({
user_id: message.author.id,
start_time: startDate,
end_time: endDate
})
.then(() => {
console.log(`Saved restriction for ${message.author.id}`);
});
}
};

const emojiMod = bot => ({
handleReaction: ({ reaction, user }) => {
const { message } = reaction;
const { member } = message;
if (user.id === bot.user.id) {
return;
}
if (message.author.id === bot.user.id) {
const setupRestrictionsIntervals = async bot => {
const expiredRestrictions = await knex("restrictions").where(
"end_time",
"<",
new Date()
);

const guild = bot.guilds.find(g => g.name === "Reactiflux");

expiredRestrictions.forEach(expiredRestriction => {
const { id, user_id, start_time } = expiredRestriction;
const startDate = new Date(start_time).toLocaleString();

const modLogChannel = bot.channels.find(
channel => channel.name === "mod-log"
);

let logMessage = `<@${user_id}>'s restriction from ${startDate} has been lifted.`;
modLogChannel.send(logMessage);

const restrictedRole = guild.roles.find(role => role.name === "restricted");
const targetUser = guild.members.get(user_id);

knex("restrictions")
.where({ user_id })
.delete();

if (!targetUser) {
// on the wrong server, not a problem
return;
}
let emoji = reaction.emoji.toString();

if (thumbsDownEmojis.includes(emoji)) {
emoji = "👎";
}
targetUser.roles.remove(restrictedRole);
});
};

const reactionHandler = reactionHandlers[emoji];
if (reactionHandler) {
reactionHandler(
bot,
reaction,
message,
message.guild.members.get(user.id)
const emojiMod = bot => {
setTimeout(() => {
setupRestrictionsIntervals(bot);

setInterval(() => {
setupRestrictionsIntervals(bot);
}, 12 * 60 * 60 * 1000); // 12 hours
}, 10000);
thisRaptori marked this conversation as resolved.
Show resolved Hide resolved

return {
handleReaction: ({ reaction, user }) => {
const { message } = reaction;
const { member } = message;
if (user.id === bot.user.id) {
return;
}
if (message.author.id === bot.user.id) {
return;
}
let emoji = reaction.emoji.toString();

if (thumbsDownEmojis.includes(emoji)) {
emoji = "👎";
}

const reactionHandler = reactionHandlers[emoji];
if (reactionHandler) {
reactionHandler(
bot,
reaction,
message,
message.guild.members.get(user.id)
);
}
},
handleJoin: async member => {
const existingRestrictionsFromUser = await knex
.table("restrictions")
.where({
user_id: member.user.id
})
.first();

if (!existingRestrictionsFromUser) {
return;
}

const restrictedRole = member.guild.roles.find(
role => role.name === "restricted"
);

const modLogChannel = bot.channels.find(
channel => channel.name === "mod-log"
);

const logMessage = `<@${member.user.id}> has been re-restricted since they rejoined the server.`;

member.roles.add(restrictedRole);
modLogChannel.send(logMessage);
}
}
});
};
};

module.exports = {
default: emojiMod
Expand Down
11 changes: 11 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
development: {
client: "sqlite3",
connection: {
filename: "./db/dev.sqlite3"
},
migrations: {
directory: "./db/migrations"
}
}
};
Loading