generated from project-mirai/mirai-console-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Main.kt
131 lines (118 loc) · 3.86 KB
/
Main.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Copyright (c) 2020-2021 Samarium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>
*/
package com.github.samarium150.mirai.plugin
import io.ktor.client.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
import io.ktor.util.*
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregister
import net.mamoe.mirai.console.permission.AbstractPermitteeId
import net.mamoe.mirai.console.permission.PermissionService.Companion.cancel
import net.mamoe.mirai.console.permission.PermissionService.Companion.permit
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import java.net.InetSocketAddress
import java.net.Proxy
/**
* Plugin instance
* <br>
* 插件实例
*
* @constructor Create a KotlinPlugin instance <br> 实例化插件
* @see net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
*/
object Main: KotlinPlugin(
JvmPluginDescription(
id = "com.github.samarium150.mirai-console-lolicon",
version = "4.1.0",
name = "mirai-console-lolicon"
)
) {
lateinit var client: HttpClient
/**
* Will be invoked when the plugin is enabled
* <br>
* 插件启用时将被调用
*/
@OptIn(KtorExperimentalAPI::class)
override fun onEnable() {
/**
* Load configurations and data
* <br>
* 加载配置及数据
*/
PluginConfig.reload()
CommandConfig.reload()
ReplyConfig.reload()
ProxyConfig.reload()
PluginData.reload()
if (PluginConfig.master != 0L) {
PluginData.trustedUsers.add(PluginConfig.master)
PluginData.userSet.add(PluginConfig.master)
PluginData.reload()
} else logger.warning("请先在配置文件设置Bot所有者id")
client = HttpClient {
engine {
proxy = if (ProxyConfig.type != "DIRECT") Proxy(
Utils.getProxyType(ProxyConfig.type),
InetSocketAddress(ProxyConfig.hostname, ProxyConfig.port)
) else Proxy.NO_PROXY
}
install(JsonFeature) {
serializer = KotlinxSerializer(kotlinx.serialization.json.Json {
prettyPrint = true
isLenient = true
})
}
}
/**
* Register commands
* <br>
* 注册命令
*/
Lolicon.register()
/**
* Grant permissions
* <br>
* 授予权限
*/
AbstractPermitteeId.AnyContact.permit(Lolicon.permission)
logger.info("Plugin mirai-console-lolicon loaded")
}
/**
* Will be invoked when the plugin is disabled
* <br>
* 插件禁用时调用
*/
override fun onDisable() {
/**
* Revoke permissions
* <br>
* 撤销权限
*/
AbstractPermitteeId.AnyContact.cancel(Lolicon.permission, true)
/**
* Unregister commands
* <br>
* 注销命令
*/
Lolicon.unregister()
client.close()
logger.info("Plugin mirai-console-lolicon unloaded")
}
}