Skip to content

Commit

Permalink
chore: 移除kook/discord平台的代码块包裹
Browse files Browse the repository at this point in the history
chore: 在Android平台下拉起gocq时主动设置其环境变量
chore: 为ExtInfo.AutoActive添加jsbind
  • Loading branch information
Szzrain committed Jun 2, 2023
1 parent 3f36c1a commit 7906a44
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 13 deletions.
4 changes: 2 additions & 2 deletions dice/dice.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ type ExtInfo struct {
Version string `yaml:"-" json:"version" jsbind:"version"` // 版本
// 作者
// 更新时间
AutoActive bool `yaml:"-" json:"-"` // 是否自动开启
CmdMap CmdMapCls `yaml:"-" json:"-" jsbind:"cmdMap"` // 指令集合
AutoActive bool `yaml:"-" json:"-" jsbind:"autoActive"` // 是否自动开启
CmdMap CmdMapCls `yaml:"-" json:"-" jsbind:"cmdMap"` // 指令集合
Brief string `yaml:"-" json:"-"`
ActiveOnPrivate bool `yaml:"-" json:"-"`

Expand Down
7 changes: 5 additions & 2 deletions dice/gocqhttp_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"regexp"
"runtime"
"runtime/debug"
"sealdice-core/utils/procs"
"strings"
Expand Down Expand Up @@ -238,7 +238,7 @@ func GenerateDeviceJsonAndroid(dice *Dice, protocol int) (string, []byte, error)
if _, err := os.Stat("./my_device.json"); err == nil {
dice.Logger.Info("检测到my_device.json,将使用该文件中的设备信息")
// file exists
data, err := ioutil.ReadFile("./my_device.json")
data, err := os.ReadFile("./my_device.json")
if err == nil {
deviceJson := deviceFile{}
err = json.Unmarshal(data, &deviceJson)
Expand Down Expand Up @@ -522,6 +522,9 @@ func GoCqHttpServe(dice *Dice, conn *EndPointInfo, password string, protocol int
p := procs.NewProcess(fmt.Sprintf(`"%s" faststart`, gocqhttpExePath))
p.Dir = workDir

if runtime.GOOS == "android" {
p.Env = os.Environ()
}
chQrCode := make(chan int, 1)
riskCount := 0
isSeldKilling := false
Expand Down
7 changes: 1 addition & 6 deletions dice/platform_adapter_discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (pa *PlatformAdapterDiscord) sendToChannelRaw(channelId string, text string
})
msgSend.Files = files
if msgSend.Content != "" {
msgSend.Content = "```\n" + msgSend.Content + "\n```"
msgSend.Content = antiMarkdownFormat(msgSend.Content)
}
_, err = pa.IntentSession.ChannelMessageSendComplex(id, msgSend)
msgSend = &discordgo.MessageSend{Content: ""}
Expand All @@ -219,14 +219,10 @@ func (pa *PlatformAdapterDiscord) sendToChannelRaw(channelId string, text string
Reader: f.Stream,
})
msgSend.Files = files
if msgSend.Content != "" {
msgSend.Content = "```\n" + msgSend.Content + "\n```"
}
_, err = pa.IntentSession.ChannelMessageSendComplex(id, msgSend)
msgSend = &discordgo.MessageSend{Content: ""}
case *TTSElement:
if msgSend.Content != "" || msgSend.Files != nil {
msgSend.Content = "```\n" + msgSend.Content + "\n```"
_, err = pa.IntentSession.ChannelMessageSendComplex(id, msgSend)
}
if err != nil {
Expand All @@ -253,7 +249,6 @@ func (pa *PlatformAdapterDiscord) sendToChannelRaw(channelId string, text string
}
}
if msgSend.Content != "" || msgSend.Files != nil {
msgSend.Content = "```\n" + msgSend.Content + "\n```"
_, err = pa.IntentSession.ChannelMessageSendComplex(id, msgSend)
}
if err != nil {
Expand Down
36 changes: 34 additions & 2 deletions dice/platform_adapter_kook.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (pa *PlatformAdapterKook) SendToChannelRaw(id string, text string, private
switch e := element.(type) {
case *TextElement:
//goldmark.DefaultParser().Parse(txt.NewReader([]byte(e.Content)))
msgb.Content += "```\n" + e.Content + "\n```"
msgb.Content += antiMarkdownFormat(e.Content)
case *ImageElement:
if msgb.Content != "``````" && msgb.Content != "" {
err = pa.MessageCreateRaw(msgb, id, private)
Expand Down Expand Up @@ -348,7 +348,7 @@ func (pa *PlatformAdapterKook) SendToChannelRaw(id string, text string, private
case *AtElement:
msgb.Content = msgb.Content + fmt.Sprintf("(met)%s(met)", e.Target)
case *TTSElement:
msgb.Content += "```\n" + e.Content + "\n```"
msgb.Content += antiMarkdownFormat(e.Content)
case *ReplyElement:
msgb.Quote = e.Target
}
Expand All @@ -361,6 +361,38 @@ func (pa *PlatformAdapterKook) SendToChannelRaw(id string, text string, private
}
}

func antiMarkdownFormat(text string) string {
text = strings.ReplaceAll(text, "\\", "\\\\")
text = strings.ReplaceAll(text, "_", "\\_")
text = strings.ReplaceAll(text, "~", "\\~")
//text = strings.ReplaceAll(text, "|", "\\|")
//text = strings.ReplaceAll(text, ">", "\\>")
//text = strings.ReplaceAll(text, "<", "\\<")
text = strings.ReplaceAll(text, "`", "\\`")
//text = strings.ReplaceAll(text, "#", "\\#")
//text = strings.ReplaceAll(text, "+", "\\+")
//text = strings.ReplaceAll(text, "-", "\\-")
//text = strings.ReplaceAll(text, "=", "\\=")
//text = strings.ReplaceAll(text, "{", "\\{")
//text = strings.ReplaceAll(text, "}", "\\}")
text = strings.ReplaceAll(text, ".", "\\.")
text = strings.ReplaceAll(text, "!", "\\!")
text = strings.ReplaceAll(text, "(", "\\(")
text = strings.ReplaceAll(text, ")", "\\)")
text = strings.ReplaceAll(text, "[", "\\[")
text = strings.ReplaceAll(text, "]", "\\]")
text = strings.ReplaceAll(text, "*", "\\*")
//text = strings.ReplaceAll(text, ":", "\\:")
//text = strings.ReplaceAll(text, "\"", "\\\"")
//text = strings.ReplaceAll(text, "'", "\\'")
//text = strings.ReplaceAll(text, "/", "\\/")
//text = strings.ReplaceAll(text, "@", "\\@")
//text = strings.ReplaceAll(text, "%", "\\%")
//text = strings.ReplaceAll(text, ",", "\\,")
//text = strings.ReplaceAll(text, " ", "\\ ")
return text
}

func (pa *PlatformAdapterKook) MessageCreateRaw(base kook.MessageCreateBase, id string, isPrivate bool) error {
bot := pa.IntentSession
var err error
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func main() {
Delay int64 `long:"delay"`
JustForTest bool `long:"just-for-test"`
DBCheck bool `long:"db-check" description:"检查数据库是否有问题"`
ShowEnv bool `long:"show-env" description:"显示环境变量"`
}

//dice.SetDefaultNS([]string{"114.114.114.114:53", "8.8.8.8:53"}, false)
Expand All @@ -155,6 +156,12 @@ func main() {
model.DBCheck("data/default")
return
}
if opts.ShowEnv {
for i, e := range os.Environ() {
println(i, e)
}
return
}
deleteOldWrongFile()

if opts.Delay != 0 {
Expand Down
5 changes: 4 additions & 1 deletion utils/procs/procs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Process struct {
StdIn io.WriteCloser
Cmd *exec.Cmd
Dir string
Env []string
// show stdout, return value will be written to stdin
OutputHandler OutHandler
}
Expand Down Expand Up @@ -44,7 +45,9 @@ func (p *Process) Start() error {
if p.Dir != "" {
cmd.Dir = p.Dir
}

if p.Env != nil {
cmd.Env = p.Env
}
p.Cmd = cmd
stdin, err := cmd.StdinPipe()
if err != nil {
Expand Down

0 comments on commit 7906a44

Please sign in to comment.