Skip to content

Commit

Permalink
АААААААААААААААААА
Browse files Browse the repository at this point in the history
  • Loading branch information
NazrinNya committed Aug 8, 2023
1 parent 3cba629 commit 398eee4
Show file tree
Hide file tree
Showing 10 changed files with 372 additions and 89 deletions.
2 changes: 2 additions & 0 deletions src/main/java/plugin/ConfigJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ConfigJson {
public static String discordurl;
public static String moderatorid;
public static String adminid;
public static String prefix;
// reads variables from config.json
public static void read() throws IOException, ParseException {
try {
Expand All @@ -31,6 +32,7 @@ public static void read() throws IOException, ParseException {
discordurl = (String) object.get("discordurl");
moderatorid = (String) object.get("moderatorid");
adminid= (String) object.get("adminid");
prefix = (String) object.get("prefix");
}catch(Exception e){
Log.err("Skill issue: " + e);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/plugin/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void init() {
MongoDbPlayerRankCheck(plr.uuid());
});
MongoDbPlaytimeTimer();
MongoDbCheck();
Events.on(EventType.PlayerConnect.class, event -> kickIfBanned(event.player));
Events.on(EventType.PlayerChatEvent.class, event ->{
if (isVoting){
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/plugin/commands/ConsoleCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@

import java.util.ArrayList;

import static arc.util.Strings.canParseInt;
import static arc.util.Strings.parseInt;
import static plugin.Plugin.plrCollection;
import static plugin.functions.MongoDB.MongoDbPlayerRankCheck;
import static plugin.functions.MongoDB.MongoDbUpdate;
import static plugin.utils.FindDocument.getDoc;
import static plugin.utils.FindDocument.getDocAnyway;

public class ConsoleCommands {
public static void loadServerCommands(CommandHandler handler){
Expand All @@ -29,15 +32,12 @@ public static void loadServerCommands(CommandHandler handler){
}, 1f);
});
handler.register("setrank", "<id> <rank>", "Sets rank to player", (args, params) -> {
int id = 0;
try {
id = Integer.parseInt(args[0]);
} catch (NumberFormatException e){
Log.warn("Please, type an ID");
Document user = getDocAnyway(args[0]);
if (!canParseInt(args[1])){
Log.warn("Type a number!");
return;
}
int rankid = Integer.parseInt(args[1]);
Document user = plrCollection.find(Filters.eq("id", id)).first();
int rankid = parseInt(args[1]);
if (user == null){
Log.warn("No such player!");
return;
Expand Down
337 changes: 286 additions & 51 deletions src/main/java/plugin/discord/Bot.java

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions src/main/java/plugin/discord/DiscordFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.javacord.api.entity.permission.Role;
import org.javacord.api.entity.user.User;
import org.javacord.api.event.interaction.SlashCommandCreateEvent;
import org.javacord.api.event.message.MessageCreateEvent;

import java.util.List;
import java.util.Optional;
Expand All @@ -12,27 +13,27 @@
import static plugin.discord.Embed.noRoleEmbed;

public class DiscordFunctions {
public static boolean isModerator(SlashCommandCreateEvent listener){
User user = listener.getInteraction().getUser();
org.javacord.api.entity.server.Server server = listener.getInteraction().getServer().get();
public static boolean isModerator(MessageCreateEvent listener){
User user = listener.getMessageAuthor().asUser().get();
org.javacord.api.entity.server.Server server = listener.getServer().get();
Optional<Role> moderatorRole = server.getRoleById(moderatorid);
List<Role> roles = user.getRoles(server);
if (roles.contains(moderatorRole.get())){
return true;
} else {
listener.getSlashCommandInteraction().createImmediateResponder().addEmbed(noRoleEmbed(moderatorRole)).respond();
listener.getChannel().sendMessage(noRoleEmbed(moderatorRole));
return false;
}
}
public static boolean isAdmin(SlashCommandCreateEvent listener){
User user = listener.getInteraction().getUser();
org.javacord.api.entity.server.Server server = listener.getInteraction().getServer().get();
public static boolean isAdmin(MessageCreateEvent listener){
User user = listener.getMessageAuthor().asUser().get();
org.javacord.api.entity.server.Server server = listener.getServer().get();
Optional<Role> adminRole = server.getRoleById(adminid);
List<Role> roles = user.getRoles(server);
if (roles.contains(adminRole.get())){
return true;
} else {
listener.getSlashCommandInteraction().createImmediateResponder().addEmbed(noRoleEmbed(adminRole)).respond();
listener.getChannel().sendMessage(noRoleEmbed(adminRole));
return false;
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/plugin/etc/Ranks.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,24 @@ public class Ranks{
public static String adminRank = "[red]Administrator";
public static String consoleRank = "[purple]Console";
public static String ownerRank = "[cyan]Owner";
public static String rankName(int rankid){
switch (rankid){
case 0 -> {
return playerRank;
}
case 1 -> {
return trustedRank;
}
case 2 -> {
return adminRank;
}
case 3 -> {
return consoleRank;
}
case 4 -> {
return ownerRank;
}
}
return playerRank;
}
}
15 changes: 15 additions & 0 deletions src/main/java/plugin/functions/MongoDB.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package plugin.functions;

import arc.util.Log;
import arc.util.Timer;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.Updates;
Expand All @@ -11,6 +13,7 @@
import org.bson.types.ObjectId;

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

import static plugin.Plugin.plrCollection;
import static plugin.utils.FindDocument.getDoc;
Expand Down Expand Up @@ -84,4 +87,16 @@ public static void MongoDbPlaytimeTimer(){
}
}, 0, 60);
}
public static void MongoDbCheck(){
AtomicInteger totalUpdates = new AtomicInteger(0);
try (MongoCursor<Document> cursor = plrCollection.find().iterator()) {
while (cursor.hasNext()) {
Document csr = cursor.next();
int i = (int) csr.getOrDefault("playtime", 0);
MongoDbUpdate(csr, Updates.set("playtime", i));
totalUpdates.getAndAdd(1);
}
}
Log.info(totalUpdates + " Documents has been fetched/updated!");
}
}
36 changes: 17 additions & 19 deletions src/main/java/plugin/functions/Other.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package plugin.functions;

import arc.Core;
import arc.struct.Seq;
import com.mongodb.client.model.Filters;
import mindustry.gen.Call;
import mindustry.gen.Player;
import org.bson.Document;
import plugin.utils.MenuHandler;
import useful.Bundle;

import java.lang.reflect.Array;
import java.time.Duration;
import java.util.Arrays;
import java.util.Date;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -48,31 +51,26 @@ public static void kickIfBanned(Player player){
}
public static void statsMenu(Player player, Player reqPlayer){
Document user = getDoc(reqPlayer.uuid());
String rank = playerRank;
switch (user.getInteger("rank")){
case 0 -> {
rank = playerRank;
}
case 1 -> {
rank = trustedRank;
}
case 2 -> {
rank = adminRank;
}
case 3 -> {
rank = consoleRank;
}
case 4 -> {
rank = ownerRank;
}
}
String rank = rankName(user.getInteger("rank"));
String title = "\uE86B Stats";
long playtime = Long.parseLong((String) user.getOrDefault("playtime", 0));
long playtime = Long.parseLong(String.valueOf(user.getInteger("playtime")));
String description = "[orange]Name: " + reqPlayer.name()
+ "\n[orange]ID: [white]" + user.getInteger("id")
+ "\n[orange]Rank: " + rank
+ "\n\n[orange]Playtime: [white]" + Bundle.formatDuration(Duration.ofMinutes(playtime));
String button = "[red]Close";
Call.menu(player.con, MenuHandler.statsMenu, title, description, new String[][]{{button}});
}
public static String readValueFromArraySeparated(String[] array, int startPoint, int endPoint){
String[] newArray = Arrays.copyOfRange(array, startPoint, endPoint);
StringBuilder ha = new StringBuilder();
for (String element : newArray){
ha.append(element).append(" ");
}
String a = String.valueOf(ha);
return a.substring(0, a.length()-1);
}
public static <T> boolean inBounds(T[] array, int index){
return (index >= 0) && (index < array.length);
}
}
10 changes: 10 additions & 0 deletions src/main/java/plugin/utils/FindDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import java.util.regex.Pattern;

import static arc.util.Strings.canParseInt;
import static arc.util.Strings.parseInt;
import static com.mongodb.client.model.Filters.eq;
import static plugin.Plugin.plrCollection;
import static plugin.utils.Utilities.notNullElse;
Expand All @@ -21,4 +23,12 @@ public static Document getDoc(String uuidOrName) {
return notNullElse(plrCollection.find(Filters.eq("uuid", uuidOrName)).first(), plrCollection.find(Filters.eq("name", uuidOrName)).first());
}
}
public static Document getDocAnyway(String uuidOrNameOrID){
if (canParseInt(uuidOrNameOrID)){
int i = parseInt(uuidOrNameOrID);
return plrCollection.find(Filters.eq("id", i)).first();
} else {
return getDoc(uuidOrNameOrID);
}
};
}
9 changes: 5 additions & 4 deletions src/main/java/plugin/utils/MenuHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bson.Document;
import org.bson.conversions.Bson;
import org.javacord.api.event.interaction.SlashCommandCreateEvent;
import org.javacord.api.event.message.MessageCreateEvent;
import plugin.Plugin;

import static plugin.ConfigJson.discordurl;
Expand All @@ -34,14 +35,14 @@ public class MenuHandler {
}
}));
public static int loginMenu;
public static void loginMenuFunction(SlashCommandCreateEvent listener){
public static void loginMenuFunction(MessageCreateEvent listener){
loginMenu = Menus.registerMenu(((player, option) -> {
switch (option){
case -1 -> {
return;
}
case 0 -> {
long discordId = listener.getInteraction().getUser().getId();
long discordId = listener.getMessageAuthor().getId();
Document user = getDoc(player.uuid());
Bson updates;
if (user.getInteger("rank") == 0){
Expand All @@ -55,8 +56,8 @@ public static void loginMenuFunction(SlashCommandCreateEvent listener){
);
}
Plugin.plrCollection.updateOne(user, updates, new UpdateOptions().upsert(true));
player.sendMessage("[blue]Successfully connected your discord: " + listener.getInteraction().getUser().getName());
listener.getSlashCommandInteraction().createImmediateResponder().setContent("Successfully connected your mindustry account!").respond();
player.sendMessage("[blue]Successfully connected your discord: " + listener.getMessageAuthor().getName());
listener.getChannel().sendMessage("Successfully connected your mindustry account!");
MongoDbPlayerRankCheck(user.getString("uuid"));
}
case 1 -> {
Expand Down

0 comments on commit 398eee4

Please sign in to comment.