OttoBot/src/main/kotlin/nl/voidcorp/dbot/commands/Commands.kt

187 lines
8.3 KiB
Kotlin

package nl.voidcorp.dbot.commands
import net.dv8tion.jda.core.EmbedBuilder
import net.dv8tion.jda.core.MessageBuilder
import net.dv8tion.jda.core.entities.ChannelType
import net.dv8tion.jda.core.entities.MessageEmbed
import net.dv8tion.jda.core.managers.GuildController
import nl.voidcorp.dbot.catMap
import nl.voidcorp.dbot.commands
import nl.voidcorp.dbot.emptyOr
import nl.voidcorp.dbot.random
import java.time.LocalDateTime
import java.time.temporal.ChronoUnit
import kotlin.random.Random
val helloCommand = UnityCommand("hello", "Say hello to Andy!") {
val i = Random.nextInt(10)
if (i > 8) {
it.reply("Can you speak up or I'll throw you a microphone")
} else {
it.reply(MessageBuilder("Hello, ").append(it.author).append("!").build())
}
}
val pingCommand =
UnityCommand("ping", help = "Check the bot's ping", aliases = mutableListOf("pong"), cooldown = 10) { event ->
event.reply("Ping: ...") { m ->
val ping = event.message.creationTime.until(m.creationTime, ChronoUnit.MILLIS)
m.editMessage("Ping: " + ping + "ms | Websocket: " + event.jda.ping + "ms").queue()
}
}
val replies =
listOf("Hello %%", "Why hello there %%!", "Hello there %%", "General %%. You are a bold one", "A wild %% appeared!")
val helpCommand =
UnityCommand("help", "Guess what?", category = object : UnityCategory("hidden") {
override fun test(ce: UnityCommandEvent): Boolean {
if (ce.member.roles.firstOrNull { it.name.equals("admin", true) } != null) return true
if (channels.all { ce.guild.getTextChannelsByName(it, true).firstOrNull() == null }) return true
val s = (ce.channelType == ChannelType.TEXT) and (ce.textChannel!!.name.toLowerCase().run {
contains("bot") or (ce.guild.textChannels.none {
it.name.contains(
"music-bot"
)
} and contains("music"))
})
if (!s) {
ce.reply("This command can only be used in music and bot channels!")
}
return s
}
}) { event ->
val greeting = replies.random().replace("%%", event.member.effectiveName)
val eb = EmbedBuilder()
when {
event.args.isEmpty() -> {
val prefix = GSM.getSettings(event.guild).primaryPrefix
eb.setTitle(greeting)
.appendDescription("My name is OttoBot, and I am definitely the best Discord bot around!\n\nUse `${prefix}help command` for a chance that I have more info about a command!")
.setColor(event.selfMember.color)
.setFooter("Requested by ${event.member.effectiveName}", event.author.effectiveAvatarUrl)
.setTimestamp(LocalDateTime.now())
val m = commands.catMap().filter { it.key != "hidden" }
for (e in m.entries) {
val f = MessageEmbed.Field(
e.key,
e.value.asSequence().filter { it.name != "help" }.joinToString(
separator = "\n",
postfix = "\n"
) { "`$prefix${it.name}`" },
true
)
eb.addField(f)
}
eb.setThumbnail(event.selfUser.effectiveAvatarUrl)
}
event.args == "help" -> {
eb.setTitle("Ha Ha")
.appendDescription("Ha ha, very funny ${event.member.effectiveName}")
.setColor(event.selfMember.color)
.setFooter("Requested by ${event.member.effectiveName}", event.author.effectiveAvatarUrl)
.setTimestamp(LocalDateTime.now())
}
commands.any { it.name == event.args } -> {
val cmd = commands.first { it.name == event.args }
eb.setTitle("**!${cmd.name}**")
.appendDescription(cmd.help)
.setColor(event.selfMember.color)
.setFooter("Requested by ${event.member.effectiveName}", event.author.effectiveAvatarUrl)
.setTimestamp(LocalDateTime.now())
if (cmd.aliases.isNotEmpty()) eb.addField("Aliases", cmd.aliases.joinToString { "`$it`" }, true)
eb.addField(
"Usage",
"`${GSM.getSettings(event.guild).primaryPrefix}${cmd.howTo.emptyOr(cmd.name)}`",
true
)
val cat = cmd.category
if (cat.roles.isNotEmpty())
eb.addField("Roles", cat.roles.joinToString { "`$it`" }, true)
}
else -> {
eb.setTitle(greeting)
.appendDescription("I honestly have no idea what the command `${event.args}` does...")
.setColor(event.selfMember.color)
.setFooter("Requested by ${event.member.effectiveName}", event.author.effectiveAvatarUrl)
.setTimestamp(LocalDateTime.now())
}
}
event.reply(eb.build())
}
val joinRoleCommand = UnityCommand(
"role",
"Join the role specified, use without parameters for a list!",
aliases = mutableListOf("joinrole", "jr")
) { ce ->
val gm = GSM.getSettings(ce.guild)
val eb = EmbedBuilder().setColor(ce.selfMember.color)
.setFooter("Requested by ${ce.member.effectiveName}", ce.author.effectiveAvatarUrl)
.setTimestamp(LocalDateTime.now())
if (!ce.hasArgs) {
val field = MessageEmbed.Field(
"Roles",
gm.roleMap.entries.map { it.key to ce.guild.getRoleById(it.value)!! }.joinToString { "`${it.first}`: ${it.second.name}" },
false
)
eb.addField(field)
eb.setTitle("Here's a list of all this servers roles, ${ce.member.effectiveName}!")
} else {
if (!gm.roleMap.containsKey(ce.args)) {
eb.setTitle("There was an error")
eb.setDescription("This role does not exist?\nUse `${gm.primaryPrefix}joinrole` to find the available roles!")
} else {
val id = gm.roleMap[ce.args]!!
val r = ce.guild.getRoleById(id)!!
GuildController(ce.guild).addSingleRoleToMember(ce.member, r).queue()
eb.setTitle("Have fun with your new role!")
eb.setDescription("I have given you the role ${r.name}!")
}
}
ce.reply(eb.build())
}
val removeRoleCommand =
UnityCommand("removerole", "Remove the role specified, if you have it!", aliases = mutableListOf("rr")) { ce ->
val gm = GSM.getSettings(ce.guild)
val eb = EmbedBuilder().setColor(ce.selfMember.color)
.setFooter("Requested by ${ce.member.effectiveName}", ce.author.effectiveAvatarUrl)
.setTimestamp(LocalDateTime.now())
if (!ce.hasArgs) {
val roles = gm.roleMap.map { it.key to ce.guild.getRoleById(it.value)!! }
.filter { ce.member.roles.contains(it.second) }
val field = MessageEmbed.Field(
"Roles",
roles.joinToString { "`${it.first}`: ${it.second.name}" },
false
)
eb.addField(field)
eb.setTitle("Here's a list of all your removable roles, ${ce.member.effectiveName}!")
} else {
if (!gm.roleMap.containsKey(ce.args)) {
eb.setTitle("There was an error")
eb.setDescription("This role does not exist?\nUse `${gm.primaryPrefix}removerole` to find the removable roles!")
} else {
val id = gm.roleMap[ce.args]!!
val r = ce.guild.getRoleById(id)!!
if (ce.member.roles.contains(r)) {
GuildController(ce.guild).removeSingleRoleFromMember(ce.member, r).queue()
eb.setTitle("Have fun (?) without the role?")
eb.setDescription("I have removed the role ${r.name}!")
} else {
eb.setTitle("Yeah this wont work don't you think?")
eb.setDescription("You need to have the role you are trying to remove to be able to remove it...\nHonestly what did you expect?")
}
}
}
ce.reply(eb.build())
}