Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.aqupd.fabricdswl;
- import com.aqupd.fabricdswl.utils.AqConfig;
- import net.dv8tion.jda.api.JDA;
- import net.dv8tion.jda.api.JDABuilder;
- import net.dv8tion.jda.api.entities.Message;
- import net.dv8tion.jda.api.entities.MessageChannel;
- import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
- import net.dv8tion.jda.api.hooks.ListenerAdapter;
- import net.fabricmc.api.DedicatedServerModInitializer;
- import org.apache.commons.io.IOUtils;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import javax.security.auth.login.LoginException;
- import java.io.*;
- import java.net.URL;
- import java.nio.charset.StandardCharsets;
- import java.util.Objects;
- import static com.aqupd.fabricdswl.utils.AqLogger.*;
- public class Main implements DedicatedServerModInitializer {
- String token = AqConfig.INSTANCE.getStringProperty("bot.token");
- @Override
- public void onInitializeServer() {
- try {
- JDA api = JDABuilder.createDefault(token).build();
- api.awaitReady();
- api.addEventListener(new MyListener());
- } catch (InterruptedException | LoginException e) {
- e.printStackTrace();
- }
- logInfo("mod initialized");
- }
- public static class MyListener extends ListenerAdapter {
- @Override
- public void onMessageReceived(MessageReceivedEvent event) {
- String chid = AqConfig.INSTANCE.getStringProperty("channel.id");
- if (event.getAuthor().isBot()) return;
- // We don't want to respond to other bot accounts, including ourself
- Message message = event.getMessage();
- String content = message.getContentRaw();
- MessageChannel channel = event.getChannel();
- if(channel.getId().equals(chid)) {
- try {
- File file = new File("whitelist.json");
- if(file.exists()) {
- InputStream is = new FileInputStream("whitelist.json");
- String jsonTxt = IOUtils.toString(is, StandardCharsets.UTF_8);
- JSONArray array = new JSONArray(jsonTxt);
- String url = "https://api.mojang.com/users/profiles/minecraft/"+content;
- String UUIDJson = IOUtils.toString(new URL(url));
- if(UUIDJson.isEmpty()) channel.sendMessage("Nope(already or not exist)").queue();
- JSONObject uuid = new JSONObject(UUIDJson);
- uuid.put("uuid", insertDashUUID(uuid.getString("id")));
- uuid.remove("id");
- logInfo(uuid.toString());
- boolean whitelisted = false;
- for(int i=0; i < array.length(); i++){
- if(Objects.equals(array.getJSONObject(i).toString(), uuid.toString())){
- logInfo("already whitelisted");
- whitelisted = true;
- }
- }
- if(!whitelisted){
- array.put(uuid);
- logInfo(array.toString());
- }
- var writer = new FileWriter(file);
- writer.write(array.toString());
- writer.flush();
- writer.close();
- channel.sendMessage("Whitelisted!").queue();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public static String insertDashUUID(String uuid) {
- StringBuilder sb = new StringBuilder(uuid);
- sb.insert(8, "-");
- sb = new StringBuilder(sb.toString());
- sb.insert(13, "-");
- sb = new StringBuilder(sb.toString());
- sb.insert(18, "-");
- sb = new StringBuilder(sb.toString());
- sb.insert(23, "-");
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement