-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from simple-robot/dev/support-request-event
增加对Request事件的组件事件类型实现;增加OneBotStranger类型
- Loading branch information
Showing
6 changed files
with
343 additions
and
2 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
...src/commonMain/kotlin/love/forte/simbot/component/onebot/v11/core/actor/OneBotStranger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (c) 2024. ForteScarlet. | ||
* | ||
* This file is part of simbot-component-onebot. | ||
* | ||
* simbot-component-onebot is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Lesser General Public License as published by the Free Software Foundation, | ||
* either version 3 of the License, or (at your option) any later version. | ||
* | ||
* simbot-component-onebot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-onebot. | ||
* If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package love.forte.simbot.component.onebot.v11.core.actor | ||
|
||
import love.forte.simbot.common.id.ID | ||
import love.forte.simbot.common.id.literal | ||
import love.forte.simbot.component.onebot.v11.common.utils.qqAvatar640 | ||
import love.forte.simbot.component.onebot.v11.core.api.GetStrangerInfoApi | ||
import love.forte.simbot.definition.User | ||
|
||
|
||
/** | ||
* 一个陌生人。 | ||
* 通常是通过 [GetStrangerInfoApi] | ||
* 得到的信息。 | ||
* | ||
* @author ForteScarlet | ||
*/ | ||
public interface OneBotStranger : User { | ||
override val id: ID | ||
override val name: String | ||
|
||
override val avatar: String | ||
get() = qqAvatar640(id.literal) | ||
|
||
/** | ||
* 年龄。 | ||
* 如果无法获取则有可能会被填充一个默认值。 | ||
*/ | ||
public val age: Int | ||
|
||
/** | ||
* 性别。`male` 或 `female` 或 `unknown` | ||
*/ | ||
public val sex: String | ||
} |
56 changes: 56 additions & 0 deletions
56
...n/kotlin/love/forte/simbot/component/onebot/v11/core/actor/internal/OneBotStrangerImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2024. ForteScarlet. | ||
* | ||
* This file is part of simbot-component-onebot. | ||
* | ||
* simbot-component-onebot is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Lesser General Public License as published by the Free Software Foundation, | ||
* either version 3 of the License, or (at your option) any later version. | ||
* | ||
* simbot-component-onebot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-onebot. | ||
* If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package love.forte.simbot.component.onebot.v11.core.actor.internal | ||
|
||
import love.forte.simbot.common.id.ID | ||
import love.forte.simbot.component.onebot.v11.core.actor.OneBotStranger | ||
import love.forte.simbot.component.onebot.v11.core.api.GetStrangerInfoResult | ||
import love.forte.simbot.component.onebot.v11.core.bot.internal.OneBotBotImpl | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
|
||
/** | ||
* | ||
* @author ForteScarlet | ||
*/ | ||
internal class OneBotStrangerImpl( | ||
override val coroutineContext: CoroutineContext, | ||
private val source: GetStrangerInfoResult | ||
) : OneBotStranger { | ||
|
||
override val id: ID | ||
get() = source.userId | ||
|
||
override val name: String | ||
get() = source.nickname | ||
|
||
override val age: Int | ||
get() = source.age | ||
|
||
override val sex: String | ||
get() = source.sex | ||
|
||
override fun toString(): String = | ||
"OneBotStranger(source=$source)" | ||
} | ||
|
||
internal fun GetStrangerInfoResult.toStranger( | ||
bot: OneBotBotImpl, | ||
coroutineContext: CoroutineContext = bot.subContext, | ||
): OneBotStrangerImpl = | ||
OneBotStrangerImpl(coroutineContext, this) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...e/forte/simbot/component/onebot/v11/core/event/internal/request/OneBotRequestEventImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright (c) 2024. ForteScarlet. | ||
* | ||
* This file is part of simbot-component-onebot. | ||
* | ||
* simbot-component-onebot is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Lesser General Public License as published by the Free Software Foundation, | ||
* either version 3 of the License, or (at your option) any later version. | ||
* | ||
* simbot-component-onebot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-onebot. | ||
* If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package love.forte.simbot.component.onebot.v11.core.event.internal.request | ||
|
||
import love.forte.simbot.common.id.ID | ||
import love.forte.simbot.common.id.UUID | ||
import love.forte.simbot.common.time.Timestamp | ||
import love.forte.simbot.component.onebot.v11.core.actor.OneBotGroup | ||
import love.forte.simbot.component.onebot.v11.core.actor.OneBotStranger | ||
import love.forte.simbot.component.onebot.v11.core.actor.internal.toStranger | ||
import love.forte.simbot.component.onebot.v11.core.api.GetStrangerInfoApi | ||
import love.forte.simbot.component.onebot.v11.core.api.SetFriendAddRequestApi | ||
import love.forte.simbot.component.onebot.v11.core.api.SetGroupAddRequestApi | ||
import love.forte.simbot.component.onebot.v11.core.bot.OneBotBot | ||
import love.forte.simbot.component.onebot.v11.core.bot.internal.OneBotBotImpl | ||
import love.forte.simbot.component.onebot.v11.core.bot.requestDataBy | ||
import love.forte.simbot.component.onebot.v11.core.event.internal.eventToString | ||
import love.forte.simbot.component.onebot.v11.core.event.request.OneBotFriendRequestEvent | ||
import love.forte.simbot.component.onebot.v11.core.event.request.OneBotGroupRequestEvent | ||
import love.forte.simbot.component.onebot.v11.core.event.request.OneBotRequestEvent | ||
import love.forte.simbot.component.onebot.v11.core.utils.timestamp | ||
import love.forte.simbot.component.onebot.v11.event.request.FriendRequestEvent | ||
import love.forte.simbot.component.onebot.v11.event.request.GroupRequestEvent | ||
|
||
|
||
internal abstract class OneBotRequestEventImpl : OneBotRequestEvent { | ||
override val id: ID = UUID.random() | ||
|
||
override val time: Timestamp | ||
get() = sourceEvent.timestamp() | ||
|
||
override suspend fun accept() { | ||
doSetRequest(true) | ||
} | ||
|
||
override suspend fun reject() { | ||
doSetRequest(false) | ||
} | ||
|
||
protected abstract suspend fun doSetRequest(approve: Boolean) | ||
} | ||
|
||
internal class OneBotFriendRequestEventImpl( | ||
override val sourceEventRaw: String?, | ||
override val sourceEvent: FriendRequestEvent, | ||
override val bot: OneBotBot, | ||
) : OneBotRequestEventImpl(), OneBotFriendRequestEvent { | ||
override suspend fun doSetRequest(approve: Boolean) { | ||
SetFriendAddRequestApi.create( | ||
flag = sourceEvent.flag, | ||
approve = approve, | ||
).requestDataBy(bot) | ||
} | ||
|
||
override fun toString(): String = | ||
eventToString("OneBotFriendRequestEvent") | ||
} | ||
|
||
internal class OneBotGroupRequestEventImpl( | ||
override val sourceEventRaw: String?, | ||
override val sourceEvent: GroupRequestEvent, | ||
override val bot: OneBotBotImpl, | ||
) : OneBotRequestEventImpl(), OneBotGroupRequestEvent { | ||
override suspend fun doSetRequest(approve: Boolean) { | ||
SetGroupAddRequestApi.create( | ||
flag = sourceEvent.flag, | ||
subType = sourceEvent.subType, | ||
approve = approve | ||
).requestDataBy(bot) | ||
} | ||
|
||
override suspend fun content(): OneBotGroup { | ||
return bot.groupRelation.group(sourceEvent.groupId) | ||
?: error("Group with id ${sourceEvent.groupId} not found") | ||
} | ||
|
||
override suspend fun requester(): OneBotStranger { | ||
return GetStrangerInfoApi | ||
.create(sourceEvent.userId) | ||
.requestDataBy(bot) | ||
.toStranger(bot) | ||
} | ||
|
||
override fun toString(): String = | ||
eventToString("OneBotGroupRequestEvent") | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters