server.js: Add the functionality to auto-moderate media in a channel (#59)

This makes it possible to auto-delete text-messages, sent in a channel meant for images and other media.
This commit is contained in:
Tobias 2019-07-22 02:55:55 +02:00 committed by Flame Sage
parent 592a018825
commit c3cc3820d4
2 changed files with 20 additions and 0 deletions

View File

@ -17,6 +17,9 @@
"DISCORD_LOG_CHANNEL": {
"description": "The unique ID for the channel the bot will output logs to."
},
"DISCORD_MEDIA_CHANNEL": {
"description": "The unique ID for the channel the bot will moderate images in."
},
"DISCORD_RULES_CHANNEL": {
"description": "The unique ID for the channel that the bot will manage rules from."
},

View File

@ -16,6 +16,8 @@ var cachedModules = [];
var cachedTriggers = [];
var client = new discord.Client();
let mediaUsers = new Map();
logger.info('Application startup. Configuring environment.');
process.on('unhandledRejection', (error, promise) => {
@ -94,6 +96,21 @@ client.on('message', message => {
logger.verbose(`${message.author.username} ${message.author} [Channel: ${message.channel.name} ${message.channel}]: ${message.content}`);
if (message.channel.id === process.env.DISCORD_MEDIA_CHANNEL && !message.author.bot) {
const AllowedMediaRoles = ['Administrators', 'Moderators', 'Team', 'VIP'];
if (!findArray(message.member.roles.map(function (x) { return x.name; }), AllowedMediaRoles)) {
const urlRegex = new RegExp(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/gi);
if (message.attachments.size > 0 || message.content.match(urlRegex)) {
mediaUsers.set(message.author.id, true);
} else if (mediaUsers.get(message.author.id)) {
mediaUsers.set(message.author.id, false);
} else {
message.delete();
mediaUsers.set(message.author.id, false);
}
}
}
// Check if the channel is #rules, if so we want to follow a different logic flow.
if (message.channel.id === process.env.DISCORD_RULES_CHANNEL) {
if (message.content.toLowerCase().includes(process.env.DISCORD_RULES_TRIGGER)) {