Skip to content

Commit

Permalink
Add option to turn off Discord embed bridging to IRC
Browse files Browse the repository at this point in the history
  • Loading branch information
zachbr committed Sep 3, 2023
1 parent a977f59 commit 59a804e
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ data class IrcConfiguration (
val noPrefixRegex: Pattern?,
val announceForwardedCommands: Boolean,
val discordReplyContextLimit: Int,
val startupRawCommands: List<String>
val startupRawCommands: List<String>,
val sendDiscordEmbeds: Boolean
) {
fun toLoggable(): String {
return "IrcConfiguration(" +
Expand All @@ -69,6 +70,7 @@ data class IrcConfiguration (
"announceForwardedCommands=$announceForwardedCommands, " +
"discordReplyContextLimit=$discordReplyContextLimit, " +
"startupRawCommands=$startupRawCommands" +
"sendDiscordEmbeds=$sendDiscordEmbeds" +
")"
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/main/kotlin/io/zachbr/dis4irc/bridge/message/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ data class Message(
/**
* A list of attachment URLs on the message
*/
val attachments: MutableList<String>? = null,
val attachments: MutableList<String> = ArrayList(),
/**
* The message that this message references. Will be null in most cases, only applicable to messages from Discord.
* This is _not_ expected to resolve down infinitely (ie, reference that references another reference that ...)
*/
val referencedMessage: Message? = null,
/**
* Embeds associated with the message. Only applicable to messages from Discord.
*/
val embeds: List<Embed> = ArrayList(),
/**
* Destination to be bridged to
*/
Expand Down Expand Up @@ -74,3 +78,8 @@ data class Message(
*/
fun originatesFromBridgeItself() = sender == BOT_SENDER
}

data class Embed(
var string: String?,
val imageUrl: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class TranslateFormatting : Mutator {
PlatformType.DISCORD -> formatForIrc(message.contents)
}

for (embed in message.embeds) {
embed.string = embed.string?.let { formatForIrc(it) }
}

return Mutator.LifeCycle.CONTINUE
}

Expand Down
103 changes: 33 additions & 70 deletions src/main/kotlin/io/zachbr/dis4irc/bridge/pier/discord/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package io.zachbr.dis4irc.bridge.pier.discord

import io.zachbr.dis4irc.bridge.message.Embed
import io.zachbr.dis4irc.bridge.message.PlatformType
import io.zachbr.dis4irc.bridge.message.Sender
import io.zachbr.dis4irc.bridge.message.Source
Expand Down Expand Up @@ -74,15 +75,7 @@ fun Message.toBridgeMsg(logger: Logger, receiveTimestamp: Long = System.nanoTime
}

// embeds - this only works with channel embeds, not for listening to slash commands and interaction hooks
if (embeds.isNotEmpty()) {
val parsed = ParsedEmbeds(embeds)
if (parsed.embedString.isNotBlank()) {
messageText += parsed.embedString
}
if (parsed.embedImageUrls.isNotEmpty()) {
attachmentUrls += parsed.embedImageUrls
}
}
val parsedEmbeds = parseEmbeds(embeds)

// discord replies
var bridgeMsgRef: io.zachbr.dis4irc.bridge.message.Message? = null
Expand All @@ -103,7 +96,8 @@ fun Message.toBridgeMsg(logger: Logger, receiveTimestamp: Long = System.nanoTime
channel,
receiveTimestamp,
attachmentUrls,
bridgeMsgRef
bridgeMsgRef,
parsedEmbeds
)
}

Expand All @@ -119,80 +113,49 @@ fun makeLottieViewerUrl(discordCdnUrl: String): String? {
return "$LOTTIE_PLAYER_BASE_URL?p=$encodedString"
}

class ParsedEmbeds(embeds: List<MessageEmbed>) {
val embedString: String
val embedImageUrls: List<String>

init {
fun parseEmbeds(embeds: List<MessageEmbed>): List<Embed> {
val parsed = ArrayList<Embed>()
for (embed in embeds) {
val strBuilder = StringBuilder()
val imageUrls = ArrayList<String>()
val embedCount = embeds.count()
for ((i, embed) in embeds.withIndex()) {
val imageUrl = embed.image?.url
if (imageUrl != null) {
imageUrls.add(imageUrl)
}

if (embed.title != null) {
strBuilder.append(embed.title)
}
val imageUrl = embed.image?.url

if (embed.title != null && embed.description != null) {
strBuilder.append(": ")
}
if (embed.title != null) {
strBuilder.append(embed.title)
}

if (embed.description != null) {
strBuilder.append(embed.description)
}
if (embed.title != null && embed.description != null) {
strBuilder.append(": ")
}

if (embed.title != null || embed.description != null) {
strBuilder.append('\n')
}
if (embed.description != null) {
strBuilder.append(embed.description)
}

val fieldsCount = embed.fields.count()
for ((fi, field) in embed.fields.withIndex()) {
if (field.name != null) {
strBuilder.append(field.name)
}
if (embed.title != null || embed.description != null) {
strBuilder.append('\n')
}

if (field.name != null && field.value != null) {
strBuilder.append(": ")
}
val fieldsCount = embed.fields.count()
for ((fi, field) in embed.fields.withIndex()) {
if (field.name != null) {
strBuilder.append(field.name)
}

if (field.value != null) {
strBuilder.append(field.value)
}
if (field.name != null && field.value != null) {
strBuilder.append(": ")
}

if (fi < fieldsCount - 1) {
strBuilder.append('\n')
}
if (field.value != null) {
strBuilder.append(field.value)
}

if (i < embedCount - 1) {
if (fi < fieldsCount - 1) {
strBuilder.append('\n')
}
}

this.embedString = strBuilder.toString()
this.embedImageUrls = imageUrls
parsed.add(Embed(strBuilder.toString(), imageUrl))
}

override fun toString(): String {
return "ParsedEmbeds{${embedString}; ${embedImageUrls.joinToString { it }}}"
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as ParsedEmbeds
if (embedString != other.embedString) return false
return embedImageUrls == other.embedImageUrls
}

override fun hashCode(): Int {
var result = embedString.hashCode()
result = 31 * result + embedImageUrls.hashCode()
return result
}
return parsed
}
15 changes: 15 additions & 0 deletions src/main/kotlin/io/zachbr/dis4irc/bridge/pier/irc/IrcPier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ class IrcPier(private val bridge: Bridge) : Pier {

var msgContent = msg.contents

// discord embed bridging
if (bridge.config.irc.sendDiscordEmbeds) {
for (embed in msg.embeds) {
if (!embed.string.isNullOrBlank()) {
if (msgContent.isNotEmpty()) {
msgContent += ' '
}
msgContent += embed.string
}
if (!embed.imageUrl.isNullOrBlank()) {
msg.attachments.add(embed.imageUrl)
}
}
}

if (msg.attachments != null && msg.attachments.isNotEmpty()) {
msg.attachments.forEach { msgContent += " $it"}
}
Expand Down
11 changes: 10 additions & 1 deletion src/main/kotlin/io/zachbr/dis4irc/config/ConfigurationUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ fun CommentedConfigurationNode.makeDefaultNode() {
ircCommandsList.set(arrayListOf("PRIVMSG NICKSERV info", "PRIVMSG NICKSERV help"))
ircCommandsList.comment("A list of __raw__ irc messages to send")

val ircSendDiscordEmbeds = ircBaseNode.node("send-discord-embeds")
ircSendDiscordEmbeds.set(true)
ircSendDiscordEmbeds.comment("Whether to send Discord channel embeds to IRC.")

val discordApiKey = this.node("discord-api-key")
discordApiKey.comment("Your discord API key you registered your bot with")
discordApiKey.set("")
Expand Down Expand Up @@ -155,6 +159,11 @@ fun CommentedConfigurationNode.toBridgeConfiguration(): BridgeConfiguration {
val ircAnnounceForwards = this.node("irc", "announce-forwarded-messages-sender").boolean
val ircDiscordReplyContextLimit = this.node("irc", "discord-reply-context-limit").int
val ircCommandsChildren = this.node("irc", "init-commands-list").childrenList()
val ircSendDiscordEmbedsNode = this.node("irc", "send-discord-embeds")
if (ircSendDiscordEmbedsNode.virtual()) {
ircSendDiscordEmbedsNode.set(true)
}
val ircSendDiscordEmbeds = ircSendDiscordEmbedsNode.boolean
val discordApiKey = getStringNonNull("Discord API key cannot be null in $bridgeName!", "discord-api-key")
val announceJoinsQuits = this.node("announce-joins-and-quits").boolean
val announceExtras = this.node("announce-extras").boolean
Expand Down Expand Up @@ -241,7 +250,7 @@ fun CommentedConfigurationNode.toBridgeConfiguration(): BridgeConfiguration {
val discordConfig = DiscordConfiguration(discordApiKey, webhookMappings, discordActivityType, discordActivityDesc, discordActivityUrl, discordOnlineStatus)
val ircConfig = IrcConfiguration(ircHost, ircPass, ircPort, ircUseSsl, ircAllowBadSsl, ircNickName, ircUserName,
ircRealName, ircAntiPing, ircUseNickNameColor, ircNoPrefixPattern, ircAnnounceForwards, ircDiscordReplyContextLimit,
ircCommandsList)
ircCommandsList, ircSendDiscordEmbeds)

return BridgeConfiguration(
bridgeName,
Expand Down

0 comments on commit 59a804e

Please sign in to comment.