Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions chattore/src/main/kotlin/feature/Discord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ fun String.discordEscape() = this.replace("""_""", "\\_")
data class DiscordConfig(
val enable: Boolean = false,
val networkToken: String = "nouNetwork",
val channelId: Long = 1234L,
val chadId: Long = 1234L,
val gameChatChannelId: Long = 1234L,
val patrickId: Long = 1234L,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes in config like this require a matching migration in Config.kt, so that we don't have to manually edit config files unless actually necessary.

For the comment: Better to remove it and instead rename the channelId field itself to reflect the usage, if it's important. I think channelId is ok, but gameChatChannelId would indeed be more explicit. (And this would also require a migration)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thought here would be to have an allowedBots list of IDs instead. It'd be a bit cleaner, if not a bit overengineered for our needs. (Although I can see that being useful on a testing server with multiple game chat bridges and Patrick instances... hmm)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I thought about a list too, but I didn't want to put too much into this PR. If you say these should come together though, I'll work on that today or tomorrow.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine to put it in this PR since it's being changed anyway (saves one config migration later too 😎). I think it's a nice idea so ye go ahead

val playingMessage: String = "on the ORE Network",
val discordFormat: String = "`%prefix%` **%sender%**: %message%",
val serverTokens: Map<String, String> = mapOf(
Expand Down Expand Up @@ -70,8 +70,8 @@ fun PluginScope.createDiscordFeature(
}
}
val discordMap = spawnServerBots(proxy, logger, config)
val serverChannels = discordMap.mapValues { (_, api) -> getGameChat(api, config.channelId) }
val mainBotChannel = getGameChat(discordNetwork, config.channelId)
val serverChannels = discordMap.mapValues { (_, api) -> getGameChat(api, config.gameChatChannelId) }
val mainBotChannel = getGameChat(discordNetwork, config.gameChatChannelId)
val listener = DiscordListener(logger, messenger, proxy, emojis, config)
@OptIn(KordPreview::class)
mainBotChannel.live().onMessageCreate(block = listener::onMessageCreate)
Expand All @@ -90,8 +90,8 @@ fun PluginScope.createDiscordFeature(
}
}

private suspend fun getGameChat(api: Kord, id: Long): TextChannel = api.getChannelOf(Snowflake(id))
?: throw IllegalArgumentException("Cannot find game-chat channel")
private suspend fun getGameChat(api: Kord, channelId: Long): TextChannel = api.getChannelOf(Snowflake(channelId))
?: throw IllegalArgumentException("Cannot find game-chat channel (with id $channelId)")

private class DiscordBroadcastListener(
private val config: DiscordConfig,
Expand Down Expand Up @@ -141,10 +141,15 @@ private class DiscordListener(
fun onMessageCreate(event: MessageCreateEvent) {
// guaranteed to not happen because events are filtered beforehand
val sender = event.member ?: throw IllegalStateException("onMessageCreate: event.member is null")
if (sender.isBot && sender.id != Snowflake(config.chadId)) return

var displayName = sender.effectiveName
if (sender.isBot) {
if (sender.id != Snowflake(config.patrickId)) return
displayName = "[Bot] " + displayName

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is the easiest way to attach the bot prefix, it doesn't stand out from the rest of the display name. (Someone could nick themselves [Bot] SomeBot in Discord and you wouldn't be able to tell the difference. In practice, not a problem, but it highlights the issue.) We should think if we want a prefix that stands out more (or how much we even need the prefix in the first place). That can also be addressed later, this already solves the issue

}

val attachments = event.message.attachments.joinToString(" ", " ") { it.url }
val toSend = replaceEmojis(event.message.content) + attachments
val displayName = sender.effectiveName
logger.info("[Discord] $displayName (${sender.id}): $toSend")
val transformedMessage = toSend.replace(urlMarkdownRegex) { matchResult ->
val text = matchResult.groupValues[1].trim()
Expand All @@ -170,10 +175,10 @@ private suspend fun CoroutineScope.spawnServerBots(
if (availableServers != configServers) {
logger.warn(
"""
Supplied server keys in Discord configuration section does not match available servers:
Available servers: ${availableServers.joinToString()}
Configured servers: ${configServers.joinToString()}
""".trimIndent()
Supplied server keys in Discord configuration section does not match available servers:
Available servers: ${availableServers.joinToString()}
Configured servers: ${configServers.joinToString()}
""".trimIndent()
)
}
return serverTokens.mapValues { (_, token) ->
Expand Down