Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.2 kB
0
Indexable
Never
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import javax.security.auth.login.LoginException;
import java.util.Scanner;

public class MinecraftBot extends ListenerAdapter {

    private static final String BOT_TOKEN = "YOUR_DISCORD_BOT_TOKEN";
    private static final String CHANNEL_ID = "YOUR_DISCORD_CHANNEL_ID";

    public static void main(String[] args) {
        try {
            JDABuilder.createDefault(BOT_TOKEN)
                    .addEventListeners(new MinecraftBot())
                    .build();
        } catch (LoginException e) {
            e.printStackTrace();
        }
    }

    private void sendNotification(String content) {
        try {
            JDABuilder.createDefault(BOT_TOKEN).build().getTextChannelById(CHANNEL_ID).sendMessage(content).queue();
        } catch (LoginException e) {
            e.printStackTrace();
        }
    }

    // Method to manually input the Minecraft server IP
    private String getMinecraftServerIP() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the Minecraft server IP: ");
        return scanner.nextLine();
    }

    // Event handler for when the Minecraft server starts
    @Override
    public void onMinecraftServerStart() {
        String serverIP = getMinecraftServerIP();
        sendNotification("The Minecraft server at IP " + serverIP + " has started.");
    }

    // Event handler for when a player joins the Minecraft server
    @Override
    public void onMinecraftJoin(String playerName, String uuid, TextChannel textChannel) {
        String serverIP = getMinecraftServerIP();
        sendNotification("Player " + playerName + " has joined the server at IP " + serverIP + ".");
    }

    // Event handler for when a player leaves the Minecraft server
    @Override
    public void onMinecraftLeave(String playerName, String uuid, TextChannel textChannel) {
        String serverIP = getMinecraftServerIP();
        sendNotification("Player " + playerName + " has left the server at IP " + serverIP + ".");
    }
}