Skip to content

Commit

Permalink
chore: 0.0.18_5.0.0-alpha.11
Browse files Browse the repository at this point in the history
feat: add PurgeMessages to /system-command
fix: Modal TextInput defaults are now working if null
  • Loading branch information
itsmefox committed Apr 26, 2022
1 parent ad44444 commit 72c15ea
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 90 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
}

group "io.viascom.discord.bot"
version "0.0.17_5.0.0-alpha.11"
version "0.0.18_5.0.0-alpha.11"

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.viascom.discord.bot.starter.bot.emotes.AlunaEmote
import io.viascom.discord.bot.starter.bot.handler.Command
import io.viascom.discord.bot.starter.bot.handler.DiscordCommand
import io.viascom.discord.bot.starter.util.getOptionAsString
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent
Expand Down Expand Up @@ -44,14 +45,18 @@ class SystemCommand(
return
}

val ephemeral = if (event.user.idLong in alunaProperties.modIds){
val ephemeral = if (event.user.idLong in alunaProperties.modIds) {
false
} else {
selectedProvider!!.ephemeral
}

val hook = event.deferReply(ephemeral).complete()
selectedProvider!!.execute(event, hook, this)
if(selectedProvider!!.keepCommandOpen){
selectedProvider!!.execute(event, null, this)
} else{
val hook = event.deferReply(ephemeral).complete()
selectedProvider!!.execute(event, hook, this)
}
}

override fun onButtonInteraction(event: ButtonInteractionEvent, additionalData: HashMap<String, Any?>): Boolean {
Expand All @@ -70,6 +75,14 @@ class SystemCommand(
selectedProvider?.onSelectMenuInteractionTimeout()
}

override fun onModalInteraction(event: ModalInteractionEvent, additionalData: HashMap<String, Any?>): Boolean {
return selectedProvider?.onModalInteraction(event, additionalData) ?: true
}

override fun onModalInteractionTimeout(additionalData: HashMap<String, Any?>) {
selectedProvider?.onModalInteractionTimeout(additionalData)
}

override fun onAutoCompleteEvent(option: String, event: CommandAutoCompleteInteractionEvent) {
super.onAutoCompleteEvent(option, event)

Expand All @@ -90,10 +103,10 @@ class SystemCommand(
}

if (option == "args") {
val possibleProvider = dataProviders.firstOrNull { it.id == event.getOptionAsString("command", "")!! && it.supportArgsAutoComplete}
if(possibleProvider != null){
val possibleProvider = dataProviders.firstOrNull { it.id == event.getOptionAsString("command", "")!! && it.supportArgsAutoComplete }
if (possibleProvider != null) {
possibleProvider.onArgsAutoComplete(event)
}else {
} else {
event.replyChoices().queue()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class AdminSearchDataProvider(
lateinit var discordChannel: Channel
lateinit var discordEmote: Emote

override fun execute(event: SlashCommandInteractionEvent, hook: InteractionHook, command: SystemCommand) {
lastHook = hook
override fun execute(event: SlashCommandInteractionEvent, hook: InteractionHook?, command: SystemCommand) {
lastHook = hook!!

val id = event.getOptionAsString("args", "")!!
if (id.isEmpty()) {
Expand Down Expand Up @@ -278,7 +278,8 @@ class AdminSearchDataProvider(

val emotes = checkForEmote(arg)
if (emotes != null && emotes.isNotEmpty()) {
event.replyChoices(emotes.map { net.dv8tion.jda.api.interactions.commands.Command.Choice(it.name + " (Emote) (${it.guild?.name ?: ""})", it.id) }).queue()
event.replyChoices(emotes.map { net.dv8tion.jda.api.interactions.commands.Command.Choice(it.name + " (Emote) (${it.guild?.name ?: ""})", it.id) })
.queue()
return
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class LeaveServerProvider(
lateinit var lastEmbed: EmbedBuilder
lateinit var selectedServer: Guild

override fun execute(event: SlashCommandInteractionEvent, hook: InteractionHook, command: SystemCommand) {
lastHook = hook
override fun execute(event: SlashCommandInteractionEvent, hook: InteractionHook?, command: SystemCommand) {
lastHook = hook!!

val id = event.getOptionAsString("args", "")!!
if (id.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package io.viascom.discord.bot.starter.bot.command.systemcommand

import io.viascom.discord.bot.starter.bot.command.SystemCommand
import io.viascom.discord.bot.starter.bot.emotes.AlunaEmote
import io.viascom.discord.bot.starter.bot.handler.Command
import io.viascom.discord.bot.starter.bot.handler.queueAndRegisterInteraction
import io.viascom.discord.bot.starter.util.createTextInput
import io.viascom.discord.bot.starter.util.getOptionAsString
import io.viascom.discord.bot.starter.util.getValueAsString
import net.dv8tion.jda.api.entities.GuildMessageChannel
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.interactions.InteractionHook
import net.dv8tion.jda.api.interactions.components.ActionRow
import net.dv8tion.jda.api.interactions.components.Modal
import net.dv8tion.jda.api.interactions.components.text.TextInput
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle
import net.dv8tion.jda.api.sharding.ShardManager
import java.util.stream.Collectors

@Command
class PurgeMessagesProvider(
private val shardManager: ShardManager
) : SystemCommandDataProvider(
"purge_messages",
"Purge Messages",
true,
false,
true,
true
) {

lateinit var selectedChannel: GuildMessageChannel

override fun execute(event: SlashCommandInteractionEvent, hook: InteractionHook?, command: SystemCommand) {

val id = event.getOptionAsString("args", "")!!
if (id.isEmpty()) {
event.deferReply().setContent("${AlunaEmote.BOT_CROSS.asMention()} Please specify an ID as argument for this command").queue()
return
}

val channel = shardManager.getChannelById(GuildMessageChannel::class.java, id)
if (channel == null) {
event.deferReply().setContent("${AlunaEmote.BOT_CROSS.asMention()} Please specify a valid channel ID as argument for this command").queue()
return
}

selectedChannel = channel

//Show modal
val amount: TextInput = createTextInput("amount", "Amount of messages", TextInputStyle.SHORT)

val modal: Modal = Modal.create("purge", "Purge")
.addActionRows(ActionRow.of(amount))
.build()

event.replyModal(modal).queueAndRegisterInteraction(command)
}

override fun onModalInteraction(event: ModalInteractionEvent, additionalData: HashMap<String, Any?>): Boolean {
event.deferReply(true).queue {
it.editOriginal(
"${AlunaEmote.BOT_CHECK.asMention()} Removing ${
event.getValueAsString(
"amount",
"0"
)
} messages from ${selectedChannel.asMention}..."
).queue()

val number = event.getValueAsString("amount", "0")!!.toLong()
selectedChannel.iterableHistory.queue {
val messages = it.stream().limit(number).collect(Collectors.toList())
selectedChannel.purgeMessages(messages)
}
}
return true
}

override fun onArgsAutoComplete(event: CommandAutoCompleteInteractionEvent) {
val input = event.getOptionAsString("args", "")!!

if (input == "") {
event.replyChoices().queue()
return
}

val possibleChannel = shardManager.getChannelById(GuildMessageChannel::class.java, input)

if (possibleChannel != null) {
event.replyChoices(net.dv8tion.jda.api.interactions.commands.Command.Choice(possibleChannel.name, possibleChannel.id)).queue()
} else {
event.replyChoices().queue()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.viascom.discord.bot.starter.bot.command.systemcommand

import io.viascom.discord.bot.starter.bot.command.SystemCommand
import io.viascom.discord.bot.starter.bot.handler.CommandScopedObject
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent
Expand All @@ -15,6 +16,7 @@ abstract class SystemCommandDataProvider(
var ephemeral: Boolean = true,
var allowMods: Boolean = false,
var supportArgsAutoComplete: Boolean = false,
var keepCommandOpen: Boolean = false
) : CommandScopedObject {

override lateinit var uniqueId: String
Expand All @@ -23,7 +25,7 @@ abstract class SystemCommandDataProvider(
override var beanUseAutoCompleteBean: Boolean = true
override var beanRemoveObserverOnDestroy: Boolean = false

abstract fun execute(event: SlashCommandInteractionEvent, hook: InteractionHook, command: SystemCommand)
abstract fun execute(event: SlashCommandInteractionEvent, hook: InteractionHook?, command: SystemCommand)
open fun onButtonInteraction(event: ButtonInteractionEvent): Boolean {
return true
}
Expand All @@ -36,4 +38,9 @@ abstract class SystemCommandDataProvider(
open fun onSelectMenuInteractionTimeout() {}
open fun onArgsAutoComplete(event: CommandAutoCompleteInteractionEvent) {}


open fun onModalInteraction(event: ModalInteractionEvent, additionalData: HashMap<String, Any?>): Boolean {
return true
}
open fun onModalInteractionTimeout(additionalData: HashMap<String, Any?>) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class AdminSearchOverviewPage(
"${discordServer.owner?.asMention} | ${discordServer.owner?.effectiveName} (`${discordServer.ownerId}`)\n" +
"Owner on Support Server: " + (if (discordServer.owner?.user?.mutualGuilds?.any { it.id == alunaProperties.command.systemCommand.supportServer } == true) AlunaEmote.SMALL_TICK.asMention() + " Yes" else AlunaEmote.SMALL_CROSS.asMention() + " No"),
false)
if(alunaProperties.discord.gatewayIntents.any { it == GatewayIntent.GUILD_MEMBERS }) {
if (alunaProperties.discord.gatewayIntents.any { it == GatewayIntent.GUILD_MEMBERS }) {
embedBuilder.addField("Members", discordServer.memberCount.toString(), true)
}
embedBuilder.addField("Channels", discordServer.channels.size.toString(), true)
Expand All @@ -83,7 +83,7 @@ class AdminSearchOverviewPage(
)
embedBuilder.addField("In-Server-Name", discordServer.selfMember.effectiveName, true)
embedBuilder.addField("Features", discordServer.features.joinToString(" | "), false)
if(alunaProperties.discord.gatewayIntents.any { it == GatewayIntent.GUILD_MEMBERS }) {
if (alunaProperties.discord.gatewayIntents.any { it == GatewayIntent.GUILD_MEMBERS }) {
embedBuilder.addField("Other Bots", discordServer.loadMembers().get().filter { it.user.isBot }.joinToString(", ") { it.user.asTag }, false)
}
}
Expand Down Expand Up @@ -133,14 +133,12 @@ class AdminSearchOverviewPage(
embedBuilder.addBlankField(true)
}
embedBuilder.addField("Color", (if (discordRole.color != null) "`${discordRole.color!!.toHex()}`" else "n/a"), true)
if(alunaProperties.discord.gatewayIntents.any { it == GatewayIntent.GUILD_MEMBERS }) {
val memberCount = discordRole.guild.members.count { it.roles.contains(discordRole) }
embedBuilder.addField(
"Member-Count",
memberCount.toString() + " (${(memberCount.toDouble() / discordRole.guild.members.size * 100).round(2)}%)",
false
)
}
val memberCount = discordRole.guild.members.count { it.roles.contains(discordRole) }
embedBuilder.addField(
"Member-Count",
memberCount.toString() + " (${(memberCount.toDouble() / discordRole.guild.members.size * 100).round(2)}%)",
false
)
}

override fun onChannelRequest(discordChannel: Channel, embedBuilder: EmbedBuilder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,13 @@ open class SlashCommandInteractionInitializer(

//Check if system command should be global
if (server == null) {
shardManager.shards.first().upsertCommand(command)?.queue { discordCommand ->
printCommand(command)
discordBot.commands[command.name] = command.javaClass
discordBot.commandsWithAutocomplete.add(command.name)
val serverCommand = shardManager.shards.first().retrieveCommands().complete().first { it.name == command.name }
if(!compareCommands(command, serverCommand)) {
shardManager.shards.first().upsertCommand(command)?.queue { discordCommand ->
printCommand(command)
discordBot.commands[command.name] = command.javaClass
discordBot.commandsWithAutocomplete.add(command.name)
}
}
} else {
var upsert = serverCommands != null && serverCommands.none { it.name == "system-command" }
Expand Down
19 changes: 14 additions & 5 deletions src/main/kotlin/io/viascom/discord/bot/starter/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,20 @@ fun createTextInput(
placeholder: String? = null,
min: Int = -1,
max: Int = -1
): TextInput = TextInput.create(id, label, style)
.setPlaceholder(placeholder)
.setMinLength(min)
.setMaxLength(max)
.build()
): TextInput {
val builder = TextInput.create(id, label, style)
.setPlaceholder(placeholder)

if (min != -1) {
builder.minLength = min
}

if (max != -1) {
builder.maxLength = max
}

return builder.build()
}

fun createPrimaryButton(id: String, label: String? = null, emoji: Emoji? = null): Button = Button.of(ButtonStyle.PRIMARY, id, label, emoji)
fun createSecondaryButton(id: String, label: String? = null, emoji: Emoji? = null): Button = Button.of(ButtonStyle.SECONDARY, id, label, emoji)
Expand Down

0 comments on commit 72c15ea

Please sign in to comment.