Services

Services contain and share your business logic. For example, we can create a MathService to apply common calculations.

@Service
class MathService {
    fun add(a: Int, b: Int) = a + b
    fun sub(a: Int, b: Int) = a - b
    fun mult(a: Int, b: Int) = a * b
    fun div(a: Int, b: Int) = a / b
}

To create a service, annotate a class with @Service. DiscordKt uses dependency injection to provide access to these objects. To use it, accept it as a parameter. This can be done anywhere you might need it, such as CommandSets, Preconditions, Listeners, and even other Services. It will automatically be given wherever it is requested.

@Service
class A

@Service
class B(val a: A)

In this case, an instance of class A is created, then used to create an instance of class B. Each of which can be used further.