mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-04-29 14:17:30 +00:00
feat: 为 BotExtensionFactory 提供一些可用的信息. (#125)
为 BotExtensionFactory 的 createExtensionInstance 方法添加 BotExtensionCreateOptions 参数, 通过 BotExtensionCreateOptions 为扩展包 Factory 提供更多信息, 以避免扩展重复声明配置文件. Issue #4
This commit is contained in:
parent
92a1deac50
commit
9dc4bfa28f
@ -1,6 +1,7 @@
|
||||
package net.lamgc.scalabot
|
||||
|
||||
import mu.KotlinLogging
|
||||
import net.lamgc.scalabot.extension.BotExtensionCreateOptions
|
||||
import net.lamgc.scalabot.extension.BotExtensionFactory
|
||||
import net.lamgc.scalabot.util.getPriority
|
||||
import org.eclipse.aether.artifact.Artifact
|
||||
@ -122,7 +123,12 @@ internal class ExtensionLoader(
|
||||
for (factory in extClassLoader.serviceLoader) {
|
||||
try {
|
||||
val extension =
|
||||
factory.createExtensionInstance(bot, getExtensionDataFolder(extensionArtifact))
|
||||
factory.createExtensionInstance(
|
||||
bot, getExtensionDataFolder(extensionArtifact),
|
||||
BotExtensionCreateOptions(
|
||||
bot.botConfig.proxy.copy()
|
||||
)
|
||||
)
|
||||
if (extension == null) {
|
||||
log.debug { "Factory ${factory::class.java} 创建插件时返回了 null, 已跳过. (BotName: ${bot.botUsername})" }
|
||||
continue
|
||||
|
@ -29,7 +29,7 @@ internal class ScalaBot(
|
||||
db: DBContext,
|
||||
options: DefaultBotOptions,
|
||||
extensionFinders: Set<ExtensionPackageFinder>,
|
||||
botConfig: BotConfig,
|
||||
val botConfig: BotConfig,
|
||||
private val creatorId: Long = botConfig.account.creatorId,
|
||||
val accountId: Long = botConfig.account.id,
|
||||
val extensions: Set<Artifact> = botConfig.extensions
|
||||
|
@ -1,5 +1,6 @@
|
||||
package net.lamgc.scalabot.simple;
|
||||
|
||||
import net.lamgc.scalabot.extension.BotExtensionCreateOptions;
|
||||
import net.lamgc.scalabot.extension.BotExtensionFactory;
|
||||
import org.telegram.abilitybots.api.bot.BaseAbilityBot;
|
||||
import org.telegram.abilitybots.api.util.AbilityExtension;
|
||||
@ -9,7 +10,7 @@ import java.io.File;
|
||||
public class SimpleExtensionFactory implements BotExtensionFactory {
|
||||
|
||||
@Override
|
||||
public AbilityExtension createExtensionInstance(BaseAbilityBot bot, File shareDataFolder) {
|
||||
public AbilityExtension createExtensionInstance(BaseAbilityBot bot, File shareDataFolder, BotExtensionCreateOptions options) {
|
||||
return new SayHelloExtension(bot);
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,7 @@ plugins {
|
||||
dependencies {
|
||||
implementation("commons-codec:commons-codec:1.16.1")
|
||||
api("org.telegram:telegrambots-abilities:6.9.7.1")
|
||||
api(project(":scalabot-meta"))
|
||||
|
||||
testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.1")
|
||||
testImplementation("org.mockito:mockito-core:5.11.0")
|
||||
|
@ -0,0 +1,22 @@
|
||||
package net.lamgc.scalabot.extension;
|
||||
|
||||
import net.lamgc.scalabot.config.ProxyConfig;
|
||||
|
||||
/**
|
||||
* BotExtension 创建参数.
|
||||
* <p>
|
||||
* 通过该类可向 {@link BotExtensionFactory} 提供更多创建 BotExtension 时可用的参数.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class BotExtensionCreateOptions {
|
||||
|
||||
private final ProxyConfig proxy;
|
||||
|
||||
public BotExtensionCreateOptions(ProxyConfig proxy) {
|
||||
this.proxy = proxy;
|
||||
}
|
||||
|
||||
public ProxyConfig getProxy() {
|
||||
return proxy;
|
||||
}
|
||||
}
|
@ -31,7 +31,34 @@ public interface BotExtensionFactory {
|
||||
* <b>同一个扩展包的 Factory</b> 接收到的共享数据目录<b>都是一样的</b>,
|
||||
* 建议将数据存储在数据目录中, 便于数据的存储管理.
|
||||
* @return 返回为该 Bot 对象创建的扩展对象, 如果不希望为该机器人提供扩展, 可返回 {@code null}.
|
||||
* @deprecated 请使用 {@link #createExtensionInstance(BaseAbilityBot, File, BotExtensionCreateOptions)},
|
||||
* 该方法最迟在 1.0.0 正式版中移除.
|
||||
* @since 0.0.1
|
||||
*/
|
||||
AbilityExtension createExtensionInstance(BaseAbilityBot bot, File shareDataFolder);
|
||||
@Deprecated(since = "0.7.0", forRemoval = true)
|
||||
default AbilityExtension createExtensionInstance(BaseAbilityBot bot, File shareDataFolder) {
|
||||
throw new UnsupportedOperationException("The method has not been implemented.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 为给定的 {@link BaseAbilityBot} 对象创建扩展.
|
||||
*
|
||||
* <p> 如扩展无使用 {@link org.telegram.abilitybots.api.db.DBContext} 的话,
|
||||
* 也可以返回扩展单例, 因为 AbilityBot 本身并不禁止多个机器人共用一个扩展对象
|
||||
* (AbilityBot 只是调用了扩展中的方法来创建 Ability 对象).
|
||||
*
|
||||
* @param bot 机器人对象.
|
||||
* @param shareDataFolder ScalaBot App 为扩展提供的共享数据目录.
|
||||
* <p>路径格式为:
|
||||
* <pre> $DATA_ROOT/data/extensions/{GroupId}/{ArtifactId}</pre>
|
||||
* <b>同一个扩展包的 Factory</b> 接收到的共享数据目录<b>都是一样的</b>,
|
||||
* 建议将数据存储在数据目录中, 便于数据的存储管理.
|
||||
* @param options 创建扩展时可用的参数.
|
||||
* @return 返回为该 Bot 对象创建的扩展对象, 如果不希望为该机器人提供扩展, 可返回 {@code null}.
|
||||
* @since 0.7.0
|
||||
*/
|
||||
default AbilityExtension createExtensionInstance(BaseAbilityBot bot, File shareDataFolder, BotExtensionCreateOptions options) {
|
||||
return createExtensionInstance(bot, shareDataFolder);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user