Skip to content

Commit

Permalink
fix: allow users to use #channel syntax for channel-id parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkAtra committed Jun 17, 2024
1 parent 81b21e0 commit b80c48f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 40 deletions.
4 changes: 2 additions & 2 deletions docs/bot-companion.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ It does this by exposing [additional http endpoints](#endpoints) on the servers

### Enabling the activity or kill feed

1. Use the `/update-server` command to update the status monitor and set `player-activity-feed-channel` to the discord channel you want the activity feed to
appear in. You can do the same for the kill feed by setting `pvp-kill-feed-channel`.
1. Use the `/update-server` command to update the status monitor and set `player-activity-feed-channel-id` to the id of the discord channel you want the
activity feed to appear in. You can do the same for the kill feed by setting `pvp-kill-feed-channel-id`.
2. The bot will now post a message whenever a player joins or leaves the server or whenever someone was killed in a PvP battle. It looks something like this:

<img alt="Companion Preview" src="assets/companion-preview.png" width="400"/>
Expand Down
56 changes: 28 additions & 28 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,40 @@ Admins can use this command in DMs, see [Configuration Properties](configuration

Adds a server to the status monitor.

| Parameter | Description | Required | Default value |
|--------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|---------------|
| `server-hostname` | The hostname of the server to add a status monitor for. | `true` | `null` |
| `server-query-port` | The query port of the server to add a status monitor for. | `true` | `null` |
| `server-api-hostname` | The hostname to use when querying the server's api. Only required if you're planing to use the [v-rising-discord-bot-companion integration](bot-companion.md). | `false` | `null` |
| `server-api-port` | The api port of the server. Only required if you're planing to use the [v-rising-discord-bot-companion integration](bot-companion.md). | `false` | `null` |
| `server-api-username` | The username used to authenticate to the api of the server. Only required if you're planing to use the [v-rising-discord-bot-companion integration](bot-companion.md). | `false` | `null` |
| `server-api-password` | The password used to authenticate to the api of the server. Only required if you're planing to use the [v-rising-discord-bot-companion integration](bot-companion.md). | `false` | `null` |
| `embed-enabled` | Whether or not a discord status embed should be posted. | `false` | `true` |
| `display-server-description` | Whether or not to display the v rising server description on discord. | `false` | `true` |
| `display-player-gear-level` | Whether or not to display each player's gear level. Only has an effect if `server-api-hostname` and `server-api-port` are set. | `false` | `true` |
| `player-activity-feed-channel` | The channel to post the player activity feed in. Only has an effect if `server-api-hostname` and `server-api-port` are set. | `false` | `null` |
| `pvp-kill-feed-channel` | The channel to post the pvp kill feed in. Only has an effect if `server-api-hostname` and `server-api-port` are set. Requires at least version `0.4.0` of the [v-rising-discord-bot-companion](bot-companion.md). | `false` | `null` |
| Parameter | Description | Required | Default value |
|-----------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|---------------|
| `server-hostname` | The hostname of the server to add a status monitor for. | `true` | `null` |
| `server-query-port` | The query port of the server to add a status monitor for. | `true` | `null` |
| `server-api-hostname` | The hostname to use when querying the server's api. Only required if you're planing to use the [v-rising-discord-bot-companion integration](bot-companion.md). | `false` | `null` |
| `server-api-port` | The api port of the server. Only required if you're planing to use the [v-rising-discord-bot-companion integration](bot-companion.md). | `false` | `null` |
| `server-api-username` | The username used to authenticate to the api of the server. Only required if you're planing to use the [v-rising-discord-bot-companion integration](bot-companion.md). | `false` | `null` |
| `server-api-password` | The password used to authenticate to the api of the server. Only required if you're planing to use the [v-rising-discord-bot-companion integration](bot-companion.md). | `false` | `null` |
| `embed-enabled` | Whether or not a discord status embed should be posted. | `false` | `true` |
| `display-server-description` | Whether or not to display the v rising server description on discord. | `false` | `true` |
| `display-player-gear-level` | Whether or not to display each player's gear level. Only has an effect if `server-api-hostname` and `server-api-port` are set. | `false` | `true` |
| `player-activity-feed-channel-id` | The id of the channel to post the player activity feed in. Only has an effect if `server-api-hostname` and `server-api-port` are set. | `false` | `null` |
| `pvp-kill-feed-channel-id` | The id of the channel to post the pvp kill feed in. Only has an effect if `server-api-hostname` and `server-api-port` are set. Requires at least version `0.4.0` of the [v-rising-discord-bot-companion](bot-companion.md). | `false` | `null` |

## `/update-server`

Updates the given server status monitor. Only parameters specified are updated, all other parameters remain unchanged.
Admins can use this command in DMs, see [Configuration Properties](configuration-properties.md) for details.

| Parameter | Description | Required | Default value |
|--------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|---------------|
| `server-status-monitor-id` | The id of the server status monitor to update. | `true` | `null` |
| `server-hostname` | The hostname of the server to add a status monitor for. | `false` | `null` |
| `server-query-port` | The query port of the server to add a status monitor for. | `false` | `null` |
| `server-api-hostname` | The hostname to use when querying the server's api. Only required if you're planing to use the [v-rising-discord-bot-companion integration](bot-companion.md). | `false` | `null` |
| `server-api-port` | The api port of the server. Only required if you're planing to use the [v-rising-discord-bot-companion integration](bot-companion.md). | `false` | `null` |
| `server-api-username` | The username used to authenticate to the api of the server. Only required if you're planing to use the [v-rising-discord-bot-companion integration](bot-companion.md). | `false` | `null` |
| `server-api-password` | The password used to authenticate to the api of the server. Only required if you're planing to use the [v-rising-discord-bot-companion integration](bot-companion.md). | `false` | `null` |
| `status` | Determines if a server status monitor should be updated or not. Either `ACTIVE` or `INACTIVE`. | `false` | `null` |
| `embed-enabled` | Whether or not a discord status embed should be posted. Set this to false if you only want to use the activity or kill feed feature of the bot. | `false` | `true` |
| `display-server-description` | Whether or not to display the v rising server description on discord. | `false` | `true` |
| `display-player-gear-level` | Whether or not to display each player's gear level. Only has an effect if `server-api-hostname` and `server-api-port` are set. | `false` | `true` |
| `player-activity-feed-channel` | The channel to post the player activity feed in. Only has an effect if `server-api-hostname` and `server-api-port` are set. | `false` | `null` |
| `pvp-kill-feed-channel` | The channel to post the pvp kill feed in. Only has an effect if `server-api-hostname` and `server-api-port` are set. Requires at least version `0.4.0` of the [v-rising-discord-bot-companion](bot-companion.md). | `false` | `null` |
| Parameter | Description | Required | Default value |
|-----------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|---------------|
| `server-status-monitor-id` | The id of the server status monitor to update. | `true` | `null` |
| `server-hostname` | The hostname of the server to add a status monitor for. | `false` | `null` |
| `server-query-port` | The query port of the server to add a status monitor for. | `false` | `null` |
| `server-api-hostname` | The hostname to use when querying the server's api. Only required if you're planing to use the [v-rising-discord-bot-companion integration](bot-companion.md). | `false` | `null` |
| `server-api-port` | The api port of the server. Only required if you're planing to use the [v-rising-discord-bot-companion integration](bot-companion.md). | `false` | `null` |
| `server-api-username` | The username used to authenticate to the api of the server. Only required if you're planing to use the [v-rising-discord-bot-companion integration](bot-companion.md). | `false` | `null` |
| `server-api-password` | The password used to authenticate to the api of the server. Only required if you're planing to use the [v-rising-discord-bot-companion integration](bot-companion.md). | `false` | `null` |
| `status` | Determines if a server status monitor should be updated or not. Either `ACTIVE` or `INACTIVE`. | `false` | `null` |
| `embed-enabled` | Whether or not a discord status embed should be posted. Set this to false if you only want to use the activity or kill feed feature of the bot. | `false` | `true` |
| `display-server-description` | Whether or not to display the v rising server description on discord. | `false` | `true` |
| `display-player-gear-level` | Whether or not to display each player's gear level. Only has an effect if `server-api-hostname` and `server-api-port` are set. | `false` | `true` |
| `player-activity-feed-channel-id` | The id of the channel to post the player activity feed in. Only has an effect if `server-api-hostname` and `server-api-port` are set. | `false` | `null` |
| `pvp-kill-feed-channel-id` | The id of the channel to post the pvp kill feed in. Only has an effect if `server-api-hostname` and `server-api-port` are set. Requires at least version `0.4.0` of the [v-rising-discord-bot-companion](bot-companion.md). | `false` | `null` |

## `/remove-server`

Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/de/darkatra/vrising/discord/KordExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import de.darkatra.vrising.discord.serverstatus.exceptions.InvalidDiscordChannel
import dev.kord.common.entity.Snowflake
import dev.kord.core.Kord
import dev.kord.core.behavior.channel.MessageChannelBehavior
import dev.kord.core.entity.interaction.InteractionCommand

suspend fun Kord.getDiscordChannel(discordChannelId: String): Result<MessageChannelBehavior> {
val channel = try {
Expand All @@ -16,3 +17,11 @@ suspend fun Kord.getDiscordChannel(discordChannelId: String): Result<MessageChan
}
return Result.success(channel)
}

private val channelPattern = Regex("<#([0-9]+)>")

fun InteractionCommand.getChannelIdFromStringParameter(parameterName: String): String? {
val value = strings[parameterName] ?: return null
val match = channelPattern.find(value) ?: return value
return match.groups[1]!!.value
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package de.darkatra.vrising.discord.commands.parameters

import de.darkatra.vrising.discord.getChannelIdFromStringParameter
import dev.kord.core.entity.interaction.ChatInputCommandInteraction
import dev.kord.rest.builder.interaction.GlobalChatInputCreateBuilder
import dev.kord.rest.builder.interaction.channel
import dev.kord.rest.builder.interaction.string

object PlayerActivityFeedChannelIdParameter {
const val NAME = "player-activity-feed-channel"
const val NAME = "player-activity-feed-channel-id"
}

fun GlobalChatInputCreateBuilder.addPlayerActivityFeedChannelIdParameter(required: Boolean = true) {
channel(
string(
name = PlayerActivityFeedChannelIdParameter.NAME,
description = "The channel to post the player activity feed in."
description = "The id of the channel to post the player activity feed in."
) {
this.required = required
}
}

fun ChatInputCommandInteraction.getPlayerActivityFeedChannelIdParameter(): String? {
return command.channels[PlayerActivityFeedChannelIdParameter.NAME]?.id?.toString()
return command.getChannelIdFromStringParameter(PlayerActivityFeedChannelIdParameter.NAME)
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package de.darkatra.vrising.discord.commands.parameters

import de.darkatra.vrising.discord.getChannelIdFromStringParameter
import dev.kord.core.entity.interaction.ChatInputCommandInteraction
import dev.kord.rest.builder.interaction.GlobalChatInputCreateBuilder
import dev.kord.rest.builder.interaction.channel
import dev.kord.rest.builder.interaction.string

object PvpKillFeedChannelIdParameter {
const val NAME = "pvp-kill-feed-channel"
const val NAME = "pvp-kill-feed-channel-id"
}

fun GlobalChatInputCreateBuilder.addPvpKillFeedChannelIdParameter(required: Boolean = true) {
channel(
string(
name = PvpKillFeedChannelIdParameter.NAME,
description = "The channel to post the pvp kill feed in."
description = "The id of the channel to post the pvp kill feed in."
) {
this.required = required
}
}

fun ChatInputCommandInteraction.getPvpKillFeedChannelIdParameter(): String? {
return command.channels[PvpKillFeedChannelIdParameter.NAME]?.id?.toString()
return command.getChannelIdFromStringParameter(PvpKillFeedChannelIdParameter.NAME)
}

0 comments on commit b80c48f

Please sign in to comment.