2022-08-25 21:51:00 +02:00
|
|
|
package wtf.beatrice.hidekobot;
|
|
|
|
|
|
|
|
import net.dv8tion.jda.api.JDA;
|
|
|
|
import net.dv8tion.jda.api.JDABuilder;
|
2022-08-26 00:39:55 +02:00
|
|
|
import net.dv8tion.jda.api.OnlineStatus;
|
2022-08-25 22:18:36 +02:00
|
|
|
import net.dv8tion.jda.api.entities.Activity;
|
2022-08-25 23:20:51 +02:00
|
|
|
import net.dv8tion.jda.api.requests.GatewayIntent;
|
2022-11-20 03:17:37 +01:00
|
|
|
import sun.misc.Signal;
|
2022-11-21 00:14:13 +01:00
|
|
|
import wtf.beatrice.hidekobot.database.DatabaseManager;
|
2022-11-20 22:09:58 +01:00
|
|
|
import wtf.beatrice.hidekobot.listeners.ButtonInteractionListener;
|
2022-08-25 23:20:51 +02:00
|
|
|
import wtf.beatrice.hidekobot.listeners.MessageListener;
|
2022-11-20 18:54:13 +01:00
|
|
|
import wtf.beatrice.hidekobot.listeners.SlashCommandCompleter;
|
2022-11-20 03:01:46 +01:00
|
|
|
import wtf.beatrice.hidekobot.listeners.SlashCommandListener;
|
2022-11-21 00:14:13 +01:00
|
|
|
import wtf.beatrice.hidekobot.utils.ExpiredMessageRunner;
|
2022-08-25 22:13:39 +02:00
|
|
|
import wtf.beatrice.hidekobot.utils.Logger;
|
2022-11-20 03:01:46 +01:00
|
|
|
import wtf.beatrice.hidekobot.utils.SlashCommandsUtil;
|
2022-08-25 21:51:00 +02:00
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
import java.io.File;
|
2022-08-26 00:13:31 +02:00
|
|
|
import java.util.ArrayList;
|
2022-08-26 20:43:49 +02:00
|
|
|
import java.util.Arrays;
|
2022-08-26 00:13:31 +02:00
|
|
|
import java.util.List;
|
2022-11-20 03:17:37 +01:00
|
|
|
import java.util.concurrent.Executors;
|
2022-11-21 00:14:13 +01:00
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
2022-11-20 03:17:37 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2022-08-25 21:51:00 +02:00
|
|
|
|
|
|
|
public class HidekoBot
|
|
|
|
{
|
2022-08-25 22:20:05 +02:00
|
|
|
private static String botToken;
|
2022-11-21 00:28:51 +01:00
|
|
|
private static final String version = "0.1.0-slash"; // we should probably find a way to make this consistent with Maven
|
2022-08-25 21:51:00 +02:00
|
|
|
|
2022-08-26 00:13:31 +02:00
|
|
|
private static JDA jda;
|
|
|
|
|
2022-08-25 22:59:47 +02:00
|
|
|
|
|
|
|
// create a logger instance for ease of use
|
|
|
|
private static final Logger logger = new Logger(HidekoBot.class);
|
2022-08-25 22:54:08 +02:00
|
|
|
|
2022-08-25 21:51:00 +02:00
|
|
|
public static void main(String[] args)
|
|
|
|
{
|
2022-08-25 22:20:05 +02:00
|
|
|
|
|
|
|
// check if bot token was specified as a startup argument
|
|
|
|
if(args.length < 1)
|
|
|
|
{
|
|
|
|
logger.log("Please specify your bot token!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-26 00:13:31 +02:00
|
|
|
// load token from args
|
2022-08-25 22:20:05 +02:00
|
|
|
botToken = args[0];
|
|
|
|
|
2022-08-25 21:51:00 +02:00
|
|
|
try
|
|
|
|
{
|
2022-08-25 22:37:32 +02:00
|
|
|
// try to create the bot object and authenticate it with discord.
|
2022-08-26 00:13:31 +02:00
|
|
|
JDABuilder jdaBuilder = JDABuilder.createDefault(botToken);
|
2022-08-25 23:20:51 +02:00
|
|
|
|
2022-08-26 00:13:31 +02:00
|
|
|
// enable necessary intents.
|
|
|
|
jdaBuilder.enableIntents(
|
|
|
|
GatewayIntent.MESSAGE_CONTENT,
|
2022-08-25 23:20:51 +02:00
|
|
|
GatewayIntent.DIRECT_MESSAGES,
|
2022-08-26 00:13:31 +02:00
|
|
|
GatewayIntent.GUILD_MESSAGES
|
|
|
|
);
|
2022-08-25 23:20:51 +02:00
|
|
|
|
|
|
|
jda = jdaBuilder.build().awaitReady();
|
2022-11-20 06:13:52 +01:00
|
|
|
} catch (Exception e)
|
2022-08-25 21:51:00 +02:00
|
|
|
{
|
2022-08-25 22:37:32 +02:00
|
|
|
logger.log(e.getMessage()); // print the error message, omit the stack trace.
|
2022-11-20 03:25:51 +01:00
|
|
|
shutdown(); // if we failed connecting and authenticating, then quit.
|
2022-08-25 21:51:00 +02:00
|
|
|
}
|
2022-08-25 22:37:32 +02:00
|
|
|
|
2022-11-20 18:11:00 +01:00
|
|
|
// find the bot's user/application id
|
2022-11-20 16:07:04 +01:00
|
|
|
String botUserId = jda.getSelfUser().getId();
|
2022-11-20 18:11:00 +01:00
|
|
|
Configuration.setBotApplicationId(botUserId);
|
2022-08-25 22:29:14 +02:00
|
|
|
|
2022-11-20 18:56:57 +01:00
|
|
|
// store if we have to force refresh commands despite no apparent changes.
|
2022-11-20 18:54:13 +01:00
|
|
|
boolean forceUpdateCommands = false;
|
2022-11-20 03:01:46 +01:00
|
|
|
|
2022-08-26 20:57:23 +02:00
|
|
|
// if there is more than 1 arg, then iterate through them because we have additional things to do.
|
2022-08-26 20:43:12 +02:00
|
|
|
// we are doing this at the end because we might need the API to be already initialized for some things.
|
|
|
|
if(args.length > 1) {
|
2022-08-26 20:43:49 +02:00
|
|
|
List<String> argsList = new ArrayList<>(Arrays.asList(args).subList(1, args.length));
|
2022-08-26 20:43:12 +02:00
|
|
|
|
|
|
|
if(argsList.contains("verbose")) Configuration.setVerbose(true);
|
2022-11-20 18:54:13 +01:00
|
|
|
if(argsList.contains("refresh")) forceUpdateCommands = true;
|
2022-08-26 20:43:12 +02:00
|
|
|
}
|
|
|
|
|
2022-11-20 18:54:13 +01:00
|
|
|
// register listeners
|
|
|
|
jda.addEventListener(new MessageListener());
|
|
|
|
jda.addEventListener(new SlashCommandListener());
|
|
|
|
jda.addEventListener(new SlashCommandCompleter());
|
2022-11-20 22:09:58 +01:00
|
|
|
jda.addEventListener(new ButtonInteractionListener());
|
2022-11-20 18:54:13 +01:00
|
|
|
|
|
|
|
// update slash commands (delayed)
|
|
|
|
final boolean finalForceUpdateCommands = forceUpdateCommands;
|
|
|
|
Executors.newSingleThreadScheduledExecutor().schedule(() ->
|
|
|
|
SlashCommandsUtil.updateSlashCommands(finalForceUpdateCommands), 1, TimeUnit.SECONDS);
|
|
|
|
|
|
|
|
// set the bot's status
|
|
|
|
jda.getPresence().setStatus(OnlineStatus.ONLINE);
|
|
|
|
jda.getPresence().setActivity(Activity.playing("Hatsune Miku: Project DIVA"));
|
|
|
|
|
2022-11-21 00:14:13 +01:00
|
|
|
// connect to database
|
|
|
|
logger.log("Connecting to database...");
|
|
|
|
String dbFilePath = System.getProperty("user.dir") + File.separator + "db.sqlite"; // in current directory
|
|
|
|
DatabaseManager dbManager = new DatabaseManager(dbFilePath);
|
|
|
|
if(dbManager.connect() && dbManager.initDb())
|
|
|
|
{
|
|
|
|
logger.log("Database connection initialized!");
|
|
|
|
Configuration.setDatabaseManagerInstance(dbManager);
|
|
|
|
|
|
|
|
// load data here...
|
|
|
|
|
|
|
|
logger.log("Database data loaded into memory!");
|
|
|
|
} else {
|
|
|
|
logger.log("Error initializing database connection!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// start scheduled runnables
|
|
|
|
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
|
|
|
ExpiredMessageRunner task = new ExpiredMessageRunner();
|
|
|
|
int initDelay = 5;
|
|
|
|
int periodicDelay = 5;
|
|
|
|
scheduler.scheduleAtFixedRate(task, initDelay, periodicDelay, TimeUnit.SECONDS);
|
|
|
|
|
|
|
|
|
2022-08-25 22:54:08 +02:00
|
|
|
// print the bot logo.
|
2022-08-26 20:57:23 +02:00
|
|
|
logger.log("\n\n" + logger.getLogo() + "\nv" + version + " - bot is ready!\n", 2);
|
2022-08-25 22:54:08 +02:00
|
|
|
|
2022-11-20 03:17:37 +01:00
|
|
|
// register shutdown interrupt signal listener for proper shutdown.
|
|
|
|
Signal.handle(new Signal("INT"), signal -> shutdown());
|
|
|
|
|
2022-08-25 22:37:32 +02:00
|
|
|
// log the invite-link to console so noob users can just click on it.
|
2022-08-26 20:29:10 +02:00
|
|
|
logger.log("Bot User ID: " + botUserId, 3);
|
2022-11-20 16:07:04 +01:00
|
|
|
logger.log("Invite Link: " + Configuration.getInviteUrl(), 4);
|
2022-08-25 23:20:51 +02:00
|
|
|
|
2022-08-26 00:13:31 +02:00
|
|
|
}
|
|
|
|
public static JDA getAPI()
|
|
|
|
{
|
|
|
|
return jda;
|
2022-08-25 21:51:00 +02:00
|
|
|
}
|
2022-08-25 22:13:39 +02:00
|
|
|
|
2022-11-20 03:17:37 +01:00
|
|
|
public static void shutdown()
|
|
|
|
{
|
|
|
|
logger.log("WARNING! Shutting down!");
|
2022-11-20 03:25:51 +01:00
|
|
|
if(jda != null) jda.shutdown();
|
2022-11-20 03:17:37 +01:00
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
|
2022-08-25 21:51:00 +02:00
|
|
|
}
|