Skip to content

Commit

Permalink
fix: set attributes for onAutocomplete
Browse files Browse the repository at this point in the history
feat: change bot list sender logic to check if config is valid
chore: bump to 0.0.37_5.0.0-beta.2
  • Loading branch information
itsmefox committed Dec 16, 2022
1 parent 2fa4176 commit 7ed6c2c
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 74 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
Gradle:
```gradle
dependencies {
implementation 'io.viascom.discord.bot:aluna-spring-boot-starter:0.0.36_5.0.0-beta.2'
implementation 'io.viascom.discord.bot:aluna-spring-boot-starter:0.0.37_5.0.0-beta.2'
}
```

Expand All @@ -40,7 +40,7 @@ Maven:
<dependency>
<groupId>io.viascom.discord.bot</groupId>
<artifactId>aluna-spring-boot-starter</artifactId>
<version>0.0.36_5.0.0-beta.2</version>
<version>0.0.37_5.0.0-beta.2</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ targetCompatibility = JavaVersion.VERSION_17
ext {
major = 0
minor = 0
patch = 36
patch = 37

isCiServer = System.getenv("GITHUB_ACTIONS") != null || System.getProperty("GITHUB_ACTIONS") != null

Expand Down
44 changes: 27 additions & 17 deletions src/main/kotlin/io/viascom/discord/bot/aluna/bot/DiscordCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ abstract class DiscordCommand @JvmOverloads constructor(

@JvmSynthetic
internal fun onAutoCompleteEventCall(option: String, event: CommandAutoCompleteInteractionEvent) {
setProperties(event)

currentSubFullCommandName = event.fullCommandName
MDC.put("interaction", event.fullCommandName)

discordInteractionLoadAdditionalData.loadDataBeforeAdditionalRequirements(this, event)

//Check additional requirements for this command
Expand All @@ -415,6 +420,26 @@ abstract class DiscordCommand @JvmOverloads constructor(
onAutoCompleteEvent(option, event)
}

@JvmSynthetic
internal fun setProperties(event: GenericInteractionCreateEvent) {
MDC.put("uniqueId", uniqueId)

guild = event.guild
guild?.let { MDC.put("discord.server", "${it.id} (${it.name})") }
author = event.user
MDC.put("discord.author", "${author.id} (${author.asTag})")

userLocale = event.userLocale
MDC.put("discord.author_locale", userLocale.locale)

if (guild != null) {
member = guild!!.getMember(author)
guildChannel = event.guildChannel
guildLocale = event.guildLocale
MDC.put("discord.server_locale", guildLocale.locale)
}
}

/**
* This method gets called if Aluna can not find a registered sub command
*
Expand Down Expand Up @@ -556,7 +581,6 @@ abstract class DiscordCommand @JvmOverloads constructor(
stopWatch!!.start()
}


if (!discordBot.discordRepresentations.containsKey(event.name)) {
val exception = AlunaInteractionRepresentationNotFoundException(event.name)
try {
Expand All @@ -569,26 +593,12 @@ abstract class DiscordCommand @JvmOverloads constructor(

discordRepresentation = discordBot.discordRepresentations[event.name]!!

setProperties(event)

currentSubFullCommandName = event.fullCommandName
MDC.put("interaction", event.fullCommandName)
MDC.put("uniqueId", uniqueId)

guild = event.guild
guild?.let { MDC.put("discord.server", "${it.id} (${it.name})") }
channel = event.channel
MDC.put("discord.channel", channel.id)
author = event.user
MDC.put("discord.author", "${author.id} (${author.asTag})")

userLocale = event.userLocale
MDC.put("discord.author_locale", userLocale.locale)

if (guild != null) {
member = guild!!.getMember(author)
guildChannel = event.guildChannel
guildLocale = event.guildLocale
MDC.put("discord.server_locale", guildLocale.locale)
}

//Check if this is an owner command
if (isOwnerCommand && author.idLong !in ownerIdProvider.getOwnerIds()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,31 @@ open class BotListHandler(

init {
val enabledSenders = senders.filter { it.isEnabled() }
if (alunaProperties.productionMode && enabledSenders.isNotEmpty()) {
val validSenders = enabledSenders.filter { it.isValid() }
val invalidSenders = enabledSenders.filter { !it.isValid() }

invalidSenders.forEach { sender ->
logger.warn(
"BotListSender ${sender.getName()} is not valid and will therefore not be used:\n" + sender.getValidationErrors()
.joinToString("\n") { "- $it" })
}

if (alunaProperties.productionMode && validSenders.isNotEmpty()) {
logger.info("Your bot stats are sent every 10 min to [${enabledSenders.joinToString(", ") { it.getName() }}]")
}

if (!alunaProperties.productionMode && enabledSenders.any { it.onProductionModeOnly() }) {
if (!alunaProperties.productionMode && validSenders.any { it.onProductionModeOnly() }) {
logger.info(
"Your bot stats will be sent every 10 min to [${
enabledSenders.filter { it.onProductionModeOnly() }.joinToString(", ") { it.getName() }
validSenders.filter { it.onProductionModeOnly() }.joinToString(", ") { it.getName() }
}] in production mode only."
)
}

if (!alunaProperties.productionMode && enabledSenders.any { !it.onProductionModeOnly() }) {
if (!alunaProperties.productionMode && validSenders.any { !it.onProductionModeOnly() }) {
logger.info(
"Your bot stats are sent every 10 min to [${
enabledSenders.filter { !it.onProductionModeOnly() }.joinToString(", ") { it.getName() }
validSenders.filter { !it.onProductionModeOnly() }.joinToString(", ") { it.getName() }
}]"
)
}
Expand All @@ -68,10 +77,15 @@ open class BotListHandler(
@Scheduled(cron = "0 */10 * * * *", zone = "UTC") //Send updates every 10 minutes
open fun sendStats() {
runBlocking(AlunaCoroutinesDispatcher.IO) {
senders.forEach { sender ->
senders.filter { it.isEnabled() && it.isValid() }.forEach { sender ->
try {
if (alunaProperties.productionMode || !sender.onProductionModeOnly()) {
launch(AlunaCoroutinesDispatcher.IO) { sender.sendStats(shardManager.guilds.size, shardManager.shardsTotal) }
launch(AlunaCoroutinesDispatcher.IO) {
sender.sendStats(
shardManager.guilds.size,
shardManager.shardsTotal
)
}
}
} catch (e: Exception) {
logger.error("Was not able to send stats to ${sender::class.qualifiedName}: " + e.stackTraceToString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ interface BotListSender {
fun isEnabled(): Boolean

fun getName(): String

fun isValid(): Boolean

fun getValidationErrors(): List<String>

fun sendStats(totalServer: Int, totalShards: Int)

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@ class BotsOnDiscordBotListSender(

override fun onProductionModeOnly(): Boolean = true

override fun isEnabled(): Boolean = alunaProperties.botList.botsOnDiscordToken != null
override fun isEnabled(): Boolean = alunaProperties.botList.botsOnDiscordToken?.enabled == true

override fun getName(): String = "bots.ondiscord.xyz"

override fun isValid(): Boolean = alunaProperties.botList.botsOnDiscordToken?.token != null

override fun getValidationErrors(): List<String> =
arrayListOf("Stats are not sent to bots.ondiscord.xyz because token (aluna.botList.botsOnDiscordToken.token) is not set")

override fun sendStats(totalServer: Int, totalShards: Int) {
val botsOnDiscordToken = alunaProperties.botList.botsOnDiscordToken ?: ""
if (botsOnDiscordToken.isBlank()) {
logger.debug("Stats are not sent to bots.ondiscord.xyz because token is not set")
return
}
val botsOnDiscordToken = alunaProperties.botList.botsOnDiscordToken?.token ?: ""

logger.debug("Send stats to bots.ondiscord.xyz")

Expand All @@ -63,9 +64,10 @@ class BotsOnDiscordBotListSender(
.readTimeout(120, TimeUnit.SECONDS)
.build()

val request = Request.Builder().url("https://bots.ondiscord.xyz/bot-api/bots/${alunaProperties.discord.applicationId}/guilds").post(
"{\"guildCount\": ${shardManager.guilds.size}}".toRequestBody("application/json".toMediaType())
).header("Authorization", botsOnDiscordToken).build()
val request = Request.Builder()
.url("https://bots.ondiscord.xyz/bot-api/bots/${alunaProperties.discord.applicationId}/guilds").post(
"{\"guildCount\": ${shardManager.guilds.size}}".toRequestBody("application/json".toMediaType())
).header("Authorization", botsOnDiscordToken).build()

httpClient.newCall(request).execute().body?.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@ class DiscordBotListBotListSender(

override fun onProductionModeOnly(): Boolean = true

override fun isEnabled(): Boolean = alunaProperties.botList.discordBotListToken != null
override fun isEnabled(): Boolean = alunaProperties.botList.discordBotListToken?.enabled == true

override fun getName(): String = "discordbotlist.com"

override fun isValid(): Boolean = alunaProperties.botList.discordBotListToken?.token != null

override fun getValidationErrors(): List<String> =
arrayListOf("Stats are not sent to discordbotlist.com because token (aluna.botList.discordBotListToken.token) is not set")

override fun sendStats(totalServer: Int, totalShards: Int) {
val discordBotListToken = alunaProperties.botList.discordBotListToken ?: ""
if (discordBotListToken.isBlank()) {
logger.debug("Stats are not sent to discordbotlist.com because token is not set")
return
}
val discordBotListToken = alunaProperties.botList.discordBotListToken?.token ?: ""

logger.debug("Send stats to discordbotlist.com")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@ class DiscordBotListEuBotListSender(

override fun onProductionModeOnly(): Boolean = true

override fun isEnabled(): Boolean = alunaProperties.botList.discordBotListEuToken != null
override fun isEnabled(): Boolean = alunaProperties.botList.discordBotListEuToken?.enabled == true

override fun getName(): String = "discord-botlist.eu"

override fun isValid(): Boolean = alunaProperties.botList.discordBotListEuToken?.token != null

override fun getValidationErrors(): List<String> =
arrayListOf("Stats are not sent to discord-botlist.eu because token (aluna.botList.discordBotListEuToken.token) is not set")

override fun sendStats(totalServer: Int, totalShards: Int) {
val discordBotListEuToken = alunaProperties.botList.discordBotListEuToken ?: ""
if (discordBotListEuToken.isBlank()) {
logger.debug("Stats are not sent to discord-botlist.eu because token is not set")
return
}
val discordBotListEuToken = alunaProperties.botList.discordBotListEuToken?.token ?: ""

logger.debug("Send stats to discord-botlist.eu")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@ class DiscordBotsBotListSender(

override fun onProductionModeOnly(): Boolean = true

override fun isEnabled(): Boolean = alunaProperties.botList.discordBotsToken != null
override fun isEnabled(): Boolean = alunaProperties.botList.discordBotsToken?.enabled == true

override fun getName(): String = "discord.bots.gg"

override fun isValid(): Boolean = alunaProperties.botList.discordBotsToken?.token != null

override fun getValidationErrors(): List<String> =
arrayListOf("Stats are not sent to discord.bots.gg because token (aluna.botList.discordBotsToken.token) is not set")

override fun sendStats(totalServer: Int, totalShards: Int) {
val discordBotsToken = alunaProperties.botList.discordBotsToken ?: ""
if (discordBotsToken.isBlank()) {
logger.debug("Stats are not sent to discord.bots.gg because token is not set")
return
}
val discordBotsToken = alunaProperties.botList.discordBotsToken?.token ?: ""

logger.debug("Send stats to discord.bots.gg")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ class TopGGBotListSender(

override fun onProductionModeOnly(): Boolean = true

override fun isEnabled(): Boolean = alunaProperties.botList.topggToken != null
override fun isEnabled(): Boolean = alunaProperties.botList.topggToken?.enabled == true

override fun getName(): String = "top.gg"
override fun isValid(): Boolean = alunaProperties.botList.topggToken?.token != null

override fun getValidationErrors(): List<String> =
arrayListOf("Stats are not sent to top.gg because token (aluna.botList.topggToken.token) is not set")

override fun sendStats(totalServer: Int, totalShards: Int) {
val topGGToken = alunaProperties.botList.topggToken ?: ""
if (topGGToken.isBlank()) {
logger.debug("Stats are not sent to top.gg because token is not set")
return
}
val topGGToken = alunaProperties.botList.topggToken?.token ?: ""

logger.debug("Send stats to top.gg")

Expand All @@ -65,9 +65,11 @@ class TopGGBotListSender(
.readTimeout(120, TimeUnit.SECONDS)
.build()

val request = Request.Builder().url("https://top.gg/api/bots/${alunaProperties.discord.applicationId}/stats").post(
objectMapper.writeValueAsBytes(TopGGData(shardManager.shards.map { it.guilds.size })).toRequestBody("application/json".toMediaType())
).header("Authorization", topGGToken).build()
val request =
Request.Builder().url("https://top.gg/api/bots/${alunaProperties.discord.applicationId}/stats").post(
objectMapper.writeValueAsBytes(TopGGData(shardManager.shards.map { it.guilds.size }))
.toRequestBody("application/json".toMediaType())
).header("Authorization", topGGToken).build()

httpClient.newCall(request).execute().body?.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,30 @@ class AlunaBotListProperties {
/**
* bots.ondiscord.xyz token
*/
var botsOnDiscordToken: String? = null
var botsOnDiscordToken: Configuration? = null

/**
* discord.bots.gg token
*/
var discordBotsToken: String? = null
var discordBotsToken: Configuration? = null

/**
* discordbotlist.com token
*/
var discordBotListToken: String? = null
var discordBotListToken: Configuration? = null

/**
* discord-botlist.eu token
*/
var discordBotListEuToken: String? = null
var discordBotListEuToken: Configuration? = null

/**
* Top.gg token
*/
var topggToken: String? = null

var topggToken: Configuration? = null

class Configuration {
var enabled: Boolean = false
var token: String? = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ import io.viascom.discord.bot.aluna.bot.SubCommandElement
import io.viascom.discord.bot.aluna.bot.interaction.animal.BunnyCommand
import io.viascom.discord.bot.aluna.bot.interaction.animal.ForestSubCommandGroup
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import org.springframework.beans.factory.annotation.Autowired

@Interaction
class AnimalsCommand(
@SubCommandElement
private val forestSubCommandGroup: ForestSubCommandGroup,
@SubCommandElement
private val bunnyCommand: BunnyCommand
) : DiscordCommand("animal", "Show images of animals", handleSubCommands = true) {

@Autowired
@SubCommandElement
private lateinit var bunnyCommand: BunnyCommand

override fun execute(event: SlashCommandInteractionEvent) {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ package io.viascom.discord.bot.aluna.bot.interaction.animal
import io.viascom.discord.bot.aluna.bot.DiscordSubCommandGroup
import io.viascom.discord.bot.aluna.bot.Interaction
import io.viascom.discord.bot.aluna.bot.SubCommandElement
import org.springframework.beans.factory.annotation.Autowired

@Interaction
class ForestSubCommandGroup(
class ForestSubCommandGroup : DiscordSubCommandGroup("forest", "Forest animals") {

@Autowired
@SubCommandElement
private val foxCommand: FoxCommand
) : DiscordSubCommandGroup("forest", "Forest animals")
private lateinit var foxCommand: FoxCommand
}
Loading

0 comments on commit 7ed6c2c

Please sign in to comment.