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
...
@@ -12,6 +12,10 @@ import net.mamoe.mirai.plugin.PluginManager
object
CommandManager
{
object
CommandManager
{
private
val
registeredCommand
:
MutableMap
<
String
,
ICommand
>
=
mutableMapOf
()
private
val
registeredCommand
:
MutableMap
<
String
,
ICommand
>
=
mutableMapOf
()
fun
getCommands
():
Collection
<
ICommand
>
{
return
registeredCommand
.
values
}
fun
register
(
command
:
ICommand
)
{
fun
register
(
command
:
ICommand
)
{
val
allNames
=
mutableListOf
(
command
.
name
).
also
{
it
.
addAll
(
command
.
alias
)
}
val
allNames
=
mutableListOf
(
command
.
name
).
also
{
it
.
addAll
(
command
.
alias
)
}
...
@@ -32,6 +36,10 @@ object CommandManager {
...
@@ -32,6 +36,10 @@ object CommandManager {
}
}
}
}
fun
unregister
(
commandName
:
String
)
{
registeredCommand
.
remove
(
commandName
)
}
fun
runCommand
(
fullCommand
:
String
):
Boolean
{
fun
runCommand
(
fullCommand
:
String
):
Boolean
{
val
blocks
=
fullCommand
.
split
(
" "
)
val
blocks
=
fullCommand
.
split
(
" "
)
val
commandHead
=
blocks
[
0
].
replace
(
"/"
,
""
)
val
commandHead
=
blocks
[
0
].
replace
(
"/"
,
""
)
...
@@ -99,6 +107,10 @@ class CommandBuilder internal constructor() {
...
@@ -99,6 +107,10 @@ class CommandBuilder internal constructor() {
var
description
:
String
=
""
var
description
:
String
=
""
var
onCommand
:
(
ICommand
.(
args
:
List
<
String
>)
->
Boolean
)?
=
null
var
onCommand
:
(
ICommand
.(
args
:
List
<
String
>)
->
Boolean
)?
=
null
fun
onCommand
(
commandProcess
:
ICommand
.(
args
:
List
<
String
>)
->
Boolean
)
{
onCommand
=
commandProcess
}
fun
register
():
ICommand
{
fun
register
():
ICommand
{
if
(
name
==
null
||
onCommand
==
null
)
{
if
(
name
==
null
||
onCommand
==
null
)
{
error
(
"CommandBuilder not complete"
)
error
(
"CommandBuilder not complete"
)
...
@@ -111,8 +123,6 @@ class CommandBuilder internal constructor() {
...
@@ -111,8 +123,6 @@ class CommandBuilder internal constructor() {
}
}
fun
buildCommand
(
builder
:
CommandBuilder
.()
->
Unit
):
ICommand
{
fun
buildCommand
(
builder
:
CommandBuilder
.()
->
Unit
):
ICommand
{
val
builder2
=
CommandBuilder
()
return
CommandBuilder
().
apply
(
builder
).
register
()
builder
.
invoke
(
builder2
)
return
builder2
.
register
()
}
}
mirai-console/src/main/kotlin/MiraiConsole.kt
View file @
155ad75c
...
@@ -12,14 +12,25 @@ import kotlinx.serialization.UnstableDefault
...
@@ -12,14 +12,25 @@ import kotlinx.serialization.UnstableDefault
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.alsoLogin
import
net.mamoe.mirai.alsoLogin
import
net.mamoe.mirai.api.http.generateSessionKey
import
net.mamoe.mirai.api.http.generateSessionKey
import
net.mamoe.mirai.contact.sendMessage
import
net.mamoe.mirai.plugin.*
import
net.mamoe.mirai.plugin.*
import
java.io.File
import
java.io.File
import
kotlin.concurrent.thread
import
kotlin.concurrent.thread
import
kotlin.math.log
object
MiraiConsole
{
object
MiraiConsole
{
val
bots
val
bots
get
()
=
Bot
.
instances
get
()
=
Bot
.
instances
fun
getBotByUIN
(
uin
:
Long
):
Bot
?
{
bots
.
forEach
{
if
(
it
.
get
()
?.
uin
==
uin
)
{
return
it
.
get
()
}
}
return
null
}
val
pluginManager
:
PluginManager
val
pluginManager
:
PluginManager
get
()
=
PluginManager
get
()
=
PluginManager
...
@@ -46,7 +57,6 @@ object MiraiConsole {
...
@@ -46,7 +57,6 @@ object MiraiConsole {
DefaultCommands
()
DefaultCommands
()
pluginManager
.
loadPlugins
()
pluginManager
.
loadPlugins
()
CommandListener
.
start
()
CommandListener
.
start
()
println
(
MiraiProperties
.
HTTP_API_ENABLE
)
logger
(
"\"/login qqnumber qqpassword \" to login a bot"
)
logger
(
"\"/login qqnumber qqpassword \" to login a bot"
)
logger
(
"\"/login qq号 qq密码 \" 来登陆一个BOT"
)
logger
(
"\"/login qq号 qq密码 \" 来登陆一个BOT"
)
...
@@ -64,19 +74,19 @@ object MiraiConsole {
...
@@ -64,19 +74,19 @@ object MiraiConsole {
buildCommand
{
buildCommand
{
name
=
"login"
name
=
"login"
description
=
"Mirai-Console default bot login command"
description
=
"Mirai-Console default bot login command"
onCommand
=
{
onCommand
{
if
(
it
.
size
<
2
)
{
if
(
it
.
size
<
2
)
{
logger
(
"\"/login qqnumber qqpassword \" to login a bot"
)
logger
(
"\"/login qqnumber qqpassword \" to login a bot"
)
logger
(
"\"/login qq号 qq密码 \" 来登录一个BOT"
)
logger
(
"\"/login qq号 qq密码 \" 来登录一个BOT"
)
false
return
@
onCommand
false
}
}
val
qqNumber
=
it
[
0
].
toLong
()
val
qqNumber
=
it
[
0
].
toLong
()
val
qqPassword
=
it
[
1
]
val
qqPassword
=
it
[
1
]
println
(
"login..."
)
logger
(
"login..."
)
try
{
try
{
runBlocking
{
runBlocking
{
Bot
(
qqNumber
,
qqPassword
).
alsoLogin
()
Bot
(
qqNumber
,
qqPassword
).
alsoLogin
()
println
(
"$qqNumber login successe
d
"
)
println
(
"$qqNumber login successe
s
"
)
}
}
}
catch
(
e
:
Exception
)
{
}
catch
(
e
:
Exception
)
{
println
(
"$qqNumber login failed"
)
println
(
"$qqNumber login failed"
)
...
@@ -88,13 +98,23 @@ object MiraiConsole {
...
@@ -88,13 +98,23 @@ object MiraiConsole {
buildCommand
{
buildCommand
{
name
=
"status"
name
=
"status"
description
=
"Mirai-Console default status command"
description
=
"Mirai-Console default status command"
onCommand
=
{
onCommand
{
when
(
it
.
size
)
{
when
(
it
.
size
)
{
0
->
{
0
->
{
logger
(
"当前有"
+
bots
.
size
+
"个BOT在线"
)
}
}
1
->
{
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
true
...
@@ -105,14 +125,36 @@ object MiraiConsole {
...
@@ -105,14 +125,36 @@ object MiraiConsole {
buildCommand
{
buildCommand
{
name
=
"say"
name
=
"say"
description
=
"Mirai-Console default say command"
description
=
"Mirai-Console default say command"
onCommand
=
{
onCommand
{
when
(
it
.
size
)
{
if
(
it
.
size
<
2
)
{
0
->
{
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
true
}
}
...
@@ -123,16 +165,14 @@ object MiraiConsole {
...
@@ -123,16 +165,14 @@ object MiraiConsole {
name
=
"plugins"
name
=
"plugins"
alias
=
listOf
(
"plugin"
)
alias
=
listOf
(
"plugin"
)
description
=
"show all plugins"
description
=
"show all plugins"
onCommand
=
{
onCommand
{
when
(
it
.
size
)
{
PluginManager
.
getAllPluginDescriptions
().
let
{
0
->
{
println
(
"loaded "
+
it
.
size
+
" plugins"
)
it
.
forEach
{
}
logger
(
"\t"
+
it
.
name
+
" v"
+
it
.
version
+
" by"
+
it
.
author
+
" "
+
it
.
info
)
1
->
{
}
}
true
}
}
true
}
}
}
}
...
@@ -140,13 +180,11 @@ object MiraiConsole {
...
@@ -140,13 +180,11 @@ object MiraiConsole {
name
=
"command"
name
=
"command"
alias
=
listOf
(
"commands"
,
"help"
,
"helps"
)
alias
=
listOf
(
"commands"
,
"help"
,
"helps"
)
description
=
"show all commands"
description
=
"show all commands"
onCommand
=
{
onCommand
{
when
(
it
.
size
)
{
CommandManager
.
getCommands
().
let
{
0
->
{
println
(
"currently have "
+
it
.
size
+
" commands"
)
it
.
toSet
().
forEach
{
}
logger
(
"\t"
+
it
.
name
+
" :"
+
it
.
description
)
1
->
{
}
}
}
}
true
true
...
@@ -155,16 +193,13 @@ object MiraiConsole {
...
@@ -155,16 +193,13 @@ object MiraiConsole {
buildCommand
{
buildCommand
{
name
=
"about"
name
=
"about"
description
=
""
description
=
"About Mirai-Console"
onCommand
=
{
onCommand
{
when
(
it
.
size
)
{
logger
(
"v${version} $build is still in testing stage, majority feature is available"
)
0
->
{
logger
(
"now running under "
+
System
.
getProperty
(
"user.dir"
))
logger
(
"在Github中获取项目最新进展: https://github.com/mamoe/mirai"
)
}
logger
(
"Mirai为开源项目,请自觉遵守开源项目协议"
)
1
->
{
logger
(
"Powered by Mamoe Technology"
)
}
}
true
true
}
}
}
}
...
@@ -180,10 +215,12 @@ object MiraiConsole {
...
@@ -180,10 +215,12 @@ object MiraiConsole {
}
}
tailrec
fun
processNextCommandLine
()
{
tailrec
fun
processNextCommandLine
()
{
val
fullCommand
=
readLine
()
var
fullCommand
=
readLine
()
if
(
fullCommand
!=
null
&&
fullCommand
.
startsWith
(
"/"
))
{
if
(
fullCommand
!=
null
)
{
if
(!
fullCommand
.
startsWith
(
"/"
))
{
fullCommand
=
"/$fullCommand"
}
if
(!
CommandManager
.
runCommand
(
fullCommand
))
{
if
(!
CommandManager
.
runCommand
(
fullCommand
))
{
logger
(
"unknown command $fullCommand"
)
logger
(
"未知指令 $fullCommand"
)
logger
(
"未知指令 $fullCommand"
)
}
}
}
}
...
...
mirai-console/src/main/kotlin/net/mamoe/mirai/plugin/PluginBase.kt
View file @
155ad75c
...
@@ -163,6 +163,7 @@ object PluginManager {
...
@@ -163,6 +163,7 @@ object PluginManager {
//已完成加载的
//已完成加载的
private
val
nameToPluginBaseMap
:
MutableMap
<
String
,
PluginBase
>
=
mutableMapOf
()
private
val
nameToPluginBaseMap
:
MutableMap
<
String
,
PluginBase
>
=
mutableMapOf
()
private
val
pluginDescriptions
:
MutableMap
<
String
,
PluginDescription
>
=
mutableMapOf
()
fun
onCommand
(
command
:
ICommand
,
args
:
List
<
String
>)
{
fun
onCommand
(
command
:
ICommand
,
args
:
List
<
String
>)
{
this
.
nameToPluginBaseMap
.
values
.
forEach
{
this
.
nameToPluginBaseMap
.
values
.
forEach
{
...
@@ -170,6 +171,10 @@ object PluginManager {
...
@@ -170,6 +171,10 @@ object PluginManager {
}
}
}
}
fun
getAllPluginDescriptions
():
Collection
<
PluginDescription
>
{
return
pluginDescriptions
.
values
}
/**
/**
* 尝试加载全部插件
* 尝试加载全部插件
*/
*/
...
@@ -177,6 +182,8 @@ object PluginManager {
...
@@ -177,6 +182,8 @@ object PluginManager {
val
pluginsFound
:
MutableMap
<
String
,
PluginDescription
>
=
mutableMapOf
()
val
pluginsFound
:
MutableMap
<
String
,
PluginDescription
>
=
mutableMapOf
()
val
pluginsLocation
:
MutableMap
<
String
,
File
>
=
mutableMapOf
()
val
pluginsLocation
:
MutableMap
<
String
,
File
>
=
mutableMapOf
()
logger
.
info
(
"""开始加载${this.pluginsPath}下的插件"""
)
File
(
pluginsPath
).
listFiles
()
?.
forEach
{
file
->
File
(
pluginsPath
).
listFiles
()
?.
forEach
{
file
->
if
(
file
!=
null
&&
file
.
extension
==
"jar"
)
{
if
(
file
!=
null
&&
file
.
extension
==
"jar"
)
{
val
jar
=
JarFile
(
file
)
val
jar
=
JarFile
(
file
)
...
@@ -270,6 +277,7 @@ object PluginManager {
...
@@ -270,6 +277,7 @@ object PluginManager {
logger
.
info
(
description
.
info
)
logger
.
info
(
description
.
info
)
nameToPluginBaseMap
[
description
.
name
]
=
plugin
nameToPluginBaseMap
[
description
.
name
]
=
plugin
pluginDescriptions
[
description
.
name
]
=
description
plugin
.
init
(
description
)
plugin
.
init
(
description
)
true
true
}
catch
(
e
:
ClassCastException
)
{
}
catch
(
e
:
ClassCastException
)
{
...
@@ -290,6 +298,9 @@ object PluginManager {
...
@@ -290,6 +298,9 @@ object PluginManager {
nameToPluginBaseMap
.
values
.
forEach
{
nameToPluginBaseMap
.
values
.
forEach
{
it
.
enable
()
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