diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/lookup/WeatherCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/lookup/WeatherCommand.kt index 2461aef..e47aca6 100644 --- a/src/main/kotlin/cn/rtast/fancybot/commands/lookup/WeatherCommand.kt +++ b/src/main/kotlin/cn/rtast/fancybot/commands/lookup/WeatherCommand.kt @@ -11,14 +11,47 @@ import cn.rtast.fancybot.configManager import cn.rtast.fancybot.entity.weather.Geo import cn.rtast.fancybot.entity.weather.Weather import cn.rtast.fancybot.util.Http +import cn.rtast.fancybot.util.Resources +import cn.rtast.fancybot.util.drawCenteredText +import cn.rtast.fancybot.util.drawCustomImage +import cn.rtast.fancybot.util.str.encodeToBase64 +import cn.rtast.fancybot.util.toBufferedImage +import cn.rtast.fancybot.util.toByteArray import cn.rtast.rob.entity.GroupMessage import cn.rtast.rob.util.BaseCommand import cn.rtast.rob.util.ob.MessageChain import cn.rtast.rob.util.ob.OBMessage +import java.awt.Color +import java.awt.Font +import java.awt.image.BufferedImage class WeatherCommand : BaseCommand() { override val commandNames = listOf("/weather", "/天气") + private val canvasWidth = 800 + private val canvasHeight = 600 + private val font = Font("Serif", Font.ITALIC, 60).deriveFont(Font.BOLD) + private val locationFont = Font("Serif", Font.ITALIC, 50) + + private fun createWeatherCard(weather: Weather, geoResult: Geo.Location): String { + val emptyImage = BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_RGB) + val g2d = emptyImage.createGraphics() + val weatherIcon = Resources.loadFromResourcesAsBytes("qweather/${weather.now.icon}.png") + ?: Resources.loadFromResourcesAsBytes("qweather/100.png")!! + g2d.color = Color(209, 238, 238) + g2d.fillRect(0, 0, canvasWidth, canvasHeight) + g2d.drawCustomImage(weatherIcon.toBufferedImage(), canvasWidth / 2 - 230, canvasHeight / 2 - 80, 230.0, 230.0) + g2d.color = Color.BLACK + g2d.font = font + g2d.drawString("${weather.now.temp}℃", canvasWidth / 2, canvasHeight / 2 + 20) + g2d.drawString(weather.now.text, canvasWidth / 2, canvasHeight / 2 + 100) + g2d.font = locationFont + g2d.drawCenteredText("${geoResult.adm1}-${geoResult.adm2}-${geoResult.name}", canvasWidth / 2, 90) + g2d.color = Color(155, 48, 255) + g2d.drawCenteredText("数据来源: ${weather.refer.sources.joinToString(",")}", canvasWidth / 2, canvasHeight - 80) + return emptyImage.toByteArray().encodeToBase64() + } + override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List) { if (args.isEmpty()) { val msg = MessageChain.Builder() @@ -39,16 +72,8 @@ class WeatherCommand : BaseCommand() { mapOf("location" to lookupLocation.id, "key" to configManager.qweatherKey) ) val msg = MessageChain.Builder() - .addAt(message.sender.userId) - .addNewLine() - .addText("地区: ${lookupLocation.country}") - .addText("-${lookupLocation.adm1}") - .addText("-${lookupLocation.adm2}") - .addText("-${lookupLocation.name}") - .addNewLine() - .addText("温度: ${weather.now.temp} ℃ | ${weather.now.text}/${weather.now.windDir}") - .addNewLine() - .addText("数据来源: ${weather.refer.sources.joinToString(",")}") + .addReply(message.messageId) + .addImage(this.createWeatherCard(weather, lookupLocation), true) .build() listener.sendGroupMessage(message.groupId, msg) } catch (_: NullPointerException) { diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/parse/BVParseCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/parse/BVParseCommand.kt index bc54ab8..d0e1bed 100644 --- a/src/main/kotlin/cn/rtast/fancybot/commands/parse/BVParseCommand.kt +++ b/src/main/kotlin/cn/rtast/fancybot/commands/parse/BVParseCommand.kt @@ -34,8 +34,8 @@ object BVParseCommand { private const val USER_STAT_URL = "https://api.bilibili.com/x/relation/stat" private const val CANVAS_WIDTH = 1000 private const val CANVAS_HEIGHT = 600 - private val titleFont = Font("Serif", Font.ITALIC, 40).deriveFont(Font.ITALIC) - private val numberFont = Font("Serif", Font.ITALIC, 25).deriveFont(Font.ITALIC) + private val titleFont = Font("Serif", Font.ITALIC, 40) + private val numberFont = Font("Serif", Font.ITALIC, 25) private val twoTwoLogo = ImageIO.read(Resources.loadFromResources("bili/22-coin.png")) private val likeIcon = ImageIO.read(Resources.loadFromResources("bili/like.png")) private val coinIcon = ImageIO.read(Resources.loadFromResources("bili/coin.png")) diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/parse/GitHubParseCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/parse/GitHubParseCommand.kt index 478d162..5bf95a5 100644 --- a/src/main/kotlin/cn/rtast/fancybot/commands/parse/GitHubParseCommand.kt +++ b/src/main/kotlin/cn/rtast/fancybot/commands/parse/GitHubParseCommand.kt @@ -34,9 +34,9 @@ object GitHubParseCommand { private val starIcon = ImageIO.read(Resources.loadFromResources("github/star.png")) private val issueIcon = ImageIO.read(Resources.loadFromResources("github/issue.png")) private val forkIcon = ImageIO.read(Resources.loadFromResources("github/fork.png")) - private val customFont = Font("Serif", Font.ITALIC, 50).deriveFont(Font.ITALIC).deriveFont(Font.BOLD) - private val titleCustomFont = Font("Serif", Font.ITALIC, 75).deriveFont(Font.ITALIC).deriveFont(Font.BOLD) - private val descriptionCustomFont = Font("Serif", Font.ITALIC, 40).deriveFont(Font.ITALIC).deriveFont(Font.BOLD) + private val customFont = Font("Serif", Font.ITALIC, 50).deriveFont(Font.BOLD) + private val titleCustomFont = Font("Serif", Font.ITALIC, 75).deriveFont(Font.BOLD) + private val descriptionCustomFont = Font("Serif", Font.ITALIC, 40).deriveFont(Font.BOLD) private val languageColors = mapOf( "Kotlin" to Color(169, 123, 255), diff --git a/src/main/kotlin/cn/rtast/fancybot/entity/weather/Weather.kt b/src/main/kotlin/cn/rtast/fancybot/entity/weather/Weather.kt index 223b7bf..a443610 100644 --- a/src/main/kotlin/cn/rtast/fancybot/entity/weather/Weather.kt +++ b/src/main/kotlin/cn/rtast/fancybot/entity/weather/Weather.kt @@ -14,7 +14,8 @@ data class Weather( data class Now( val temp: String, val text: String, - val windDir: String + val windDir: String, + val icon: String, ) data class Refer( diff --git a/src/main/kotlin/cn/rtast/fancybot/util/Image.kt b/src/main/kotlin/cn/rtast/fancybot/util/Image.kt index c4404ac..fce5ed3 100644 --- a/src/main/kotlin/cn/rtast/fancybot/util/Image.kt +++ b/src/main/kotlin/cn/rtast/fancybot/util/Image.kt @@ -20,7 +20,7 @@ fun Graphics2D.drawCustomImage( y: Int, maxWidth: Double, maxHeight: Double, - clip: Boolean + clip: Boolean = false ) { val originalWidth = image.getWidth(null) val originalHeight = image.getHeight(null) @@ -61,4 +61,13 @@ fun BufferedImage.toByteArray(): ByteArray { ImageIO.write(this, "png", outputStream) return outputStream.toByteArray() } +} + +fun Graphics2D.drawCenteredText(text: String, x: Int, y: Int) { + val metrics = this.fontMetrics + val textWidth = metrics.stringWidth(text) + val textHeight = metrics.height + val drawX = x - textWidth / 2 + val drawY = y + textHeight / 2 + this.drawString(text, drawX, drawY) } \ No newline at end of file diff --git a/src/main/kotlin/cn/rtast/fancybot/util/Resources.kt b/src/main/kotlin/cn/rtast/fancybot/util/Resources.kt index a4c2f97..1642f41 100644 --- a/src/main/kotlin/cn/rtast/fancybot/util/Resources.kt +++ b/src/main/kotlin/cn/rtast/fancybot/util/Resources.kt @@ -13,4 +13,9 @@ object Resources { fun loadFromResources(filename: String): InputStream { return this::class.java.classLoader.getResourceAsStream(filename) } + + fun loadFromResourcesAsBytes(filename: String): ByteArray? { + val inputStream = this::class.java.classLoader.getResourceAsStream(filename) + return inputStream?.use { it.readBytes() } + } } diff --git a/src/main/resources/qweather/100.png b/src/main/resources/qweather/100.png new file mode 100644 index 0000000..ab36393 Binary files /dev/null and b/src/main/resources/qweather/100.png differ diff --git a/src/main/resources/qweather/101.png b/src/main/resources/qweather/101.png new file mode 100644 index 0000000..4b98813 Binary files /dev/null and b/src/main/resources/qweather/101.png differ diff --git a/src/main/resources/qweather/102.png b/src/main/resources/qweather/102.png new file mode 100644 index 0000000..f7be2ca Binary files /dev/null and b/src/main/resources/qweather/102.png differ diff --git a/src/main/resources/qweather/103.png b/src/main/resources/qweather/103.png new file mode 100644 index 0000000..350e5b2 Binary files /dev/null and b/src/main/resources/qweather/103.png differ diff --git a/src/main/resources/qweather/104.png b/src/main/resources/qweather/104.png new file mode 100644 index 0000000..cf75c85 Binary files /dev/null and b/src/main/resources/qweather/104.png differ diff --git a/src/main/resources/qweather/150.png b/src/main/resources/qweather/150.png new file mode 100644 index 0000000..6e62325 Binary files /dev/null and b/src/main/resources/qweather/150.png differ diff --git a/src/main/resources/qweather/151.png b/src/main/resources/qweather/151.png new file mode 100644 index 0000000..9809be5 Binary files /dev/null and b/src/main/resources/qweather/151.png differ diff --git a/src/main/resources/qweather/152.png b/src/main/resources/qweather/152.png new file mode 100644 index 0000000..153d0d0 Binary files /dev/null and b/src/main/resources/qweather/152.png differ diff --git a/src/main/resources/qweather/153.png b/src/main/resources/qweather/153.png new file mode 100644 index 0000000..fb14470 Binary files /dev/null and b/src/main/resources/qweather/153.png differ diff --git a/src/main/resources/qweather/154.png b/src/main/resources/qweather/154.png new file mode 100644 index 0000000..a397b04 Binary files /dev/null and b/src/main/resources/qweather/154.png differ diff --git a/src/main/resources/qweather/300.png b/src/main/resources/qweather/300.png new file mode 100644 index 0000000..39d7ae6 Binary files /dev/null and b/src/main/resources/qweather/300.png differ diff --git a/src/main/resources/qweather/301.png b/src/main/resources/qweather/301.png new file mode 100644 index 0000000..0ed630a Binary files /dev/null and b/src/main/resources/qweather/301.png differ diff --git a/src/main/resources/qweather/302.png b/src/main/resources/qweather/302.png new file mode 100644 index 0000000..a565077 Binary files /dev/null and b/src/main/resources/qweather/302.png differ diff --git a/src/main/resources/qweather/303.png b/src/main/resources/qweather/303.png new file mode 100644 index 0000000..1411809 Binary files /dev/null and b/src/main/resources/qweather/303.png differ diff --git a/src/main/resources/qweather/304.png b/src/main/resources/qweather/304.png new file mode 100644 index 0000000..e3e0d83 Binary files /dev/null and b/src/main/resources/qweather/304.png differ diff --git a/src/main/resources/qweather/305.png b/src/main/resources/qweather/305.png new file mode 100644 index 0000000..85ae414 Binary files /dev/null and b/src/main/resources/qweather/305.png differ diff --git a/src/main/resources/qweather/306.png b/src/main/resources/qweather/306.png new file mode 100644 index 0000000..54e224c Binary files /dev/null and b/src/main/resources/qweather/306.png differ diff --git a/src/main/resources/qweather/307.png b/src/main/resources/qweather/307.png new file mode 100644 index 0000000..f9baa36 Binary files /dev/null and b/src/main/resources/qweather/307.png differ diff --git a/src/main/resources/qweather/308.png b/src/main/resources/qweather/308.png new file mode 100644 index 0000000..da3b767 Binary files /dev/null and b/src/main/resources/qweather/308.png differ diff --git a/src/main/resources/qweather/309.png b/src/main/resources/qweather/309.png new file mode 100644 index 0000000..5bdf33f Binary files /dev/null and b/src/main/resources/qweather/309.png differ diff --git a/src/main/resources/qweather/310.png b/src/main/resources/qweather/310.png new file mode 100644 index 0000000..5ec2b29 Binary files /dev/null and b/src/main/resources/qweather/310.png differ diff --git a/src/main/resources/qweather/311.png b/src/main/resources/qweather/311.png new file mode 100644 index 0000000..ba877ce Binary files /dev/null and b/src/main/resources/qweather/311.png differ diff --git a/src/main/resources/qweather/312.png b/src/main/resources/qweather/312.png new file mode 100644 index 0000000..817a868 Binary files /dev/null and b/src/main/resources/qweather/312.png differ diff --git a/src/main/resources/qweather/313.png b/src/main/resources/qweather/313.png new file mode 100644 index 0000000..dc0e98a Binary files /dev/null and b/src/main/resources/qweather/313.png differ diff --git a/src/main/resources/qweather/314.png b/src/main/resources/qweather/314.png new file mode 100644 index 0000000..b83983a Binary files /dev/null and b/src/main/resources/qweather/314.png differ diff --git a/src/main/resources/qweather/315.png b/src/main/resources/qweather/315.png new file mode 100644 index 0000000..27d1f10 Binary files /dev/null and b/src/main/resources/qweather/315.png differ diff --git a/src/main/resources/qweather/316.png b/src/main/resources/qweather/316.png new file mode 100644 index 0000000..b3a7378 Binary files /dev/null and b/src/main/resources/qweather/316.png differ diff --git a/src/main/resources/qweather/317.png b/src/main/resources/qweather/317.png new file mode 100644 index 0000000..4d2b987 Binary files /dev/null and b/src/main/resources/qweather/317.png differ diff --git a/src/main/resources/qweather/318.png b/src/main/resources/qweather/318.png new file mode 100644 index 0000000..a882b22 Binary files /dev/null and b/src/main/resources/qweather/318.png differ diff --git a/src/main/resources/qweather/350.png b/src/main/resources/qweather/350.png new file mode 100644 index 0000000..28a589b Binary files /dev/null and b/src/main/resources/qweather/350.png differ diff --git a/src/main/resources/qweather/351.png b/src/main/resources/qweather/351.png new file mode 100644 index 0000000..854517f Binary files /dev/null and b/src/main/resources/qweather/351.png differ diff --git a/src/main/resources/qweather/399.png b/src/main/resources/qweather/399.png new file mode 100644 index 0000000..9ac9fb3 Binary files /dev/null and b/src/main/resources/qweather/399.png differ diff --git a/src/main/resources/qweather/400.png b/src/main/resources/qweather/400.png new file mode 100644 index 0000000..6b0e83a Binary files /dev/null and b/src/main/resources/qweather/400.png differ diff --git a/src/main/resources/qweather/401.png b/src/main/resources/qweather/401.png new file mode 100644 index 0000000..ea69d76 Binary files /dev/null and b/src/main/resources/qweather/401.png differ diff --git a/src/main/resources/qweather/402.png b/src/main/resources/qweather/402.png new file mode 100644 index 0000000..4e9b4c7 Binary files /dev/null and b/src/main/resources/qweather/402.png differ diff --git a/src/main/resources/qweather/403.png b/src/main/resources/qweather/403.png new file mode 100644 index 0000000..462b323 Binary files /dev/null and b/src/main/resources/qweather/403.png differ diff --git a/src/main/resources/qweather/404.png b/src/main/resources/qweather/404.png new file mode 100644 index 0000000..b9c3aae Binary files /dev/null and b/src/main/resources/qweather/404.png differ diff --git a/src/main/resources/qweather/405.png b/src/main/resources/qweather/405.png new file mode 100644 index 0000000..df605de Binary files /dev/null and b/src/main/resources/qweather/405.png differ diff --git a/src/main/resources/qweather/406.png b/src/main/resources/qweather/406.png new file mode 100644 index 0000000..f72076a Binary files /dev/null and b/src/main/resources/qweather/406.png differ diff --git a/src/main/resources/qweather/407.png b/src/main/resources/qweather/407.png new file mode 100644 index 0000000..a00a70d Binary files /dev/null and b/src/main/resources/qweather/407.png differ diff --git a/src/main/resources/qweather/408.png b/src/main/resources/qweather/408.png new file mode 100644 index 0000000..62dbea2 Binary files /dev/null and b/src/main/resources/qweather/408.png differ diff --git a/src/main/resources/qweather/409.png b/src/main/resources/qweather/409.png new file mode 100644 index 0000000..010bde4 Binary files /dev/null and b/src/main/resources/qweather/409.png differ diff --git a/src/main/resources/qweather/410.png b/src/main/resources/qweather/410.png new file mode 100644 index 0000000..99c9bb1 Binary files /dev/null and b/src/main/resources/qweather/410.png differ diff --git a/src/main/resources/qweather/456.png b/src/main/resources/qweather/456.png new file mode 100644 index 0000000..1579fd6 Binary files /dev/null and b/src/main/resources/qweather/456.png differ diff --git a/src/main/resources/qweather/457.png b/src/main/resources/qweather/457.png new file mode 100644 index 0000000..fb957b4 Binary files /dev/null and b/src/main/resources/qweather/457.png differ diff --git a/src/main/resources/qweather/499.png b/src/main/resources/qweather/499.png new file mode 100644 index 0000000..a945149 Binary files /dev/null and b/src/main/resources/qweather/499.png differ diff --git a/src/main/resources/qweather/500.png b/src/main/resources/qweather/500.png new file mode 100644 index 0000000..b276eec Binary files /dev/null and b/src/main/resources/qweather/500.png differ diff --git a/src/main/resources/qweather/501.png b/src/main/resources/qweather/501.png new file mode 100644 index 0000000..b276eec Binary files /dev/null and b/src/main/resources/qweather/501.png differ diff --git a/src/main/resources/qweather/502.png b/src/main/resources/qweather/502.png new file mode 100644 index 0000000..a7c9d42 Binary files /dev/null and b/src/main/resources/qweather/502.png differ diff --git a/src/main/resources/qweather/503.png b/src/main/resources/qweather/503.png new file mode 100644 index 0000000..7d629ee Binary files /dev/null and b/src/main/resources/qweather/503.png differ diff --git a/src/main/resources/qweather/504.png b/src/main/resources/qweather/504.png new file mode 100644 index 0000000..e0494cb Binary files /dev/null and b/src/main/resources/qweather/504.png differ diff --git a/src/main/resources/qweather/507.png b/src/main/resources/qweather/507.png new file mode 100644 index 0000000..110581e Binary files /dev/null and b/src/main/resources/qweather/507.png differ diff --git a/src/main/resources/qweather/508.png b/src/main/resources/qweather/508.png new file mode 100644 index 0000000..6c33ef3 Binary files /dev/null and b/src/main/resources/qweather/508.png differ diff --git a/src/main/resources/qweather/509.png b/src/main/resources/qweather/509.png new file mode 100644 index 0000000..662948b Binary files /dev/null and b/src/main/resources/qweather/509.png differ diff --git a/src/main/resources/qweather/510.png b/src/main/resources/qweather/510.png new file mode 100644 index 0000000..76eed64 Binary files /dev/null and b/src/main/resources/qweather/510.png differ diff --git a/src/main/resources/qweather/511.png b/src/main/resources/qweather/511.png new file mode 100644 index 0000000..90f9cf0 Binary files /dev/null and b/src/main/resources/qweather/511.png differ diff --git a/src/main/resources/qweather/512.png b/src/main/resources/qweather/512.png new file mode 100644 index 0000000..3bee6eb Binary files /dev/null and b/src/main/resources/qweather/512.png differ diff --git a/src/main/resources/qweather/513.png b/src/main/resources/qweather/513.png new file mode 100644 index 0000000..41d6856 Binary files /dev/null and b/src/main/resources/qweather/513.png differ diff --git a/src/main/resources/qweather/514.png b/src/main/resources/qweather/514.png new file mode 100644 index 0000000..0564840 Binary files /dev/null and b/src/main/resources/qweather/514.png differ diff --git a/src/main/resources/qweather/515.png b/src/main/resources/qweather/515.png new file mode 100644 index 0000000..0564840 Binary files /dev/null and b/src/main/resources/qweather/515.png differ diff --git a/src/main/resources/qweather/800.png b/src/main/resources/qweather/800.png new file mode 100644 index 0000000..6ca7970 Binary files /dev/null and b/src/main/resources/qweather/800.png differ diff --git a/src/main/resources/qweather/801.png b/src/main/resources/qweather/801.png new file mode 100644 index 0000000..33c3032 Binary files /dev/null and b/src/main/resources/qweather/801.png differ diff --git a/src/main/resources/qweather/802.png b/src/main/resources/qweather/802.png new file mode 100644 index 0000000..f0537c6 Binary files /dev/null and b/src/main/resources/qweather/802.png differ diff --git a/src/main/resources/qweather/803.png b/src/main/resources/qweather/803.png new file mode 100644 index 0000000..71ea5bd Binary files /dev/null and b/src/main/resources/qweather/803.png differ diff --git a/src/main/resources/qweather/804.png b/src/main/resources/qweather/804.png new file mode 100644 index 0000000..1d8af3d Binary files /dev/null and b/src/main/resources/qweather/804.png differ diff --git a/src/main/resources/qweather/805.png b/src/main/resources/qweather/805.png new file mode 100644 index 0000000..e3f826d Binary files /dev/null and b/src/main/resources/qweather/805.png differ diff --git a/src/main/resources/qweather/806.png b/src/main/resources/qweather/806.png new file mode 100644 index 0000000..30ebf10 Binary files /dev/null and b/src/main/resources/qweather/806.png differ diff --git a/src/main/resources/qweather/807.png b/src/main/resources/qweather/807.png new file mode 100644 index 0000000..04cc537 Binary files /dev/null and b/src/main/resources/qweather/807.png differ diff --git a/src/main/resources/qweather/900.png b/src/main/resources/qweather/900.png new file mode 100644 index 0000000..ce35527 Binary files /dev/null and b/src/main/resources/qweather/900.png differ diff --git a/src/main/resources/qweather/901.png b/src/main/resources/qweather/901.png new file mode 100644 index 0000000..4a0941c Binary files /dev/null and b/src/main/resources/qweather/901.png differ diff --git a/src/main/resources/qweather/999.png b/src/main/resources/qweather/999.png new file mode 100644 index 0000000..5965406 Binary files /dev/null and b/src/main/resources/qweather/999.png differ