查看消息是否提到了 “@here”。

huangapple go评论78阅读模式
英文:

See if a message mentions @here

问题

我想查看在使用Java的JDA库时,Discord上的一条消息是否提到了@here。

我已经知道可以使用net.dv8tion.jda.api.entities.Message#mentionsEveryone()来查看消息是否提到了@everyone,但是如何判断消息是否也提到了@here呢?

我一直在研究Message#getMentions(Message.MentionType...),但是我不太确定如何正确使用它,因为它会返回类型IMentionable

英文:

I want to see if a message mentions @here on Discord using the JDA library for Java.

I can already see if a message mentions @everyone using net.dv8tion.jda.api.entities.Message#mentionsEveryone() but how do I see if the message mentions @here as well?

I've been looking into Message#getMentions(Message.MentionType...) but I'm not sure how to use it correctly as it returns the type IMentionable.

答案1

得分: 1

你只需要检查 message.mentionsEveryone() && message.getContentRaw().contains("@here")mentionsEveryone() 方法用于检查是否是全体成员提及,@here 也算在内,因为它确实提及了当时在线的所有人。要区分是 @here 还是 @everyone,你可以分别使用 contains("...") 来检查消息内容中是否包含字面量 @here@everyone

英文:

You just have to check message.mentionsEveryone() && message.getContentRaw().contains("@here"). The mentionsEveryone() method checks whether it was an everyone mention, @here counts as one since it does mention everyone who is online at the time. To see whether it was @here or @everyone you can simply check the content of the message for the literal @here or @everyone using contains("...") for each type respectively.

答案2

得分: 0

我从未使用过这个 API,但是 IMentionable 的 JavaDoc 显示,有一个名为 getAsMention 的方法。

@Nonnull String getAsMention()

检索此实体的提及。对于公共角色 (@everyone),这将返回字面字符串 “@everyone”。

因此,看起来你只需要遍历你的 IMentionable 实例,然后检查它们中是否有一个是 “@here”

英文:

I've never used this API, but the JavaDoc for IMentionable shows, that there is a method called getAsMention

> @Nonnull String getAsMention()
>
> Retrieve a Mention for this Entity. For the public Role (@everyone),
> this will return the literal string "@everyone".

So it seems all you need to do is iterate over your IMentionable instances and check if one of them is "@here"

huangapple
  • 本文由 发表于 2020年8月24日 03:24:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63551132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定