package com.alttd.webinterface.bot;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.requests.GatewayIntent;

@Slf4j
public class DiscordBotInstance {

    @Getter
    private JDA jda;
    private volatile boolean ready = false;

    public synchronized void start(String token) {
        if (jda != null) {
            return;
        }
        jda = JDABuilder.createDefault(token,
                GatewayIntent.GUILD_MEMBERS,
                GatewayIntent.GUILD_PRESENCES,
                GatewayIntent.GUILD_MESSAGES,
                GatewayIntent.MESSAGE_CONTENT)
                .addEventListeners(new ReadyListener(() -> {
                    ready = true;
                }))
                .build();
    }

    public boolean isReady() {
        return ready && jda != null && jda.getStatus() == JDA.Status.CONNECTED;
    }
}
