Skip to content

Commit

Permalink
завтра починю
Browse files Browse the repository at this point in the history
  • Loading branch information
NazrinNya committed Jul 5, 2023
1 parent 5a40102 commit 915219f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 60 deletions.
15 changes: 10 additions & 5 deletions src/main/java/plugin/Ploogin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -97,8 +99,10 @@ public void registerClientCommands(CommandHandler handler){
}
player.sendMessage(String.valueOf(list));
});
/*handler.<Player>register("js", "<code...>", "Execute JavaScript code.", (args, player) -> {
if (player.admin()) {
handler.<Player>register("js", "<code...>", "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));
Expand All @@ -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
Expand All @@ -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"));
});
}
}
94 changes: 70 additions & 24 deletions src/main/java/plugin/discord/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,28 +16,40 @@
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
public static DiscordApi api;
public static TextChannel channel;
public static TextChannel banchannel;
// main bot
@SuppressWarnings("UnnecessaryLocalVariable")
public static void load(){
api = new DiscordApiBuilder()
.setToken(ConfigJson.token)
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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" -> {
Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/plugin/functions/MongoDB.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand All @@ -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 ->{
Expand Down
28 changes: 0 additions & 28 deletions src/main/java/plugin/utils/MenuHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down

0 comments on commit 915219f

Please sign in to comment.