chore: improve use experience

This commit is contained in:
2024-09-20 14:05:20 +08:00
parent ca9065ef65
commit 849b164749
2 files changed
+75 -60

No files matched your search

@@ -65,6 +65,8 @@ class FancyBot : OneBotListener {
val messageId = message.messageId val messageId = message.messageId
println("$sender($senderId: $groupId >>> $messageId): $msg") println("$sender($senderId: $groupId >>> $messageId): $msg")
AsciiArtCommand.callback(message)
if (message.rawMessage.startsWith("https://github.com/") || message.rawMessage.startsWith("git@github.com:")) { if (message.rawMessage.startsWith("https://github.com/") || message.rawMessage.startsWith("git@github.com:")) {
GitHubParseCommand.parse(this, message) GitHubParseCommand.parse(this, message)
} }
@@ -22,10 +22,12 @@ import javax.imageio.ImageIO
import kotlin.math.roundToInt import kotlin.math.roundToInt
class AsciiArtCommand : BaseCommand() { class AsciiArtCommand : BaseCommand() {
override val commandNames = listOf("/ascii") override val commandNames = listOf("/ascii", "/asc")
private val fontSize = 12 companion object {
private val font = Font("Monospaced", Font.PLAIN, fontSize) private const val FONT_SIZE = 12
private val font = Font("Monospaced", Font.PLAIN, FONT_SIZE)
val waitingList = mutableListOf<Long>()
private fun BufferedImage.convertToAscii(): String { private fun BufferedImage.convertToAscii(): String {
val width = this.width val width = this.width
@@ -60,9 +62,10 @@ class AsciiArtCommand : BaseCommand() {
return asciiArt.toString() return asciiArt.toString()
} }
private fun String.saveAsciiArtToImage(width: Int, height: Int): String { private fun String.saveAsciiArtToImage(width: Int, height: Int): String {
val imgWidth = width * fontSize / 2 val imgWidth = width * FONT_SIZE / 2
val imgHeight = height * fontSize / 2 val imgHeight = height * FONT_SIZE / 2
val img = BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB) val img = BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB)
val g2d = img.createGraphics() val g2d = img.createGraphics()
g2d.color = Color.BLACK g2d.color = Color.BLACK
@@ -71,30 +74,40 @@ class AsciiArtCommand : BaseCommand() {
g2d.font = font g2d.font = font
val lines = this.split("\n") val lines = this.split("\n")
for ((index, line) in lines.withIndex()) { for ((index, line) in lines.withIndex()) {
g2d.drawString(line, 0, (index + 1) * fontSize) g2d.drawString(line, 0, (index + 1) * FONT_SIZE)
} }
g2d.dispose() g2d.dispose()
return img.toByteArray().encodeToBase64() return img.toByteArray().encodeToBase64()
} }
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) { suspend fun callback(message: GroupMessage) {
if (args.isEmpty()) { if (message.sender.userId in waitingList) {
val msg = MessageChain.Builder().addText("发送`/ascii <图片>`可以将图片转成ascii字符").build() if (message.message.any { it.type == ArrayMessageType.image }) {
val url = message.message.find { it.type == ArrayMessageType.image }!!.data.file!!
val bufferedImage = ImageIO.read(URI(url).toURL())
val imageBase64 =
bufferedImage.convertToAscii().saveAsciiArtToImage(bufferedImage.width, bufferedImage.height)
val msg = MessageChain.Builder().addImage(imageBase64, true).build()
message.reply(msg) message.reply(msg)
return } else {
message.reply("回复错误已取消本次操作")
} }
val image = message.message.find { it.type == ArrayMessageType.image } waitingList.removeIf { it == message.sender.userId }
if (image == null) { }
val msg = MessageChain.Builder().addText("输入一个图片来继续操作").build() }
}
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
if (message.sender.userId !in waitingList) {
val msg = MessageChain.Builder()
.addText("请继续发送一张图片,如果输入错误则取消本次操作")
.build()
message.reply(msg) message.reply(msg)
waitingList.add(message.sender.userId)
return return
} else { } else {
val url = image.data.file!! message.reply("先发送`/ascii`再继续操作吧~")
val bufferedImage = ImageIO.read(URI(url).toURL()) return
val ascii = bufferedImage.convertToAscii()
val image = ascii.saveAsciiArtToImage(bufferedImage.width, bufferedImage.height)
val msg = MessageChain.Builder().addImage(image, true).build()
message.reply(msg)
} }
} }
} }