Files
src
build.gradle
manifest.kod
Code
6 258
src/main/java/com/discordbot/Main.java
package com.discordbot;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
public class Main {
private static JDA jda;
private static Config config;
public static void main(String[] args) {
System.out.println("Starting Discord Bot...");
config = new Config();
config.load();
String token = config.getBotToken();
if (token == null || token.equals("YOUR_BOT_TOKEN_HERE")) {
System.err.println("ERROR: Please set your bot token in config.yml!");
System.exit(1);
return;
}
try {
jda = JDABuilder.createDefault(token)
.enableIntents(GatewayIntent.GUILD_MESSAGES, GatewayIntent.MESSAGE_CONTENT)
.setActivity(Activity.playing(config.getBotStatus()))
.build();
jda.awaitReady();
SendCommand sendCommand = new SendCommand(config);
jda.addEventListener(sendCommand);
// Register commands (instant if guild-id is set, otherwise up to 1 hour globally)
String guildId = config.getGuildId();
if (guildId != null && !guildId.equals("YOUR_GUILD_ID_HERE")) {
// Guild-specific registration (instant)
jda.getGuildById(guildId).updateCommands()
.addCommands(sendCommand.getCommandData())
.queue(success -> System.out.println("β Successfully registered /send command in your server (instant)!"),
error -> System.err.println("β Failed to register commands: " + error.getMessage()));
} else {
// Global registration (can take up to 1 hour)
jda.updateCommands()
.addCommands(sendCommand.getCommandData())
.queue(success -> System.out.println("β Successfully registered /send command globally (may take up to 1 hour)!"),
error -> System.err.println("β Failed to register commands: " + error.getMessage()));
}
System.out.println("Bot is ready! Use /send <message> in Discord.");
} catch (Exception e) {
System.err.println("Failed to start bot: " + e.getMessage());
e.printStackTrace();
System.exit(1);
}
}
public static JDA getJDA() {
return jda;
}
public static Config getConfig() {
return config;
}
}