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
c42169a2
Commit
c42169a2
authored
Jan 25, 2020
by
Him188
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/master'
parents
a5ddba6e
d8feefcc
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
21 deletions
+62
-21
mirai-console/src/main/kotlin/MiraiConsole.kt
mirai-console/src/main/kotlin/MiraiConsole.kt
+37
-18
mirai-console/src/main/kotlin/net/mamoe/mirai/plugin/Command.kt
...console/src/main/kotlin/net/mamoe/mirai/plugin/Command.kt
+19
-1
mirai-console/src/main/kotlin/net/mamoe/mirai/plugin/PluginBase.kt
...sole/src/main/kotlin/net/mamoe/mirai/plugin/PluginBase.kt
+6
-2
No files found.
mirai-console/src/main/kotlin/MiraiConsole.kt
View file @
c42169a2
import
kotlinx.coroutines.runBlocking
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.alsoLogin
import
net.mamoe.mirai.plugin.Command
import
net.mamoe.mirai.plugin.CommandManager
import
net.mamoe.mirai.plugin.PluginManager
import
kotlin.concurrent.thread
fun
main
()
{
val
bots
=
mutableMapOf
<
Long
,
Bot
>()
fun
main
()
{
println
(
"loading Mirai in console environments"
)
println
(
"正在控制台环境中启动Mirai "
)
println
()
...
...
@@ -15,39 +17,56 @@ fun main() {
println
(
"Mirai-console now running on "
+
System
.
getProperty
(
"user.dir"
))
println
(
"Mirai-console 正在 "
+
System
.
getProperty
(
"user.dir"
)
+
" 运行"
)
println
()
println
(
"\"login qqnumber qqpassword \" to login a bot"
)
println
(
"\"login qq号 qq密码 \" 来登陆一个BOT"
)
println
(
"\"
/
login qqnumber qqpassword \" to login a bot"
)
println
(
"\"
/
login qq号 qq密码 \" 来登陆一个BOT"
)
thread
{
processNextCommandLine
()
}
PluginManager
.
loadPlugins
()
defaultCommands
()
Runtime
.
getRuntime
().
addShutdownHook
(
thread
(
start
=
false
)
{
PluginManager
.
disableAllPlugins
()
})
}
tailrec
fun
processNextCommandLine
()
{
val
commandArgs
=
readLine
()
?.
split
(
" "
)
?:
return
when
(
commandArgs
[
0
])
{
"login"
->
{
if
(
commandArgs
.
size
<
3
)
{
println
(
"\"login qqnumber qqpassword \" to login a bot"
)
println
(
"\"login qq号 qq密码 \" 来登录一个BOT"
)
return
processNextCommandLine
()
fun
defaultCommands
()
{
class
LoginCommand
:
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
}
val
qqNumber
=
commandArgs
[
1
].
toLong
()
val
qqPassword
=
commandArgs
[
2
]
val
qqNumber
=
args
[
0
].
toLong
()
val
qqPassword
=
args
[
1
]
println
(
"login..."
)
runBlocking
{
try
{
Bot
(
qqNumber
,
qqPassword
).
alsoLogin
()
Bot
(
qqNumber
,
qqPassword
).
also
{
it
.
login
()
bots
[
qqNumber
]
=
it
}
}
catch
(
e
:
Exception
)
{
println
(
"login failed"
)
println
(
"
$qqNumber
login failed"
)
}
}
return
true
}
}
CommandManager
.
register
(
LoginCommand
())
}
tailrec
fun
processNextCommandLine
()
{
val
fullCommand
=
readLine
()
if
(
fullCommand
!=
null
&&
fullCommand
.
startsWith
(
"/"
))
{
if
(!
CommandManager
.
runCommand
(
fullCommand
))
{
println
(
"unknown command $fullCommand"
)
println
(
"未知指令 $fullCommand"
)
}
}
return
processNextCommandLine
()
processNextCommandLine
();
}
mirai-console/src/main/kotlin/net/mamoe/mirai/plugin/Command.kt
View file @
c42169a2
...
...
@@ -16,6 +16,24 @@ object CommandManager {
}
}
fun
runCommand
(
fullCommand
:
String
):
Boolean
{
val
blocks
=
fullCommand
.
split
(
" "
)
val
commandHead
=
blocks
[
0
].
replace
(
"/"
,
""
)
if
(!
registeredCommand
.
containsKey
(
commandHead
))
{
return
false
}
val
args
=
blocks
.
subList
(
1
,
blocks
.
size
)
registeredCommand
[
commandHead
]
?.
run
{
if
(
onCommand
(
blocks
.
subList
(
1
,
blocks
.
size
)
)
)
{
PluginManager
.
onCommand
(
this
,
args
)
}
}
return
true
}
}
...
...
@@ -27,7 +45,7 @@ abstract class Command(
* 最高优先级监听器
* 如果return [false] 这次指令不会被[PluginBase]的全局onCommand监听器监听
* */
fun
onCommand
(
args
:
List
<
String
>):
Boolean
{
open
fun
onCommand
(
args
:
List
<
String
>):
Boolean
{
return
true
}
}
mirai-console/src/main/kotlin/net/mamoe/mirai/plugin/PluginBase.kt
View file @
c42169a2
...
...
@@ -48,7 +48,7 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
/**
* 当任意指令被使用
*/
open
fun
onCommand
(
command
:
Command
)
{
open
fun
onCommand
(
command
:
Command
,
args
:
List
<
String
>
)
{
}
...
...
@@ -176,6 +176,11 @@ object PluginManager {
//已完成加载的
private
val
nameToPluginBaseMap
:
MutableMap
<
String
,
PluginBase
>
=
mutableMapOf
()
fun
onCommand
(
command
:
Command
,
args
:
List
<
String
>)
{
this
.
nameToPluginBaseMap
.
values
.
forEach
{
it
.
onCommand
(
command
,
args
)
}
}
/**
* 尝试加载全部插件
...
...
@@ -196,7 +201,6 @@ object PluginManager {
PluginDescription
.
readFromContent
(
URL
(
"jar:file:"
+
file
.
absoluteFile
+
"!/"
+
pluginYml
.
name
).
openConnection
().
inputStream
.
use
{
it
.
readBytes
().
encodeToString
()
})
println
(
description
)
pluginsFound
[
description
.
name
]
=
description
pluginsLocation
[
description
.
name
]
=
file
}
...
...
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