diff --git a/src/main/java/com/jagrosh/vortex/automod/AutoMod.java b/src/main/java/com/jagrosh/vortex/automod/AutoMod.java index 8b39686..7de7ca3 100644 --- a/src/main/java/com/jagrosh/vortex/automod/AutoMod.java +++ b/src/main/java/com/jagrosh/vortex/automod/AutoMod.java @@ -207,19 +207,19 @@ // ignore users vortex cant interact with if(!member.getGuild().getSelfMember().canInteract(member)) return false; - + // ignore users that can kick if(member.hasPermission(Permission.KICK_MEMBERS)) return false; - + // ignore users that can ban if(member.hasPermission(Permission.BAN_MEMBERS)) return false; - + // ignore users that can manage server if(member.hasPermission(Permission.MANAGE_SERVER)) return false; - + // if a channel is specified, ignore users that can manage messages in that channel if(channel!=null && (member.hasPermission(channel, Permission.MESSAGE_MANAGE) || vortex.getDatabase().ignores.isIgnored(channel))) return false; @@ -257,6 +257,7 @@ //get the settings AutomodSettings settings = vortex.getDatabase().automod.getSettings(message.getGuild()); + List inviteWhitelist = vortex.getDatabase().inviteWhitelist.readWhitelist(message.getGuild()); if(settings==null) return; @@ -398,7 +399,7 @@ for(String inviteCode : invites) { long gid = inviteResolver.resolve(message.getJDA(), inviteCode); - if(gid != message.getGuild().getIdLong() && !settings.whitelistedInvites.contains(gid)) + if(gid != message.getGuild().getIdLong() && !inviteWhitelist.contains(gid)) { strikeTotal += settings.inviteStrikes; reason.append(", Advertising"); @@ -483,7 +484,7 @@ if(settings.inviteStrikes>0 && resolved.matches(INVITE_LINK)) { long invite = inviteResolver.resolve(message.getJDA(), resolved.replaceAll(INVITE_LINK, "$1")); - if(invite != message.getGuild().getIdLong() && !settings.whitelistedInvites.contains(invite)) + if(invite != message.getGuild().getIdLong() && !inviteWhitelist.contains(invite)) containsInvite = true; } if(settings.refStrikes>0) diff --git a/src/main/java/com/jagrosh/vortex/commands/automod/WhitelistInvitesCmd.java b/src/main/java/com/jagrosh/vortex/commands/automod/WhitelistInvitesCmd.java index 11cb9ad..91af04e 100644 --- a/src/main/java/com/jagrosh/vortex/commands/automod/WhitelistInvitesCmd.java +++ b/src/main/java/com/jagrosh/vortex/commands/automod/WhitelistInvitesCmd.java @@ -20,7 +20,6 @@ import com.jagrosh.vortex.Vortex; import net.dv8tion.jda.core.Permission; -import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -50,9 +49,9 @@ String[] args = event.getArgs().toLowerCase().split("\\s+"); if(event.getArgs().equalsIgnoreCase("show") || (args.length == 2 && (args[0].equals("add") || args[0].equals("remove")))) { - List currentWL = vortex.getDatabase().automod.getSettings(event.getGuild()).whitelistedInvites; if(event.getArgs().equalsIgnoreCase("show")) { + List currentWL = vortex.getDatabase().inviteWhitelist.readWhitelist(event.getGuild()); event.replySuccess("Whitelisted Guild IDs:\n" + (currentWL.isEmpty() ? "None" : currentWL.stream().map(String::valueOf).collect(Collectors.joining(", ")))); } @@ -68,35 +67,28 @@ event.replyWarning("Invalid Guild-ID provided!"); return; } - List newWhitelist; if(args[0].equals("add")) { - if(currentWL.contains(guildId)) + if(!vortex.getDatabase().inviteWhitelist.addToWhitelist(event.getGuild(), guildId)) { event.replyWarning("Given Guild was already whitelisted"); return; } - newWhitelist = new ArrayList<>(currentWL.size() + 1); - newWhitelist.addAll(currentWL); - newWhitelist.add(guildId); } else { - if(!currentWL.contains(guildId)) + if(!vortex.getDatabase().inviteWhitelist.removeFromWhitelist(event.getGuild(), guildId)) { event.replyWarning("Given Guild was not whitelisted"); return; } - newWhitelist = new ArrayList<>(currentWL.size() - 1); - currentWL.stream().filter(id -> id != guildId).forEach(newWhitelist::add); } - vortex.getDatabase().automod.setWhitelistedInvites(event.getGuild(), newWhitelist); event.replySuccess("Whitelist has been modified"); } } else { - event.replyWarning(DESCRIPTION+"\nValid options are `ADD GUILD_ID`, `REMOVE GUILD_ID` and `show`"); + event.replyWarning(DESCRIPTION+"\nValid options are `ADD GUILD_ID`, `REMOVE GUILD_ID` and `SHOW`"); } } diff --git a/src/main/java/com/jagrosh/vortex/database/ArrayColumn.java b/src/main/java/com/jagrosh/vortex/database/ArrayColumn.java deleted file mode 100644 index b0cfd2f..0000000 --- a/src/main/java/com/jagrosh/vortex/database/ArrayColumn.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2017 John Grosh (john.a.grosh@gmail.com). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.jagrosh.vortex.database; - -import com.jagrosh.easysql.SQLColumn; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * - * @author John Grosh (john.a.grosh@gmail.com) - */ -public class ArrayColumn extends SQLColumn> -{ - public ArrayColumn(String name) - { - this(name, false); - } - - public ArrayColumn(String name, boolean publicKey) - { - super(name, false, null, publicKey); - } - - @Override - public String getDataDescription() - { - return "ARRAY DEFAULT ()" + nullable() + (primaryKey ? " PRIMARY KEY" : ""); - } - - @Override - public List getValue(ResultSet results) throws SQLException - { - return new ArrayList<>(Arrays.asList((T[]) results.getArray(name).getArray())); - } - - @Override - public void updateValue(ResultSet results, List newValue) throws SQLException - { - results.updateObject(name, newValue.toArray()); - } -} diff --git a/src/main/java/com/jagrosh/vortex/database/Database.java b/src/main/java/com/jagrosh/vortex/database/Database.java index b2e0f5c..a3b8c9d 100644 --- a/src/main/java/com/jagrosh/vortex/database/Database.java +++ b/src/main/java/com/jagrosh/vortex/database/Database.java @@ -33,6 +33,7 @@ public final TempMuteManager tempmutes; public final TempBanManager tempbans; public final PremiumManager premium; + public final InviteWhitelistManager inviteWhitelist; public Database(String host, String user, String pass) throws Exception { @@ -47,6 +48,7 @@ tempmutes = new TempMuteManager(this); tempbans = new TempBanManager(this); premium = new PremiumManager(this); + inviteWhitelist = new InviteWhitelistManager(this); init(); } diff --git a/src/main/java/com/jagrosh/vortex/database/managers/AutomodManager.java b/src/main/java/com/jagrosh/vortex/database/managers/AutomodManager.java index 5b4fcbc..8e42168 100644 --- a/src/main/java/com/jagrosh/vortex/database/managers/AutomodManager.java +++ b/src/main/java/com/jagrosh/vortex/database/managers/AutomodManager.java @@ -20,12 +20,9 @@ import com.jagrosh.easysql.SQLColumn; import com.jagrosh.easysql.columns.*; import com.jagrosh.vortex.Action; -import com.jagrosh.vortex.database.ArrayColumn; import com.jagrosh.vortex.utils.FixedCache; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.Collections; -import java.util.List; import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.MessageEmbed.Field; @@ -62,8 +59,6 @@ public final static SQLColumn DUPE_STRIKE_THRESH = new IntegerColumn("DUPE_STRIKES_THRESH", false, 0); public final static SQLColumn DEHOIST_CHAR = new IntegerColumn("DEHOIST_CHAR", false, 0); - - public final static SQLColumn> WHITELISTED_INVITES = new ArrayColumn("WHITELISTED_INVITES"); // Cache private final FixedCache cache = new FixedCache<>(1000); @@ -368,26 +363,6 @@ }); } - public void setWhitelistedInvites(Guild guild, List whitelistedIds) - { - invalidateCache(guild); - readWrite(selectAll(GUILD_ID.is(guild.getIdLong())), rs -> - { - if(rs.next()) - { - WHITELISTED_INVITES.updateValue(rs, whitelistedIds); - rs.updateRow(); - } - else - { - rs.moveToInsertRow(); - GUILD_ID.updateValue(rs, guild.getIdLong()); - WHITELISTED_INVITES.updateValue(rs, whitelistedIds); - rs.insertRow(); - } - }); - } - private void invalidateCache(Guild guild) { invalidateCache(guild.getIdLong()); @@ -407,8 +382,7 @@ public final int inviteStrikes, refStrikes, copypastaStrikes, everyoneStrikes; public final int dupeStrikes, dupeDeleteThresh, dupeStrikeThresh; public final char dehoistChar; - public final List whitelistedInvites; - + private AutomodSettings() { this.resolveUrls = false; @@ -425,7 +399,6 @@ this.dupeDeleteThresh = 0; this.dupeStrikeThresh = 0; this.dehoistChar = 0; - this.whitelistedInvites = Collections.emptyList(); } private AutomodSettings(ResultSet rs) throws SQLException @@ -444,7 +417,6 @@ this.dupeDeleteThresh = DUPE_DELETE_THRESH.getValue(rs); this.dupeStrikeThresh = DUPE_STRIKE_THRESH.getValue(rs); this.dehoistChar = (char)((int)DEHOIST_CHAR.getValue(rs)); - this.whitelistedInvites = Collections.unmodifiableList(WHITELISTED_INVITES.getValue(rs)); } public boolean useAutoRaidMode() diff --git a/src/main/java/com/jagrosh/vortex/database/managers/InviteWhitelistManager.java b/src/main/java/com/jagrosh/vortex/database/managers/InviteWhitelistManager.java new file mode 100644 index 0000000..0096f25 --- /dev/null +++ b/src/main/java/com/jagrosh/vortex/database/managers/InviteWhitelistManager.java @@ -0,0 +1,81 @@ +package com.jagrosh.vortex.database.managers; + +import com.jagrosh.easysql.DataManager; +import com.jagrosh.easysql.DatabaseConnector; +import com.jagrosh.easysql.SQLColumn; +import com.jagrosh.easysql.columns.LongColumn; +import com.jagrosh.vortex.utils.FixedCache; +import net.dv8tion.jda.core.entities.Guild; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public class InviteWhitelistManager extends DataManager +{ + + public static final SQLColumn GUILD_ID = new LongColumn("GUILD_ID", false, 0L); + public static final SQLColumn WHITELIST_ID = new LongColumn("WL_ID", false, 0L); + + private final FixedCache> cache = new FixedCache<>(1000); + + public InviteWhitelistManager(DatabaseConnector connector) + { + super(connector, "INVITE_WL"); + } + + @Override + protected String primaryKey() + { + return GUILD_ID+", "+WHITELIST_ID; + } + + public boolean addToWhitelist(Guild guild, long whitelistId) + { + invalidateCache(guild); + return readWrite(selectAll(GUILD_ID.is(guild.getId()) + " AND " + WHITELIST_ID.is(whitelistId)), rs -> + { + if(rs.next()) + return false; + rs.moveToInsertRow(); + GUILD_ID.updateValue(rs, guild.getIdLong()); + WHITELIST_ID.updateValue(rs, whitelistId); + rs.insertRow(); + return true; + }); + } + + public boolean removeFromWhitelist(Guild guild, long whitelistId) + { + invalidateCache(guild); + return readWrite(selectAll(GUILD_ID.is(guild.getId())+" AND "+WHITELIST_ID.is(whitelistId)), rs -> + { + if(rs.next()) + { + rs.deleteRow(); + return true; + } + return false; + }); + } + + public List readWhitelist(Guild guild) + { + if(cache.contains(guild.getIdLong())) + return cache.get(guild.getIdLong()); + List whitelist = read(selectAll(GUILD_ID.is(guild.getId())), rs -> + { + List list = new LinkedList<>(); + while(rs.next()) + list.add(WHITELIST_ID.getValue(rs)); + return Collections.unmodifiableList(list); + }); + cache.put(guild.getIdLong(), whitelist); + return whitelist; + } + + private void invalidateCache(Guild guild) + { + cache.pull(guild.getIdLong()); + } +}