Skip to content

Commit

Permalink
feat(core): OneBotMember 支持设置头衔API
Browse files Browse the repository at this point in the history
  • Loading branch information
ForteScarlet committed Jun 21, 2024
1 parent e9da669 commit dc033b1
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
branches:
- main
- master
- dev/main
# - dev/main
paths:
- 'Writerside/**'
- '.github/workflows/deploy-website.yml'
Expand Down
46 changes: 46 additions & 0 deletions Writerside/topics/onebot11-OneBotMember.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,52 @@ member.unbanReserve()
</tab>
</tabs>

### 设置头衔

可以通过 `setSpecialTitle(String?)` 设置此成员在群内的特殊头衔。

<tip>

想要获取头衔,可以通过
[获取原始类型](#获取原始类型)
取到 `title`

</tip>

<tabs group="code">
<tab title="Kotlin" group-key="Kotlin">

```Kotlin
val member: OneBotMember = ...
member.setSpecialTitle("newTitle")
```

</tab>
<tab title="Java" group-key="Java">

```Java
OneBotMember member = ...;
member.setSpecialTitleAsync("newTitle");
```
{switcher-key=%ja%}

```Java
OneBotMember member = ...;
member.setSpecialTitleBlocking("newTitle");
```
{switcher-key=%jb%}

```Java
OneBotMember member = ...;
member.setSpecialTitleReserve("newTitle")
.transform(SuspendReserves.mono())
.subscribe();
```
{switcher-key=%jr%}

</tab>
</tabs>


## 获取 OneBotMember

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,53 @@ public interface OneBotMember : Member, DeleteSupport, OneBotStrangerAware {
@ST
public suspend fun getSourceMemberInfo(): GetGroupMemberInfoResult

/**
* 使用 [SetGroupSpecialTitleApi] 为群成员设置专属头衔。
* 通常要求bot拥有群主权限。
*
* 如果想要获取当前成员的专属头衔,使用 [getSourceMemberInfo]
* 后可通过 [GetGroupMemberInfoResult.title] 获取。
*
* @param specialTitle 要设置的新头衔。为空或为 `null` 则代表取消头衔。
* @throws Throwable 任何可能在请求API过程中产生的异常。
*/
@ST
public suspend fun setSpecialTitle(specialTitle: String? = null)

/**
* 使用 [SetGroupSpecialTitleApi] 为群成员设置专属头衔。
* 通常要求bot拥有群主权限。
*
* 如果想要获取当前成员的专属头衔,使用 [getSourceMemberInfo]
* 后可通过 [GetGroupMemberInfoResult.title] 获取。
*
* @param specialTitle 要设置的新头衔。为空或为 `null` 则代表取消头衔。
* @param duration 有效期,最终取秒值,如果小于0则会被忽略。
* 不一定有效果,更多说明参考 [SetGroupSpecialTitleApi] 和 [SetGroupSpecialTitleApi.create]。
* @param timeUnit [duration] 的时间单位。
* @throws Throwable 任何可能在请求API过程中产生的异常。
*/
@ST
@JvmSynthetic
public suspend fun setSpecialTitle(specialTitle: String, duration: Long, timeUnit: TimeUnit)

/**
* 使用 [SetGroupSpecialTitleApi] 为群成员设置专属头衔。
* 通常要求bot拥有群主权限。
*
* 如果想要获取当前成员的专属头衔,使用 [getSourceMemberInfo]
* 后可通过 [GetGroupMemberInfoResult.title] 获取。
*
* @param specialTitle 要设置的新头衔。为空或为 `null` 则代表取消头衔。
* @param duration 有效期,最终取秒值,如果小于0则会被忽略。
* 不一定有效果,更多说明参考 [SetGroupSpecialTitleApi] 和 [SetGroupSpecialTitleApi.create]。
* @throws Throwable 任何可能在请求API过程中产生的异常。
*/
@JvmSynthetic
public suspend fun setSpecialTitle(specialTitle: String, duration: Duration)



/**
* 向此成员发送消息。
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,37 @@ internal abstract class OneBotMemberImpl(
).requestDataBy(bot)
}

override suspend fun setSpecialTitle(specialTitle: String?) {
setSpecialTitle0(specialTitle, null)
}

override suspend fun setSpecialTitle(specialTitle: String, duration: Long, timeUnit: TimeUnit) {
if (duration < 0L) {
setSpecialTitle0(specialTitle, null)
} else {
setSpecialTitle0(specialTitle, timeUnit.toSeconds(duration))
}
}

override suspend fun setSpecialTitle(specialTitle: String, duration: Duration) {
val seconds = duration.inWholeSeconds

if (seconds < 0L) {
setSpecialTitle0(specialTitle, null)
} else {
setSpecialTitle0(specialTitle, seconds)
}
}

private suspend fun setSpecialTitle0(specialTitle: String?, duration: Long?) {
SetGroupSpecialTitleApi.create(
groupId = groupIdOrFailure,
userId = id,
specialTitle = specialTitle,
duration = duration
).requestDataBy(bot)
}

override suspend fun setNick(newNick: String?) {
SetGroupCardApi.create(
groupId = groupIdOrFailure,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,20 @@ public data class GetGroupMemberInfoResult @ApiResultConstructor constructor(
@SerialName("user_id")
public val userId: LongID,
public val nickname: String,
public val card: String,
public val sex: String,
public val age: Int,
public val area: String,
public val card: String = "",
public val sex: String = "unknown",
public val age: Int = -1,
public val area: String = "",
@SerialName("join_time")
public val joinTime: Int,
public val joinTime: Int = -1,
@SerialName("last_sent_time")
public val lastSentTime: Int,
public val level: String,
public val role: String,
public val unfriendly: Boolean,
public val title: String,
public val lastSentTime: Int = -1,
public val level: String = "",
public val role: String = "member",
public val unfriendly: Boolean = false,
public val title: String = "",
@SerialName("title_expire_time")
public val titleExpireTime: Int,
public val titleExpireTime: Int = -1,
@SerialName("card_changeable")
public val cardChangeable: Boolean,
public val cardChangeable: Boolean = false,
)

0 comments on commit dc033b1

Please sign in to comment.