Ignore group messages sent by bot itself

This commit is contained in:
Him188 2020-01-01 19:01:07 +08:00
parent 2fed87a1b9
commit 45bcd0fa83

View File

@ -4,8 +4,10 @@ import net.mamoe.mirai.Bot
import net.mamoe.mirai.contact.Group import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.contact.Member import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.contact.MemberPermission import net.mamoe.mirai.contact.MemberPermission
import net.mamoe.mirai.event.BroadcastControllable
import net.mamoe.mirai.message.data.At import net.mamoe.mirai.message.data.At
import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.qqAccount
import net.mamoe.mirai.utils.unsafeWeakRef import net.mamoe.mirai.utils.unsafeWeakRef
@Suppress("unused", "NOTHING_TO_INLINE") @Suppress("unused", "NOTHING_TO_INLINE")
@ -19,7 +21,7 @@ class GroupMessage(
val permission: MemberPermission, val permission: MemberPermission,
sender: Member, sender: Member,
override val message: MessageChain override val message: MessageChain
) : MessagePacket<Member, Group>(bot) { ) : MessagePacket<Member, Group>(bot), BroadcastControllable {
val group: Group by group.unsafeWeakRef() val group: Group by group.unsafeWeakRef()
override val sender: Member by sender.unsafeWeakRef() override val sender: Member by sender.unsafeWeakRef()
@ -32,4 +34,8 @@ class GroupMessage(
inline fun Long.member(): Member = group.getMember(this) inline fun Long.member(): Member = group.getMember(this)
override fun toString(): String = override fun toString(): String =
"GroupMessage(group=${group.id}, senderName=$senderName, sender=${sender.id}, permission=${permission.name}, message=$message)" "GroupMessage(group=${group.id}, senderName=$senderName, sender=${sender.id}, permission=${permission.name}, message=$message)"
override val shouldBroadcast: Boolean
get() = bot.qqAccount != sender.id // 自己会收到自己发的消息
} }