mirror of
https://github.com/Shimeo98/DiscordWhitelisterSpigot.git
synced 2024-11-28 21:55:31 +01:00
more progress on discord client
This commit is contained in:
parent
a6c6bfabec
commit
ef5d2fc122
5
pom.xml
5
pom.xml
@ -58,6 +58,11 @@
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.json-simple</groupId>
|
||||
<artifactId>json-simple</artifactId>
|
||||
<version>1.1.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -7,7 +7,6 @@ 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;
|
||||
|
||||
@ -17,16 +16,21 @@ public class DiscordWhitelister extends JavaPlugin
|
||||
private ServerDiscordClient serverDiscordClient;
|
||||
|
||||
private File whitelisterBotConfigFile;
|
||||
private FileConfiguration whitelisterBotConfig;
|
||||
private static FileConfiguration whitelisterBotConfig;
|
||||
|
||||
private boolean configCreated = false;
|
||||
|
||||
private String botToken;
|
||||
private boolean botEnabled = true;
|
||||
|
||||
private static JavaPlugin thisPlugin;
|
||||
|
||||
@Override
|
||||
public void onEnable()
|
||||
{
|
||||
thisPlugin = this;
|
||||
whitelisterBotConfig = new YamlConfiguration();
|
||||
|
||||
ConfigSetup();
|
||||
|
||||
AssignVars();
|
||||
@ -48,16 +52,21 @@ public class DiscordWhitelister extends JavaPlugin
|
||||
}
|
||||
}
|
||||
|
||||
public FileConfiguration getWhitelisterBotConfig()
|
||||
public static JavaPlugin getPlugin()
|
||||
{
|
||||
return this.whitelisterBotConfig;
|
||||
return thisPlugin;
|
||||
}
|
||||
|
||||
public static FileConfiguration getWhitelisterBotConfig()
|
||||
{
|
||||
return whitelisterBotConfig;
|
||||
}
|
||||
|
||||
public void ConfigSetup()
|
||||
{
|
||||
String fileString = "discord-whitelister.yml";
|
||||
//String fileString = "discord-whitelister.yml";
|
||||
|
||||
whitelisterBotConfigFile = new File(getDataFolder(), fileString);
|
||||
whitelisterBotConfigFile = new File(getDataFolder(), "discord-whitelister.yml");
|
||||
|
||||
if(!whitelisterBotConfigFile.exists())
|
||||
{
|
||||
@ -79,8 +88,6 @@ public class DiscordWhitelister extends JavaPlugin
|
||||
configCreated = true;
|
||||
}
|
||||
|
||||
whitelisterBotConfig = new YamlConfiguration();
|
||||
|
||||
try
|
||||
{
|
||||
getWhitelisterBotConfig().load(whitelisterBotConfigFile);
|
||||
@ -119,9 +126,5 @@ public class DiscordWhitelister extends JavaPlugin
|
||||
{
|
||||
botToken = getWhitelisterBotConfig().getString("discord-bot-token");
|
||||
botEnabled = getWhitelisterBotConfig().getBoolean("bot-enabled");
|
||||
|
||||
serverDiscordClient.allowedRoles = getWhitelisterBotConfig().getList("allowed-to-use-roles");
|
||||
serverDiscordClient.channelIds = getWhitelisterBotConfig().getList("target-text-channels");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,62 +4,68 @@ 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.User;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.events.role.RoleCreateEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import javax.security.auth.login.LoginException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
// handles Discord interaction
|
||||
public class ServerDiscordClient extends ListenerAdapter
|
||||
{
|
||||
public List<String> allowedRoles;
|
||||
public List<String> channelIds;
|
||||
|
||||
public void InitializeClient(String clientToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
JDA jda = new JDABuilder(AccountType.BOT)
|
||||
JDA javaDiscordAPI = new JDABuilder(AccountType.BOT)
|
||||
.setToken(clientToken)
|
||||
.addEventListeners(new ServerDiscordClient())
|
||||
.build();
|
||||
javaDiscordAPI.awaitReady();
|
||||
}
|
||||
catch(LoginException e)
|
||||
catch(LoginException | InterruptedException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void onMessageRecieved(MessageReceivedEvent messageReceivedEvent)
|
||||
@Override
|
||||
public void onMessageReceived(MessageReceivedEvent messageReceivedEvent)
|
||||
{
|
||||
if(messageReceivedEvent.isFromType(ChannelType.TEXT))
|
||||
{
|
||||
boolean correctChannel = false;
|
||||
|
||||
for(int channel = 0; channel < channelIds.size(); ++channel)
|
||||
for(int channel = 0; channel < DiscordWhitelister.getWhitelisterBotConfig().getList("target-text-channels").size(); ++channel)
|
||||
{
|
||||
if(messageReceivedEvent.getTextChannel().getId().equals(channelIds.get(channel)))
|
||||
if(messageReceivedEvent.getTextChannel().getId().equals(DiscordWhitelister.getWhitelisterBotConfig().getList("target-text-channels").get(channel)))
|
||||
{
|
||||
correctChannel = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(correctChannel)
|
||||
if(correctChannel && !messageReceivedEvent.getAuthor().isBot())
|
||||
{
|
||||
// message context
|
||||
User author = messageReceivedEvent.getAuthor();
|
||||
String messageContents = messageReceivedEvent.getMessage().getContentDisplay();
|
||||
MessageChannel channel = messageReceivedEvent.getTextChannel();
|
||||
|
||||
boolean userHasPerms = false;
|
||||
|
||||
// TODO: fix cast exception
|
||||
for(Role role : messageReceivedEvent.getGuild().getMember(messageReceivedEvent.getAuthor()).getRoles())
|
||||
{
|
||||
if(Arrays.stream((String[])allowedRoles.toArray()).parallel().anyMatch(role.getName()::contains))
|
||||
if(Arrays.stream((String[])DiscordWhitelister.getWhitelisterBotConfig().getList("allowed-to-use-roles").toArray()).parallel().anyMatch(role.getName()::contains))
|
||||
{
|
||||
userHasPerms = true;
|
||||
}
|
||||
@ -68,10 +74,54 @@ public class ServerDiscordClient extends ListenerAdapter
|
||||
if(messageContents.contains("!whitelist") && userHasPerms)
|
||||
{
|
||||
String nameToWhitelist = messageContents;
|
||||
nameToWhitelist = nameToWhitelist.replaceAll("!whitelist", "");
|
||||
nameToWhitelist = nameToWhitelist.substring(nameToWhitelist.indexOf("!whitelist")+1); // get everything after !whitelist
|
||||
nameToWhitelist = nameToWhitelist.replaceAll(" ", "");
|
||||
nameToWhitelist = nameToWhitelist.toLowerCase();
|
||||
|
||||
// TODO: execute whitelist command on server, read whitelist.json and check if user was successfully added
|
||||
DiscordWhitelister.getPlugin().getLogger().info(author.getName() + "(" + author.getId() + ", " + author.getMutualGuilds() + ") attempted to whitelist " + 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
|
||||
{
|
||||
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();
|
||||
}
|
||||
else
|
||||
{
|
||||
channel.sendMessage("Failed to add **" + nameToWhitelist + "** to the whitelist, this is most likely due to an invalid minecraft username").queue();
|
||||
}
|
||||
}
|
||||
else if(messageContents.contains("!whitelist") && !userHasPerms && !messageReceivedEvent.getAuthor().isBot())
|
||||
{
|
||||
channel.sendMessage(author.getAsMention() + ", you do not have permission to use this command").queue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user