finished discord client for the most part

This commit is contained in:
JoeShimo 2019-08-01 15:10:58 +01:00
parent ef5d2fc122
commit d16122cdce
6 changed files with 81 additions and 43 deletions

0
.gitignore vendored Normal file → Executable file
View File

0
README.md Normal file → Executable file
View File

0
plugin.yml Normal file → Executable file
View File

0
pom.xml Normal file → Executable file
View File

View File

@ -64,8 +64,6 @@ public class DiscordWhitelister extends JavaPlugin
public void ConfigSetup()
{
//String fileString = "discord-whitelister.yml";
whitelisterBotConfigFile = new File(getDataFolder(), "discord-whitelister.yml");
if(!whitelisterBotConfigFile.exists())
@ -118,7 +116,6 @@ public class DiscordWhitelister extends JavaPlugin
{
e.printStackTrace();
}
}
}

121
src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java Normal file → Executable file
View File

@ -4,13 +4,14 @@ import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import javax.security.auth.login.LoginException;
@ -58,14 +59,20 @@ public class ServerDiscordClient extends ListenerAdapter
// message context
User author = messageReceivedEvent.getAuthor();
String messageContents = messageReceivedEvent.getMessage().getContentDisplay();
MessageChannel channel = messageReceivedEvent.getTextChannel();
TextChannel channel = messageReceivedEvent.getTextChannel();
boolean userHasPerms = false;
// TODO: fix cast exception
String[] allowedToUseRoles = new String[DiscordWhitelister.getWhitelisterBotConfig().getList("allowed-to-use-roles").size()];
for(int roles = 0; roles < allowedToUseRoles.length; ++roles)
{
allowedToUseRoles[roles] = DiscordWhitelister.getWhitelisterBotConfig().getList("allowed-to-use-roles").get(roles).toString();
}
for(Role role : messageReceivedEvent.getGuild().getMember(messageReceivedEvent.getAuthor()).getRoles())
{
if(Arrays.stream((String[])DiscordWhitelister.getWhitelisterBotConfig().getList("allowed-to-use-roles").toArray()).parallel().anyMatch(role.getName()::contains))
if(Arrays.stream(allowedToUseRoles).parallel().anyMatch(role.getName()::contains))
{
userHasPerms = true;
}
@ -74,49 +81,45 @@ public class ServerDiscordClient extends ListenerAdapter
if(messageContents.contains("!whitelist") && userHasPerms)
{
String nameToWhitelist = messageContents;
nameToWhitelist = nameToWhitelist.substring(nameToWhitelist.indexOf("!whitelist")+1); // get everything after !whitelist
nameToWhitelist = nameToWhitelist.replaceAll(" ", "");
nameToWhitelist = nameToWhitelist.toLowerCase();
nameToWhitelist = nameToWhitelist.substring(nameToWhitelist.indexOf("!whitelist")+10); // get everything after !whitelist
nameToWhitelist = nameToWhitelist.replaceAll(" ", "");
DiscordWhitelister.getPlugin().getLogger().info(author.getName() + "(" + author.getId() + ", " + author.getMutualGuilds() + ") attempted to whitelist " + nameToWhitelist);
final String finalNameToWhitelist = nameToWhitelist;
DiscordWhitelister.getPlugin().getServer().dispatchCommand(DiscordWhitelister.getPlugin().getServer().getConsoleSender(),
"whitelist add " + nameToWhitelist);
File whitelistJSON = new File(DiscordWhitelister.getPlugin().getDataFolder(), "whitelist.json");
org.json.simple.parser.JSONParser jsonParser = new org.json.simple.parser.JSONParser();
boolean successfullyAdded = false;
try
if(finalNameToWhitelist.isEmpty())
{
JSONArray jsonArray = (JSONArray)jsonParser.parse(new FileReader(whitelistJSON));
for(Object object : jsonArray)
{
JSONObject player = (JSONObject) object;
String name = (String)player.get("name");
if(name.equals(nameToWhitelist))
{
successfullyAdded = true;
}
}
}
catch(IOException | ParseException e)
{
e.printStackTrace();
}
if(successfullyAdded)
{
channel.sendMessage("Successfully added **" + nameToWhitelist + "** to the whitelist").queue();
channel.sendMessage(author.getAsMention() + ", please enter a valid Minecraft username").queue();
}
else
{
channel.sendMessage("Failed to add **" + nameToWhitelist + "** to the whitelist, this is most likely due to an invalid minecraft username").queue();
File whitelistJSON = (new File(".", "whitelist.json"));
DiscordWhitelister.getPlugin().getLogger().info(author.getName() + "(" + author.getId() + ") attempted to whitelist " + nameToWhitelist);
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;
});
}
}
}
else if(messageContents.contains("!whitelist") && !userHasPerms && !messageReceivedEvent.getAuthor().isBot())
@ -126,4 +129,42 @@ public class ServerDiscordClient extends ListenerAdapter
}
}
}
private boolean checkWhitelistJSON(File whitelistFile, String minecraftUsername)
{
boolean correctUsername = false;
try
{
JSONParser jsonParser = new JSONParser();
JSONArray jsonArray = (JSONArray)jsonParser.parse(new FileReader(whitelistFile));
for(Object object : jsonArray)
{
JSONObject player = (JSONObject) object;
String name = (String)player.get("name");
name = name.toLowerCase();
if(name.equals(minecraftUsername))
{
correctUsername = true;
}
}
}
catch(IOException | ParseException e)
{
e.printStackTrace();
}
if(correctUsername)
{
return true;
}
else
{
return false;
}
}
}