diff --git a/src/main/java/plugin/Ploogin.java b/src/main/java/plugin/Ploogin.java index 9c47cca..4a30c70 100644 --- a/src/main/java/plugin/Ploogin.java +++ b/src/main/java/plugin/Ploogin.java @@ -9,6 +9,7 @@ import com.mongodb.client.model.Filters; import com.mongodb.client.model.UpdateOptions; import com.mongodb.client.model.Updates; +import mindustry.Vars; import mindustry.game.*; import mindustry.gen.Call; import mindustry.gen.Groups; @@ -23,6 +24,7 @@ import java.io.IOException; import java.util.Date; +import static mindustry.Vars.mods; import static plugin.commands.BanMenu.loadBanMenu; import static plugin.functions.MongoDB.MongoDbPlayerCreation; import static plugin.functions.MongoDB.MongoDbPlayerRankCheck; @@ -55,7 +57,7 @@ public void init() { Events.on(EventType.PlayerJoin.class, event -> { Player plr = event.player; MongoDbPlayerCreation(plr); - MongoDbPlayerRankCheck(plr); + MongoDbPlayerRankCheck(plr.uuid()); }); Events.on(EventType.PlayerConnect.class, event -> { Document user = playerCollection.find(Filters.eq("uuid", event.player.uuid())).first(); @@ -97,8 +99,10 @@ public void registerClientCommands(CommandHandler handler){ } player.sendMessage(String.valueOf(list)); }); - /*handler.register("js", "", "Execute JavaScript code.", (args, player) -> { - if (player.admin()) { + handler.register("js", "", "Execute JavaScript code.", (args, player) -> { + Document user = playerCollection.find(Filters.eq("uuid", player.uuid())).first(); + boolean isConsole = user.getInteger("rank") == 2; + if (player.admin() && isConsole) { try { String output = mods.getScripts().runConsole(args[0]); player.sendMessage("> " + ("[#ff341c]" + output)); @@ -107,9 +111,9 @@ public void registerClientCommands(CommandHandler handler){ return; } } else { - player.sendMessage("[scarlet]You must be admin to use this command."); + player.sendMessage("[scarlet]You must be console to use this command."); } - });*/ + }); } // shit to register console commands because yes @Override @@ -134,6 +138,7 @@ public void registerServerCommands(CommandHandler handler){ ); playerCollection.updateOne(user, updates, new UpdateOptions().upsert(true)); Log.info("Rank has been given!"); + MongoDbPlayerRankCheck(user.getString("uuid")); }); } } diff --git a/src/main/java/plugin/discord/Bot.java b/src/main/java/plugin/discord/Bot.java index 04339cb..f621bb6 100644 --- a/src/main/java/plugin/discord/Bot.java +++ b/src/main/java/plugin/discord/Bot.java @@ -4,6 +4,8 @@ import arc.Events; import arc.util.Log; import com.mongodb.client.model.Filters; +import com.mongodb.client.model.UpdateOptions; +import com.mongodb.client.model.Updates; import mindustry.Vars; import mindustry.content.Blocks; import mindustry.core.GameState; @@ -14,21 +16,32 @@ import mindustry.maps.Map; import mindustry.net.Packets; import org.bson.Document; +import org.bson.conversions.Bson; import org.javacord.api.*; import mindustry.mod.*; import org.javacord.api.entity.channel.TextChannel; import org.javacord.api.entity.intent.Intent; import org.javacord.api.entity.message.Message; +import org.javacord.api.entity.message.embed.EmbedBuilder; import org.javacord.api.entity.permission.PermissionType; import org.javacord.api.event.interaction.SlashCommandCreateEvent; import org.javacord.api.event.message.MessageCreateEvent; import org.javacord.api.interaction.*; +import plugin.Ploogin; import plugin.utils.Utilities; + +import java.time.Duration; +import java.util.Arrays; import java.util.Collections; +import java.util.Date; + import mindustry.gen.Player; import plugin.ConfigJson; +import useful.Bundle; + import static mindustry.Vars.*; import static plugin.Ploogin.playerCollection; +import static plugin.utils.Utilities.findPlayerByName; public class Bot { // variables for load function @@ -36,6 +49,7 @@ public class Bot { public static TextChannel channel; public static TextChannel banchannel; // main bot + @SuppressWarnings("UnnecessaryLocalVariable") public static void load(){ api = new DiscordApiBuilder() .setToken(ConfigJson.token) @@ -73,13 +87,26 @@ private static void onMessageCreate(MessageCreateEvent listener){ // registers slash commands so user can see them and use private static void registerSlashCommands(){ SlashCommand banCommand = SlashCommand.with("ban", "Bans the player", - Collections.singletonList( - SlashCommandOption.create( - SlashCommandOptionType.STRING, - "name", - "name of the player" - ) - ) + Arrays.asList( + SlashCommandOption.create( + SlashCommandOptionType.LONG, + "id", + "id of the player", + true + ), + SlashCommandOption.create( + SlashCommandOptionType.LONG, + "time", + "Duration of ban (in days)", + true + ), + SlashCommandOption.create( + SlashCommandOptionType.STRING, + "reason", + "Reason of ban", + true + ) + ) ).setDefaultEnabledForPermissions(PermissionType.KICK_MEMBERS) .createGlobal(api).join(); SlashCommand exitCommand = SlashCommand.with("exit", "exits the servar" @@ -88,13 +115,13 @@ private static void registerSlashCommands(){ SlashCommand listCommand = SlashCommand.with("list", "Lists the players" ).createGlobal(api).join(); SlashCommand adminaddCommand = SlashCommand.with("adminadd", "gives admin to player (use it carefully)", - Collections.singletonList( - SlashCommandOption.create( - SlashCommandOptionType.STRING, - "name", - "name of the player" - ) - ) + Arrays.asList( + SlashCommandOption.create( + SlashCommandOptionType.STRING, + "name", + "name of the player", + true + )) ).setDefaultEnabledForPermissions(PermissionType.KICK_MEMBERS) .createGlobal(api).join(); SlashCommand gameoverCommand = SlashCommand.with("gameover", "Executes gameover event" @@ -112,24 +139,43 @@ private static void addSlashCommandListener(SlashCommandCreateEvent listener) { String response; - String PlayerName = listener.getSlashCommandInteraction().getOptionByName("name").get().getStringValue().get(); - Player player = Utilities.findPlayerByName(PlayerName); - if (player == null) { + int id = Math.toIntExact(listener.getSlashCommandInteraction().getOptionByName("id").get().getLongValue().get()); + String reason = listener.getSlashCommandInteraction().getOptionByName("reason").get().getStringValue().get(); + Long time = listener.getSlashCommandInteraction().getOptionByName("time").get().getLongValue().get(); + Date date = new Date(); + long banTime = date.getTime() + time*86400000; + String timeUntilUnban = Bundle.formatDuration(Duration.ofDays(time)); + Document user = playerCollection.find(Filters.eq("id", id)).first(); + if (user == null) { response = "Could not find that player."; listener.getSlashCommandInteraction() .createImmediateResponder().setContent(response) .respond(); return; } - if (player.admin()){ + Player plr = Groups.player.find(p -> p.uuid().equals(user.getString("uuid"))); + if (user.getInteger("rank") == 2 || user.getInteger("rank") == 1){ listener.getSlashCommandInteraction().createImmediateResponder().setContent("You cant ban an admin!").respond(); return; } - netServer.admins.banPlayerID(player.uuid()); - netServer.admins.banPlayerIP(netServer.admins.getInfo(player.uuid()).lastIP); - player.con.kick(Packets.KickReason.banned); + if (plr == null) { + Log.info("Player is offline, not kicking him"); + } else { + plr.con.kick("You have been banned for: " + reason + ". Wait " + timeUntilUnban + " until unban!", 0); + } listener.getSlashCommandInteraction() - .createImmediateResponder().setContent("Banned: " + PlayerName) + .createImmediateResponder().setContent("Banned: " + user.getString("name")) .respond(); + + Call.sendMessage(user.getString("name") +" has been banned for: " + reason); + Bson updates = Updates.combine( + Updates.set("lastBan", banTime) + ); + Ploogin.playerCollection.updateOne(user, updates, new UpdateOptions().upsert(true)); + EmbedBuilder embed = new EmbedBuilder() + .setTitle("Ban event") + .setDescription(user.getString("name") + " has been banned for: " + reason) + .addField("Moderator", listener.getInteraction().getUser().getName()); + Bot.banchannel.sendMessage(embed); return; } case "exit" -> { @@ -157,8 +203,8 @@ private static void addSlashCommandListener(SlashCommandCreateEvent listener) { return; } case "adminadd" -> { - String PlayerName = listener.getSlashCommandInteraction().getOptionByName("name").get().getStringValue().get(); - Player player = Utilities.findPlayerByName(PlayerName); + String name = listener.getSlashCommandInteraction().getOptionByName("name").get().getStringValue().get(); + Player player = findPlayerByName(name); if (player == null){ listener.getSlashCommandInteraction().createImmediateResponder().setContent("No such player!").respond(); return; } diff --git a/src/main/java/plugin/functions/MongoDB.java b/src/main/java/plugin/functions/MongoDB.java index 2482d01..8ea25dd 100644 --- a/src/main/java/plugin/functions/MongoDB.java +++ b/src/main/java/plugin/functions/MongoDB.java @@ -1,6 +1,7 @@ package plugin.functions; import com.mongodb.client.model.Filters; +import mindustry.gen.Groups; import mindustry.gen.Player; import org.bson.Document; import org.bson.types.ObjectId; @@ -14,6 +15,7 @@ public static void MongoDbPlayerCreation(Player eventPlayer){ Document plrDoc = new Document("_id", id); plrDoc.append("id", (int) playerCollection.countDocuments()); plrDoc.append("uuid", eventPlayer.uuid()); + plrDoc.append("name", eventPlayer.name()); plrDoc.append("rank", 0); plrDoc.append("lastBan", lastBan); Document chk = playerCollection.find(Filters.eq("uuid", eventPlayer.uuid())).first(); @@ -23,9 +25,10 @@ public static void MongoDbPlayerCreation(Player eventPlayer){ return; } } - public static void MongoDbPlayerRankCheck(Player eventPlayer){ - String tempName = eventPlayer.name; - Document user = playerCollection.find(Filters.eq("uuid", eventPlayer.uuid())).first(); + public static void MongoDbPlayerRankCheck(String uuid){ + Player eventPlayer = Groups.player.find(p -> p.uuid().contains(uuid)); + Document user = playerCollection.find(Filters.eq("uuid", uuid)).first(); + String tempName = user.getString("name"); int rank = user.getInteger("rank"); switch (rank){ case 0 ->{ diff --git a/src/main/java/plugin/utils/MenuHandler.java b/src/main/java/plugin/utils/MenuHandler.java index 6ea3a9a..c17bd0b 100644 --- a/src/main/java/plugin/utils/MenuHandler.java +++ b/src/main/java/plugin/utils/MenuHandler.java @@ -23,34 +23,6 @@ public class MenuHandler { public static Player plr = Ploogin.victim; public static Player moderator = Ploogin.moderator; - public static int banMenu = Menus.registerMenu((player, option) -> { - switch (option) { - case -1 -> { - return; - } - case 0 -> { - Document usr = Ploogin.playerCollection.find(Filters.eq("uuid", plr.uuid())).first(); - Date date = new Date(); - long banTime = date.getTime() + Ploogin.time*86400000; - String timeUntilUnban = Bundle.formatDuration(Ploogin.time*86400000); - plr.con.kick("You have been banned for: " + Ploogin.reason + ". Wait " + timeUntilUnban + " until unban!", 0); - Call.sendMessage(plr.plainName() + " has been banned for: " + Ploogin.reason); - Bson updates = Updates.combine( - Updates.set("lastBan", banTime) - ); - Ploogin.playerCollection.updateOne(usr, updates, new UpdateOptions().upsert(true)); - EmbedBuilder embed = new EmbedBuilder() - .setTitle("Ban event") - .setDescription(plr.plainName() + " has been banned for: " + Ploogin.reason) - .setColor(Color.RED) - .addField("Moderator", moderator.plainName()); - Bot.banchannel.sendMessage(embed); - } - case 1 -> { - return; - } - } - }); public static int welcomeMenu = Menus.registerMenu(((player, option) -> { switch (option){ case -1 -> {