Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: 移除kook/discord平台的代码块包裹 #111

Merged
merged 4 commits into from
Jun 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ jobs:
if: github.event.name == 'pull_request'
with:
go-version: '1.18'
version: 'v1.51.2'
args: '--timeout 9999s'
only-new-issues: true
skip-pkg-cache: true
skip-build-cache: true
Expand All @@ -46,4 +48,5 @@ jobs:
uses: golangci/golangci-lint-action@v3
if: github.event.name != 'pull_request'
with:
version: latest
version: 'v1.51.2'
args: '--timeout 9999s'
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
16 changes: 6 additions & 10 deletions dice/platform_adapter_discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (pa *PlatformAdapterDiscord) sendToChannelRaw(channelId string, text string
for _, element := range elem {
switch e := element.(type) {
case *TextElement:
msgSend.Content = msgSend.Content + e.Content
msgSend.Content = msgSend.Content + antiMarkdownFormat(e.Content)
case *AtElement:
if e.Target == "all" {
msgSend.Content = msgSend.Content + "@everyone "
Expand All @@ -205,9 +205,6 @@ func (pa *PlatformAdapterDiscord) sendToChannelRaw(channelId string, text string
Reader: e.Stream,
})
msgSend.Files = files
if msgSend.Content != "" {
msgSend.Content = "```\n" + msgSend.Content + "\n```"
}
_, err = pa.IntentSession.ChannelMessageSendComplex(id, msgSend)
msgSend = &discordgo.MessageSend{Content: ""}
case *ImageElement:
Expand All @@ -219,14 +216,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 +246,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 Expand Up @@ -309,6 +301,9 @@ func FormatDiceIdDiscord(diceDiscord string) string {
func FormatDiceIdDiscordChannel(diceDiscord string) string {
return fmt.Sprintf("DISCORD-CH-Group:%s", diceDiscord)
}
func FormatDiceIdDiscordGuild(diceDiscord string) string {
return fmt.Sprintf("DISCORD-Guild:%s", diceDiscord)
}

func ExtractDiscordUserId(id string) string {
if strings.HasPrefix(id, "DISCORD:") {
Expand Down Expand Up @@ -339,7 +334,8 @@ func (pa *PlatformAdapterDiscord) toStdMessage(m *discordgo.MessageCreate) *Mess
msg.MessageType = "private"
} else {
msg.MessageType = "group"
msg.GroupId = FormatDiceIdDiscordChannel(m.ChannelID)
msg.GroupId = FormatDiceIdDiscordGuild(m.ChannelID)
msg.GuildId = ch.GuildID
}
send := new(SenderBase)
send.UserId = FormatDiceIdDiscord(m.Author.ID)
Expand Down
41 changes: 39 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 All @@ -377,6 +409,10 @@ func FormatDiceIdKook(diceKook string) string {
return fmt.Sprintf("KOOK:%s", diceKook)
}

func FormatDiceIdKookGuild(diceKook string) string {
return fmt.Sprintf("KOOK-Guild:%s", diceKook)
}

func FormatDiceIdKookChannel(diceKook string) string {
return fmt.Sprintf("KOOK-CH-Group:%s", diceKook)
}
Expand Down Expand Up @@ -495,6 +531,7 @@ func (pa *PlatformAdapterKook) toStdMessage(ctx *kook.KmarkdownMessageContext) *
} else {
msg.MessageType = "group"
msg.GroupId = FormatDiceIdKookChannel(ctx.Common.TargetID)
msg.GuildId = FormatDiceIdKookGuild(ctx.Extra.GuildID)
if pa.checkIfGuildAdmin(ctx) {
send.GroupRole = "admin"
}
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