Skip to content

Commit

Permalink
Add possibility to add/remove multiple guilds to/from whitelist at once
Browse files Browse the repository at this point in the history
  • Loading branch information
kantenkugel committed Jul 18, 2019
1 parent c670390 commit f583b2a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.jagrosh.vortex.Vortex;
import net.dv8tion.jda.core.Permission;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -37,7 +38,7 @@ public WhitelistInvitesCmd(Vortex vortex)
this.name = "whitelist";
this.guildOnly = true;
this.category = new Category("AutoMod");
this.arguments = "<ADD GUILD_ID|REMOVE GUILD_ID|SHOW>";
this.arguments = "<ADD GUILD_ID[ GUILD_ID...]|REMOVE GUILD_ID[ GUILD_ID...]|SHOW>";
this.help = "if strikes for invites are enabled, add/remove whitelisted guilds";
this.userPermissions = new Permission[]{Permission.MANAGE_SERVER};
}
Expand All @@ -46,7 +47,7 @@ public WhitelistInvitesCmd(Vortex vortex)
protected void execute(CommandEvent event)
{
String[] args = event.getArgs().toLowerCase().split("\\s+");
if(event.getArgs().equalsIgnoreCase("show") || (args.length == 2 && (args[0].equals("add") || args[0].equals("remove"))))
if(event.getArgs().equalsIgnoreCase("show") || (args.length > 1 && (args[0].equals("add") || args[0].equals("remove"))))
{
if(event.getArgs().equalsIgnoreCase("show"))
{
Expand All @@ -56,27 +57,31 @@ protected void execute(CommandEvent event)
}
else
{
long guildId;
try
List<Long> guildIds = readIds(args);
if(guildIds == null)
{
guildId = Long.parseUnsignedLong(args[1]);
}
catch(NumberFormatException ex)
{
event.replyWarning("Invalid Guild-ID provided!");
event.replyWarning("Invalid Guild-ID(s) provided!");
return;
}
if(args[0].equals("add"))
{
if(!vortex.getDatabase().inviteWhitelist.addToWhitelist(event.getGuild(), guildId))
if(guildIds.size() > 1)
{
vortex.getDatabase().inviteWhitelist.addAllToWhitelist(event.getGuild(), guildIds);
}
else if(!vortex.getDatabase().inviteWhitelist.addToWhitelist(event.getGuild(), guildIds.get(0)))
{
event.replyWarning("Given Guild was already whitelisted");
return;
}
}
else
{
if(!vortex.getDatabase().inviteWhitelist.removeFromWhitelist(event.getGuild(), guildId))
if(guildIds.size() > 1)
{
vortex.getDatabase().inviteWhitelist.removeAllFromWhitelist(event.getGuild(), guildIds);
}
else if(!vortex.getDatabase().inviteWhitelist.removeFromWhitelist(event.getGuild(), guildIds.get(0)))
{
event.replyWarning("Given Guild was not whitelisted");
return;
Expand All @@ -87,8 +92,25 @@ protected void execute(CommandEvent event)
}
else
{
event.replyWarning(DESCRIPTION+"\nValid options are `ADD GUILD_ID`, `REMOVE GUILD_ID` and `SHOW`");
event.replyWarning(DESCRIPTION+"\nValid options are `ADD GUILD_ID[ GUILD_ID...]`, `REMOVE GUILD_ID[ GUILD_ID...]` and `SHOW`");
}

}

private List<Long> readIds(String[] args)
{
List<Long> guildIds = new ArrayList<>(args.length - 1);
try
{
for(int i = 1; i < args.length; i++)
{
guildIds.add(Long.parseUnsignedLong(args[i]));
}
}
catch(NumberFormatException ex)
{
return null;
}
return guildIds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
import com.jagrosh.vortex.utils.FixedCache;
import net.dv8tion.jda.core.entities.Guild;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class InviteWhitelistManager extends DataManager
{
Expand Down Expand Up @@ -45,6 +49,28 @@ public boolean addToWhitelist(Guild guild, long whitelistId)
});
}

public void addAllToWhitelist(Guild guild, Collection<Long> whitelistIds)
{
invalidateCache(guild);
Set<Long> ids = new HashSet<>(whitelistIds);
readWrite(selectAll(String.format("%s AND %s IN (%s)",
GUILD_ID.is(guild.getId()), WHITELIST_ID.name,
ids.stream().map(String::valueOf).collect(Collectors.joining(",")))),rs ->
{
while(rs.next())
{
ids.remove(WHITELIST_ID.getValue(rs));
}
for(long id : ids)
{
rs.moveToInsertRow();
GUILD_ID.updateValue(rs, guild.getIdLong());
WHITELIST_ID.updateValue(rs, id);
rs.insertRow();
}
});
}

public boolean removeFromWhitelist(Guild guild, long whitelistId)
{
invalidateCache(guild);
Expand All @@ -59,6 +85,20 @@ public boolean removeFromWhitelist(Guild guild, long whitelistId)
});
}

public void removeAllFromWhitelist(Guild guild, Collection<Long> whitelistIds)
{
invalidateCache(guild);
readWrite(selectAll(String.format("%s AND %s IN (%s)",
GUILD_ID.is(guild.getId()), WHITELIST_ID.name,
whitelistIds.stream().map(String::valueOf).collect(Collectors.joining(",")))),rs ->
{
while(rs.next())
{
rs.deleteRow();
}
});
}

public List<Long> readWhitelist(Guild guild)
{
if(cache.contains(guild.getIdLong()))
Expand Down
45 changes: 25 additions & 20 deletions src/main/java/com/jagrosh/vortex/pro/VortexPro.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,36 @@ public static <T> T from(Class<T> apiClass, Object... args)
}
implClass = (Class<? extends T>) anno.mock();
}
T instance = null;
if(implClass != null)
{
instance = createInstance(implClass, args);
}
IMPLS.put(apiClass, instance);
return instance;
}

private static <T> T createInstance(Class<? extends T> clazz, Object[] args)
{
Set<Constructor<? extends T>> constructors = Arrays.stream(clazz.getConstructors())
.filter(c -> c.getParameterCount() == 0 || c.getParameterCount() == args.length)
.map(c -> (Constructor<? extends T>) c)
.collect(Collectors.toSet());
if(constructors.size() != 1)
{
throw new IllegalArgumentException("Could not find a (single) Constructor with 0 or matching amount of arguments. Arguments provided: " + Arrays.toString(args));
}
Constructor<? extends T> constructor = constructors.iterator().next();
try
{
T instance = null;
if(implClass != null)
{
Set<Constructor<? extends T>> constructors = Arrays.stream(implClass.getConstructors())
.filter(c -> c.getParameterCount() == 0 || c.getParameterCount() == args.length)
.map(c -> (Constructor<? extends T>) c)
.collect(Collectors.toSet());
if(constructors.size() != 1)
{
throw new IllegalArgumentException("Could not find a (single) Constructor with 0 or matching amount of arguments. Arguments provided: " + Arrays.toString(args));
}
Constructor<? extends T> constructor = constructors.iterator().next();
if(constructor.getParameterCount() == 0)
instance = constructor.newInstance();
else
instance = constructor.newInstance(args);
}
IMPLS.put(apiClass, instance);
return instance;
if(constructor.getParameterCount() == 0)
return constructor.newInstance();
else
return constructor.newInstance(args);
}
catch(InvocationTargetException | InstantiationException | IllegalAccessException e)
{
throw new IllegalStateException("Can not instanciate class "+implClass.getName()+". Does it have a public no-arg constructor?", e);
throw new IllegalStateException("Can not instantiate class "+clazz.getName()+". Does it have a public no-arg constructor?", e);
}
}

Expand Down

0 comments on commit f583b2a

Please sign in to comment.