Untitled

 avatar
unknown
javascript
a year ago
5.1 kB
10
Indexable
let Color = Java.type("java.awt.Color");
let Level = Java.type("java.util.logging.Level");

let Bukkit = Java.type("org.bukkit.Bukkit");
let PlayerJoinEvent = Java.type("org.bukkit.event.player.PlayerJoinEvent");

let EmbedBuilder = Java.type("github.scarsz.discordsrv.dependencies.jda.api.EmbedBuilder");

class Action {
    constructor(type, value, author) {
        this.type = type;
        this.value = value;
        this.author = author;
    }
}

class Data {
    static getJda() {
        return Bukkit.getPluginManager().getPlugin("DiscordSRV").getJda();
    }

    static isStaff(player) {
        return this.ranks.some(rank => API.hasRank(player, rank));
    }

    update() {
        let messages = this.info.channel.getIterableHistory().cache(false);
        let iterator = messages.iterator();

        while (iterator.hasNext()) {
            let message = iterator.next();
            let content = message.getContentDisplay();

            if (content.startsWith("> "))
                continue;

            let splited = content.split(" ");

            if (splited.length < 3)
                continue;

            let action = new Action(splited[1], splited[2], message.getAuthor().getName());
            
            this.actions.push(action);

            if (this.deleteAfterUpdate.includes(action.type))
                message.delete().queue();
        }

        for (let action in this.actions) {
            switch (action.type) {
                case "STAFF":
                    this.staffs.add(action.value);
                    break;

                case "SCRIPT":
                    try {
                        eval(action.value);
                        this.log("Se ejecutó correctamente un script desde discord. \n\n STAFF: \n" + action.author + "\n\n SCRIPT: \n" + action.value, Level.INFO);
                    } catch (e) {
                        this.log("Ocurrió un error al ejecutar un script desde discord. \n\n STAFF: \n" + action.author + "\n\n SCRIPT: \n" + action.value + "\n\n ERROR: \n" + e, Level.WARNING);
                    }

                    break;

                case "COMMAND":
                    API.executeCommand(action.value);
                    this.log("Se ejecutó correctamente un comando desde discord. \n\n STAFF: \n" + action.author + "\n\n COMANDO: \n" + action.value, Level.INFO);
                    break;
            }
        }

        this.actions = [];
    }

    log(log, level) {
        this.console.channel.sendMessageEmbeds(new EmbedBuilder()
            .setTitle("SERVER GUARD - " + level.getName())
            .addField("**LOG:**", log, false)
            .setColor(new Color(75, 75, 75))
            .setThumbnail("https://imgur.com/jWGuIaO.png")
            .build()).queue();
    }  
    
    isRegistered(name) {
        return this.staffs.has(name);
    }

    constructor(id, ranks, deleteAfterUpdate, info, console) {
        this.id = id;

        this.ranks = ranks;
        this.staffs = new Set();

        this.deleteAfterUpdate = deleteAfterUpdate;
        this.actions = [];

        this.info = info;
        this.console = console;
    }
}

class Guild {
    constructor(id) {
        this.id = id;
        this.guild = Data.getJda().getGuildById(id);
    }
}

class Channel {
    constructor(id, guild) {
        this.id = id;
        this.channel = guild.guild.getTextChannelById(id);
    }
}

function onInit() {
    const GUILD = new Guild("353676760006524928");
    const INFO = new Channel("1187154612034215986", GUILD);
    const CONSOLE = new Channel("1133524788564992130", GUILD);

    const DATA = new Data("SERVER-GUARD", [
        "Dueño", 
        "Admin",
        "SoporteDeCalidad", 
        "Decorador",
        "Diseñador", 
        "Programador",
        "ProgramadorBeta", 
        "Moderador",
        "ConstructorPlus", 
        "Constructor",
        "ConstructorBeta", 
        "CreadorDeContenidoPlus",
        "CreadorDeContenido", 
        "Ayudante",
        "AyudanteBeta"
    ], ["COMMAND, SCRIPT"], INFO, CONSOLE);

    DATA.update();

    if (API.containsTempData(DATA.id)) 
        API.getTempData(DATA.id).task.cancel();

    API.setTempData(DATA.id, {
        task: API.runRepeatingTask(function() {
            DATA.update();
        }, 600)
    });

    API.registerListener(PlayerJoinEvent.class, function(event) {
        let player = event.getPlayer();
        let name = player.getName();

        if (!Data.isStaff(player) || DATA.isRegistered(name))
            return;

        player.kickPlayer("Server Guard: Staff no registrado como staff!");
        DATA.log("Un staff que no está registrado como staff intentó ingresar al servidor. \n\n **NICK:** \n" + name, Level.WARNING);
    });

    DATA.log("Server Guard iniciado correctamente. \n\n **STAFFS ENCONTRADOS:** \n" + Array.from(DATA.staffs).join(", ") + ".", Level.INFO);
}
Editor is loading...
Leave a Comment