feat: check for required role at startup

This commit is contained in:
Xyntexx 2021-03-17 00:07:33 +02:00
parent c07191e18f
commit ab6c20b447
2 changed files with 122 additions and 73 deletions

View File

@ -1206,74 +1206,133 @@ public class DiscordClient extends ListenerAdapter
}
}
public static void StartUpMemberCheck() throws IOException
{
if(!MainConfig.getMainConfig().getBoolean("un-whitelist-on-server-leave"))
return;
public static void RequiredRoleStartupCheck() {
try {
if (!checkForMissingRole)
return;
// Don't attempt to remove members if not connected
if(javaDiscordAPI.getStatus() != JDA.Status.CONNECTED)
return;
DiscordWhitelister.getPluginLogger().info("Checking Discord IDs for leavers...");
Yaml idYaml = new Yaml();
UserList.SaveStore();
InputStream inputStream = new FileInputStream(UserList.getUserListFile());
PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream);
int b = pushbackInputStream.read();
if(b == -1)
return;
else
pushbackInputStream.unread(b);
Map<String, List<String>> userObject = idYaml.load(pushbackInputStream);
for(Map.Entry<String, List<String>> entry : userObject.entrySet())
{
// Check if the ID is in any guilds
boolean inGuild = false;
// Check all guilds
for(int i = 0; i < javaDiscordAPI.getGuilds().size(); i++)
{
if(javaDiscordAPI.getGuilds().get(i).getMemberById(entry.getKey()) != null)
inGuild = true;
if (roleToCheck == null || roleToCheck.equals("")) {
DiscordWhitelister.getPluginLogger().warning("'un-whitelist-if-missing-role' is enabled but " +
"'role-to-check-for' is null or empty, please double check the config");
return;
}
// un-whitelist associated minecraft usernames if not in any guilds
if(!inGuild)
{
for(int i = 0; i < entry.getValue().size(); i++)
{
// un-whitelist
if(!WhitelistedPlayers.usingEasyWhitelist)
{
DiscordClient.ExecuteServerCommand("whitelist remove " + entry.getValue().get(i));
DiscordWhitelister.getPluginLogger().info("Checking Discord IDs for required roles...");
Yaml idYaml = new Yaml();
UserList.SaveStore();
InputStream inputStream = new FileInputStream(UserList.getUserListFile());
PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream);
int b = pushbackInputStream.read();
if (b == -1)
return;
else
pushbackInputStream.unread(b);
Map<String, List<String>> userObject = idYaml.load(pushbackInputStream);
for (Map.Entry<String, List<String>> entry : userObject.entrySet()) {
// Check if the ID is in any guilds
boolean hasRole = false;
// Check all guilds
boolean requiredRole = false;
for (int i = 0; i < javaDiscordAPI.getGuilds().size(); i++) {
Member member = javaDiscordAPI.getGuilds().get(i).getMemberById(entry.getKey());
if (member != null) {
for (Role role : member.getRoles()) {
if (role.getId().equals(roleToCheck)) {
requiredRole = true;
break;
}
}
}
else
{
DiscordClient.ExecuteServerCommand("easywl remove " + entry.getValue().get(i));
}
//remove permissions
RemovePerms(entry.getValue().get(i));
DiscordWhitelister.getPluginLogger().info("Removed " + entry.getValue().get(i)
+ " from the whitelist as Discord ID: " + entry.getKey() + " has left the server.");
}
// Clear entries in user-list
if(userObject.get(entry.getKey()) != null)
{
UserList.getUserList().set(entry.getKey(), null);
UserList.SaveStore();
DiscordWhitelister.getPlugin().getLogger().info("Discord ID: " + entry.getKey()
+ " left. Successfully removed their whitelisted entries from the user list.");
if (!requiredRole) {
for (int i = 0; i < entry.getValue().size(); i++) {
// un-whitelist
if (!WhitelistedPlayers.usingEasyWhitelist) {
DiscordClient.ExecuteServerCommand("whitelist remove " + entry.getValue().get(i));
} else {
DiscordClient.ExecuteServerCommand("easywl remove " + entry.getValue().get(i));
}
//remove permissions
RemovePerms(entry.getValue().get(i));
DiscordWhitelister.getPluginLogger().info("Removed " + entry.getValue().get(i)
+ " from the whitelist as Discord ID: " + entry.getKey() + " due to missing required role (" + roleToCheck + ").");
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void ServerLeaveStartupCheck() {
try {
if (MainConfig.getMainConfig().getBoolean("un-whitelist-on-server-leave")) {
// Don't attempt to remove members if not connected
if (javaDiscordAPI.getStatus() != JDA.Status.CONNECTED)
return;
DiscordWhitelister.getPluginLogger().info("Checking Discord IDs for leavers...");
Yaml idYaml = new Yaml();
UserList.SaveStore();
InputStream inputStream = new FileInputStream(UserList.getUserListFile());
PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream);
int b = pushbackInputStream.read();
if (b == -1)
return;
else
pushbackInputStream.unread(b);
Map<String, List<String>> userObject = idYaml.load(pushbackInputStream);
for (Map.Entry<String, List<String>> entry : userObject.entrySet()) {
// Check if the ID is in any guilds
boolean inGuild = false;
// Check all guilds
for (int i = 0; i < javaDiscordAPI.getGuilds().size(); i++) {
if (javaDiscordAPI.getGuilds().get(i).getMemberById(entry.getKey()) != null)
inGuild = true;
}
// un-whitelist associated minecraft usernames if not in any guilds
if (!inGuild) {
for (int i = 0; i < entry.getValue().size(); i++) {
// un-whitelist
if (!WhitelistedPlayers.usingEasyWhitelist) {
DiscordClient.ExecuteServerCommand("whitelist remove " + entry.getValue().get(i));
} else {
DiscordClient.ExecuteServerCommand("easywl remove " + entry.getValue().get(i));
}
//remove permissions
RemovePerms(entry.getValue().get(i));
DiscordWhitelister.getPluginLogger().info("Removed " + entry.getValue().get(i)
+ " from the whitelist as Discord ID: " + entry.getKey() + " has left the server.");
}
// Clear entries in user-list
if (userObject.get(entry.getKey()) != null) {
UserList.getUserList().set(entry.getKey(), null);
UserList.SaveStore();
DiscordWhitelister.getPlugin().getLogger().info("Discord ID: " + entry.getKey()
+ " left. Successfully removed their whitelisted entries from the user list.");
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

View File

@ -15,7 +15,6 @@ import uk.co.angrybee.joe.stores.RemovedList;
import uk.co.angrybee.joe.stores.UserList;
import uk.co.angrybee.joe.stores.WhitelistedPlayers;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;
@ -69,17 +68,8 @@ public class DiscordWhitelister extends JavaPlugin
}
// Check for leavers if enabled
if(MainConfig.getMainConfig().getBoolean("un-whitelist-on-server-leave"))
{
try
{
DiscordClient.StartUpMemberCheck();
}
catch (IOException e)
{
e.printStackTrace();
}
}
DiscordClient.ServerLeaveStartupCheck();
DiscordClient.RequiredRoleStartupCheck();
this.getCommand("discordwhitelister").setExecutor(new CommandStatus());
this.getCommand("discordwhitelisterabout").setExecutor(new CommandAbout());