From 6706adf586ac669ec823b34e16812c5448a2a85c Mon Sep 17 00:00:00 2001 From: Yiwen-Chan Date: Sun, 18 Apr 2021 23:05:18 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E5=A2=9E=E5=8A=A0=E5=9C=A8?= =?UTF-8?q?=E7=BA=BF=E8=BF=90=E8=A1=8C=E4=BB=A3=E7=A0=81=E7=9A=84=E9=99=90?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- runcode/code_runner.go | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/runcode/code_runner.go b/runcode/code_runner.go index 94976064..9bbd1759 100644 --- a/runcode/code_runner.go +++ b/runcode/code_runner.go @@ -10,9 +10,12 @@ import ( "github.com/tidwall/gjson" zero "github.com/wdvxdr1123/ZeroBot" + "github.com/wdvxdr1123/ZeroBot/extension/rate" "github.com/wdvxdr1123/ZeroBot/message" ) +var limit = rate.NewManager(time.Minute*3, 5) + func init() { RunAllow := true @@ -123,6 +126,10 @@ func init() { zero.OnRegex(`^>runcode\s(.+?)\s([\s\S]+)$`).SetBlock(true).SecondPriority(). Handle(func(ctx *zero.Ctx) { + if !limit.Load(ctx.Event.UserID).Acquire() { + ctx.Send("请稍后重试0x0...") + return + } language := ctx.State["regex_matched"].([]string)[1] language = strings.ToLower(language) if runType, exist := table[language]; !exist { @@ -217,5 +224,33 @@ func runCode(code string, runType [2]string) (string, error) { return "", fmt.Errorf(e) } output := content.Get("output").Str - return output[:len(output)-1], nil + for strings.HasSuffix(output, "\n") { + output = output[:len(output)-1] + } + temp := []rune(output) + isCut := false + if strings.Count(output, "\n") > 30 { + count := 0 + for i, r := range temp { + if r == 10 { + count++ + fmt.Println(i) + } + if count > 30 { + temp = temp[:i] + break + } + } + isCut = true + } + if len(temp) > 1000 { + // 超长截断 + temp = temp[:1000] + isCut = true + } + if isCut { + output = string(temp) + output += "\n............\n............" + } + return output, nil }