HidekoBot/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java
Beatrice DellacĂ  8b9ce25684 Add shutdown interrupt signal listener
This way, we can nicely close the API connection and perform general cleanup.
2022-11-20 03:17:37 +01:00

114 lines
3.9 KiB
Java

package wtf.beatrice.hidekobot;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
import sun.misc.Signal;
import wtf.beatrice.hidekobot.listeners.MessageListener;
import wtf.beatrice.hidekobot.listeners.SlashCommandListener;
import wtf.beatrice.hidekobot.utils.Logger;
import wtf.beatrice.hidekobot.utils.SlashCommandsUtil;
import javax.security.auth.login.LoginException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class HidekoBot
{
private static String botToken;
private static String standardInviteLink =
"https://discord.com/oauth2/authorize?client_id=%userid%&scope=bot+applications.commands&permissions=8";
private static String botUserId;
private static final String version = "0.0.1"; // we should probably find a way to make this consistent with Maven
private static JDA jda;
// create a logger instance for ease of use
private static final Logger logger = new Logger(HidekoBot.class);
public static void main(String[] args)
{
// check if bot token was specified as a startup argument
if(args.length < 1)
{
logger.log("Please specify your bot token!");
return;
}
// load token from args
botToken = args[0];
try
{
// try to create the bot object and authenticate it with discord.
JDABuilder jdaBuilder = JDABuilder.createDefault(botToken);
// enable necessary intents.
jdaBuilder.enableIntents(
GatewayIntent.MESSAGE_CONTENT,
GatewayIntent.DIRECT_MESSAGES,
GatewayIntent.GUILD_MESSAGES
);
jda = jdaBuilder.build().awaitReady();
} catch (LoginException | InterruptedException e)
{
logger.log(e.getMessage()); // print the error message, omit the stack trace.
return; // if we failed connecting and authenticating, then quit.
}
// find the bot's user id and generate an invite-link.
botUserId = jda.getSelfUser().getId();
standardInviteLink = standardInviteLink.replace("%userid%", botUserId);
// register listeners
jda.addEventListener(new MessageListener());
jda.addEventListener(new SlashCommandListener());
// update slash commands (delayed)
Executors.newSingleThreadScheduledExecutor().schedule(SlashCommandsUtil::updateSlashCommands, 5, TimeUnit.SECONDS);
// set the bot's status
jda.getPresence().setStatus(OnlineStatus.ONLINE);
jda.getPresence().setActivity(Activity.playing("Hatsune Miku: Project DIVA"));
// if there is more than 1 arg, then iterate through them because we have additional things to do.
// we are doing this at the end because we might need the API to be already initialized for some things.
if(args.length > 1) {
List<String> argsList = new ArrayList<>(Arrays.asList(args).subList(1, args.length));
if(argsList.contains("verbose")) Configuration.setVerbose(true);
}
// print the bot logo.
logger.log("\n\n" + logger.getLogo() + "\nv" + version + " - bot is ready!\n", 2);
// register shutdown interrupt signal listener for proper shutdown.
Signal.handle(new Signal("INT"), signal -> shutdown());
// log the invite-link to console so noob users can just click on it.
logger.log("Bot User ID: " + botUserId, 3);
logger.log("Invite Link: " + standardInviteLink, 4);
}
public static JDA getAPI()
{
return jda;
}
public static void shutdown()
{
logger.log("WARNING! Shutting down!");
jda.shutdown();
System.exit(0);
}
}