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
3f2845b3
Commit
3f2845b3
authored
Feb 22, 2020
by
jiahua.liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rebuild Command
parent
e9c79429
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
70 additions
and
27 deletions
+70
-27
mirai-console/src/main/kotlin/net/mamoe/mirai/console/BlockingCommand.kt
...rc/main/kotlin/net/mamoe/mirai/console/BlockingCommand.kt
+62
-19
mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt
...e/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt
+5
-5
mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginBase.kt
...main/kotlin/net/mamoe/mirai/console/plugins/PluginBase.kt
+3
-3
No files found.
mirai-console/src/main/kotlin/net/mamoe/mirai/console/Command.kt
→
mirai-console/src/main/kotlin/net/mamoe/mirai/console/
Blocking
Command.kt
View file @
3f2845b3
...
@@ -9,17 +9,25 @@
...
@@ -9,17 +9,25 @@
package
net.mamoe.mirai.console
package
net.mamoe.mirai.console
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.runBlocking
import
kotlinx.coroutines.withContext
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.console.plugins.PluginManager
import
net.mamoe.mirai.console.plugins.PluginManager
import
net.mamoe.mirai.contact.Contact
import
net.mamoe.mirai.contact.QQ
import
net.mamoe.mirai.contact.sendMessage
import
net.mamoe.mirai.message.data.MessageChain
object
CommandManager
{
object
CommandManager
{
private
val
registeredCommand
:
MutableMap
<
String
,
I
Command
>
=
mutableMapOf
()
private
val
registeredCommand
:
MutableMap
<
String
,
Command
>
=
mutableMapOf
()
fun
getCommands
():
Collection
<
I
Command
>
{
fun
getCommands
():
Collection
<
Command
>
{
return
registeredCommand
.
values
return
registeredCommand
.
values
}
}
fun
register
(
command
:
I
Command
)
{
fun
register
(
command
:
Command
)
{
val
allNames
=
mutableListOf
(
command
.
name
).
also
{
it
.
addAll
(
command
.
alias
)
}
val
allNames
=
mutableListOf
(
command
.
name
).
also
{
it
.
addAll
(
command
.
alias
)
}
allNames
.
forEach
{
allNames
.
forEach
{
if
(
registeredCommand
.
containsKey
(
it
))
{
if
(
registeredCommand
.
containsKey
(
it
))
{
...
@@ -31,7 +39,7 @@ object CommandManager {
...
@@ -31,7 +39,7 @@ object CommandManager {
}
}
}
}
fun
unregister
(
command
:
I
Command
)
{
fun
unregister
(
command
:
Command
)
{
val
allNames
=
mutableListOf
<
String
>(
command
.
name
).
also
{
it
.
addAll
(
command
.
alias
)
}
val
allNames
=
mutableListOf
<
String
>(
command
.
name
).
also
{
it
.
addAll
(
command
.
alias
)
}
allNames
.
forEach
{
allNames
.
forEach
{
registeredCommand
.
remove
(
it
)
registeredCommand
.
remove
(
it
)
...
@@ -42,7 +50,7 @@ object CommandManager {
...
@@ -42,7 +50,7 @@ object CommandManager {
registeredCommand
.
remove
(
commandName
)
registeredCommand
.
remove
(
commandName
)
}
}
suspend
fun
runCommand
(
fullCommand
:
String
):
Boolean
{
suspend
fun
runCommand
(
sender
:
CommandSender
,
fullCommand
:
String
):
Boolean
{
val
blocks
=
fullCommand
.
split
(
" "
)
val
blocks
=
fullCommand
.
split
(
" "
)
val
commandHead
=
blocks
[
0
].
replace
(
"/"
,
""
)
val
commandHead
=
blocks
[
0
].
replace
(
"/"
,
""
)
if
(!
registeredCommand
.
containsKey
(
commandHead
))
{
if
(!
registeredCommand
.
containsKey
(
commandHead
))
{
...
@@ -51,6 +59,7 @@ object CommandManager {
...
@@ -51,6 +59,7 @@ object CommandManager {
val
args
=
blocks
.
subList
(
1
,
blocks
.
size
)
val
args
=
blocks
.
subList
(
1
,
blocks
.
size
)
registeredCommand
[
commandHead
]
?.
run
{
registeredCommand
[
commandHead
]
?.
run
{
if
(
onCommand
(
if
(
onCommand
(
sender
,
blocks
.
subList
(
1
,
blocks
.
size
)
blocks
.
subList
(
1
,
blocks
.
size
)
)
)
)
{
)
{
...
@@ -62,26 +71,60 @@ object CommandManager {
...
@@ -62,26 +71,60 @@ object CommandManager {
}
}
interface
ICommand
{
interface
CommandSender
{
suspend
fun
sendMessage
(
messageChain
:
MessageChain
)
suspend
fun
sendMessage
(
message
:
String
)
fun
sendMessageBlocking
(
messageChain
:
MessageChain
)
=
runBlocking
{
sendMessage
(
messageChain
)
}
fun
sendMessageBlocking
(
message
:
String
)
=
runBlocking
{
sendMessage
(
message
)
}
}
object
ConsoleCommandSender
:
CommandSender
{
override
suspend
fun
sendMessage
(
messageChain
:
MessageChain
)
{
MiraiConsole
.
logger
(
messageChain
.
toString
())
}
override
suspend
fun
sendMessage
(
message
:
String
)
{
MiraiConsole
.
logger
(
message
)
}
}
class
ContactCommandSender
(
val
contact
:
Contact
)
:
CommandSender
{
override
suspend
fun
sendMessage
(
messageChain
:
MessageChain
)
{
contact
.
sendMessage
(
messageChain
)
}
override
suspend
fun
sendMessage
(
message
:
String
)
{
contact
.
sendMessage
(
message
)
}
}
interface
Command
{
val
name
:
String
val
name
:
String
val
alias
:
List
<
String
>
val
alias
:
List
<
String
>
val
description
:
String
val
description
:
String
suspend
fun
onCommand
(
args
:
List
<
String
>):
Boolean
suspend
fun
onCommand
(
sender
:
CommandSender
,
args
:
List
<
String
>):
Boolean
fun
register
()
fun
register
()
}
}
abstract
class
Command
(
abstract
class
Blocking
Command
(
override
val
name
:
String
,
override
val
name
:
String
,
override
val
alias
:
List
<
String
>
=
listOf
(),
override
val
alias
:
List
<
String
>
=
listOf
(),
override
val
description
:
String
=
""
override
val
description
:
String
=
""
)
:
I
Command
{
)
:
Command
{
/**
/**
* 最高优先级监听器
* 最高优先级监听器
* 如果 return `false` 这次指令不会被 [PluginBase] 的全局 onCommand 监听器监听
* 如果 return `false` 这次指令不会被 [PluginBase] 的全局 onCommand 监听器监听
* */
* */
open
override
suspend
fun
onCommand
(
args
:
List
<
String
>):
Boolean
{
final
override
suspend
fun
onCommand
(
sender
:
CommandSender
,
args
:
List
<
String
>):
Boolean
{
return
true
return
withContext
(
Dispatchers
.
IO
)
{
onCommandBlocking
(
sender
,
args
)
}
}
}
abstract
fun
onCommandBlocking
(
sender
:
CommandSender
,
args
:
List
<
String
>):
Boolean
override
fun
register
()
{
override
fun
register
()
{
CommandManager
.
register
(
this
)
CommandManager
.
register
(
this
)
...
@@ -92,10 +135,10 @@ class AnonymousCommand internal constructor(
...
@@ -92,10 +135,10 @@ class AnonymousCommand internal constructor(
override
val
name
:
String
,
override
val
name
:
String
,
override
val
alias
:
List
<
String
>,
override
val
alias
:
List
<
String
>,
override
val
description
:
String
,
override
val
description
:
String
,
val
onCommand
:
suspend
ICommand
.(
args
:
List
<
String
>)
->
Boolean
val
onCommand
:
suspend
CommandSender
.(
args
:
List
<
String
>)
->
Boolean
)
:
I
Command
{
)
:
Command
{
override
suspend
fun
onCommand
(
args
:
List
<
String
>):
Boolean
{
override
suspend
fun
onCommand
(
sender
:
CommandSender
,
args
:
List
<
String
>):
Boolean
{
return
onCommand
.
invoke
(
this
,
args
)
return
onCommand
.
invoke
(
sender
,
args
)
}
}
override
fun
register
()
{
override
fun
register
()
{
...
@@ -107,13 +150,13 @@ class CommandBuilder internal constructor() {
...
@@ -107,13 +150,13 @@ class CommandBuilder internal constructor() {
var
name
:
String
?
=
null
var
name
:
String
?
=
null
var
alias
:
List
<
String
>?
=
null
var
alias
:
List
<
String
>?
=
null
var
description
:
String
=
""
var
description
:
String
=
""
var
onCommand
:
(
suspend
ICommand
.(
args
:
List
<
String
>)
->
Boolean
)?
=
null
var
onCommand
:
(
suspend
CommandSender
.(
args
:
List
<
String
>)
->
Boolean
)?
=
null
fun
onCommand
(
commandProcess
:
suspend
ICommand
.(
args
:
List
<
String
>)
->
Boolean
)
{
fun
onCommand
(
commandProcess
:
suspend
CommandSender
.(
args
:
List
<
String
>)
->
Boolean
)
{
onCommand
=
commandProcess
onCommand
=
commandProcess
}
}
fun
register
():
I
Command
{
fun
register
():
Command
{
if
(
name
==
null
||
onCommand
==
null
)
{
if
(
name
==
null
||
onCommand
==
null
)
{
error
(
"net.mamoe.mirai.CommandBuilder not complete"
)
error
(
"net.mamoe.mirai.CommandBuilder not complete"
)
}
}
...
@@ -124,7 +167,7 @@ class CommandBuilder internal constructor() {
...
@@ -124,7 +167,7 @@ class CommandBuilder internal constructor() {
}
}
}
}
fun
registerCommand
(
builder
:
CommandBuilder
.()
->
Unit
):
I
Command
{
fun
registerCommand
(
builder
:
CommandBuilder
.()
->
Unit
):
Command
{
return
CommandBuilder
().
apply
(
builder
).
register
()
return
CommandBuilder
().
apply
(
builder
).
register
()
}
}
mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt
View file @
3f2845b3
...
@@ -50,11 +50,11 @@ object MiraiConsole {
...
@@ -50,11 +50,11 @@ object MiraiConsole {
var
path
:
String
=
System
.
getProperty
(
"user.dir"
)
var
path
:
String
=
System
.
getProperty
(
"user.dir"
)
val
version
=
"v0.01"
private
val
version
=
"v0.01"
var
coreVersion
=
"v0.18.0"
private
var
coreVersion
=
"v0.18.0"
val
build
=
"Zeta"
private
val
build
=
"Zeta"
var
allDown
=
false
private
var
allDown
=
false
lateinit
var
frontEnd
:
MiraiConsoleUI
lateinit
var
frontEnd
:
MiraiConsoleUI
fun
start
(
fun
start
(
...
@@ -314,7 +314,7 @@ object MiraiConsole {
...
@@ -314,7 +314,7 @@ object MiraiConsole {
if
(!
fullCommand
.
startsWith
(
"/"
))
{
if
(!
fullCommand
.
startsWith
(
"/"
))
{
fullCommand
=
"/$fullCommand"
fullCommand
=
"/$fullCommand"
}
}
if
(!
CommandManager
.
runCommand
(
fullCommand
))
{
if
(!
CommandManager
.
runCommand
(
ConsoleCommandSender
,
fullCommand
))
{
logger
(
"未知指令 $fullCommand"
)
logger
(
"未知指令 $fullCommand"
)
}
}
}
}
...
...
mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginBase.kt
View file @
3f2845b3
...
@@ -9,7 +9,7 @@
...
@@ -9,7 +9,7 @@
package
net.mamoe.mirai.console.plugins
package
net.mamoe.mirai.console.plugins
import
net.mamoe.mirai.console.
I
Command
import
net.mamoe.mirai.console.Command
import
kotlinx.coroutines.*
import
kotlinx.coroutines.*
import
net.mamoe.mirai.console.MiraiConsole
import
net.mamoe.mirai.console.MiraiConsole
import
net.mamoe.mirai.utils.DefaultLogger
import
net.mamoe.mirai.utils.DefaultLogger
...
@@ -58,7 +58,7 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
...
@@ -58,7 +58,7 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
/**
/**
* 当任意指令被使用
* 当任意指令被使用
*/
*/
open
fun
onCommand
(
command
:
I
Command
,
args
:
List
<
String
>)
{
open
fun
onCommand
(
command
:
Command
,
args
:
List
<
String
>)
{
}
}
...
@@ -173,7 +173,7 @@ object PluginManager {
...
@@ -173,7 +173,7 @@ object PluginManager {
private
val
nameToPluginBaseMap
:
MutableMap
<
String
,
PluginBase
>
=
mutableMapOf
()
private
val
nameToPluginBaseMap
:
MutableMap
<
String
,
PluginBase
>
=
mutableMapOf
()
private
val
pluginDescriptions
:
MutableMap
<
String
,
PluginDescription
>
=
mutableMapOf
()
private
val
pluginDescriptions
:
MutableMap
<
String
,
PluginDescription
>
=
mutableMapOf
()
fun
onCommand
(
command
:
I
Command
,
args
:
List
<
String
>)
{
fun
onCommand
(
command
:
Command
,
args
:
List
<
String
>)
{
nameToPluginBaseMap
.
values
.
forEach
{
nameToPluginBaseMap
.
values
.
forEach
{
it
.
onCommand
(
command
,
args
)
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