generated from MineInAbyss/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
433 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
chatty-paper/src/main/kotlin/com/mineinabyss/chatty/helpers/DiscordEmoteFixer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.mineinabyss.chatty.helpers | ||
|
||
import com.mineinabyss.chatty.chattyPlugin | ||
import com.mineinabyss.idofront.config.IdofrontConfig | ||
import kotlinx.serialization.Serializable | ||
import kotlin.io.path.div | ||
|
||
object DiscordEmoteFixer : IdofrontConfig<DiscordEmoteFixer.Emotes>( | ||
chattyPlugin, | ||
Emotes.serializer(), | ||
file = (chattyPlugin.dataFolder.toPath() / "emotefixer.yml") | ||
) { | ||
|
||
@Serializable | ||
data class Emotes(val emotes: Map<String, String>) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
chatty-paper/src/main/kotlin/com/mineinabyss/chatty/listeners/DiscordListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package com.mineinabyss.chatty.listeners | ||
|
||
import com.mineinabyss.chatty.components.playerData | ||
import com.mineinabyss.chatty.helpers.emoteFixer | ||
import com.mineinabyss.chatty.helpers.getChannelFromId | ||
import com.mineinabyss.idofront.messaging.broadcast | ||
import github.scarsz.discordsrv.api.Subscribe | ||
import github.scarsz.discordsrv.api.events.* | ||
import github.scarsz.discordsrv.dependencies.jda.api.MessageBuilder | ||
import github.scarsz.discordsrv.dependencies.jda.api.entities.MessageEmbed | ||
import github.scarsz.discordsrv.dependencies.kyori.adventure.text.Component | ||
import github.scarsz.discordsrv.dependencies.kyori.adventure.text.TextReplacementConfig | ||
import github.scarsz.discordsrv.dependencies.kyori.adventure.text.minimessage.MiniMessage | ||
|
||
|
||
class DiscordListener { | ||
|
||
@Subscribe | ||
fun GameChatMessagePreProcessEvent.onChat() { | ||
val channel = getChannelFromId(player.playerData.channelId) ?: return | ||
if (!channel.discordsrv) isCancelled = true | ||
messageComponent = messageComponent.translateEmoteIDsToComponent() | ||
} | ||
|
||
@Subscribe | ||
fun VentureChatMessagePreProcessEvent.onProxyChat() { | ||
val channelId = messageComponent.deserialize().substringBefore(" ") | ||
val channel = getChannelFromId(channelId) ?: return | ||
if (!channel.discordsrv) isCancelled = true | ||
else messageComponent = | ||
messageComponent.removeAttachedChannel(channelId).translateEmoteIDsToComponent() | ||
} | ||
|
||
@Subscribe | ||
fun DeathMessagePreProcessEvent.onDeath() { | ||
val channel = getChannelFromId(player.playerData.channelId) ?: return | ||
if (!channel.discordsrv) isCancelled = true | ||
deathMessage = deathMessage.translateEmoteIDs() | ||
} | ||
|
||
@Subscribe | ||
fun AchievementMessagePreProcessEvent.onAchievement() { | ||
achievementName = achievementName.translateEmoteIDs() | ||
val format = messageFormat | ||
if (format.content != null) | ||
format.content = format.content.translateEmoteIDs() | ||
if (format.description != null) | ||
format.description = format.description.translateEmoteIDs() | ||
if (format.title != null) | ||
format.title = format.title.translateEmoteIDs() | ||
if (format.fields != null) { | ||
val fields: MutableList<MessageEmbed.Field> = ArrayList() | ||
format.fields.forEach { f -> | ||
fields.add(MessageEmbed.Field(f.name?.translateEmoteIDs(), f.value?.translateEmoteIDs(), f.isInline)) | ||
} | ||
format.fields = fields | ||
} | ||
messageFormat = format | ||
} | ||
|
||
@Subscribe | ||
fun AchievementMessagePostProcessEvent.onAchievement() { | ||
val message = discordMessage | ||
val embeds = mutableListOf<MessageEmbed>() | ||
|
||
message.embeds.forEach { embed -> | ||
val fields = mutableListOf<MessageEmbed.Field>() | ||
if (embed.fields.isNotEmpty()) | ||
embed.fields.forEach { field -> | ||
fields.add( | ||
MessageEmbed.Field( | ||
field.name?.translateEmoteIDs(), | ||
field.value?.translateEmoteIDs(), | ||
field.isInline | ||
) | ||
) | ||
} | ||
embeds.add( | ||
MessageEmbed( | ||
embed.url, embed.title?.translateEmoteIDs(), embed.description?.translateEmoteIDs(), | ||
embed.type, embed.timestamp, embed.colorRaw, embed.thumbnail, embed.siteProvider, | ||
embed.author, embed.videoInfo, embed.footer, embed.image, fields | ||
) | ||
) | ||
} | ||
val builder = MessageBuilder(message) | ||
if (embeds.isNotEmpty()) builder.setEmbeds(embeds) | ||
discordMessage = builder.build() | ||
} | ||
} | ||
|
||
private fun Component.removeAttachedChannel(id: String) = | ||
this.replaceText(TextReplacementConfig.builder().match(id).replacement("").build()) | ||
|
||
private fun Component.cleanUpHackyFix() = | ||
this.replaceText(TextReplacementConfig.builder().match("<<").replacement("<").build()) | ||
|
||
|
||
private fun String.cleanUpHackyFix() = this.replace("<<", "<") | ||
|
||
private fun String.translateEmoteIDs(): String { | ||
var translated = this | ||
emoteFixer.emotes.entries.forEach { (emoteId, replacement) -> | ||
val id = ":$emoteId:" | ||
if (id in this) { | ||
translated = translated.replace(id, replacement) | ||
} | ||
} | ||
return translated.cleanUpHackyFix() | ||
} | ||
|
||
private fun String.translateEmoteIDsToComponent(): Component { | ||
var translated = this | ||
emoteFixer.emotes.entries.forEach { (emoteId, replacement) -> | ||
val id = ":$emoteId:" | ||
if (id in this) { | ||
translated = translated.replace(id, "<$replacement") | ||
} | ||
} | ||
return translated.miniMessage().cleanUpHackyFix() | ||
} | ||
|
||
private fun Component.translateEmoteIDsToComponent(): Component { | ||
var translated = this | ||
emoteFixer.emotes.entries.forEach { (emoteId, replacement) -> | ||
val id = ":$emoteId:" | ||
if (id in translated.deserialize()) { | ||
translated = translated.replaceText( | ||
TextReplacementConfig.builder().match(id) | ||
.replacement("<$replacement".miniMessage()).build() | ||
) | ||
} | ||
} | ||
return translated.cleanUpHackyFix() | ||
} | ||
|
||
private fun Component.deserialize(): String { | ||
return MiniMessage.builder().build() | ||
.serialize(this) | ||
} | ||
|
||
private fun Component.stripTags(): String { | ||
broadcast(this.deserialize()) | ||
return MiniMessage.builder().build().stripTokens(this.deserialize()) | ||
} | ||
|
||
private fun String.miniMessage(): Component { | ||
return MiniMessage.builder().build() | ||
.deserialize(this) | ||
} |
Oops, something went wrong.