Skip to content

Commit

Permalink
обновил чёта
Browse files Browse the repository at this point in the history
  • Loading branch information
NazrinNya committed Jul 19, 2023
1 parent c425930 commit 3ce4eb1
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 27 deletions.
6 changes: 2 additions & 4 deletions src/main/java/plugin/commands/BanMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static mindustry.Vars.net;
import static plugin.ConfigJson.discordurl;
import static plugin.discord.Embed.banEmbed;
import static plugin.functions.MongoDB.MongoDbUpdate;

public class BanMenu {

Expand Down Expand Up @@ -123,10 +124,7 @@ public static void loadBanMenu() {
String timeUntilUnban = Bundle.formatDuration(Duration.ofDays(duration));
target.con.kick("[red]You have been banned!\n\n" + "[white]Reason: " + text +"\nDuration: " + timeUntilUnban + " until unban\nIf you think this is a mistake, make sure to appeal ban in our discord: " + discordurl, 0);
Call.sendMessage(target.plainName() + " has been banned for: " + text);
Bson updates = Updates.combine(
Updates.set("lastBan", banTime)
);
Plugin.plrCollection.updateOne(user, updates, new UpdateOptions().upsert(true));
MongoDbUpdate(user, Updates.set("lastBan", banTime));
Bot.banchannel.sendMessage(banEmbed(user,text,banTime, view.player.plainName()));
});
});
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/plugin/commands/ConsoleCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static plugin.Plugin.plrCollection;
import static plugin.functions.MongoDB.MongoDbPlayerRankCheck;
import static plugin.functions.MongoDB.MongoDbUpdate;

public class ConsoleCommands {
public static void loadServerCommands(CommandHandler handler){
Expand All @@ -40,10 +41,7 @@ public static void loadServerCommands(CommandHandler handler){
Log.warn("This user doesnt exist!");
return;
}
Bson updates = Updates.combine(
Updates.set("rank", rankid)
);
plrCollection.updateOne(user, updates, new UpdateOptions().upsert(true));
MongoDbUpdate(user, Updates.set("rank", rankid));
Log.info("Rank has been given!");
Player player = Groups.player.find(p -> p.uuid().equals(user.getString("uuid")));
if (player == null){
Expand Down
24 changes: 20 additions & 4 deletions src/main/java/plugin/commands/MainCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
import org.bson.Document;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static arc.util.Strings.canParseInt;
import static mindustry.Vars.mods;
import static mindustry.Vars.player;
import static plugin.Plugin.plrCollection;
import static plugin.utils.Checks.isConsole;
import static plugin.utils.Utilities.voteCanceled;
import static plugin.utils.Utilities.voteSuccess;
import static plugin.utils.Utilities.*;

public class MainCommands {
public static AtomicInteger votes = new AtomicInteger(0);
Expand Down Expand Up @@ -67,6 +65,24 @@ public static void loadClientCommands(CommandHandler handler){
player.sendMessage("[scarlet]You must be console to use this command.");
}
});
handler.<Player>register("maps", "<page>","List all maps", (args, player) -> {
StringBuilder list = new StringBuilder();
if (!canParseInt(args[0])){
player.sendMessage("[red]Page must be number!");
return;
}
int mapsPerPage = 10;
int page = Integer.parseInt(args[0]);
Seq<Map> maps = getMaps();
maps.list().stream().skip(page*10L).limit(mapsPerPage + (page * 10L)).forEach(
map -> list.append(map.name() + "[white], by " + map.author())
);
if (!String.valueOf(list).contains("by")){
player.sendMessage("[red]No maps detected!");
return;
}
player.sendMessage(String.valueOf(list));
});
handler.<Player>register("rtv", "<name...>", "Rock the vote to change map!", (args, player) -> {
final int[] votesRequired = new int[1];
AtomicInteger time = new AtomicInteger(60);
Expand Down
48 changes: 34 additions & 14 deletions src/main/java/plugin/discord/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import static plugin.ConfigJson.discordurl;
import static plugin.Plugin.plrCollection;
import static plugin.discord.Embed.banEmbed;
import static plugin.functions.MongoDB.MongoDbUpdate;
import static plugin.utils.Checks.isAdmin;
import static plugin.utils.FindDocument.getDoc;
import static plugin.utils.MenuHandler.loginMenu;
Expand Down Expand Up @@ -96,7 +97,7 @@ private static void onMessageCreate(MessageCreateEvent listener){
}
}
// registers slash commands so user can see them and use
private static void registerSlashCommands(){
private static void registerSlashCommands() {
SlashCommand banCommand = SlashCommand.with("ban", "Bans the player",
Arrays.asList(
SlashCommandOption.create(
Expand All @@ -118,10 +119,10 @@ private static void registerSlashCommands(){
true
)
)
).setDefaultEnabledForPermissions(PermissionType.MODERATE_MEMBERS)
).setDefaultEnabledForPermissions(PermissionType.MODERATE_MEMBERS)
.createGlobal(api).join();
SlashCommand exitCommand = SlashCommand.with("exit", "exits the servar"
).setDefaultEnabledForPermissions(PermissionType.ADMINISTRATOR)
).setDefaultEnabledForPermissions(PermissionType.ADMINISTRATOR)
.createGlobal(api).join();
SlashCommand listCommand = SlashCommand.with("list", "Lists the players"
).createGlobal(api).join();
Expand All @@ -136,7 +137,7 @@ private static void registerSlashCommands(){
).setDefaultEnabledForPermissions(PermissionType.MODERATE_MEMBERS)
.createGlobal(api).join();
SlashCommand gameoverCommand = SlashCommand.with("gameover", "Executes gameover event"
).setDefaultEnabledForPermissions(PermissionType.MODERATE_MEMBERS)
).setDefaultEnabledForPermissions(PermissionType.MODERATE_MEMBERS)
.createGlobal(api).join();
SlashCommand loginCommand = SlashCommand.with("login", "Connects your discord and mindustry account!",
Collections.singletonList(
Expand All @@ -146,7 +147,7 @@ private static void registerSlashCommands(){
"id",
true
))
).createGlobal(api).join();
).createGlobal(api).join();
SlashCommand getInfoCommand = SlashCommand.with("stats", "Gets stats of player",
Arrays.asList(
SlashCommandOption.create(
Expand All @@ -169,6 +170,18 @@ private static void registerSlashCommands(){
true
))
).createGlobal(api).join();

SlashCommand unbanCommand = SlashCommand.with("unban", "Unbans the player",
Collections.singletonList(
SlashCommandOption.create(
SlashCommandOptionType.LONG,
"id",
"id of the player",
true
)
)
).setDefaultEnabledForPermissions(PermissionType.MODERATE_MEMBERS)
.createGlobal(api).join();
}
// calling slash command functions once they got used
private static void addSlashCommandListener(SlashCommandCreateEvent listener) {
Expand Down Expand Up @@ -196,10 +209,6 @@ private static void addSlashCommandListener(SlashCommandCreateEvent listener) {
return;
}
Player plr = Groups.player.find(p -> p.uuid().equals(user.getString("uuid")));
if (isAdmin(id)) {
listener.getSlashCommandInteraction().createImmediateResponder().setContent("You cant ban an admin!").respond();
return;
}
if (plr == null) {
Log.info("Player is offline, not kicking him");
} else {
Expand All @@ -210,10 +219,7 @@ private static void addSlashCommandListener(SlashCommandCreateEvent listener) {
.respond();

Call.sendMessage(user.getString("name") + " has been banned for: " + reason);
Bson updates = Updates.combine(
Updates.set("lastBan", banTime)
);
Plugin.plrCollection.updateOne(user, updates, new UpdateOptions().upsert(true));
MongoDbUpdate(user, Updates.set("lastBan", banTime));
Bot.banchannel.sendMessage(banEmbed(user, reason, banTime, listener.getInteraction().getUser().getName()));
return;
}
Expand Down Expand Up @@ -266,7 +272,7 @@ private static void addSlashCommandListener(SlashCommandCreateEvent listener) {
}
Player player = Groups.player.find(p -> p.uuid().equals(user.getString("uuid")));
if (player == null){
listener.getSlashCommandInteraction().createImmediateResponder().setContent("This player doesnt exists or offline!").respond();
listener.getSlashCommandInteraction().createImmediateResponder().setContent("This player is offline!").respond();
return;
}
loginMenuFunction(listener);
Expand Down Expand Up @@ -311,6 +317,20 @@ private static void addSlashCommandListener(SlashCommandCreateEvent listener) {
list.append("```");
listener.getSlashCommandInteraction().createImmediateResponder().setContent(String.valueOf(list)).respond();
}
case "unban" -> {
int id = Math.toIntExact(listener.getSlashCommandInteraction().getOptionByName("id").get().getLongValue().get());
Document user = getDoc(id);
if (user == null){
listener.getSlashCommandInteraction().createImmediateResponder().setContent("Could not find that player!").respond();
return;
}
if (user.getLong("lastBan") == 0L){
listener.getSlashCommandInteraction().createImmediateResponder().setContent("User is not banned!").respond();
return;
}
MongoDbUpdate(user, Updates.set("lastBan", 0L));
listener.getSlashCommandInteraction().createImmediateResponder().setContent(user.getString("name") + " has been unbanned!").respond();
}
}
}
}
1 change: 1 addition & 0 deletions src/main/java/plugin/discord/Embed.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static EmbedBuilder banEmbed(Document user, String reason, long banTime,
return new EmbedBuilder()
.setTitle("Ban event")
.setColor(Color.RED)
.addField("**ID**", String.valueOf(user.getInteger("id")))
.addField("**Name**", stripColors(user.getString("name")))
.addField("**Reason**", reason)
.addField("**Expires**", "<t:" + banTime/1000 +":D>")
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/plugin/functions/MongoDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;

import java.util.Arrays;

import static plugin.Plugin.plrCollection;
import static plugin.utils.FindDocument.getDoc;

Expand Down Expand Up @@ -59,7 +61,13 @@ public static void MongoDbPlayerNameCheck(Player player){
Updates.set("name", player.plainName()),
Updates.set("rawName", player.name())
);
plrCollection.updateOne(user, updates, new UpdateOptions().upsert(true));
MongoDbUpdate(user, Updates.set("name", player.plainName()), Updates.set("rawName", player.name()));
}
}
public static <T> void MongoDbUpdate(Document user, Bson... updates){
Bson update = Updates.combine(
updates
);
plrCollection.updateOne(user, update, new UpdateOptions().upsert(true));
}
}
8 changes: 8 additions & 0 deletions src/main/java/plugin/utils/Utilities.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package plugin.utils;
import arc.struct.Seq;
import mindustry.Vars;
import mindustry.gen.Call;
import mindustry.gen.Groups;
Expand Down Expand Up @@ -42,5 +43,12 @@ public static void voteSuccess(Map map){
votes.set(0);
votedPlayer.clear();
}
public static Seq<Map> getMaps(){
Seq<Map> maps = new Seq<>();
for(Map map : Vars.maps.customMaps()){
maps.add(map);
}
return maps;
}
}

0 comments on commit 3ce4eb1

Please sign in to comment.