added a role group with limited whitelists

This commit is contained in:
JoeShimo 2019-10-30 23:07:30 +00:00
parent 404246835f
commit e79625f392
3 changed files with 280 additions and 37 deletions

View File

@ -1,5 +1,5 @@
name: DiscordWhitelister
version: 1.0.3
version: 1.0.4
author: Joe Shimell
main: uk.co.angrybee.joe.DiscordWhitelister
description: Discord whitelister bot.

View File

@ -1,5 +1,6 @@
package uk.co.angrybee.joe;
import net.dv8tion.jda.api.entities.ISnowflake;
import org.bukkit.Server;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
@ -9,8 +10,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.*;
public class DiscordWhitelister extends JavaPlugin
{
@ -18,12 +18,23 @@ public class DiscordWhitelister extends JavaPlugin
private ServerDiscordClient serverDiscordClient;
private File whitelisterBotConfigFile;
private static FileConfiguration whitelisterBotConfig;
static private File userListFile;
private File removedListFile;
private boolean configCreated = false;
private static FileConfiguration whitelisterBotConfig;
private static FileConfiguration userList;
private static FileConfiguration removedList;
// private LinkedHashMap<String, Integer> userInfo;
private String botToken;
private boolean botEnabled = true;
private boolean configCreated = false;
private boolean userListCreated = false;
private boolean removedListCreated = false;
private boolean botEnabled;
public boolean limitedAddRolesEnabled;
private static JavaPlugin thisPlugin;
@ -32,6 +43,8 @@ public class DiscordWhitelister extends JavaPlugin
{
thisPlugin = this;
whitelisterBotConfig = new YamlConfiguration();
userList = new YamlConfiguration();
removedList = new YamlConfiguration();
ConfigSetup();
@ -64,6 +77,13 @@ public class DiscordWhitelister extends JavaPlugin
serverDiscordClient.allowedToAddRoles[roles] = DiscordWhitelister.getWhitelisterBotConfig().getList("add-roles").get(roles).toString();
}
// set limited add roles
serverDiscordClient.allowedToAddLimitedRoles = new String[DiscordWhitelister.getWhitelisterBotConfig().getList("limited-add-roles").size()];
for(int roles = 0; roles < serverDiscordClient.allowedToAddLimitedRoles.length; ++roles)
{
serverDiscordClient.allowedToAddLimitedRoles[roles] = DiscordWhitelister.getWhitelisterBotConfig().getList("limited-add-roles").get(roles).toString();
}
serverDiscordClient.InitializeClient(botToken);
getLogger().info("Successfully initialized Discord client");
}
@ -79,16 +99,34 @@ public class DiscordWhitelister extends JavaPlugin
return whitelisterBotConfig;
}
public static FileConfiguration getUserList()
{
return userList;
}
public static File getUserListFile()
{
return userListFile;
}
public static FileConfiguration getRemovedList()
{
return removedList;
}
public void ConfigSetup()
{
whitelisterBotConfigFile = new File(getDataFolder(), "discord-whitelister.yml");
userListFile = new File(getDataFolder(), "user-list.yml");
removedListFile = new File(getDataFolder(), "removed-list");
if(!whitelisterBotConfigFile.getParentFile().exists())
{
whitelisterBotConfigFile.getParentFile().mkdirs();
}
if(!whitelisterBotConfigFile.exists())
{
whitelisterBotConfigFile.getParentFile().mkdirs();
//saveResource(fileString, false); // from example, doesn't seem to work?
try
{
whitelisterBotConfigFile.createNewFile();
@ -98,7 +136,7 @@ public class DiscordWhitelister extends JavaPlugin
e.printStackTrace();
}
getLogger().info("Configuration file created at " + whitelisterBotConfigFile.getPath() +
getLogger().info("Configuration file created at: " + whitelisterBotConfigFile.getPath() +
", please edit this else the plugin will not work!");
configCreated = true;
}
@ -112,6 +150,54 @@ public class DiscordWhitelister extends JavaPlugin
e.printStackTrace();
}
if(!userListFile.exists())
{
try
{
userListFile.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
}
getLogger().info("User list created at: " + userListFile.getPath());
userListCreated = true;
}
try
{
getUserList().load(userListFile);
}
catch (IOException | InvalidConfigurationException e)
{
e.printStackTrace();
}
if(!removedListFile.exists())
{
try
{
removedListFile.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
}
getLogger().info("Removed list created at: " + removedListFile.getPath());
removedListCreated = true;
}
try
{
getRemovedList().load(removedListFile);
}
catch (IOException | InvalidConfigurationException e)
{
e.printStackTrace();
}
if(configCreated)
{
getWhitelisterBotConfig().set("discord-bot-token", "Discord bot token goes here, you can find it here: " +
@ -125,6 +211,16 @@ public class DiscordWhitelister extends JavaPlugin
List<String> tempAddRoles = Arrays.asList("Mod", "Whitelister");
getWhitelisterBotConfig().set("add-roles", tempAddRoles);
// if the limited whitelist feature should be enabled
getWhitelisterBotConfig().set("limited-whitelist-enabled", true);
// the amount of times a non-staff user is allowed to whitelist
getWhitelisterBotConfig().set("max-whitelist-amount", 3);
// roles that are allowed whitelist a limited amount of times
List<String> tempLimitedRoles = Arrays.asList("VIP", "LimitedWhitelister");
getWhitelisterBotConfig().set("limited-add-roles", tempLimitedRoles);
List<String> tempChannelIds = Arrays.asList("445666834382061569", "488450157881327616");
getWhitelisterBotConfig().set("target-text-channels", tempChannelIds);
@ -139,11 +235,24 @@ public class DiscordWhitelister extends JavaPlugin
e.printStackTrace();
}
}
if(userListCreated)
{
try
{
getUserList().save(userListFile.getPath());
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public void AssignVars()
{
botToken = getWhitelisterBotConfig().getString("discord-bot-token");
botEnabled = getWhitelisterBotConfig().getBoolean("bot-enabled");
limitedAddRolesEnabled = getWhitelisterBotConfig().getBoolean("limited-whitelist-enabled");
}
}

View File

@ -15,16 +15,23 @@ import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import javax.security.auth.login.LoginException;
import java.io.DataInput;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
// handles Discord interaction
public class ServerDiscordClient extends ListenerAdapter
{
public static String[] allowedToAddRemoveRoles;
public static String[] allowedToAddRoles;
public static String[] allowedToAddLimitedRoles;
private boolean limitedAddRolesEnabled;
public void InitializeClient(String clientToken)
{
@ -66,6 +73,7 @@ public class ServerDiscordClient extends ListenerAdapter
boolean userCanAddRemove = false;
boolean userCanAdd = false;
boolean userHasLimitedAdd = false;
for(Role role : messageReceivedEvent.getGuild().getMember(messageReceivedEvent.getAuthor()).getRoles())
{
@ -83,12 +91,44 @@ public class ServerDiscordClient extends ListenerAdapter
}
}
// limited add check
if(DiscordWhitelister.getWhitelisterBotConfig().getBoolean("limited-whitelist-enabled"))
{
// check if user is in a limited add role
for(Role role : messageReceivedEvent.getGuild().getMember(messageReceivedEvent.getAuthor()).getRoles())
{
if(Arrays.stream(allowedToAddLimitedRoles).parallel().anyMatch(role.getName()::contains))
{
userHasLimitedAdd = true;
}
}
// check if user is already in the list if in limited add role
if(userHasLimitedAdd)
{
// create entry if user is not on list
if(DiscordWhitelister.getUserList().getString(author.getId()) == null)
{
DiscordWhitelister.getUserList().set(author.getId(), 0);
try
{
DiscordWhitelister.getUserList().save(DiscordWhitelister.getUserListFile().getPath());
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
if(messageContents.toLowerCase().equals("!whitelist") && userCanAddRemove || messageContents.toLowerCase().equals("!whitelist") && userCanAdd)
{
if(userCanAddRemove || userCanAdd)
{
channel.sendMessage("```Discord Whitelister Bot For Spigot" + System.lineSeparator() +
"Version: 1.0.3" + System.lineSeparator() + "Links:" + System.lineSeparator() +
"Version: 1.0.4" + System.lineSeparator() + "Links:" + System.lineSeparator() +
"https://www.spigotmc.org/resources/discord-whitelister.69929/" + System.lineSeparator() + "https://github.com/JoeShimo/DiscordWhitelisterBot-Spigot" + System.lineSeparator() +
"Commands:" + System.lineSeparator() + "Add:" + System.lineSeparator() +
"!whitelist add <MinecraftUsername> -- Usage: Adds a user to the whitelist" + System.lineSeparator() +
@ -114,7 +154,18 @@ public class ServerDiscordClient extends ListenerAdapter
if(messageContents.toLowerCase().contains("!whitelist add"))
{
if(userCanAddRemove || userCanAdd)
boolean usedAllWhitelists = false;
if(DiscordWhitelister.getWhitelisterBotConfig().getBoolean("limited-whitelist-enabled") && userHasLimitedAdd && !userCanAddRemove && !userCanAdd)
{
if(DiscordWhitelister.getUserList().getString(author.getId()) != null
&& Integer.parseInt(DiscordWhitelister.getUserList().getString(author.getId())) >= DiscordWhitelister.getWhitelisterBotConfig().getInt("max-whitelist-amount"))
{
usedAllWhitelists = true;
}
}
if(userCanAddRemove || userCanAdd || userHasLimitedAdd)
{
String nameToWhitelist = messageContents;
nameToWhitelist = nameToWhitelist.toLowerCase();
@ -123,48 +174,131 @@ public class ServerDiscordClient extends ListenerAdapter
final String finalNameToWhitelist = nameToWhitelist;
if(finalNameToWhitelist.isEmpty())
if(userCanAddRemove || userCanAdd)
{
channel.sendMessage(author.getAsMention() + ", ```Whitelist Command:" + System.lineSeparator() +
"!whitelist add <MinecraftUsername>" + System.lineSeparator() + "Usage: Adds a user to the whitelist" + System.lineSeparator() +
"If you encounter an issue, please report it here: https://github.com/JoeShimo/DiscordWhitelisterBot-Spigot/issues```").queue();
}
else
{
File whitelistJSON = (new File(".", "whitelist.json"));
DiscordWhitelister.getPlugin().getLogger().info(author.getName() + "(" + author.getId() + ") attempted to whitelist " + finalNameToWhitelist);
if(checkWhitelistJSON(whitelistJSON, finalNameToWhitelist))
if(finalNameToWhitelist.isEmpty())
{
channel.sendMessage(author.getAsMention() + ", user is already on the whitelist!").queue();
channel.sendMessage(author.getAsMention() + ", ```Whitelist Command:" + System.lineSeparator() +
"!whitelist add <MinecraftUsername>" + System.lineSeparator() + "Usage: Adds a user to the whitelist" + System.lineSeparator() +
"If you encounter an issue, please report it here: https://github.com/JoeShimo/DiscordWhitelisterBot-Spigot/issues```").queue();
}
else
{
DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () -> DiscordWhitelister.getPlugin().getServer().dispatchCommand(DiscordWhitelister.getPlugin().getServer().getConsoleSender(),
"whitelist add " + finalNameToWhitelist));
File whitelistJSON = (new File(".", "whitelist.json"));
// run through the server so that the check doesn't execute before the server has had a chance to run the whitelist command -- unsure if this is the best way of doing this, but it works
DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () ->
DiscordWhitelister.getPlugin().getLogger().info(author.getName() + "(" + author.getId() + ") attempted to whitelist: " + finalNameToWhitelist);
if(checkWhitelistJSON(whitelistJSON, finalNameToWhitelist))
{
channel.sendMessage(author.getAsMention() + ", user is already on the whitelist!").queue();
}
else
{
DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () -> DiscordWhitelister.getPlugin().getServer().dispatchCommand(DiscordWhitelister.getPlugin().getServer().getConsoleSender(),
"whitelist add " + finalNameToWhitelist));
// run through the server so that the check doesn't execute before the server has had a chance to run the whitelist command -- unsure if this is the best way of doing this, but it works
DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () ->
{
if(checkWhitelistJSON(whitelistJSON, finalNameToWhitelist))
{
channel.sendMessage(author.getAsMention() + ", successfully added `" + finalNameToWhitelist + "` to the whitelist").queue();
}
else
{
channel.sendMessage(author.getAsMention() + ", failed to add `" + finalNameToWhitelist + "` to the whitelist, this is most likely due to an invalid Minecraft username").queue();
}
return null;
});
}
}
}
if(DiscordWhitelister.getWhitelisterBotConfig().getBoolean("limited-whitelist-enabled") && userHasLimitedAdd && !usedAllWhitelists && !userCanAddRemove && !userCanAdd)
{
if(DiscordWhitelister.getUserList().getString(author.getId()) != null)
{
int whitelistLimit = DiscordWhitelister.getWhitelisterBotConfig().getInt("max-whitelist-amount");
int timesWhitelisted = Integer.parseInt(DiscordWhitelister.getUserList().getString(author.getId()));
if(finalNameToWhitelist.isEmpty())
{
channel.sendMessage(author.getAsMention() + ", ```Whitelist Command:" + System.lineSeparator() +
"!whitelist add <MinecraftUsername>" + System.lineSeparator() + "Usage: Adds a user to the whitelist" + "```" + System.lineSeparator()
+ " **You have " + (whitelistLimit - timesWhitelisted)
+ "out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + " whitelists remaining**").queue();
}
else
{
File whitelistJSON = (new File(".", "whitelist.json"));
DiscordWhitelister.getPlugin().getLogger().info(author.getName() + "(" + author.getId() + ") attempted to whitelist: " + finalNameToWhitelist
+ ", " + (timesWhitelisted - whitelistLimit) + " whitelists remaining");
if(checkWhitelistJSON(whitelistJSON, finalNameToWhitelist))
{
channel.sendMessage(author.getAsMention() + ", successfully added **" + finalNameToWhitelist + "** to the whitelist").queue();
channel.sendMessage(author.getAsMention() + ", user is already on the whitelist!").queue();
}
else
{
channel.sendMessage(author.getAsMention() + ", failed to add **" + finalNameToWhitelist + "** to the whitelist, this is most likely due to an invalid Minecraft username").queue();
DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () -> DiscordWhitelister.getPlugin().getServer().dispatchCommand(DiscordWhitelister.getPlugin().getServer().getConsoleSender(),
"whitelist add " + finalNameToWhitelist));
int tempFinal = timesWhitelisted;
if(tempFinal < 3)
{
tempFinal = timesWhitelisted + 1;
}
int finalTimesWhitelistedInc = tempFinal;
int successfulFinalTimesWhitelisted = whitelistLimit - finalTimesWhitelistedInc;
int failedFinalTimesWhitelisted = whitelistLimit - timesWhitelisted;
// run through the server so that the check doesn't execute before the server has had a chance to run the whitelist command -- unsure if this is the best way of doing this, but it works
DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () ->
{
if(checkWhitelistJSON(whitelistJSON, finalNameToWhitelist))
{
channel.sendMessage(author.getAsMention() + ", successfully added `" + finalNameToWhitelist + "` to the whitelist"
+ " **You have " + successfulFinalTimesWhitelisted
+ " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + " whitelists remaining**").queue();
DiscordWhitelister.getUserList().set(author.getId(), finalTimesWhitelistedInc);
try
{
DiscordWhitelister.getUserList().save(DiscordWhitelister.getUserListFile().getPath());
}
catch (IOException e)
{
e.printStackTrace();
}
}
else
{
channel.sendMessage(author.getAsMention() + ", failed to add `" + finalNameToWhitelist + "` to the whitelist, this is most likely due to an invalid Minecraft username"
+ " **You have " + failedFinalTimesWhitelisted
+ " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + " whitelists remaining**").queue();
}
return null;
});
}
return null;
});
}
}
}
else if(userHasLimitedAdd && usedAllWhitelists)
{
channel.sendMessage(author.getAsMention() + ", unable to whitelist. **You have used : " + Integer.parseInt(DiscordWhitelister.getUserList().getString(author.getId()))
+ " out of " + DiscordWhitelister.getWhitelisterBotConfig().getInt("max-whitelist-amount") + " whitelists**").queue();
}
}
if (messageContents.toLowerCase().contains("!whitelist add") && !author.isBot())
{
boolean hasPerms = false;
if(userCanAddRemove || userCanAdd)
if(userCanAddRemove || userCanAdd || DiscordWhitelister.getWhitelisterBotConfig().getBoolean("limited-whitelist-enabled") && userHasLimitedAdd)
{
hasPerms = true;
}
@ -213,11 +347,11 @@ public class ServerDiscordClient extends ListenerAdapter
{
if(!checkWhitelistJSON(whitelistJSON, finalNameToRemove))
{
channel.sendMessage(author.getAsMention() + ", successfully removed **" + finalNameToRemove + "** from the whitelist").queue();
channel.sendMessage(author.getAsMention() + ", successfully removed `" + finalNameToRemove + "` from the whitelist").queue();
}
else
{
channel.sendMessage(author.getAsMention() + ", failed to remove **" + finalNameToRemove + "** from the whitelist, this should never really happen, you may have to remove the player manually and report the issue.").queue();
channel.sendMessage(author.getAsMention() + ", failed to remove `" + finalNameToRemove + "` from the whitelist, this should never really happen, you may have to remove the player manually and report the issue.").queue();
}
return null;
});