Skip to main content

Introduction

What is Botrino?

Botrino is a Java framework that provides guidelines and a set of tools to build Discord bots in a more convenient way. Pretty much in the same spirit as Spring Boot, it allows to easily setup standalone bot applications that are ready to run, embedding a few third-party libraries such as Jackson for JSON-based configuration, RDI for a reactive IoC container, and Discord4J for the interface with the Discord Bot API.

Motivations

Starting the development of a Discord bot follows most of the time the same pattern: you create a project, import your favorite Discord client library, export a configuration file or an environment variable with the bot token, and design a whole structure for your commands and your logic, before you can actually start to implement them. When working with Java, this structure is even more important otherwise you may adopt bad practices and end up with a bot that is difficult to maintain.

This is how came the idea of this project: have something that can handle for you all the initial workflow of setting up a project with a solid structure, at the only cost of letting the framework choose some libraries for you, so that you can focus on what matters. Botrino is born.

It also aims at providing a library for Discord's Interaction API that integrates well with the structure of Botrino, while still being decoupled from it.

Overview

Botrino utilizes Java modules, introduced in the JDK 9 and released in the JDK 11 as a LTS version. The classes of your application will reside in one or more modules with the following module-info.java structure:

import botrino.api.annotation.BotModule;

@BotModule
open module com.example.myproject {

requires botrino.api;
}

The annotation as well as the open modifier will allow Botrino to automatically scan through all the classes present in the module, in order to automatically register configuration entries, commands, services, etc.

Inside your module, you can create services using RDI annotations that are automatically loaded on startup:

package com.example.myproject;

import com.github.alex1304.rdi.finder.annotation.RdiFactory;
import com.github.alex1304.rdi.finder.annotation.RdiService;
import discord4j.core.GatewayDiscordClient;
import discord4j.core.event.domain.lifecycle.ReadyEvent;
import reactor.core.publisher.Mono;
import reactor.util.Logger;
import reactor.util.Loggers;

@RdiService
public final class SampleService {

private static final Logger LOGGER = Loggers.getLogger(SampleService.class);

// We can inject other services, here we are injecting
// the GatewayDiscordClient provided by Botrino
@RdiFactory
public SampleService(GatewayDiscordClient gateway) {
gateway.on(ReadyEvent.class, ready -> Mono.fromRunnable(
() -> LOGGER.info("Logged in as "
+ ready.getSelf().getTag())))
.subscribe();
}
}

The bot is configured via a JSON configuration file with contents similar to this:

{
"bot": {
"token": "yourTokenHere",
"presence": {
"status": "online",
"activity_type": "playing",
"activity_text": "Hello world!"
},
"enabled_intents": 32509
}
}

To go further and familiarize yourself with the framework, check out the Getting Started guide.