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

Add deduper feature to the bot #17

Closed
wants to merge 10 commits into from
61 changes: 61 additions & 0 deletions features/deduper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const deduper = {
monitoredChannels: [
"102860784329052160", // #general
"103696749012467712", // #need-help-0
"565213527673929729" // #need-help-1
],

dupeExpireTime: 2 * 60 * 1000, // 2 minutes to reject the duplicate as cross-post (ms)
cleanerInterval: 10 * 60 * 1000, // Frees up the memory from 10 to 10 minutes (ms)

lastMessageOfMember: {},

deleteMsgAndWarnUser: (msg, lastMsg) => {
msg.delete();
rafaelalmeidatk marked this conversation as resolved.
Show resolved Hide resolved
// prettier-ignore
msg.author
.send(`Hello there! It looks like you just posted a message to the #${msg.channel.name} channel on our server.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this whitespace intentional?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would imagine it's there to add some spacing between each section of text.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, similar to the message of #jobs channel

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You specifically need three tabs? It can't be just a newline?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I didn't even notice it lol
Thanks for pointing out, I removed it

We don't allow cross-posting, so I had to delete your message because it is identical as your last one posted on #${lastMsg.channel.name}. You can avoid this warning deleting the first message or waiting 2 minutes.

Thank you :)

:robot: This message was sent by a bot, please do not respond to it - in case of additional questions / issues, please contact one of our mods!`);
},

handleMessage: ({ msg, user }) => {
if (deduper.monitoredChannels.includes(msg.channel.id)) {
const userId = user.id;
const lastMessage = deduper.lastMessageOfMember[userId];

if (
lastMessage &&
!lastMessage.deleted &&
lastMessage.channel.id !== msg.channel.id &&
lastMessage.content === msg.content &&
msg.createdTimestamp - lastMessage.createdTimestamp <
deduper.dupeExpireTime
) {
deduper.deleteMsgAndWarnUser(msg, lastMessage);
} else {
deduper.lastMessageOfMember[userId] = msg;
rafaelalmeidatk marked this conversation as resolved.
Show resolved Hide resolved
}
}
},

registerCleaner: () => {
setInterval(() => {
Object.keys(deduper.lastMessageOfMember).forEach(userId => {
const msg = deduper.lastMessageOfMember[userId];

if (Date.now() - msg.createdTimestamp > deduper.dupeExpireTime) {
delete deduper.lastMessageOfMember[userId];
}
});
}, deduper.cleanerInterval);
}
};

module.exports = {
default: deduper
};
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const autoban = require("./features/autoban").default;
const commands = require("./features/commands").default;
const witInvite = require("./features/wit-invite").default;
const stats = require("./features/stats").default;
const deduper = require("./features/deduper").default;

const bot = new discord.Client();
bot.login(process.env.DISCORD_HASH);
Expand Down Expand Up @@ -81,6 +82,9 @@ logger.add(channelLog(bot, "479862475047567361"));
// Amplitude metrics
stats(bot);

// start deduper cleaner
deduper.registerCleaner();

// reactiflux
channelHandlers.addHandler("103882387330457600", jobs);
channelHandlers.addHandler("541673256596537366", witInvite); // #women-in-tech
Expand All @@ -94,6 +98,7 @@ channelHandlers.addHandler("479862475047567361", qna); // #general
channelHandlers.addHandler("*", commands);
// channelHandlers.addHandler('*', codeblock);
channelHandlers.addHandler("*", autoban);
channelHandlers.addHandler("*", deduper);

bot.on("messageReactionAdd", (reaction, user) => {
channelHandlers.handleReaction(reaction, user);
Expand Down