Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
155ad75c
Commit
155ad75c
authored
Feb 13, 2020
by
jiahua.liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Command Builder
parent
0e39c300
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
103 additions
and
45 deletions
+103
-45
mirai-console/src/main/kotlin/Command.kt
mirai-console/src/main/kotlin/Command.kt
+13
-3
mirai-console/src/main/kotlin/MiraiConsole.kt
mirai-console/src/main/kotlin/MiraiConsole.kt
+79
-42
mirai-console/src/main/kotlin/net/mamoe/mirai/plugin/PluginBase.kt
...sole/src/main/kotlin/net/mamoe/mirai/plugin/PluginBase.kt
+11
-0
No files found.
mirai-console/src/main/kotlin/Command.kt
View file @
155ad75c
...
...
@@ -12,6 +12,10 @@ import net.mamoe.mirai.plugin.PluginManager
object
CommandManager
{
private
val
registeredCommand
:
MutableMap
<
String
,
ICommand
>
=
mutableMapOf
()
fun
getCommands
():
Collection
<
ICommand
>
{
return
registeredCommand
.
values
}
fun
register
(
command
:
ICommand
)
{
val
allNames
=
mutableListOf
(
command
.
name
).
also
{
it
.
addAll
(
command
.
alias
)
}
...
...
@@ -32,6 +36,10 @@ object CommandManager {
}
}
fun
unregister
(
commandName
:
String
)
{
registeredCommand
.
remove
(
commandName
)
}
fun
runCommand
(
fullCommand
:
String
):
Boolean
{
val
blocks
=
fullCommand
.
split
(
" "
)
val
commandHead
=
blocks
[
0
].
replace
(
"/"
,
""
)
...
...
@@ -99,6 +107,10 @@ class CommandBuilder internal constructor() {
var
description
:
String
=
""
var
onCommand
:
(
ICommand
.(
args
:
List
<
String
>)
->
Boolean
)?
=
null
fun
onCommand
(
commandProcess
:
ICommand
.(
args
:
List
<
String
>)
->
Boolean
)
{
onCommand
=
commandProcess
}
fun
register
():
ICommand
{
if
(
name
==
null
||
onCommand
==
null
)
{
error
(
"CommandBuilder not complete"
)
...
...
@@ -111,8 +123,6 @@ class CommandBuilder internal constructor() {
}
fun
buildCommand
(
builder
:
CommandBuilder
.()
->
Unit
):
ICommand
{
val
builder2
=
CommandBuilder
()
builder
.
invoke
(
builder2
)
return
builder2
.
register
()
return
CommandBuilder
().
apply
(
builder
).
register
()
}
mirai-console/src/main/kotlin/MiraiConsole.kt
View file @
155ad75c
...
...
@@ -12,14 +12,25 @@ import kotlinx.serialization.UnstableDefault
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.alsoLogin
import
net.mamoe.mirai.api.http.generateSessionKey
import
net.mamoe.mirai.contact.sendMessage
import
net.mamoe.mirai.plugin.*
import
java.io.File
import
kotlin.concurrent.thread
import
kotlin.math.log
object
MiraiConsole
{
val
bots
get
()
=
Bot
.
instances
fun
getBotByUIN
(
uin
:
Long
):
Bot
?
{
bots
.
forEach
{
if
(
it
.
get
()
?.
uin
==
uin
)
{
return
it
.
get
()
}
}
return
null
}
val
pluginManager
:
PluginManager
get
()
=
PluginManager
...
...
@@ -46,7 +57,6 @@ object MiraiConsole {
DefaultCommands
()
pluginManager
.
loadPlugins
()
CommandListener
.
start
()
println
(
MiraiProperties
.
HTTP_API_ENABLE
)
logger
(
"\"/login qqnumber qqpassword \" to login a bot"
)
logger
(
"\"/login qq号 qq密码 \" 来登陆一个BOT"
)
...
...
@@ -64,19 +74,19 @@ object MiraiConsole {
buildCommand
{
name
=
"login"
description
=
"Mirai-Console default bot login command"
onCommand
=
{
onCommand
{
if
(
it
.
size
<
2
)
{
logger
(
"\"/login qqnumber qqpassword \" to login a bot"
)
logger
(
"\"/login qq号 qq密码 \" 来登录一个BOT"
)
false
return
@
onCommand
false
}
val
qqNumber
=
it
[
0
].
toLong
()
val
qqPassword
=
it
[
1
]
println
(
"login..."
)
logger
(
"login..."
)
try
{
runBlocking
{
Bot
(
qqNumber
,
qqPassword
).
alsoLogin
()
println
(
"$qqNumber login successe
d
"
)
println
(
"$qqNumber login successe
s
"
)
}
}
catch
(
e
:
Exception
)
{
println
(
"$qqNumber login failed"
)
...
...
@@ -88,13 +98,23 @@ object MiraiConsole {
buildCommand
{
name
=
"status"
description
=
"Mirai-Console default status command"
onCommand
=
{
onCommand
{
when
(
it
.
size
)
{
0
->
{
logger
(
"当前有"
+
bots
.
size
+
"个BOT在线"
)
}
1
->
{
val
bot
=
it
[
0
]
var
find
=
false
bots
.
forEach
{
if
(
it
.
get
()
?.
uin
.
toString
().
contains
(
bot
))
{
find
=
true
logger
(
""
+
it
.
get
()
?.
uin
+
": 在线中; 好友数量:"
+
it
.
get
()
?.
qqs
?.
size
+
"; 群组数量:"
+
it
.
get
()
?.
groups
?.
size
)
}
}
if
(!
find
)
{
logger
(
"没有找到BOT$bot"
)
}
}
}
true
...
...
@@ -105,14 +125,36 @@ object MiraiConsole {
buildCommand
{
name
=
"say"
description
=
"Mirai-Console default say command"
onCommand
=
{
when
(
it
.
size
)
{
0
->
{
onCommand
{
if
(
it
.
size
<
2
)
{
logger
(
"say [好友qq号或者群号] [文本消息] //将默认使用第一个BOT"
)
logger
(
"say [bot号] [好友qq号或者群号] [文本消息]"
)
return
@
onCommand
false
}
val
bot
:
Bot
?
=
if
(
it
.
size
==
2
)
{
if
(
bots
.
size
==
0
)
{
logger
(
"还没有BOT登陆"
)
return
@
onCommand
false
}
1
->
{
bots
[
0
].
get
()
}
else
{
getBotByUIN
(
it
[
0
].
toLong
())
}
if
(
bot
==
null
)
{
logger
(
"没有找到BOT"
)
return
@
onCommand
false
}
val
target
=
it
[
it
.
size
-
2
].
toLong
()
val
message
=
it
[
it
.
size
-
1
]
try
{
val
contact
=
bot
[
target
]
runBlocking
{
contact
.
sendMessage
(
message
)
logger
(
"消息已推送"
)
}
}
catch
(
e
:
NoSuchElementException
)
{
logger
(
"没有找到群或好友 号码为${target}"
)
return
@
onCommand
false
}
true
}
...
...
@@ -123,16 +165,14 @@ object MiraiConsole {
name
=
"plugins"
alias
=
listOf
(
"plugin"
)
description
=
"show all plugins"
onCommand
=
{
when
(
it
.
size
)
{
0
->
{
}
1
->
{
onCommand
{
PluginManager
.
getAllPluginDescriptions
().
let
{
println
(
"loaded "
+
it
.
size
+
" plugins"
)
it
.
forEach
{
logger
(
"\t"
+
it
.
name
+
" v"
+
it
.
version
+
" by"
+
it
.
author
+
" "
+
it
.
info
)
}
true
}
true
}
}
...
...
@@ -140,13 +180,11 @@ object MiraiConsole {
name
=
"command"
alias
=
listOf
(
"commands"
,
"help"
,
"helps"
)
description
=
"show all commands"
onCommand
=
{
when
(
it
.
size
)
{
0
->
{
}
1
->
{
onCommand
{
CommandManager
.
getCommands
().
let
{
println
(
"currently have "
+
it
.
size
+
" commands"
)
it
.
toSet
().
forEach
{
logger
(
"\t"
+
it
.
name
+
" :"
+
it
.
description
)
}
}
true
...
...
@@ -155,16 +193,13 @@ object MiraiConsole {
buildCommand
{
name
=
"about"
description
=
""
onCommand
=
{
when
(
it
.
size
)
{
0
->
{
}
1
->
{
}
}
description
=
"About Mirai-Console"
onCommand
{
logger
(
"v${version} $build is still in testing stage, majority feature is available"
)
logger
(
"now running under "
+
System
.
getProperty
(
"user.dir"
))
logger
(
"在Github中获取项目最新进展: https://github.com/mamoe/mirai"
)
logger
(
"Mirai为开源项目,请自觉遵守开源项目协议"
)
logger
(
"Powered by Mamoe Technology"
)
true
}
}
...
...
@@ -180,10 +215,12 @@ object MiraiConsole {
}
tailrec
fun
processNextCommandLine
()
{
val
fullCommand
=
readLine
()
if
(
fullCommand
!=
null
&&
fullCommand
.
startsWith
(
"/"
))
{
var
fullCommand
=
readLine
()
if
(
fullCommand
!=
null
)
{
if
(!
fullCommand
.
startsWith
(
"/"
))
{
fullCommand
=
"/$fullCommand"
}
if
(!
CommandManager
.
runCommand
(
fullCommand
))
{
logger
(
"unknown command $fullCommand"
)
logger
(
"未知指令 $fullCommand"
)
}
}
...
...
mirai-console/src/main/kotlin/net/mamoe/mirai/plugin/PluginBase.kt
View file @
155ad75c
...
...
@@ -163,6 +163,7 @@ object PluginManager {
//已完成加载的
private
val
nameToPluginBaseMap
:
MutableMap
<
String
,
PluginBase
>
=
mutableMapOf
()
private
val
pluginDescriptions
:
MutableMap
<
String
,
PluginDescription
>
=
mutableMapOf
()
fun
onCommand
(
command
:
ICommand
,
args
:
List
<
String
>)
{
this
.
nameToPluginBaseMap
.
values
.
forEach
{
...
...
@@ -170,6 +171,10 @@ object PluginManager {
}
}
fun
getAllPluginDescriptions
():
Collection
<
PluginDescription
>
{
return
pluginDescriptions
.
values
}
/**
* 尝试加载全部插件
*/
...
...
@@ -177,6 +182,8 @@ object PluginManager {
val
pluginsFound
:
MutableMap
<
String
,
PluginDescription
>
=
mutableMapOf
()
val
pluginsLocation
:
MutableMap
<
String
,
File
>
=
mutableMapOf
()
logger
.
info
(
"""开始加载${this.pluginsPath}下的插件"""
)
File
(
pluginsPath
).
listFiles
()
?.
forEach
{
file
->
if
(
file
!=
null
&&
file
.
extension
==
"jar"
)
{
val
jar
=
JarFile
(
file
)
...
...
@@ -270,6 +277,7 @@ object PluginManager {
logger
.
info
(
description
.
info
)
nameToPluginBaseMap
[
description
.
name
]
=
plugin
pluginDescriptions
[
description
.
name
]
=
description
plugin
.
init
(
description
)
true
}
catch
(
e
:
ClassCastException
)
{
...
...
@@ -290,6 +298,9 @@ object PluginManager {
nameToPluginBaseMap
.
values
.
forEach
{
it
.
enable
()
}
logger
.
info
(
"""加载了${this.nameToPluginBaseMap.size}个插件"""
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment