Make whitelist changes
This commit is contained in:
@@ -13,11 +13,12 @@ import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import com.fpghoti.bcraftbot.bot.ServerBot;
|
||||
import com.fpghoti.bcraftbot.command.AddExempt;
|
||||
import com.fpghoti.bcraftbot.command.BCWhitelist;
|
||||
import com.fpghoti.bcraftbot.listener.PlayerListener;
|
||||
import com.fpghoti.bcraftbot.sql.MySQLConnection;
|
||||
|
||||
import net.dv8tion.jda.core.entities.Guild;
|
||||
import net.dv8tion.jda.core.entities.User;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
|
||||
public class Main extends JavaPlugin {
|
||||
|
||||
@@ -65,6 +66,7 @@ public class Main extends JavaPlugin {
|
||||
bot = new ServerBot(this);
|
||||
bot.runBot();
|
||||
getCommand("AddExempt").setExecutor(new AddExempt(this));
|
||||
getCommand("BCWhitelist").setExecutor(new BCWhitelist(this));
|
||||
}
|
||||
|
||||
public void onDisable() {
|
||||
@@ -183,6 +185,10 @@ public class Main extends JavaPlugin {
|
||||
config.createSection("MainRecordKeeper");
|
||||
config.set("MainRecordKeeper", true);
|
||||
}
|
||||
if (config.get("Whitelist") == null){
|
||||
config.createSection("Whitelist");
|
||||
config.set("Whitelist", true);
|
||||
}
|
||||
this.saveConfig();
|
||||
}
|
||||
|
||||
@@ -229,6 +235,22 @@ public class Main extends JavaPlugin {
|
||||
}
|
||||
setExemptList(updated);
|
||||
}
|
||||
|
||||
public void setBCWhitelist(Boolean b) {
|
||||
if (config.get("Whitelist") != null){
|
||||
config.set("Whitelist", b);
|
||||
this.saveConfig();
|
||||
}else {
|
||||
log.severe("BCraftBot failed to update the whitelist in the config!");
|
||||
}
|
||||
}
|
||||
|
||||
public boolean getBCWhitelist() {
|
||||
if (config.get("Whitelist") != null){
|
||||
return config.getBoolean("Whitelist");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public ServerBot getBot() {
|
||||
return bot;
|
||||
|
||||
@@ -2,12 +2,10 @@ package com.fpghoti.bcraftbot.bot;
|
||||
|
||||
import java.util.logging.Level;
|
||||
|
||||
import com.fpghoti.bcraftbot.bot.ServerBot;
|
||||
|
||||
import net.dv8tion.jda.core.entities.ChannelType;
|
||||
import net.dv8tion.jda.core.events.ReadyEvent;
|
||||
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.core.hooks.ListenerAdapter;
|
||||
import net.dv8tion.jda.api.entities.ChannelType;
|
||||
import net.dv8tion.jda.api.events.ReadyEvent;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
|
||||
public class BotListener extends ListenerAdapter{
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.fpghoti.bcraftbot.bot;
|
||||
|
||||
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
|
||||
public interface Command {
|
||||
|
||||
|
||||
@@ -4,21 +4,23 @@ import java.util.HashMap;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import com.fpghoti.bcraftbot.Main;
|
||||
import com.fpghoti.bcraftbot.bot.BotListener;
|
||||
import com.fpghoti.bcraftbot.bot.Command;
|
||||
import com.fpghoti.bcraftbot.bot.command.AddMeCommand;
|
||||
import com.fpghoti.bcraftbot.bot.util.CommandParser;
|
||||
import com.fpghoti.bcraftbot.sql.MySQLConnection;
|
||||
|
||||
import net.dv8tion.jda.core.AccountType;
|
||||
import net.dv8tion.jda.core.JDA;
|
||||
import net.dv8tion.jda.core.JDABuilder;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||
import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder;
|
||||
import net.dv8tion.jda.api.sharding.ShardManager;
|
||||
import net.dv8tion.jda.api.utils.ChunkingFilter;
|
||||
import net.dv8tion.jda.api.utils.MemberCachePolicy;
|
||||
|
||||
public class ServerBot {
|
||||
|
||||
private Main plugin;
|
||||
|
||||
private JDA jda;
|
||||
private static ShardManager shardm;
|
||||
|
||||
private CommandParser parser;
|
||||
|
||||
@@ -34,7 +36,19 @@ public class ServerBot {
|
||||
String token = plugin.getBotToken();
|
||||
plugin.log(Level.INFO, "Connecting bot to Discord...");
|
||||
try{
|
||||
jda = new JDABuilder(AccountType.BOT).setToken(token).buildBlocking();
|
||||
shardm = DefaultShardManagerBuilder.createDefault(token)
|
||||
.setChunkingFilter(ChunkingFilter.ALL)
|
||||
.setMemberCachePolicy(MemberCachePolicy.ALL)
|
||||
.enableIntents(GatewayIntent.getIntents(GatewayIntent.DEFAULT))
|
||||
.enableIntents(GatewayIntent.GUILD_MEMBERS)
|
||||
.enableIntents(GatewayIntent.GUILD_MESSAGES)
|
||||
.build();
|
||||
jda = shardm.getShardById(0);
|
||||
try {
|
||||
jda.awaitReady();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
jda.addEventListener(new BotListener(this));
|
||||
jda.setAutoReconnect(true);
|
||||
String link = "https://discordapp.com/oauth2/authorize?&client_id=" + jda.getSelfUser().getId();
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.util.logging.Level;
|
||||
import com.fpghoti.bcraftbot.bot.Command;
|
||||
import com.fpghoti.bcraftbot.bot.ServerBot;
|
||||
|
||||
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
|
||||
public class AddMeCommand implements Command{
|
||||
|
||||
@@ -19,7 +19,6 @@ public class AddMeCommand implements Command{
|
||||
|
||||
@Override
|
||||
public boolean called(String[] args, MessageReceivedEvent event) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.fpghoti.bcraftbot.bot.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
|
||||
public class CommandParser {
|
||||
public CommandContainer parse(String rw, MessageReceivedEvent e){
|
||||
|
||||
@@ -32,7 +32,7 @@ public class AddExempt implements CommandExecutor{
|
||||
}
|
||||
}else {
|
||||
if(args.length != 1) {
|
||||
plugin.log(Level.INFO, "Usage: addexempt <username>");
|
||||
plugin.log(Level.INFO, "Usage: /addexempt <username>");
|
||||
plugin.log(Level.INFO, "There was an issue with your syntax.");
|
||||
return true;
|
||||
}
|
||||
|
||||
75
src/main/java/com/fpghoti/bcraftbot/command/BCWhitelist.java
Normal file
75
src/main/java/com/fpghoti/bcraftbot/command/BCWhitelist.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package com.fpghoti.bcraftbot.command;
|
||||
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.fpghoti.bcraftbot.Main;
|
||||
|
||||
public class BCWhitelist implements CommandExecutor{
|
||||
|
||||
private Main plugin;
|
||||
|
||||
public BCWhitelist(Main plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
|
||||
if(label.equalsIgnoreCase("bcwhitelist")){
|
||||
if(sender instanceof Player) {
|
||||
if(!((Player)sender).hasPermission("bcraftbot.whitelist")) {
|
||||
((Player)sender).sendMessage(ChatColor.RED + "[BCraftBot] You do not have permission to do this!");
|
||||
return true;
|
||||
}
|
||||
if(args.length != 1) {
|
||||
((Player)sender).sendMessage(ChatColor.GOLD + "[BCraftBot] Usage: /bcwhitelist <on/off>");
|
||||
((Player)sender).sendMessage(ChatColor.RED + "[BCraftBot] There was an issue with your syntax.");
|
||||
return true;
|
||||
}
|
||||
}else {
|
||||
if(args.length != 1) {
|
||||
plugin.log(Level.INFO, "Usage: /bcwhitelist <on/off>");
|
||||
plugin.log(Level.INFO, "There was an issue with your syntax.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
String arg = args[0].toLowerCase();
|
||||
int status = 0;
|
||||
|
||||
if(arg.equals("on") || arg.equals("true") || arg.equals("enable")) {
|
||||
status = 1;
|
||||
plugin.setBCWhitelist(true);
|
||||
}else if(arg.equals("off") || arg.equals("false") || arg.equals("disable")) {
|
||||
status = 2;
|
||||
plugin.setBCWhitelist(false);
|
||||
}
|
||||
|
||||
if(sender instanceof Player) {
|
||||
if(status == 1) {
|
||||
((Player)sender).sendMessage(ChatColor.GREEN + "[BCraftBot] Whitelist enabled.");
|
||||
}else if(status == 2) {
|
||||
((Player)sender).sendMessage(ChatColor.GREEN + "[BCraftBot] Whitelist disabled.");
|
||||
}else {
|
||||
((Player)sender).sendMessage(ChatColor.GOLD + "[BCraftBot] Usage: /bcwhitelist <on/off>");
|
||||
((Player)sender).sendMessage(ChatColor.RED + "[BCraftBot] There was an issue with your syntax.");
|
||||
}
|
||||
}else {
|
||||
if(status == 1) {
|
||||
plugin.log(Level.INFO, "[BCraftBot] Whitelist enabled.");
|
||||
}else if(status == 2) {
|
||||
plugin.log(Level.INFO, "[BCraftBot] Whitelist disabled.");
|
||||
}else {
|
||||
plugin.log(Level.INFO, "[BCraftBot] Usage: /bcwhitelist <on/off>");
|
||||
plugin.log(Level.INFO, "[BCraftBot] There was an issue with your syntax.");
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,10 +8,10 @@ import org.bukkit.event.player.PlayerLoginEvent.Result;
|
||||
|
||||
import com.fpghoti.bcraftbot.Main;
|
||||
|
||||
import net.dv8tion.jda.core.entities.Guild;
|
||||
import net.dv8tion.jda.core.entities.Member;
|
||||
import net.dv8tion.jda.core.entities.Role;
|
||||
import net.dv8tion.jda.core.entities.User;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
|
||||
public class PlayerListener implements Listener {
|
||||
|
||||
@@ -28,19 +28,20 @@ public class PlayerListener implements Listener {
|
||||
if(player.hasPermission("bcraftbot.bypasscheck")) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if(!plugin.isMember(player) && !plugin.isExempt(player.getName())) {
|
||||
event.disallow(Result.KICK_OTHER, plugin.getKickMessage());
|
||||
if(!player.isWhitelisted() && plugin.getBCWhitelist()) {
|
||||
event.disallow(Result.KICK_OTHER, plugin.getKickMessage());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if(plugin.checkRole() && !plugin.isExempt(player.getName())) {
|
||||
boolean allow = false;
|
||||
|
||||
|
||||
for(Guild guild : plugin.getBot().getJDA().getGuilds()) {
|
||||
|
||||
for(String rolename : plugin.getRequiredRole().split(",")) {
|
||||
|
||||
Role role = null;
|
||||
for(Role r : guild.getRoles()) {
|
||||
if(r.getName().equalsIgnoreCase(rolename)) {
|
||||
@@ -51,6 +52,12 @@ public class PlayerListener implements Listener {
|
||||
User user = plugin.getDiscordUser(player);
|
||||
Member mem = guild.getMember(user);
|
||||
|
||||
if(!plugin.getBCWhitelist()) {
|
||||
allow = true;
|
||||
}
|
||||
if(player.isWhitelisted()) {
|
||||
allow = true;
|
||||
}
|
||||
if(user != null && user.getMutualGuilds().contains(guild) && role != null) {
|
||||
if(mem.getRoles().contains(role)) {
|
||||
allow = true;
|
||||
@@ -80,7 +87,7 @@ public class PlayerListener implements Listener {
|
||||
Member mem = guild.getMember(user);
|
||||
|
||||
if(user != null && user.getMutualGuilds().contains(guild) && role != null) {
|
||||
guild.getController().addRolesToMember(mem, role).queue();
|
||||
guild.addRoleToMember(mem.getUser().getId(), role).queue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
main: com.fpghoti.bcraftbot.Main
|
||||
version: 1.1
|
||||
version: 1.2
|
||||
api-version: 1.13
|
||||
name: BCraftBot
|
||||
descripion: Discord-based whitelisting system for Minecraft
|
||||
@@ -7,4 +7,7 @@ author: Ghoti_Mayo
|
||||
commands:
|
||||
AddExempt:
|
||||
description: Add a player to the exempt list. Exempted users are permanently allowed to join, as long as they remain in the exempted list.
|
||||
usage: /addexempt <username>
|
||||
usage: /addexempt <username>
|
||||
BCWhitelist:
|
||||
description: Toggles plugin whitelist.
|
||||
usage: /bcwhitelist <on/off>
|
||||
Reference in New Issue
Block a user