Skip to content

Setting Up Your Bot

Pumbas600 edited this page Jan 23, 2022 · 7 revisions

Bots created using Halpbot require 2 things:

  1. A bot-config.properties file in your resources folder, which allows you to configure different aspects of your bot
  2. A class extending the Bot interface, which is used to initialise your bot

bot-config.properties

This property file allows you to configure different aspects of Halpbot. Depending on which adapters your bot is using, different properties may be required.

Note: If your config file is not named bot-config.properties and not located at resources/bot-config.properties it won't be found by Halpbot

For more infromation on the properties you can set in bot-config.properties, refer to here.

As we'll only be using the CommandAdapter for this example bot and leaving everything else with their default configurations, your bot-config.properties file should look like:

# Use your own Discord id here:
ownerId=012345678910111213
defaultPrefix=$

Not sure what your Discord id is? Refer to this tutorial

Creating your bot

To create your bot using Halpbot, you must implement the Bot interface, which requires you to implement initialise(), where you can create and configure the JDABuilder along with onCreation(ApplicationContext, HalpbotCore) which can optionally be implemented if some post-creation initialisation is required.

import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Activity.ActivityType;

import org.dockbox.hartshorn.core.annotations.activate.Activator;
import org.dockbox.hartshorn.core.annotations.service.Service;
import org.dockbox.hartshorn.core.exceptions.ApplicationException;

import nz.pumbas.halpbot.commands.annotations.UseCommands;
import nz.pumbas.halpbot.common.Bot;
import nz.pumbas.halpbot.utilities.HalpbotUtils;

// Indicates that the bot is a singleton and allows this class to be scanned for commands, etc
@Service

// Indicates that this class is an application activator
@Activator

// Activates the CommandAdapter ServicePreProcessors which scan for commands, converters, etc
@UseCommands
public class DemoBot implements Bot
{
    public static void main(String[] args) throws ApplicationException {
        /*
         * This starts up Hartshorn and automatically creates an instance of this class.
         *
         * It then calls initialise() so that you can configure the JDABuilder. Internally it uses this to add
         * the HalpbotAdapters as event listeners before building the JDA instance.
         *
         * Finally, onCreation(ApplicationContext, HalpbotCore) is called, which can be optionally implemented if
         * post-initialisation is required.
         */
        HalpbotBuilder.build(DemoBot.class, args);
    }

    @Override
    public JDABuilder initialise() {
        // Retrieve the discord token
        String token = HalpbotUtils.firstLine("Token.txt");

        // Create and configure the JDABuilder here
        return JDABuilder.createDefault(token)
                .setActivity(Activity.of(ActivityType.LISTENING, "to how cool Halpbot is!"));
    }
}

Note: Annotations, like @UseCommands are used to activate the different Halpbot adapter's ServicePreProcessors as they scan the entire classpath during startup, which can add unnecessary overhead if not required.