英文:
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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论