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
0e39c300
Commit
0e39c300
authored
Feb 13, 2020
by
jiahua.liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Command Builder
parent
f42e9857
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
169 additions
and
30 deletions
+169
-30
mirai-console/src/main/kotlin/Command.kt
mirai-console/src/main/kotlin/Command.kt
+59
-9
mirai-console/src/main/kotlin/MiraiConsole.kt
mirai-console/src/main/kotlin/MiraiConsole.kt
+107
-19
mirai-console/src/main/kotlin/net/mamoe/mirai/plugin/PluginBase.kt
...sole/src/main/kotlin/net/mamoe/mirai/plugin/PluginBase.kt
+3
-2
No files found.
mirai-console/src/main/kotlin/Command.kt
View file @
0e39c300
...
...
@@ -10,11 +10,11 @@
import
net.mamoe.mirai.plugin.PluginManager
object
CommandManager
{
private
val
registeredCommand
:
MutableMap
<
String
,
Command
>
=
mutableMapOf
()
private
val
registeredCommand
:
MutableMap
<
String
,
I
Command
>
=
mutableMapOf
()
fun
register
(
command
:
Command
)
{
val
allNames
=
mutableListOf
<
String
>
(
command
.
name
).
also
{
it
.
addAll
(
command
.
alias
)
}
fun
register
(
command
:
I
Command
)
{
val
allNames
=
mutableListOf
(
command
.
name
).
also
{
it
.
addAll
(
command
.
alias
)
}
allNames
.
forEach
{
if
(
registeredCommand
.
containsKey
(
it
))
{
error
(
"Command Name(or Alias) $it is already registered, consider if same function plugin was installed"
)
...
...
@@ -25,7 +25,7 @@ object CommandManager {
}
}
fun
unregister
(
command
:
Command
)
{
fun
unregister
(
command
:
I
Command
)
{
val
allNames
=
mutableListOf
<
String
>(
command
.
name
).
also
{
it
.
addAll
(
command
.
alias
)
}
allNames
.
forEach
{
registeredCommand
.
remove
(
it
)
...
...
@@ -52,17 +52,67 @@ object CommandManager {
}
interface
ICommand
{
val
name
:
String
val
alias
:
List
<
String
>
val
description
:
String
fun
onCommand
(
args
:
List
<
String
>):
Boolean
fun
register
()
}
abstract
class
Command
(
val
name
:
String
,
val
alias
:
List
<
String
>
=
listOf
(),
val
description
:
String
=
""
)
{
override
val
name
:
String
,
override
val
alias
:
List
<
String
>
=
listOf
(),
override
val
description
:
String
=
""
)
:
ICommand
{
/**
* 最高优先级监听器
* 如果return [false] 这次指令不会被[PluginBase]的全局onCommand监听器监听
* */
open
fun
onCommand
(
args
:
List
<
String
>):
Boolean
{
open
override
fun
onCommand
(
args
:
List
<
String
>):
Boolean
{
return
true
}
override
fun
register
()
{
CommandManager
.
register
(
this
)
}
}
class
AnonymousCommand
internal
constructor
(
override
val
name
:
String
,
override
val
alias
:
List
<
String
>,
override
val
description
:
String
,
val
onCommand
:
ICommand
.(
args
:
List
<
String
>)
->
Boolean
)
:
ICommand
{
override
fun
onCommand
(
args
:
List
<
String
>):
Boolean
{
return
onCommand
.
invoke
(
this
,
args
)
}
override
fun
register
()
{
CommandManager
.
register
(
this
)
}
}
class
CommandBuilder
internal
constructor
()
{
var
name
:
String
?
=
null
var
alias
:
List
<
String
>?
=
null
var
description
:
String
=
""
var
onCommand
:
(
ICommand
.(
args
:
List
<
String
>)
->
Boolean
)?
=
null
fun
register
():
ICommand
{
if
(
name
==
null
||
onCommand
==
null
)
{
error
(
"CommandBuilder not complete"
)
}
if
(
alias
==
null
)
{
alias
=
listOf
()
}
return
AnonymousCommand
(
name
!!
,
alias
!!
,
description
,
onCommand
!!
).
also
{
it
.
register
()
}
}
}
fun
buildCommand
(
builder
:
CommandBuilder
.()
->
Unit
):
ICommand
{
val
builder2
=
CommandBuilder
()
builder
.
invoke
(
builder2
)
return
builder2
.
register
()
}
mirai-console/src/main/kotlin/MiraiConsole.kt
View file @
0e39c300
...
...
@@ -42,7 +42,8 @@ object MiraiConsole {
logger
(
"Mirai为开源项目,请自觉遵守开源项目协议"
)
logger
(
"Powered by Mamoe Technology"
)
logger
()
CommandManager
.
register
(
DefaultCommands
.
DefaultLoginCommand
())
DefaultCommands
()
pluginManager
.
loadPlugins
()
CommandListener
.
start
()
println
(
MiraiProperties
.
HTTP_API_ENABLE
)
...
...
@@ -59,29 +60,116 @@ object MiraiConsole {
* Defaults Commands are recommend to be replaced by plugin provided commands
*/
object
DefaultCommands
{
class
DefaultLoginCommand
:
Command
(
"login"
)
{
override
fun
onCommand
(
args
:
List
<
String
>):
Boolean
{
if
(
args
.
size
<
2
)
{
println
(
"\"/login qqnumber qqpassword \" to login a bot"
)
println
(
"\"/login qq号 qq密码 \" 来登录一个BOT"
)
return
false
operator
fun
invoke
()
{
buildCommand
{
name
=
"login"
description
=
"Mirai-Console default bot login command"
onCommand
=
{
if
(
it
.
size
<
2
)
{
logger
(
"\"/login qqnumber qqpassword \" to login a bot"
)
logger
(
"\"/login qq号 qq密码 \" 来登录一个BOT"
)
false
}
val
qqNumber
=
it
[
0
].
toLong
()
val
qqPassword
=
it
[
1
]
println
(
"login..."
)
try
{
runBlocking
{
Bot
(
qqNumber
,
qqPassword
).
alsoLogin
()
println
(
"$qqNumber login successed"
)
}
}
catch
(
e
:
Exception
)
{
println
(
"$qqNumber login failed"
)
}
true
}
val
qqNumber
=
args
[
0
].
toLong
()
val
qqPassword
=
args
[
1
]
println
(
"login..."
)
try
{
runBlocking
{
Bot
(
qqNumber
,
qqPassword
).
alsoLogin
()
}
buildCommand
{
name
=
"status"
description
=
"Mirai-Console default status command"
onCommand
=
{
when
(
it
.
size
)
{
0
->
{
}
1
->
{
}
}
}
catch
(
e
:
Exception
)
{
println
(
"$qqNumber login failed"
)
true
}
return
true
}
}
buildCommand
{
name
=
"say"
description
=
"Mirai-Console default say command"
onCommand
=
{
when
(
it
.
size
)
{
0
->
{
}
1
->
{
}
}
true
}
}
buildCommand
{
name
=
"plugins"
alias
=
listOf
(
"plugin"
)
description
=
"show all plugins"
onCommand
=
{
when
(
it
.
size
)
{
0
->
{
}
1
->
{
}
}
true
}
}
buildCommand
{
name
=
"command"
alias
=
listOf
(
"commands"
,
"help"
,
"helps"
)
description
=
"show all commands"
onCommand
=
{
when
(
it
.
size
)
{
0
->
{
}
1
->
{
}
}
true
}
}
buildCommand
{
name
=
"about"
description
=
""
onCommand
=
{
when
(
it
.
size
)
{
0
->
{
}
1
->
{
}
}
true
}
}
}
}
object
CommandListener
{
...
...
mirai-console/src/main/kotlin/net/mamoe/mirai/plugin/PluginBase.kt
View file @
0e39c300
...
...
@@ -10,6 +10,7 @@
package
net.mamoe.mirai.plugin
import
Command
import
ICommand
import
kotlinx.coroutines.*
import
kotlinx.serialization.UnstableDefault
import
kotlinx.serialization.json.Json
...
...
@@ -58,7 +59,7 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
/**
* 当任意指令被使用
*/
open
fun
onCommand
(
command
:
Command
,
args
:
List
<
String
>)
{
open
fun
onCommand
(
command
:
I
Command
,
args
:
List
<
String
>)
{
}
...
...
@@ -163,7 +164,7 @@ object PluginManager {
//已完成加载的
private
val
nameToPluginBaseMap
:
MutableMap
<
String
,
PluginBase
>
=
mutableMapOf
()
fun
onCommand
(
command
:
Command
,
args
:
List
<
String
>)
{
fun
onCommand
(
command
:
I
Command
,
args
:
List
<
String
>)
{
this
.
nameToPluginBaseMap
.
values
.
forEach
{
it
.
onCommand
(
command
,
args
)
}
...
...
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