Progress on discord client

This commit is contained in:
JoeShimo 2019-07-29 00:02:12 +01:00
parent 81a70f80f4
commit a6c6bfabec
3 changed files with 68 additions and 7 deletions

10
pom.xml
View File

@ -119,5 +119,15 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -7,19 +7,21 @@ 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;
public class DiscordWhitelister extends JavaPlugin
{
private ServerDiscordClient serverDiscordClient;
private File whitelisterBotConfigFile;
private FileConfiguration whitelisterBotConfig;
private boolean configCreated = false;
private String botToken;
private List allowedRoles;
private boolean botEnabled = true;
@Override
@ -35,12 +37,12 @@ public class DiscordWhitelister extends JavaPlugin
}
else if(configCreated)
{
getLogger().info("Config newly created, token will be in-valid, doing nothing until next server start");
getLogger().info("Config newly created, please paste your bot token into the config file, doing nothing until next server start");
}
else
{
getLogger().info("Initializing Discord client");
ServerDiscordClient serverDiscordClient = new ServerDiscordClient();
serverDiscordClient = new ServerDiscordClient();
serverDiscordClient.InitializeClient(botToken);
getLogger().info("Successfully initialized Discord client");
}
@ -96,6 +98,9 @@ public class DiscordWhitelister extends JavaPlugin
List<String> tempSetupRoles = Arrays.asList("Owner", "Admin", "Mod");
getWhitelisterBotConfig().set("allowed-to-use-roles", tempSetupRoles);
List<String> tempChannelIds = Arrays.asList("445666834382061569", "488450157881327616");
getWhitelisterBotConfig().set("target-text-channels", tempChannelIds);
getWhitelisterBotConfig().set("bot-enabled", true);
try
@ -113,8 +118,10 @@ public class DiscordWhitelister extends JavaPlugin
public void AssignVars()
{
botToken = getWhitelisterBotConfig().getString("discord-bot-token");
allowedRoles = getWhitelisterBotConfig().getList("allowed-to-use-roles");
botEnabled = getWhitelisterBotConfig().getBoolean("bot-enabled");
serverDiscordClient.allowedRoles = getWhitelisterBotConfig().getList("allowed-to-use-roles");
serverDiscordClient.channelIds = getWhitelisterBotConfig().getList("target-text-channels");
}
}

View File

@ -3,15 +3,24 @@ package uk.co.angrybee.joe;
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.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 javax.security.auth.login.LoginException;
import java.lang.reflect.Array;
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
@ -28,8 +37,43 @@ public class ServerDiscordClient extends ListenerAdapter
public void onMessageRecieved(MessageReceivedEvent messageReceivedEvent)
{
// context
User author = messageReceivedEvent.getAuthor();
String messageContents = messageReceivedEvent.getMessage().getContentDisplay();
if(messageReceivedEvent.isFromType(ChannelType.TEXT))
{
boolean correctChannel = false;
for(int channel = 0; channel < channelIds.size(); ++channel)
{
if(messageReceivedEvent.getTextChannel().getId().equals(channelIds.get(channel)))
{
correctChannel = true;
}
}
if(correctChannel)
{
// message context
User author = messageReceivedEvent.getAuthor();
String messageContents = messageReceivedEvent.getMessage().getContentDisplay();
boolean userHasPerms = false;
for(Role role : messageReceivedEvent.getGuild().getMember(messageReceivedEvent.getAuthor()).getRoles())
{
if(Arrays.stream((String[])allowedRoles.toArray()).parallel().anyMatch(role.getName()::contains))
{
userHasPerms = true;
}
}
if(messageContents.contains("!whitelist") && userHasPerms)
{
String nameToWhitelist = messageContents;
nameToWhitelist = nameToWhitelist.replaceAll("!whitelist", "");
nameToWhitelist = nameToWhitelist.replaceAll(" ", "");
// TODO: execute whitelist command on server, read whitelist.json and check if user was successfully added
}
}
}
}
}