Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move permissions to config #92

Merged
merged 5 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,382 changes: 1,191 additions & 1,191 deletions .editorconfig

Large diffs are not rendered by default.

Empty file modified gradlew
100644 → 100755
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.diamondfire.helpbot.bot.config.Config;
import com.diamondfire.helpbot.bot.events.*;
import com.diamondfire.helpbot.sys.tasks.TaskRegistry;
import com.google.gson.Gson;
import net.dv8tion.jda.api.*;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
Expand All @@ -34,7 +35,8 @@ public class HelpBotInstance {
private static final Config config = new Config();
public static final long DF_GUILD = config.getGuild();
public static final long LOG_CHANNEL = config.getLogChannel();
public static final OkHttpClient HTTP_CLIENT = new OkHttpClient();

public static final Gson GSON = new Gson();

private static JDA jda;
private static final TaskRegistry loop = new TaskRegistry();
Expand Down Expand Up @@ -148,10 +150,6 @@ public static JDA getJda() {
return jda;
}

public static OkHttpClient getHttpClient() {
return HTTP_CLIENT;
}

public static Config getConfig() {
return config;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package com.diamondfire.helpbot.bot.command.argument.impl.types.minecraft;

import com.diamondfire.helpbot.bot.HelpBotInstance;
import com.diamondfire.helpbot.bot.command.argument.impl.parsing.exceptions.ArgumentException;
import com.diamondfire.helpbot.bot.command.argument.impl.types.AbstractSimpleValueArgument;
import com.diamondfire.helpbot.bot.events.CommandEvent;
import com.diamondfire.helpbot.util.Util;
import com.diamondfire.helpbot.util.*;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import okhttp3.Request;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
Expand All @@ -23,16 +19,9 @@ protected UUID parse(@NotNull String argument, CommandEvent event) throws Argume
return Util.toUuid(argument);
} else {
JsonObject responseObject = null;
ResponseBody res = null;
Request request = new Request.Builder().url("https://api.mojang.com/users/profiles/minecraft/" + argument).get().build();
try {
res = HelpBotInstance.getHttpClient().newCall(request).execute().body();
} catch (IOException e) {
e.printStackTrace();
}

try {
responseObject = JsonParser.parseString(res.string()).getAsJsonObject();
responseObject = WebUtil.getJson("https://api.mojang.com/users/profiles/minecraft/" + argument).getAsJsonObject();
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package com.diamondfire.helpbot.bot.command.permissions;

import com.diamondfire.helpbot.bot.HelpBotInstance;
import com.diamondfire.helpbot.bot.command.impl.Command;
import net.dv8tion.jda.api.entities.Member;

import java.util.*;
import java.util.stream.Collectors;

public enum Permission {
BOT_DEVELOPER(589238520145510400L, 999),
DEVELOPER(519740942861860874L, 999),
BOT_DEVELOPER(999),
DEVELOPER(999),
// Ask DragonSlasher, not me.
ADMINISTRATOR(180794313494495233L, 666),
MODERATION(180794061429538816L, 5),
SR_HELPER(299109861696995329L, 4),
SUPPORT(180794530398732288L, 3),
RETIRED_SUPPORT(235159617108181003L, 2),
USER(349434193517740033L, 1);
ADMINISTRATOR(666),
MODERATION(5),
SR_HELPER(4),
SUPPORT(3),
RETIRED_SUPPORT(2),
USER(1);

private static final HashMap<Long, Permission> roleMap = new HashMap<>();
private static final HashMap<Command, Set<Long>> overrides = new HashMap<>();
Expand All @@ -29,8 +30,8 @@ public enum Permission {
private final long role;
private final int permissionLevel;

Permission(long roleID, int permissionLevel) {
this.role = roleID;
Permission(int permissionLevel) {
this.role = HelpBotInstance.getConfig().getPermissionRoleMap().get(this.name());
this.permissionLevel = permissionLevel;
}

Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/diamondfire/helpbot/bot/config/Config.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.diamondfire.helpbot.bot.config;

import com.diamondfire.helpbot.bot.HelpBotInstance;
import com.diamondfire.helpbot.sys.externalfile.ExternalFiles;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;

import java.io.*;
import java.nio.file.Files;
import java.util.Map;
import java.util.stream.Collectors;

Expand All @@ -12,9 +15,8 @@ public class Config {
private final JsonObject config;

public Config() throws IllegalStateException {
try (BufferedReader txtReader2 = new BufferedReader(new FileReader(ExternalFiles.CONFIG.getPath()))) {
String config = txtReader2.lines().collect(Collectors.joining());
this.config = JsonParser.parseString(config).getAsJsonObject();
try {
this.config = HelpBotInstance.GSON.fromJson(Files.readString(ExternalFiles.CONFIG.toPath()), JsonObject.class);
tk2217 marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception exception) {
throw new IllegalStateException("Config not correctly structured! Please check the readme file for a config template.");
}
Expand Down Expand Up @@ -89,12 +91,15 @@ public JsonObject getForwardingChannels() {
return config.get("forwarding_channels").getAsJsonObject();
}

public Map<String, Long> getPermissionRoleMap() {
return HelpBotInstance.GSON.fromJson(config.get("permission_roles"), new TypeToken<Map<String, Long>>(){}.getType());
}

private long getPropertyLong(String property) {
return config.get(property).getAsLong();
}

private String getPropertyString(String property) {
return config.get(property).getAsString();
}

tk2217 marked this conversation as resolved.
Show resolved Hide resolved
}