From 6ab22a02bf10e36dc098fce07f4af171e5c5d45f Mon Sep 17 00:00:00 2001 From: Navy Date: Thu, 12 Nov 2020 20:12:20 +0200 Subject: [PATCH] preliminary invite filter & invite caching --- .../components/observers/Automoderation.js | 70 +++++++++++++++++-- .../components/observers/UtilityHook.js | 24 ++++++- 2 files changed, 89 insertions(+), 5 deletions(-) diff --git a/structure/client/components/observers/Automoderation.js b/structure/client/components/observers/Automoderation.js index ae9c055..e46964a 100644 --- a/structure/client/components/observers/Automoderation.js +++ b/structure/client/components/observers/Automoderation.js @@ -313,15 +313,77 @@ module.exports = class AutoModeration extends Observer { const member = message.member || await guild.members.fetch(author.id).catch(); const settings = await guild.settings(); - const { wordWatcher: setting } = settings; - const { words, bypass, ignore, channel: _logChannel } = setting; + const { inviteFilter: setting, moderationPoints } = settings; + const { bypass, ignore, actions, silent, enabled } = setting; + if (!enabled) return; const roles = member?.roles.cache.map((r) => r.id) || []; - if (!_logChannel || words.length === 0 || roles.some((r) => bypass.includes(r.id)) || ignore.includes(channel.id)) return; + if (roles.some((r) => bypass.includes(r.id)) || ignore.includes(channel.id)) return; const msg = edited || message; + const { content } = msg; + + const reg = /((discord)?\s?\.?\s?gg\s?|discord\.com\/invite)\/\s?(?[a-z0-9]+)/iu; + const match = content.match(reg); + if (!match) return; + + const result = await guild.checkInvite(match.groups.code); + if (!result) { // Doesn't resolve to the origin server + + let action = null; + if (actions.length) [action] = actions; + + msg.filtered = { + match: match[0], + matcher: 'invites' + }; + if (!action) return msg.delete(); + if (!silent) { + const res = await msg.formattedRespond('I_FILTER_DELETE', { params: { user: author.id } }); + res.delete({ timeout: 10000 }); + } + msg.filtered.sactioned = true; + await msg.delete(); + + // NOTE: this will have to be changed whenever the moderation manager is finished and properly supports sth like this + this.client.moderationManager.handleInfraction( + CONSTANTS.Infractions[action.type], + { + guild, + member: guild.me, + channel, + arguments: { + force: { + value: action.force + }, + points: { + value: action.points || moderationPoints.points[action.type] + }, + expiration: { + value: action.expiration || moderationPoints.expirations[action.type] + }, + silent: setting.silent//, + //prune: { + // name: 'prune', + // value: 50 + //} + }, + format: guild.format.bind(guild), + // eslint-disable-next-line no-empty-function + respond: () => {} + }, + { + targets: [member], + reason: msg.format('I_FILTER_ACTION'), + duration: action.duration, + data: { + filtered: msg.filtered + } + } + ); + + } - } async filterMentions(message) { diff --git a/structure/client/components/observers/UtilityHook.js b/structure/client/components/observers/UtilityHook.js index e1263cc..178e00b 100644 --- a/structure/client/components/observers/UtilityHook.js +++ b/structure/client/components/observers/UtilityHook.js @@ -17,7 +17,9 @@ class UtilityHook extends Observer { ['guildMemberAdd', this.autorole.bind(this)], ['guildMemberAdd', this.automute.bind(this)], ['guildMemberAdd', this.stickyRole.bind(this)], - ['guildMemberRemove', this.storeRoles.bind(this)] + ['guildMemberRemove', this.storeRoles.bind(this)], + ['inviteCreate', this.inviteCreate.bind(this)], + ['inviteDelete', this.inviteDelete.bind(this)] ]; } @@ -135,6 +137,26 @@ class UtilityHook extends Observer { .trim(); } + async inviteCreate(invite) { + + const { guild } = invite; + if (!guild) return; + if (!guild.me.hasPermission('MANAGE_GUILD')) return; + if (!guild.invites) guild.invites = await guild.fetchInvites(); + guild.invites.set(invite.code, invite); + + } + + async inviteDelete(invite) { + + const { guild } = invite; + if (!guild) return; + if (!guild.me.hasPermission('MANAGE_GUILD')) return; + if (!guild.invites) guild.invites = await guild.fetchInvites(); + guild.invites.delete(invite.code); + + } + } module.exports = UtilityHook; \ No newline at end of file