Skip to content

Commit e11d10e

Browse files
committed
Add a method to get the channel mentions from a message, and one to format out the ids
1 parent 0d5af89 commit e11d10e

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

ackCord/src/main/scala/net/katsstuff/ackcord/data/message.scala

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ package net.katsstuff.ackcord.data
2626
import java.time.OffsetDateTime
2727
import java.util.Base64
2828

29+
import scala.util.Try
30+
31+
import net.katsstuff.ackcord.util.MessageParser
32+
2933
//TODO
3034
class ImageData(val rawData: String) extends AnyVal
3135
object ImageData {
@@ -201,7 +205,43 @@ case class Message(
201205
nonce: Option[Snowflake],
202206
pinned: Boolean,
203207
messageType: MessageType
204-
) extends GetChannel
208+
) extends GetChannel {
209+
210+
def channelMentions: Seq[ChannelId] = {
211+
MessageParser.channelRegex
212+
.findAllMatchIn(content)
213+
.flatMap { m =>
214+
Try {
215+
ChannelId(Snowflake(m.group(1)))
216+
}.toOption
217+
}
218+
.toSeq
219+
}
220+
221+
/**
222+
* Formats mentions in this message to their normal syntax with names.
223+
*/
224+
def formatMentions(implicit c: CacheSnapshot): String = {
225+
val withUsers = mentions
226+
.flatMap(_.resolve)
227+
.foldRight(content)((user, content) => content.replace(user.mention, s"@${user.name}"))
228+
val withRoles = mentionRoles
229+
.flatMap(_.resolve)
230+
.foldRight(withUsers)((role, content) => content.replace(role.mention, s"@${role.name}"))
231+
232+
val optGuildId = channelId.resolve.collect {
233+
case channel: GuildChannel => channel.guildId
234+
}
235+
236+
optGuildId.fold(withRoles) { guildId =>
237+
val withChannels = channelMentions
238+
.flatMap(_.guildResolve(guildId))
239+
.foldRight(withRoles)((channel, content) => content.replace(channel.mention, s"@${channel.name}"))
240+
241+
withChannels
242+
}
243+
}
244+
}
205245

206246
/**
207247
* A reaction to a message

0 commit comments

Comments
 (0)