-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandAbstract.kt
107 lines (97 loc) · 3.94 KB
/
CommandAbstract.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
@file:JvmName("CommandAbstract")
package me.monmcgt.code.commands
import me.monmcgt.code.enums.SlashCommandType
import me.monmcgt.code.jda
import me.monmcgt.code.util.queueIgnoreException
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.interactions.commands.build.OptionData
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
abstract class CommandAbstract : CommandUtility() {
protected open val optionDataArray = arrayOf<OptionData>()
var commandInfo: CommandInfo
var commandName: String
var commandDescription: String
init {
commandInfo = javaClass.getAnnotation(CommandInfo::class.java) ?: throw IllegalArgumentException("Command class must be annotated with @CommandInfo")
commandName = commandInfo.name
commandDescription = commandInfo.description
}
fun execute(event: SlashCommandInteractionEvent) {
this.event = event
if (commandInfo.autoDeferReply) {
event.deferReply(true).queue()
}
onSlashCommandInteraction()
}
fun load() {
if (commandInfo.autoRegisterSlashCommand) {
val executorService = Executors.newFixedThreadPool(20)
when (commandInfo.slashCommandType) {
SlashCommandType.ALL -> {
executorService.submit {
val upsertCommand = jda!!.upsertCommand(commandName, commandDescription)
if (optionDataArray.isNotEmpty()) {
upsertCommand.addOptions(*optionDataArray)
}
upsertCommand.queueIgnoreException()
}
}
SlashCommandType.GUILD_ALL -> {
jda!!.guilds.forEach {
executorService.submit {
val upsertCommand = it.upsertCommand(commandName, commandDescription)
if (optionDataArray.isNotEmpty()) {
upsertCommand.addOptions(*optionDataArray)
}
upsertCommand.queueIgnoreException()
// println("Registered command $commandName in guild ${it.name}")
}
}
}
SlashCommandType.GUILD_SPECIFIC -> {
commandInfo.guilds.forEach {
executorService.submit {
val upsertCommand = jda!!.getGuildById(it)?.upsertCommand(commandName, commandDescription)
if (optionDataArray.isNotEmpty()) {
upsertCommand?.addOptions(*optionDataArray)
}
upsertCommand?.queueIgnoreException()
}
}
}
}
Thread {
executorService.shutdown()
val awaitTermination = executorService.awaitTermination(3, TimeUnit.MINUTES)
if (!awaitTermination) {
executorService.shutdownNow()
}
}.start()
}
}
fun isThisClass(event: SlashCommandInteractionEvent): Boolean {
if (event.name != commandName) {
return false
}
/*val options = event.options
if (options.size != optionDataArray.size) {
return false
}
for (i in 0 until options.size) {
try {
val option = options[i]
val optionData = optionDataArray[i]
if (option.type != optionData.type) {
return false
}
if (option.name != optionData.name) {
return false
}
} catch (e: ArrayIndexOutOfBoundsException) {
return false
}
}*/
return true
}
}