Skip to content

Commit

Permalink
✨ 增加在线运行代码的限制
Browse files Browse the repository at this point in the history
  • Loading branch information
kanrichan committed Apr 18, 2021
1 parent b6ff886 commit f221696
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion runcode/code_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit f221696

Please sign in to comment.