Commit 8eb59747 authored by Him188's avatar Him188

Extract group settings to `interface GroupSettings`

parent 22ade1af
...@@ -412,7 +412,7 @@ internal class GroupImpl( ...@@ -412,7 +412,7 @@ internal class GroupImpl(
@OptIn(MiraiExperimentalAPI::class) @OptIn(MiraiExperimentalAPI::class)
override val botAsMember: Member by lazy { override val botAsMember: Member by lazy {
Member(object : MemberInfo { newMember(object : MemberInfo {
override val nameCard: String override val nameCard: String
get() = bot.nick // TODO: 2020/2/21 机器人在群内的昵称获取 get() = bot.nick // TODO: 2020/2/21 机器人在群内的昵称获取
override val permission: MemberPermission override val permission: MemberPermission
...@@ -447,7 +447,7 @@ internal class GroupImpl( ...@@ -447,7 +447,7 @@ internal class GroupImpl(
owner = botAsMember owner = botAsMember
} }
null null
} else Member(it).also { member -> } else newMember(it).also { member ->
if (member.permission == MemberPermission.OWNER) { if (member.permission == MemberPermission.OWNER) {
owner = member owner = member
} }
...@@ -465,7 +465,7 @@ internal class GroupImpl( ...@@ -465,7 +465,7 @@ internal class GroupImpl(
override var name: String override var name: String
get() = _name get() = _name
set(newValue) { set(newValue) {
this.checkBotPermissionOperator() checkBotPermissionOperator()
if (_name != newValue) { if (_name != newValue) {
val oldValue = _name val oldValue = _name
_name = newValue _name = newValue
...@@ -482,101 +482,104 @@ internal class GroupImpl( ...@@ -482,101 +482,104 @@ internal class GroupImpl(
} }
} }
override var entranceAnnouncement: String override val settings: GroupSettings = object : GroupSettings{
get() = _announcement
set(newValue) { override var entranceAnnouncement: String
this.checkBotPermissionOperator() get() = _announcement
if (_announcement != newValue) { set(newValue) {
val oldValue = _announcement checkBotPermissionOperator()
_announcement = newValue if (_announcement != newValue) {
launch { val oldValue = _announcement
bot.network.run { _announcement = newValue
TroopManagement.GroupOperation.memo( launch {
client = bot.client, bot.network.run {
groupCode = id, TroopManagement.GroupOperation.memo(
newMemo = newValue client = bot.client,
).sendWithoutExpect() groupCode = id,
newMemo = newValue
).sendWithoutExpect()
}
GroupEntranceAnnouncementChangeEvent(oldValue, newValue, this@GroupImpl, null).broadcast()
} }
GroupEntranceAnnouncementChangeEvent(oldValue, newValue, this@GroupImpl, null).broadcast()
} }
} }
}
override var isAllowMemberInvite: Boolean override var isAllowMemberInvite: Boolean
get() = _allowMemberInvite get() = _allowMemberInvite
set(newValue) { set(newValue) {
this.checkBotPermissionOperator() checkBotPermissionOperator()
if (_allowMemberInvite != newValue) { if (_allowMemberInvite != newValue) {
val oldValue = _allowMemberInvite val oldValue = _allowMemberInvite
_allowMemberInvite = newValue _allowMemberInvite = newValue
launch { launch {
bot.network.run { bot.network.run {
TroopManagement.GroupOperation.allowMemberInvite( TroopManagement.GroupOperation.allowMemberInvite(
client = bot.client, client = bot.client,
groupCode = id, groupCode = id,
switch = newValue switch = newValue
).sendWithoutExpect() ).sendWithoutExpect()
}
GroupAllowMemberInviteEvent(oldValue, newValue, this@GroupImpl, null).broadcast()
} }
GroupAllowMemberInviteEvent(oldValue, newValue, this@GroupImpl, null).broadcast()
} }
} }
}
override var isAutoApproveEnabled: Boolean override var isAutoApproveEnabled: Boolean
get() = _autoApprove get() = _autoApprove
@Suppress("UNUSED_PARAMETER") @Suppress("UNUSED_PARAMETER")
set(newValue) { set(newValue) {
TODO() TODO()
} }
override var isAnonymousChatEnabled: Boolean override var isAnonymousChatEnabled: Boolean
get() = _anonymousChat get() = _anonymousChat
@Suppress("UNUSED_PARAMETER") @Suppress("UNUSED_PARAMETER")
set(newValue) { set(newValue) {
TODO() TODO()
} }
override var isConfessTalkEnabled: Boolean override var isConfessTalkEnabled: Boolean
get() = _confessTalk get() = _confessTalk
set(newValue) { set(newValue) {
this.checkBotPermissionOperator() checkBotPermissionOperator()
if (_confessTalk != newValue) { if (_confessTalk != newValue) {
val oldValue = _confessTalk val oldValue = _confessTalk
_confessTalk = newValue _confessTalk = newValue
launch { launch {
bot.network.run { bot.network.run {
TroopManagement.GroupOperation.confessTalk( TroopManagement.GroupOperation.confessTalk(
client = bot.client, client = bot.client,
groupCode = id, groupCode = id,
switch = newValue switch = newValue
).sendWithoutExpect() ).sendWithoutExpect()
}
GroupAllowConfessTalkEvent(oldValue, newValue, this@GroupImpl, true).broadcast()
} }
GroupAllowConfessTalkEvent(oldValue, newValue, this@GroupImpl, true).broadcast()
} }
} }
}
override var isMuteAll: Boolean override var isMuteAll: Boolean
get() = _muteAll get() = _muteAll
set(newValue) { set(newValue) {
this.checkBotPermissionOperator() checkBotPermissionOperator()
if (_muteAll != newValue) { if (_muteAll != newValue) {
val oldValue = _muteAll val oldValue = _muteAll
_muteAll = newValue _muteAll = newValue
launch { launch {
bot.network.run { bot.network.run {
TroopManagement.GroupOperation.muteAll( TroopManagement.GroupOperation.muteAll(
client = bot.client, client = bot.client,
groupCode = id, groupCode = id,
switch = newValue switch = newValue
).sendWithoutExpect() ).sendWithoutExpect()
}
GroupMuteAllEvent(oldValue, newValue, this@GroupImpl, null).broadcast()
} }
GroupMuteAllEvent(oldValue, newValue, this@GroupImpl, null).broadcast()
} }
} }
} }
@MiraiExperimentalAPI @MiraiExperimentalAPI
override suspend fun quit(): Boolean { override suspend fun quit(): Boolean {
...@@ -585,7 +588,7 @@ internal class GroupImpl( ...@@ -585,7 +588,7 @@ internal class GroupImpl(
} }
@OptIn(MiraiExperimentalAPI::class) @OptIn(MiraiExperimentalAPI::class)
override fun Member(memberInfo: MemberInfo): Member { override fun newMember(memberInfo: MemberInfo): Member {
return MemberImpl( return MemberImpl(
@OptIn(LowLevelAPI::class) @OptIn(LowLevelAPI::class)
bot._lowLevelNewQQ(memberInfo) as QQImpl, bot._lowLevelNewQQ(memberInfo) as QQImpl,
......
...@@ -38,54 +38,9 @@ actual abstract class Group : Contact(), CoroutineScope { ...@@ -38,54 +38,9 @@ actual abstract class Group : Contact(), CoroutineScope {
actual abstract var name: String actual abstract var name: String
/** /**
* 入群公告, 没有时为空字符串. * 群设置
*
* 在修改时将会异步上传至服务器.
*
* @see GroupEntranceAnnouncementChangeEvent
* @throws PermissionDeniedException 无权限修改时将会抛出异常
*/
actual abstract var entranceAnnouncement: String
/**
* 全体禁言状态. `true` 为开启.
*
* 当前仅能修改状态.
*
* @see GroupMuteAllEvent
* @throws PermissionDeniedException 无权限修改时将会抛出异常
*/
actual abstract var isMuteAll: Boolean
/**
* 坦白说状态. `true` 为允许.
*
* 在修改时将会异步上传至服务器.
*
* @see GroupAllowConfessTalkEvent
* @throws PermissionDeniedException 无权限修改时将会抛出异常
*/
actual abstract var isConfessTalkEnabled: Boolean
/**
* 允许群员邀请好友入群的状态. `true` 为允许
*
* 在修改时将会异步上传至服务器.
*
* @see GroupAllowMemberInviteEvent
* @throws PermissionDeniedException 无权限修改时将会抛出异常
*/
actual abstract var isAllowMemberInvite: Boolean
/**
* 自动加群审批
*/
actual abstract val isAutoApproveEnabled: Boolean
/**
* 匿名聊天
*/ */
actual abstract val isAnonymousChatEnabled: Boolean actual abstract val settings: GroupSettings
/** /**
* 同为 groupCode, 用户看到的群号码. * 同为 groupCode, 用户看到的群号码.
......
...@@ -40,50 +40,11 @@ expect abstract class Group() : Contact, CoroutineScope { ...@@ -40,50 +40,11 @@ expect abstract class Group() : Contact, CoroutineScope {
* @throws PermissionDeniedException 无权限修改时将会抛出异常 * @throws PermissionDeniedException 无权限修改时将会抛出异常
*/ */
abstract var name: String abstract var name: String
/** /**
* 入群公告, 没有时为空字符串. * 群设置
*
* 在修改时将会异步上传至服务器.
*
* @see GroupEntranceAnnouncementChangeEvent
* @throws PermissionDeniedException 无权限修改时将会抛出异常
*/
abstract var entranceAnnouncement: String
/**
* 全体禁言状态. `true` 为开启.
*
* 当前仅能修改状态.
*
* @see GroupMuteAllEvent
* @throws PermissionDeniedException 无权限修改时将会抛出异常
*/
abstract var isMuteAll: Boolean
/**
* 坦白说状态. `true` 为允许.
*
* 在修改时将会异步上传至服务器.
*
* @see GroupAllowConfessTalkEvent
* @throws PermissionDeniedException 无权限修改时将会抛出异常
*/
abstract var isConfessTalkEnabled: Boolean
/**
* 允许群员邀请好友入群的状态. `true` 为允许
*
* 在修改时将会异步上传至服务器.
*
* @see GroupAllowMemberInviteEvent
* @throws PermissionDeniedException 无权限修改时将会抛出异常
*/
abstract var isAllowMemberInvite: Boolean
/**
* 自动加群审批
*/
abstract val isAutoApproveEnabled: Boolean
/**
* 匿名聊天
*/ */
abstract val isAnonymousChatEnabled: Boolean abstract val settings: GroupSettings
/** /**
* 同为 groupCode, 用户看到的群号码. * 同为 groupCode, 用户看到的群号码.
...@@ -200,38 +161,63 @@ expect abstract class Group() : Contact, CoroutineScope { ...@@ -200,38 +161,63 @@ expect abstract class Group() : Contact, CoroutineScope {
fun toFullString(): String fun toFullString(): String
} }
internal object CommonGroupCalculations { /**
* 群设置
*
* @see Group.settings 获取群设置
*/
@SinceMirai("0.30.0")
interface GroupSettings {
/** /**
* by @kar98k * 入群公告, 没有时为空字符串.
*
* 在修改时将会异步上传至服务器.
*
* @see GroupEntranceAnnouncementChangeEvent
* @throws PermissionDeniedException 无权限修改时将会抛出异常
*/ */
fun calculateGroupUinByGroupCode(groupCode: Long): Long { var entranceAnnouncement: String
var left: Long = groupCode / 1000000L
when (left) { /**
in 0..10 -> left += 202 * 全体禁言状态. `true` 为开启.
in 11..19 -> left += 480 - 11 *
in 20..66 -> left += 2100 - 20 * 当前仅能修改状态.
in 67..156 -> left += 2010 - 67 *
in 157..209 -> left += 2147 - 157 * @see GroupMuteAllEvent
in 210..309 -> left += 4100 - 210 * @throws PermissionDeniedException 无权限修改时将会抛出异常
in 310..499 -> left += 3800 - 310 */
} var isMuteAll: Boolean
return left * 1000000L + groupCode % 1000000L /**
} * 坦白说状态. `true` 为允许.
*
* 在修改时将会异步上传至服务器.
*
* @see GroupAllowConfessTalkEvent
* @throws PermissionDeniedException 无权限修改时将会抛出异常
*/
var isConfessTalkEnabled: Boolean
fun calculateGroupCodeByGroupUin(groupUin: Long): Long { /**
var left: Long = groupUin / 1000000L * 允许群员邀请好友入群的状态. `true` 为允许
*
* 在修改时将会异步上传至服务器.
*
* @see GroupAllowMemberInviteEvent
* @throws PermissionDeniedException 无权限修改时将会抛出异常
*/
var isAllowMemberInvite: Boolean
when (left) { /**
in 0 + 202..10 + 202 -> left -= 202 * 自动加群审批
in 11 + 480 - 11..19 + 480 - 11 -> left -= 480 - 11 */
in 20 + 2100 - 20..66 + 2100 - 20 -> left -= 2100 - 20 val isAutoApproveEnabled: Boolean
in 67 + 2010 - 67..156 + 2010 - 67 -> left -= 2010 - 67
in 157 + 2147 - 157..209 + 2147 - 157 -> left -= 2147 - 157 /**
in 210 + 4100 - 210..309 + 4100 - 210 -> left -= 4100 - 210 * 匿名聊天
in 310 + 3800 - 310..499 + 3800 - 310 -> left -= 3800 - 310 */
} val isAnonymousChatEnabled: Boolean
}
/** /**
......
...@@ -39,54 +39,9 @@ actual abstract class Group : Contact(), CoroutineScope { ...@@ -39,54 +39,9 @@ actual abstract class Group : Contact(), CoroutineScope {
actual abstract var name: String actual abstract var name: String
/** /**
* 入群公告, 没有时为空字符串. * 群设置
*
* 在修改时将会异步上传至服务器.
*
* @see GroupEntranceAnnouncementChangeEvent
* @throws PermissionDeniedException 无权限修改时将会抛出异常
*/
actual abstract var entranceAnnouncement: String
/**
* 全体禁言状态. `true` 为开启.
*
* 当前仅能修改状态.
*
* @see GroupMuteAllEvent
* @throws PermissionDeniedException 无权限修改时将会抛出异常
*/ */
actual abstract var isMuteAll: Boolean actual abstract val settings: GroupSettings
/**
* 坦白说状态. `true` 为允许.
*
* 在修改时将会异步上传至服务器.
*
* @see GroupAllowConfessTalkEvent
* @throws PermissionDeniedException 无权限修改时将会抛出异常
*/
actual abstract var isConfessTalkEnabled: Boolean
/**
* 允许群员邀请好友入群的状态. `true` 为允许
*
* 在修改时将会异步上传至服务器.
*
* @see GroupAllowMemberInviteEvent
* @throws PermissionDeniedException 无权限修改时将会抛出异常
*/
actual abstract var isAllowMemberInvite: Boolean
/**
* 自动加群审批
*/
actual abstract val isAutoApproveEnabled: Boolean
/**
* 匿名聊天
*/
actual abstract val isAnonymousChatEnabled: Boolean
/** /**
* 同为 groupCode, 用户看到的群号码. * 同为 groupCode, 用户看到的群号码.
...@@ -205,5 +160,4 @@ actual abstract class Group : Contact(), CoroutineScope { ...@@ -205,5 +160,4 @@ actual abstract class Group : Contact(), CoroutineScope {
actual fun toFullString(): String { actual fun toFullString(): String {
return "Group(id=${this.id}, name=$name, owner=${owner.id}, members=${members.idContentString})" return "Group(id=${this.id}, name=$name, owner=${owner.id}, members=${members.idContentString})"
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment