Basic bot isnt... doing anything?
package aster.amo
import aster.amo.App.Companion.LOGGER
import aster.amo.App.Companion.getMessageCountInGuild
import dev.kord.core.Kord
import dev.kord.core.behavior.interaction.response.respond
import dev.kord.core.entity.Application
import dev.kord.core.entity.Guild
import dev.kord.core.entity.Member
import dev.kord.core.entity.channel.TextChannel
import dev.kord.core.event.interaction.GuildChatInputCommandInteractionCreateEvent
import dev.kord.core.on
import dev.kord.gateway.Intent
import dev.kord.gateway.PrivilegedIntent
import dev.kord.rest.builder.interaction.boolean
import dev.kord.rest.builder.interaction.string
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.count
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.flatMapConcat
import kotlinx.coroutines.launch
import kotlinx.datetime.Instant
import org.slf4j.LoggerFactory
@OptIn(PrivilegedIntent::class)
suspend fun main(args: Array<String>) {
val kord = Kord("TOKEN_WAS_HERE")
LOGGER.info("Logged in as ${kord.getSelf().username}")
val command = kord.createGlobalChatInputCommand("purge_joins_between", "purges joins between two dates") {
string("start_date", "The start date to purge joins from") {
required = true
}
string("end_date", "The end date to purge joins from") {
required = true
}
boolean("has_messaged", "Whether to remove if the user has messaged in the server") {
required = false
default = false
}
}
LOGGER.info("Registered command: " + command.name)
kord.on<GuildChatInputCommandInteractionCreateEvent> {
val response = interaction.deferPublicResponse()
val command = interaction.command
val startDate = command.strings["start_date"] ?: return@on
val endDate = command.strings["end_date"] ?: return@on
val hasMessaged = command.options["has_messaged"]?.value
val startDateEpoch: Instant = Instant.parse(startDate)
val endDateEpoch: Instant = Instant.parse(endDate)
var kickedCount = 0
this.interaction.guild.asGuild().members.collect {
if (it.joinedAt != null) {
val joinedAt = it.joinedAt ?: return@collect
if (joinedAt in startDateEpoch..endDateEpoch) {
if (hasMessaged == null || hasMessaged == false) {
it.kick()
kickedCount++
} else {
val messages = getMessageCountInGuild(it, this.interaction.guild.asGuild())
if (messages == 0) {
it.kick()
kickedCount++
}
}
}
}
}
response.respond { content = "Kicked $kickedCount members" }
}
kord.login {
intents += Intent.Guilds
intents += Intent.GuildMembers
intents += Intent.GuildMessages
intents += Intent.GuildMessageReactions
}
}
class App {
companion object {
val LOGGER = LoggerFactory.getLogger("App")
@OptIn(PrivilegedIntent::class)
suspend fun getMessageCountInGuild(member: Member, guild: Guild): Int {
val channels = guild.channels.filterIsInstance<TextChannel>()
return channels.flatMapConcat { channel ->
channel.messages.filter { it.author?.id == member.id }
}.count()
}
}
}
package aster.amo
import aster.amo.App.Companion.LOGGER
import aster.amo.App.Companion.getMessageCountInGuild
import dev.kord.core.Kord
import dev.kord.core.behavior.interaction.response.respond
import dev.kord.core.entity.Application
import dev.kord.core.entity.Guild
import dev.kord.core.entity.Member
import dev.kord.core.entity.channel.TextChannel
import dev.kord.core.event.interaction.GuildChatInputCommandInteractionCreateEvent
import dev.kord.core.on
import dev.kord.gateway.Intent
import dev.kord.gateway.PrivilegedIntent
import dev.kord.rest.builder.interaction.boolean
import dev.kord.rest.builder.interaction.string
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.count
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.flatMapConcat
import kotlinx.coroutines.launch
import kotlinx.datetime.Instant
import org.slf4j.LoggerFactory
@OptIn(PrivilegedIntent::class)
suspend fun main(args: Array<String>) {
val kord = Kord("TOKEN_WAS_HERE")
LOGGER.info("Logged in as ${kord.getSelf().username}")
val command = kord.createGlobalChatInputCommand("purge_joins_between", "purges joins between two dates") {
string("start_date", "The start date to purge joins from") {
required = true
}
string("end_date", "The end date to purge joins from") {
required = true
}
boolean("has_messaged", "Whether to remove if the user has messaged in the server") {
required = false
default = false
}
}
LOGGER.info("Registered command: " + command.name)
kord.on<GuildChatInputCommandInteractionCreateEvent> {
val response = interaction.deferPublicResponse()
val command = interaction.command
val startDate = command.strings["start_date"] ?: return@on
val endDate = command.strings["end_date"] ?: return@on
val hasMessaged = command.options["has_messaged"]?.value
val startDateEpoch: Instant = Instant.parse(startDate)
val endDateEpoch: Instant = Instant.parse(endDate)
var kickedCount = 0
this.interaction.guild.asGuild().members.collect {
if (it.joinedAt != null) {
val joinedAt = it.joinedAt ?: return@collect
if (joinedAt in startDateEpoch..endDateEpoch) {
if (hasMessaged == null || hasMessaged == false) {
it.kick()
kickedCount++
} else {
val messages = getMessageCountInGuild(it, this.interaction.guild.asGuild())
if (messages == 0) {
it.kick()
kickedCount++
}
}
}
}
}
response.respond { content = "Kicked $kickedCount members" }
}
kord.login {
intents += Intent.Guilds
intents += Intent.GuildMembers
intents += Intent.GuildMessages
intents += Intent.GuildMessageReactions
}
}
class App {
companion object {
val LOGGER = LoggerFactory.getLogger("App")
@OptIn(PrivilegedIntent::class)
suspend fun getMessageCountInGuild(member: Member, guild: Guild): Int {
val channels = guild.channels.filterIsInstance<TextChannel>()
return channels.flatMapConcat { channel ->
channel.messages.filter { it.author?.id == member.id }
}.count()
}
}
}
7 Replies
you are registering a global command and listening to guild-specific once.
it might be due to that
The "Registered command" log doesn't show up @amo ?
it does not, nothing does
you probably don't have a logging implementation
slf4j on its own won't log
you need something like logback, log4j or slf4j-simple
tried even using log4j and i get nothing
did you configure it?
yup
i moved to
lol
createGuildChatInputCommand
, command shows up now but permanently hangs, breakpointing it runs deferPublicResponse()
and then doesnt proceed
log4j half decided to start working now,
> Task :aster.amo.MainKt.main()
2025-01-28 12:55:21.411 [DefaultDispatcher-worker-9 @call-context#28] INFO App - Logged in as amo's pet bot
SLF4J(W): No SLF4J providers were found.
SLF4J(W): Defaulting to no-operation (NOP) logger implementation
SLF4J(W): See https://www.slf4j.org/codes.html#noProviders for further details.
2025-01-28 12:55:21.701 [DefaultDispatcher-worker-9 @call-context#32] INFO App - Registered command: purge_joins_between
2025-01-28 12:55:21.764 [DefaultDispatcher-worker-9 @call-context#32] INFO App - Logged in
> Task :aster.amo.MainKt.main()
2025-01-28 12:55:21.411 [DefaultDispatcher-worker-9 @call-context#28] INFO App - Logged in as amo's pet bot
SLF4J(W): No SLF4J providers were found.
SLF4J(W): Defaulting to no-operation (NOP) logger implementation
SLF4J(W): See https://www.slf4j.org/codes.html#noProviders for further details.
2025-01-28 12:55:21.701 [DefaultDispatcher-worker-9 @call-context#32] INFO App - Registered command: purge_joins_between
2025-01-28 12:55:21.764 [DefaultDispatcher-worker-9 @call-context#32] INFO App - Logged in
you seem to be missing the slf4j bindings
log4j-slf4j2-impl
afaik