Skip to content

Commit

Permalink
fixed bug, added CustomJDABuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
dkim19375 committed May 22, 2021
1 parent acf96cb commit 3bb0182
Show file tree
Hide file tree
Showing 5 changed files with 420 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
compileJava.options.encoding 'UTF-8'

group 'me.dkim19375'
version '2.3.0'
version '2.4.0'

//noinspection GrUnresolvedAccess
compileKotlin.kotlinOptions {
Expand Down
97 changes: 82 additions & 15 deletions src/main/kotlin/me/dkim19375/dkim19375jdautils/BotBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package me.dkim19375.dkim19375jdautils
import dev.minn.jda.ktx.injectKTX
import me.dkim19375.dkim19375jdautils.annotation.API
import me.dkim19375.dkim19375jdautils.command.Command
import me.dkim19375.dkim19375jdautils.command.CommandType
import me.dkim19375.dkim19375jdautils.command.HelpCommand
import me.dkim19375.dkim19375jdautils.command.OTHER_TYPE
import me.dkim19375.dkim19375jdautils.event.CustomListener
import me.dkim19375.dkim19375jdautils.event.EventListener
import me.dkim19375.dkim19375jdautils.impl.CustomJDABuilder
import me.dkim19375.dkim19375jdautils.managers.SpecialEventsManager
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.JDABuilder
import net.dv8tion.jda.api.requests.GatewayIntent
import java.util.*
import kotlin.concurrent.thread
Expand All @@ -23,21 +24,83 @@ import kotlin.system.exitProcess
@API
@Suppress("LeakingThis")
abstract class BotBase {
/**
* The name of the bot
*/
abstract val name: String

/**
* The token of the bot
*/
abstract val token: String

/**
* Whether to Inject KTS from JDA-KTS
*/
open val injectKTS = false

/**
* The base JDA builder
*/
open val baseJDABuilder: (Unit) -> CustomJDABuilder = builder@{
val builder = CustomJDABuilder.createDefault(token)
if (injectKTS) {
builder.actions.add { it.injectKTX() }
}
return@builder builder.enableIntents(intents)
.addEventListeners(eventsManager, EventListener(this))
}

/**
* Allows you to run methods on [CustomJDABuilder], overriding the ones ran by the [BotBase#onStart][BotBase.onStart]
*/
open val jdaBuilderActions: ((CustomJDABuilder) -> CustomJDABuilder)? = null

/**
* The listener to detect if a command is valid to send
*/
open val customListener: CustomListener = object : CustomListener() {}
open val intents = mutableSetOf(GatewayIntent.GUILD_MESSAGE_REACTIONS, GatewayIntent.DIRECT_MESSAGE_REACTIONS)

/**
* [GatewayIntents][GatewayIntent] that should be enabled
*/
@API
val intents = mutableSetOf(GatewayIntent.GUILD_MESSAGE_REACTIONS, GatewayIntent.DIRECT_MESSAGE_REACTIONS)

/**
* The [JDA] instance
*/
lateinit var jda: JDA
val commandTypes = mutableSetOf(OTHER_TYPE)
val commands = mutableSetOf(HelpCommand(this))

/**
* [CommandTypes][me.dkim19375.dkim19375jdautils.command.CommandType] that should be registered
*/
open val commandTypes = mutableSetOf(OTHER_TYPE)

/**
* [Commands][Command] that should be registered
*/
open val commands = mutableSetOf<Command>(HelpCommand(this))

/**
* An [Events Manager][SpecialEventsManager] useful for handling events such as reaction add listeners
*/
@API
val eventsManager: SpecialEventsManager = SpecialEventsManager(this)
open val eventsManager: SpecialEventsManager = SpecialEventsManager(this)

/**
* Console commands that should be triggered.
*
* The key of the map: The base command
*
* The value of the map: The raw string/input
*/
@API
val consoleCommands = mutableMapOf<String, (String) -> Unit>()
open val consoleCommands = mutableMapOf<String, (String) -> Unit>()

/**
* True if the bot is started, false if not
*/
private var started = false

/**
Expand All @@ -52,6 +115,17 @@ abstract class BotBase {
*/
abstract fun getPrefix(guild: String): String

/**
* Get all command types
*
* @return All [CommandTypes][CommandType] from [commands]
*/
open fun getAllCommandTypes(): Set<CommandType> {
val types = commandTypes.toMutableSet()
types.addAll(commands.map(Command::type))
return types
}

/**
* @param stopCommandEnabled True if the stop console command should be enabled, false if not
*/
Expand All @@ -62,15 +136,8 @@ abstract class BotBase {
}
started = true
println("Starting bot")
val builder = JDABuilder.createDefault(token)
if (injectKTS) {
builder.injectKTX()
}
builder.enableIntents(intents)
val jda = builder.build()
this.jda = jda
jda.addEventListener(EventListener(this))
jda.addEventListener(eventsManager)
val builder = jdaBuilderActions?.let { it(baseJDABuilder(Unit)) } ?: baseJDABuilder(Unit)
jda = builder.getBuilder().build()
Runtime.getRuntime().addShutdownHook(thread(false) {
if (jda.status != JDA.Status.SHUTDOWN && jda.status != JDA.Status.SHUTTING_DOWN) {
println("Stopping the bot!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import me.dkim19375.dkim19375jdautils.BotBase
import org.apache.commons.lang3.StringUtils

fun String.getCommandType(bot: BotBase): CommandType? {
for (type in bot.commandTypes) {
for (type in bot.getAllCommandTypes()) {
if (type.name.equals(this, ignoreCase = true)) {
return type
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import me.dkim19375.dkim19375jdautils.embed.EmbedManager
import me.dkim19375.dkim19375jdautils.embed.EmbedUtils
import me.dkim19375.dkim19375jdautils.util.getCommand
import me.dkim19375.dkim19375jdautils.util.getOfType
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent
import java.awt.Color

Expand All @@ -27,7 +26,7 @@ open class HelpCommand(private val bot: BotBase) : Command(bot) {
override val aliases = setOf<String>()
override val description = "See the bot's commands"
override val arguments: Set<CommandArg>
get() = bot.commandTypes.map { type ->
get() = bot.getAllCommandTypes().map { type ->
CommandArg(
this,
type.displayname.lowercase(),
Expand Down
Loading

0 comments on commit 3bb0182

Please sign in to comment.