Skip to content

Commit

Permalink
rewrite in kotlin, UNTESTED
Browse files Browse the repository at this point in the history
  • Loading branch information
dkim19375 committed May 13, 2021
1 parent 8104924 commit 6ec43a7
Show file tree
Hide file tree
Showing 15 changed files with 709 additions and 287 deletions.
13 changes: 9 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.5.0'
id 'com.github.johnrengelman.shadow' version '6.0.0'
}

//noinspection GroovyUnusedAssignment
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
compileJava.options.encoding 'UTF-8'

group 'me.dkim19375'
version '1.3.4'
version '2.0.0'

compileKotlin.kotlinOptions.jvmTarget = '1.8'

repositories {
mavenCentral()
jcenter()
maven { url = 'https://m2.dv8tion.net/releases' }
}

dependencies {
compileOnly 'net.dv8tion:JDA:4.2.0_227'
compileOnly 'net.dv8tion:JDA:4.2.1_262'
api 'commons-io:commons-io:2.8.0'
api 'org.apache.commons:commons-lang3:3.12.0'
}
68 changes: 68 additions & 0 deletions src/main/java/me/dkim19375/dkim19375jdautils/BotBase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package me.dkim19375.dkim19375jdautils

import me.dkim19375.dkim19375jdautils.annotation.API
import me.dkim19375.dkim19375jdautils.command.Command
import me.dkim19375.dkim19375jdautils.command.CommandType
import me.dkim19375.dkim19375jdautils.event.CustomListener
import me.dkim19375.dkim19375jdautils.event.EventListener
import net.dv8tion.jda.api.JDA
import java.util.*
import kotlin.concurrent.thread
import kotlin.system.exitProcess

@API
abstract class BotBase(
@API val jda: JDA,
val name: String,
val customListener: CustomListener = object : CustomListener() {}
) {
val commandTypes = setOf<CommandType>()

@API
val commands = setOf<Command>()

@API
val consoleCommands = mapOf<String, (String) -> Unit>()
private var started = false

fun getPrefix(guild: Long): String = getPrefix(guild.toString())
abstract fun getPrefix(guild: String): String

@API
fun onStart(stopCommandEnabled: Boolean = true) {
if (started) {
return
}
started = true
jda.addEventListener(EventListener(this))
Runtime.getRuntime().addShutdownHook(thread(false) {
if (jda.status != JDA.Status.SHUTDOWN && jda.status != JDA.Status.SHUTTING_DOWN) {
println("Stopping the bot!")
jda.shutdown()
println("Stopped")
}
})
thread {
val scanner = Scanner(System.`in`)
while (scanner.hasNext()) {
val next = scanner.nextLine()
if (next.equals("stop", ignoreCase = true) && stopCommandEnabled) {
if (jda.status != JDA.Status.SHUTDOWN && jda.status != JDA.Status.SHUTTING_DOWN) {
println("Stopping the bot!")
jda.shutdown()
println("Stopped")
exitProcess(0)
}
continue
}
for ((cmd, action) in consoleCommands) {
if (next.startsWith(cmd, ignoreCase = true)) {
action(next)
}
}
}
}
}

fun sendEvent(event: (Command) -> Unit) = commands.forEach(event)
}
13 changes: 13 additions & 0 deletions src/main/java/me/dkim19375/dkim19375jdautils/annotation/API.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.dkim19375.dkim19375jdautils.annotation

@Target(
AnnotationTarget.CLASS,
AnnotationTarget.PROPERTY,
AnnotationTarget.FIELD,
AnnotationTarget.CONSTRUCTOR,
AnnotationTarget.FUNCTION,
AnnotationTarget.FILE,
AnnotationTarget.VALUE_PARAMETER
)
@API
annotation class API
125 changes: 125 additions & 0 deletions src/main/java/me/dkim19375/dkim19375jdautils/command/Command.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package me.dkim19375.dkim19375jdautils.command

import me.dkim19375.dkim19375jdautils.BotBase
import me.dkim19375.dkim19375jdautils.annotation.API
import me.dkim19375.dkim19375jdautils.embed.EmbedManager
import me.dkim19375.dkim19375jdautils.embed.EmbedUtils
import net.dv8tion.jda.api.Permission
import net.dv8tion.jda.api.entities.GuildChannel
import net.dv8tion.jda.api.entities.Member
import net.dv8tion.jda.api.entities.User
import net.dv8tion.jda.api.events.Event
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent
import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent
import java.awt.Color

@API
abstract class Command(private val bot: BotBase) {
abstract val command: String
abstract val name: String
abstract val aliases: Set<String>
abstract val description: String
abstract val arguments: Set<CommandArg>
abstract val type: CommandType
abstract val minArgs: Int
open val permissions: Set<Permission> = setOf()
open val whitelistUsers: Set<Long> = setOf()

fun sendHelpUsage(
cmd: String,
event: Event,
command: Command = this
) {
val user = when (event) {
is GuildMessageReceivedEvent -> event.author
is PrivateMessageReceivedEvent -> event.author
is MessageReceivedEvent -> event.author
else -> return
}
val member: Member? = (event as? GuildMessageReceivedEvent)?.member
val guild = when (event) {
is GuildMessageReceivedEvent -> event.guild
is MessageReceivedEvent -> event.guild
else -> return
}
val channel = when (event) {
is GuildMessageReceivedEvent -> event.channel
is PrivateMessageReceivedEvent -> event.channel
is MessageReceivedEvent -> event.channel
else -> return
}
if (!hasPermissions(user, member, channel as? GuildChannel)) {
return
}
val embedManager = EmbedManager("${bot.name} ${command.name}", Color.BLUE, cmd, user)
embedManager.embedBuilder.addField(
"Information:",
command.description.plus(
"\n**Prefix: ${bot.getPrefix(guild.idLong)}**"
), false
)
embedManager.embedBuilder.addField(EmbedUtils.getEmbedGroup("Aliases:", command.aliases))
embedManager.embedBuilder.addField(EmbedUtils.getEmbedGroup("Arguments:", command.arguments.map { arg ->
"${arg.arg} - ${arg.description}"
}))
channel.sendMessage(embedManager.embedBuilder.build()).queue()
}

@API
fun hasPermissions(user: User, member: Member? = null, channel: GuildChannel? = null): Boolean {
if (whitelistUsers.isNotEmpty() && !whitelistUsers.contains(user.idLong)) {
return false
}
member ?: return true
if (channel != null) {
return member.hasPermission(channel, permissions)
}
return member.hasPermission(permissions)
}

open fun onMessageReceived(
message: String,
event: MessageReceivedEvent
) {
}

open fun onCommand(
cmd: String,
args: List<String>,
prefix: String,
all: String,
event: MessageReceivedEvent
) {
}

open fun onGuildMessageReceived(
message: String,
event: GuildMessageReceivedEvent
) {
}

open fun onGuildCommand(
cmd: String,
args: List<String>,
prefix: String,
all: String,
event: GuildMessageReceivedEvent
) {
}

open fun onPrivateMessageReceived(
message: String,
event: PrivateMessageReceivedEvent
) {
}

open fun onPrivateCommand(
cmd: String,
args: List<String>,
prefix: String,
all: String,
event: PrivateMessageReceivedEvent
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package me.dkim19375.dkim19375jdautils.command

import net.dv8tion.jda.api.Permission

data class CommandArg(val baseCommand: Command, val arg: String, val description: String, val permissions: Set<Permission> = setOf())
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.dkim19375.dkim19375jdautils.command

import me.dkim19375.dkim19375jdautils.BotBase
import org.apache.commons.lang3.StringUtils

fun String.getCommandType(bot: BotBase): CommandType? {
for (type in bot.commandTypes) {
if (type.name.equals(this, ignoreCase = true)) {
return type
}
if (type.displayname.equals(this, ignoreCase = true)) {
return type
}
}
return null
}

abstract class CommandType(val name: String, val displayname: String = StringUtils.capitalize(name.lowercase()))
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package me.dkim19375.dkim19375jdautils.command

import me.dkim19375.dkim19375jdautils.BotBase
import me.dkim19375.dkim19375jdautils.annotation.API
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.guild.GuildMessageReceivedEvent
import java.awt.Color

@API
class HelpCommand(private val bot: BotBase) : Command(bot) {
override val command = "help"
override val name = "Help"
override val aliases = setOf<String>()
override val description = "See the bot's commands"
override val arguments: Set<CommandArg>
get() = bot.commandTypes.map { type ->
CommandArg(
this,
type.displayname.lowercase(),
"View commands in the ${type.displayname.lowercase()} category"
)
}.toSet()
override val type = object : CommandType("OTHER", "Other") {}
override val minArgs = 1

override fun onGuildCommand(
cmd: String,
args: List<String>,
prefix: String,
all: String,
event: GuildMessageReceivedEvent
) {
val type = args[0].getCommandType(bot)
if (type == null) {
val command = args[0].getCommand(bot)
if (command == null) {
sendHelpUsage(cmd, event)
return
}
sendHelpUsage(cmd, event, command)
return
}
val embedManager = EmbedManager("UniG0 $name: ${type.displayname}", Color.BLUE, cmd, event.author)
embedManager.embedBuilder.addField(
"TIP:", "Do ${bot.getPrefix(event.guild.id)}help <command> " +
"to view information about a specific command!", false
)
embedManager.embedBuilder.addField("Information:", "Commands in the ${type.displayname} category", false)
embedManager.embedBuilder.addField(
EmbedUtils.getEmbedGroup("Commands - ${type.displayname}:", bot.commands.getOfType(type).map { c ->
"${c.command} - ${c.description}"
})
)
event.channel.sendMessage(embedManager.embedBuilder.build()).queue()
}
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,11 @@
package me.dkim19375.dkim19375jdautils.holders;
package me.dkim19375.dkim19375jdautils.data

import org.jetbrains.annotations.NotNull;
import me.dkim19375.dkim19375jdautils.annotation.API

public class MessageReceivedHolder {
// (String command, String[] args, String prefix, String all)
@NotNull
private final String command;
@NotNull
private final String[] args;
@NotNull
private final String prefix;
@NotNull
private final String all;

public MessageReceivedHolder(@NotNull String command, @NotNull String[] args, @NotNull String prefix, @NotNull String all) {
this.command = command;
this.args = args;
this.prefix = prefix;
this.all = all;
}

@NotNull
public String getCommand() {
return command;
}

@NotNull
public String[] getArgs() {
return args;
}

@NotNull
public String getPrefix() {
return prefix;
}

@NotNull
public String getAll() {
return all;
}
}
@API
class MessageReceivedData(
val command: String,
val args: List<String>,
val prefix: String,
val all: String
)
Loading

0 comments on commit 6ec43a7

Please sign in to comment.