From d877639b925247b7f7c72cec5a6301edc63c2247 Mon Sep 17 00:00:00 2001 From: Antares Date: Thu, 12 Mar 2020 17:26:16 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E9=87=8D=E5=86=99=20qrcode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins_new/make_qrcode/plugin.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/plugins_new/make_qrcode/plugin.py b/plugins_new/make_qrcode/plugin.py index 819d65d..7df5bbd 100644 --- a/plugins_new/make_qrcode/plugin.py +++ b/plugins_new/make_qrcode/plugin.py @@ -5,11 +5,9 @@ from common.event import MessageEvent, GroupMessageEvent from common.command import ChatType from common.countdown_bot import CountdownBot from typing import List, Dict - +from io import BytesIO import qrcode import base64 -import tempfile -import os class QRcodeConfig(ConfigBase): @@ -17,26 +15,26 @@ class QRcodeConfig(ConfigBase): class QRcodePlugin(Plugin): - def make_qrcode(self, text: str) -> str: - tergent = os.path.join(tempfile.mkdtemp(), "qrcode.png") - image = qrcode.make(text) - image.save(tergent) - with open(tergent, "rb") as file: - result = base64.encodebytes( - file.read()).decode().replace("\n", "") - return result - def command_qrcode(self, plugin, args: List[str], raw_string: str, context: dict, evt: MessageEvent): def wrapper(): - text = evt.raw_message - text = text.lstrip("--qrcode") - text = text.lstrip("--二维码") + text = raw_string + text = text.lstrip("qrcode") + text = text.lstrip("二维码") text = text.strip() + if len(text) > self.config.MAX_STRING_LENGTH: self.bot.client_async.send(context, "字符串超过限制") return + + buf = BytesIO() + image = qrcode.make(text) + image.save(buf) + result = base64.encodebytes( + buf.getvalue()).decode().replace("\n", "") + self.bot.client_async.send( - context, f"[CQ:image,file=base64://{self.make_qrcode(text)}]") + context, f"[CQ:image,file=base64://{result}]") + self.bot.submit_multithread_task(wrapper) def on_enable(self): -- Gitee From b8ef07bb2f89539197b26e73115f198efb8a2b0c Mon Sep 17 00:00:00 2001 From: Antares Date: Thu, 12 Mar 2020 17:36:06 +0800 Subject: [PATCH 2/2] qwq --- plugins_new/make_qrcode/plugin.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugins_new/make_qrcode/plugin.py b/plugins_new/make_qrcode/plugin.py index 7df5bbd..8d85de5 100644 --- a/plugins_new/make_qrcode/plugin.py +++ b/plugins_new/make_qrcode/plugin.py @@ -17,9 +17,10 @@ class QRcodeConfig(ConfigBase): class QRcodePlugin(Plugin): def command_qrcode(self, plugin, args: List[str], raw_string: str, context: dict, evt: MessageEvent): def wrapper(): - text = raw_string - text = text.lstrip("qrcode") - text = text.lstrip("二维码") + if raw_string.startswith("qrcode"): + text = raw_string.lstrip("qrcode") + else: + text = raw_string.lstrip("二维码") text = text.strip() if len(text) > self.config.MAX_STRING_LENGTH: -- Gitee