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
5402505a
Commit
5402505a
authored
Feb 25, 2020
by
jiahua.liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
to
merge
parent
014439e7
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
98 additions
and
36 deletions
+98
-36
mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/ConfigSection.kt
...n/kotlin/net/mamoe/mirai/console/plugins/ConfigSection.kt
+5
-3
mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginBase.kt
...main/kotlin/net/mamoe/mirai/console/plugins/PluginBase.kt
+10
-1
mirai-plugins/image-sender/src/main/java/net/mamoe/mirai/imageplugin/ImageSenderMain.kt
.../main/java/net/mamoe/mirai/imageplugin/ImageSenderMain.kt
+83
-32
mirai-plugins/image-sender/src/main/resources/Test.kt
mirai-plugins/image-sender/src/main/resources/Test.kt
+0
-0
No files found.
mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/ConfigSection.kt
View file @
5402505a
...
...
@@ -184,9 +184,11 @@ class WithDefaultWriteLoader<T : Any>(
prop
:
KProperty
<
*
>
):
ReadWriteProperty
<
Any
,
T
>
{
val
defaultValue
by
lazy
{
defaultValue
.
invoke
()
}
config
.
setIfAbsent
(
prop
.
name
,
defaultValue
)
if
(
save
)
{
config
.
save
()
if
(!
config
.
contains
(
prop
.
name
))
{
config
[
prop
.
name
]
=
defaultValue
if
(
save
)
{
config
.
save
()
}
}
return
object
:
ReadWriteProperty
<
Any
,
T
>
{
override
fun
getValue
(
thisRef
:
Any
,
property
:
KProperty
<
*
>):
T
{
...
...
mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginBase.kt
View file @
5402505a
...
...
@@ -90,15 +90,24 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
fun
getPluginManager
()
=
PluginManager
val
logger
:
MiraiLogger
by
lazy
{
DefaultLogger
(
pluginDescription
.
name
)
SimpleLogger
(
"Plugin ${pluginDescription.name}"
)
{
_
,
message
,
e
->
MiraiConsole
.
logger
(
"[${pluginDescription.name}]"
,
0
,
message
)
if
(
e
!=
null
)
{
MiraiConsole
.
logger
(
"[${pluginDescription.name}]"
,
0
,
e
.
toString
())
e
.
printStackTrace
()
}
}
}
fun
getResources
(
fileName
:
String
):
InputStream
?
{
return
PluginManager
.
getFileInJarByName
(
this
.
pluginDescription
.
name
,
fileName
)
}
//fun getResourcesConfig()
}
class
PluginDescription
(
...
...
mirai-plugins/image-sender/src/main/java/net/mamoe/mirai/imageplugin/ImageSenderMain.kt
View file @
5402505a
...
...
@@ -10,19 +10,29 @@
package
net.mamoe.mirai.imageplugin
import
kotlinx.coroutines.*
import
net.mamoe.mirai.console.MiraiConsole
import
net.mamoe.mirai.console.command.registerCommand
import
net.mamoe.mirai.console.plugins.Config
import
net.mamoe.mirai.console.plugins.ConfigSection
import
net.mamoe.mirai.console.plugins.PluginBase
import
net.mamoe.mirai.console.plugins.withDefaultWriteSave
import
net.mamoe.mirai.contact.Contact
import
net.mamoe.mirai.contact.sendMessage
import
net.mamoe.mirai.event.events.BotOnlineEvent
import
net.mamoe.mirai.event.events.MemberPermissionChangeEvent
import
net.mamoe.mirai.event.subscribeAlways
import
net.mamoe.mirai.event.subscribeGroupMessages
import
net.mamoe.mirai.event.subscribeMessages
import
net.mamoe.mirai.message.data.Image
import
net.mamoe.mirai.message.data.sendTo
import
net.mamoe.mirai.message.upload
import
net.mamoe.mirai.message.uploadAsImage
import
net.mamoe.mirai.utils.MiraiExperimentalAPI
import
org.jsoup.Jsoup
import
java.io.File
import
java.net.URL
import
java.awt.RenderingHints
import
java.awt.image.BufferedImage
import
javax.imageio.ImageIO
class
ImageSenderMain
:
PluginBase
()
{
...
...
@@ -30,42 +40,60 @@ class ImageSenderMain : PluginBase() {
lateinit
var
normal
:
List
<
ConfigSection
>
lateinit
var
r18
:
List
<
ConfigSection
>
@ExperimentalCoroutinesApi
@MiraiExperimentalAPI
val
config
by
lazy
{
loadConfig
(
"setting.yml"
)
}
val
Normal_Image_Trigger
by
config
.
withDefaultWriteSave
{
"色图"
}
val
R18_Image_Trigger
by
config
.
withDefaultWriteSave
{
"不够色"
}
val
Image_Resize_Max_Width_Height
by
config
.
withDefaultWriteSave
{
800
}
val
groupsAllowNormal
by
lazy
{
config
.
getLongList
(
"Allow_Normal_Image_Groups"
).
toMutableList
()
}
val
groupsAllowR18
by
lazy
{
config
.
getLongList
(
"Allow_R18_Image_Groups"
).
toMutableList
()
}
override
fun
onDisable
()
{
config
[
"Allow_R18_Image_Groups"
]
=
groupsAllowR18
config
[
"Allow_Normal_Image_Groups"
]
=
groupsAllowNormal
config
.
save
()
}
override
fun
onEnable
()
{
logger
.
info
(
"Image Sender plugin enabled"
)
GlobalScope
.
subscribeAlways
<
BotOnlineEvent
>
{
registerCommands
()
subscribeAlways
<
MemberPermissionChangeEvent
>
{
logger
.
info
(
"${this.bot.uin} login succeed, it will be controlled by Image Sender Plugin"
)
this
.
bot
.
subscribeMessages
{
(
contains
(
"色图"
))
{
try
{
with
(
normal
.
random
())
{
getImage
(
subject
,
this
.
getString
(
"url"
),
this
.
getString
(
"pid"
)
).
plus
(
this
.
getString
(
"tags"
)).
send
()
}
}
catch
(
e
:
Exception
)
{
reply
(
e
.
message
?:
"unknown error"
)
}
this
.
bot
.
subscribeGroupMessages
{
(
contains
(
Normal_Image_Trigger
))
{
sendImage
(
subject
,
normal
.
random
())
}
(
contains
(
"不够色"
))
{
try
{
with
(
r18
.
random
())
{
getImage
(
subject
,
this
.
getString
(
"url"
),
this
.
getString
(
"pid"
)
).
plus
(
this
.
getString
(
"tags"
)).
send
()
}
}
catch
(
e
:
Exception
)
{
reply
(
e
.
message
?:
"unknown error"
)
}
(
contains
(
R18_Image_Trigger
))
{
sendImage
(
subject
,
r18
.
random
())
}
}
}
}
suspend
fun
getImage
(
contact
:
Contact
,
url
:
String
,
pid
:
String
):
Image
{
return
withTimeoutOrNull
(
20
*
1000
)
{
private
fun
sendImage
(
contact
:
Contact
,
configSection
:
ConfigSection
)
{
launch
{
try
{
logger
.
info
(
"正在推送图片"
)
getImage
(
contact
,
configSection
.
getString
(
"url"
),
configSection
.
getString
(
"pid"
),
800
).
plus
(
configSection
.
getString
(
"tags"
)).
sendTo
(
contact
)
}
catch
(
e
:
Exception
)
{
contact
.
sendMessage
(
e
.
message
?:
"unknown error"
)
}
}
}
private
suspend
fun
getImage
(
contact
:
Contact
,
url
:
String
,
pid
:
String
,
maxWidthOrHeight
:
Int
):
Image
{
val
bodyStream
=
withTimeoutOrNull
(
20
*
1000
)
{
withContext
(
Dispatchers
.
IO
)
{
Jsoup
.
connect
(
url
)
...
...
@@ -78,9 +106,30 @@ class ImageSenderMain : PluginBase() {
.
maxBodySize
(
100000000
)
.
execute
().
also
{
check
(
it
.
statusCode
()
==
200
)
{
"Failed to download image"
}
}
}
}
?.
bodyStream
()
?.
uploadAsImage
(
contact
)
?:
error
(
"Unable to download image"
)
}
?.
bodyStream
()
?:
error
(
"Failed to download image"
)
if
(
maxWidthOrHeight
<
1
)
{
return
bodyStream
.
uploadAsImage
(
contact
)
}
val
image
=
withContext
(
Dispatchers
.
IO
)
{
ImageIO
.
read
(
bodyStream
)
}
if
(
image
.
width
.
coerceAtLeast
(
image
.
height
)
<=
maxWidthOrHeight
)
{
return
image
.
upload
(
contact
)
}
val
rate
=
(
maxWidthOrHeight
.
toFloat
()
/
image
.
width
.
coerceAtLeast
(
image
.
height
))
val
newWidth
=
(
image
.
width
*
rate
).
toInt
()
val
newHeight
=
(
image
.
height
*
rate
).
toInt
()
return
withContext
(
Dispatchers
.
IO
)
{
val
dimg
=
BufferedImage
(
newWidth
,
newHeight
,
image
.
type
)
val
g
=
dimg
.
createGraphics
()
g
.
setRenderingHint
(
RenderingHints
.
KEY_INTERPOLATION
,
RenderingHints
.
VALUE_INTERPOLATION_BILINEAR
)
g
.
drawImage
(
image
,
0
,
0
,
newWidth
,
newHeight
,
0
,
0
,
image
.
width
,
image
.
height
,
null
)
g
.
dispose
()
dimg
}.
upload
(
contact
)
}
override
fun
onLoad
()
{
logger
.
info
(
"loading local image data"
)
...
...
@@ -97,8 +146,10 @@ class ImageSenderMain : PluginBase() {
logger
.
info
(
"R18 * "
+
r18
.
size
)
}
fun
registerCommands
()
{
registerCommand
{
override
fun
onDisable
()
{
}
}
}
\ No newline at end of file
mirai-plugins/image-sender/src/main/
java/net/mamoe/mirai/imageplugin
/Test.kt
→
mirai-plugins/image-sender/src/main/
resources
/Test.kt
View file @
5402505a
File moved
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