Skip to content

Commit

Permalink
feat: add an option to hide the gear level in the player list
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkAtra committed Jun 8, 2022
1 parent e234915 commit 62155e7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ Please note that all commands are [guild](https://discord.com/developers/docs/re

## Configuration Properties

| Property | Type | Description | Default value |
|-------------------------|--------|----------------------------------------------------------------------------------------------------------------------------------|------------------------|
| `bot.discord-bot-token` | String | The token for the discord bot. You can find this in the [discord developer portal](https://discord.com/developers/applications). | `null` |
| `bot.database-path` | Path | The path to the database file. Should be overwritten when running inside a docker container. | `./bot.db` |
| `bot.database-username` | String | The username for the database. | `v-rising-discord-bot` |
| `bot.database-password` | String | The password for the database. | `null` |
| Property | Type | Description | Default value |
|---------------------------------|---------|----------------------------------------------------------------------------------------------------------------------------------|------------------------|
| `bot.discord-bot-token` | String | The token for the discord bot. You can find this in the [discord developer portal](https://discord.com/developers/applications). | `null` |
| `bot.database-path` | Path | The path to the database file. Should be overwritten when running inside a docker container. | `./bot.db` |
| `bot.database-username` | String | The username for the database. | `v-rising-discord-bot` |
| `bot.database-password` | String | The password for the database. | `null` |
| `bot.display-player-gear-level` | Boolean | Whether or not to display the gear level in the player list. | `true` |

## How to run it yourself using docker-compose

Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/de/darkatra/vrising/discord/BotProperties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ class BotProperties {

@field:NotBlank
lateinit var databasePassword: String

@field:NotNull
var displayPlayerGearLevel: Boolean? = null
}
35 changes: 29 additions & 6 deletions src/main/kotlin/de/darkatra/vrising/discord/ServerStatusEmbed.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,39 @@ import dev.kord.rest.builder.message.modify.embed

object ServerStatusEmbed {

suspend fun create(serverInfo: SourceServer, players: List<SourcePlayer>, rules: Map<String, Any>, channel: MessageChannelBehavior): Snowflake {
suspend fun create(
serverInfo: SourceServer,
players: List<SourcePlayer>,
rules: Map<String, Any>,
displayPlayerGearLevel: Boolean,
channel: MessageChannelBehavior
): Snowflake {
return channel.createEmbed {
buildEmbed(serverInfo, players, rules, this)
buildEmbed(serverInfo, players, rules, displayPlayerGearLevel, this)
}.id
}

suspend fun update(serverInfo: SourceServer, players: List<SourcePlayer>, rules: Map<String, Any>, message: Message): Snowflake {
suspend fun update(
serverInfo: SourceServer,
players: List<SourcePlayer>,
rules: Map<String, Any>,
displayPlayerGearLevel: Boolean,
message: Message
): Snowflake {
return message.edit {
embed {
buildEmbed(serverInfo, players, rules, this)
buildEmbed(serverInfo, players, rules, displayPlayerGearLevel, this)
}
}.id
}

private fun buildEmbed(serverInfo: SourceServer, players: List<SourcePlayer>, rules: Map<String, Any>, embedBuilder: EmbedBuilder) {
private fun buildEmbed(
serverInfo: SourceServer,
players: List<SourcePlayer>,
rules: Map<String, Any>,
displayPlayerGearLevel: Boolean,
embedBuilder: EmbedBuilder
) {
embedBuilder.apply {
title = serverInfo.name
color = Color(
Expand Down Expand Up @@ -65,7 +83,12 @@ object ServerStatusEmbed {
.forEach { chunk ->
field {
name = "Online players"
value = chunk.joinToString(separator = "\n") { player -> "**${player.name}** - ${player.score}" }
value = chunk.joinToString(separator = "\n") { player ->
when (displayPlayerGearLevel) {
true -> "**${player.name}** - ${player.score}"
false -> "**${player.name}**"
}
}
inline = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import kotlin.io.path.absolutePathString
@EnableConfigurationProperties(BotProperties::class)
class ServerStatusMonitorService(
private val serverQueryClient: ServerQueryClient,
botProperties: BotProperties
private val botProperties: BotProperties
) : CoroutineScope, DisposableBean {

override val coroutineContext: CoroutineContext = Dispatchers.Default
Expand Down Expand Up @@ -73,14 +73,26 @@ class ServerStatusMonitorService(
val currentEmbedMessageId = serverStatusConfiguration.currentEmbedMessageId
if (currentEmbedMessageId != null) {
try {
ServerStatusEmbed.update(serverInfo, players, rules, channel.getMessage(Snowflake(currentEmbedMessageId)))
ServerStatusEmbed.update(
serverInfo = serverInfo,
players = players,
rules = rules,
displayPlayerGearLevel = botProperties.displayPlayerGearLevel!!,
message = channel.getMessage(Snowflake(currentEmbedMessageId))
)
return@forEach
} catch (e: EntityNotFoundException) {
serverStatusConfiguration.currentEmbedMessageId = null
}
}

serverStatusConfiguration.currentEmbedMessageId = ServerStatusEmbed.create(serverInfo, players, rules, channel).toString()
serverStatusConfiguration.currentEmbedMessageId = ServerStatusEmbed.create(
serverInfo = serverInfo,
players = players,
rules = rules,
displayPlayerGearLevel = botProperties.displayPlayerGearLevel!!,
channel = channel
).toString()
putServerStatusMonitor(serverStatusConfiguration)
}
}.onFailure { throwable ->
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ bot:
database-path: ./bot.db
database-username: v-rising-discord-bot
database-password: ~
display-player-gear-level: false

0 comments on commit 62155e7

Please sign in to comment.