Skip to content

Commit

Permalink
Merge pull request #493 from CSUSTers/dev
Browse files Browse the repository at this point in the history
Merge `dev` branch
  • Loading branch information
Anthony-Hoo authored Nov 27, 2024
2 parents 65a912f + 8d6b21e commit ec63604
Show file tree
Hide file tree
Showing 18 changed files with 369 additions and 451 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: ^1.21
go-version: ^1.23

- name: Get source
uses: actions/checkout@v3
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ jobs:
name: Golangci Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: ^1.21
go-version: ^1.23

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
version: v1.59
version: v1.61
args: --issues-exit-code=1
only-new-issues: false
skip-pkg-cache: true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: ^1.21
go-version: ^1.23

- name: Get source
uses: actions/checkout@v3
Expand Down
20 changes: 11 additions & 9 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# options for analysis running
run:
go: "1.21"
go: "1.23"
# default concurrency is an available CPU number
concurrency: 16

Expand Down Expand Up @@ -134,10 +134,10 @@ linters-settings:
- truncateCmp

# Which checks should be disabled; can't be combined with 'enabled-checks'; default is empty
disabled-checks:
- regexpMust
- hugeParam
- rangeValCopy
# disabled-checks:
# - regexpMust
# - hugeParam
# - rangeValCopy

# Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint run` to see all tags and checks.
# Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags".
Expand Down Expand Up @@ -335,7 +335,7 @@ linters-settings:

predeclared:
# comma-separated list of predeclared identifiers to not report on
ignore: ""
ignore: "min,max"
# include method names and field names (i.e., qualified names) in checks
q: false

Expand All @@ -348,8 +348,6 @@ linters-settings:
ignore-generated-header: true
severity: warning
confidence: 0.8
error-code: 0
warning-code: 0
rules:
- name: atomic
# - name: bare-return
Expand Down Expand Up @@ -507,6 +505,9 @@ linters-settings:
# - $gostd
# - "github.com/stretchr/testify"

copyloopvar:
check-alias: false

linters:
enable:
- bidichk
Expand All @@ -516,7 +517,8 @@ linters:
- errname
- errorlint
- exhaustive
- exportloopref
- copyloopvar
# - exportloopref
# - forbidigo
# - gocognit
- goconst
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# build
FROM --platform=$BUILDPLATFORM golang:1.21-alpine AS buildenv
FROM --platform=$BUILDPLATFORM golang:1.23-alpine AS buildenv
ARG TARGETARCH

RUN apk add make git tzdata
Expand All @@ -21,7 +21,7 @@ RUN make deploy
FROM --platform=$BUILDPLATFORM alpine

RUN apk add --no-cache tzdata
COPY --from=hugefiver/ffmpeg:7.0.1 /ffmpeg /usr/local/bin/ffmpeg
COPY --from=hugefiver/ffmpeg:7.0.1-2 /ffmpeg /usr/local/bin/ffmpeg
# COPY --from=hugefiver/ffmpeg:7.0.1 /ffprobe /usr/local/bin/ffprobe

WORKDIR /app
Expand Down
139 changes: 32 additions & 107 deletions base/encode.go
Original file line number Diff line number Diff line change
@@ -1,136 +1,61 @@
package base

import (
"regexp"
"strings"

"bytes"
"csust-got/entities"
"math/rand/v2"
"regexp"

. "gopkg.in/telebot.v3"
)

var (
// change 'y' to 'i' if end with this.
yEndTable = [...]string{"ty", "ly", "fy", "py", "dy", "by"}

// hugeXer regex
hugeRegex = regexp.MustCompile(`^(huge)+.+(er)+$`)
hooPrePatt = regexp.MustCompile(`(?i)^h[o0]*`)
hooSufPatt = regexp.MustCompile(`(?i)[o0]*$`)
hooRunes = []rune("o0O")
)

// HugeEncoder encode 'xxx' to 'hugexxxer'.
func HugeEncoder(ctx Context) error {
arg, ok := parseHugeArgs(ctx)
if !ok {
return ctx.Reply(arg, ModeMarkdownV2)
// HooEncoder encode 'XXX' to 'hooXXXoo'.
func HooEncoder(ctx Context) error {
_, s, err := entities.CommandTakeArgs(ctx.Message(), 0)
if err != nil {
return ctx.Reply("h0oOo")
}

// encode
arg = hooEncode(arg)

return ctx.Reply(arg, ModeMarkdownV2)
s = hooEncode(s)
return ctx.Reply(s)
}

// HugeDecoder decode 'hugehugehugexxxererer' to 'hugexxxer'.
func HugeDecoder(ctx Context) error {
arg, ok := parseHugeArgs(ctx)
if !ok {
return ctx.Reply(arg, ModeMarkdownV2)
}

// decode
arg = hooDecode(arg)

return ctx.Reply(arg, ModeMarkdownV2)
}

func parseHugeArgs(ctx Context) (arg string, ok bool) {
if ctx.Message().ReplyTo != nil {
arg = ctx.Message().ReplyTo.Text
}

command := entities.FromMessage(ctx.Message())

if command.Argc() > 0 {
arg = command.ArgAllInOneFrom(0)
}

arg = strings.TrimSpace(arg)
func hooEncode(s string) string {
matches1 := hooPrePatt.FindStringIndex(s)
matches2 := hooSufPatt.FindStringIndex(s)

// no args
if arg == "" {
return "HUGEFIVER", false
i1, i2 := 0, len(s)
if matches1 != nil {
i1 = matches1[1]
}

// tldr
if len(arg) > 128 {
return "hugeTLDRer", false
if matches2 != nil {
i2 = matches2[0]
}

return arg, true
}
bs := bytes.NewBufferString("h")

func hooEncode(arg string) string {
if arg == "" {
return "HUGEFIVER"
}
// add 'huge' to prefix
if !strings.HasPrefix(arg, "huge") {
if arg[0] != 'e' {
arg = "e" + arg
}
arg = "hug" + arg
}
// add 'er' to suffix
if !strings.HasSuffix(arg, "er") {
arg = encodeParseEnd(arg)
// only add 'r' if $arg end with 'e'
if arg[len(arg)-1] != 'e' {
arg += "e"
if i1 >= i2-1 {
for range 4 {
bs.WriteRune(hooRunes[rand.N(len(hooRunes))])
}
arg += "r"
}
// if we get 'huger' after encode, we <fork> him.
if arg == "huger" {
arg = "hugeF**Ker"
return bs.String()
}

return arg
}

// encodeParseEnd change 'y' to 'i' if end with $yEndTable
func encodeParseEnd(arg string) string {
for _, v := range yEndTable {
if strings.HasSuffix(arg, v) {
arg = arg[0:len(arg)-1] + "i"
break
}
}
return arg
}

func hooDecode(arg string) string {
if !hugeRegex.MatchString(arg) {
return "hugeFAKEr"
for range 2 {
bs.WriteRune(hooRunes[rand.N(len(hooRunes))])
}

// find first 'huge' and last 'er'
huge := strings.Index(arg, "huge")
er := strings.LastIndex(arg, "er")
bs.WriteString(s[i1:i2])

// find end of first consecutive 'huge' and start of last consecutive 'er'
var hugeEnd, erStart int
for hugeEnd = huge; hugeEnd+4 < len(arg); hugeEnd += 4 {
if arg[hugeEnd:hugeEnd+4] != "huge" {
break
}
}
for erStart = er; erStart-2 >= 0; erStart -= 2 {
if arg[erStart-2:erStart] != "er" {
break
}
for range 2 {
bs.WriteRune(hooRunes[rand.N(len(hooRunes))])
}

// decode
arg = arg[0:huge+4] + arg[hugeEnd:erStart] + arg[er:]
return arg
return bs.String()
}
44 changes: 12 additions & 32 deletions base/encode_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package base

import (
"strings"
"testing"

"github.com/samber/lo"
"github.com/stretchr/testify/require"
)

Expand All @@ -11,40 +13,18 @@ func Test_encode(t *testing.T) {
args string
want string
}{
{"hugefiver", "hugefiver"},
{"hugeer", "hugeer"},
{"huger", "hugeF**Ker"},
{"", "HUGEFIVER"},
{"even", "hugevener"},
{"ray", "hugerayer"},
{"wu", "hugewuer"},
{"e", "hugeF**Ker"},
{"en", "hugener"},
{"py", "hugepier"},
{"", "h0oOo"},
{"0", "h0oOo"},
{"Hoo", "h0oOo"},
{"h0o0o00", "h0oOo"},
{"FAKER", "h0oFAKEROo"},
{"h0o0o0OFAKER", "h0oFAKEROo"},
{"FAKERo0oO0", "h0oFAKEROo"},
{"h0o0o0OFAKERo0oO0", "h0oFAKEROo"},
}
replacer := strings.NewReplacer(lo.FlatMap[string, string]([]string{"0", "o", "O"}, func(item string, _ int) []string { return []string{item, "o"} })...)
for _, tt := range tests {
got := hooEncode(tt.args)
require.Equalf(t, tt.want, got, "encode(%s)", tt.args)
}
}

func Test_decode(t *testing.T) {
tests := []struct {
args string
want string
}{
{"hugefiver", "hugefiver"},
{"hugeer", "hugeFAKEr"},
{"huger", "hugeFAKEr"},
{"", "hugeFAKEr"},
{"even", "hugeFAKEr"},
{"hugehugefiver", "hugefiver"},
{"hugefiverer", "hugefiver"},
{"hugehugefiverer", "hugefiver"},
{"hugehugeer", "hugeer"},
}
for _, tt := range tests {
got := hooDecode(tt.args)
require.Equalf(t, tt.want, got, "decode(%s)", tt.args)
require.Equalf(t, replacer.Replace(tt.want), replacer.Replace(got), "encode(%s)", tt.args)
}
}
10 changes: 5 additions & 5 deletions base/genshin_voice.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func GetVoiceV2(ctx Context) error {
return err

}
err = SendVoice(m.Chat, data)
err = SendVoice(m.Chat, &data)
return err
}

Expand Down Expand Up @@ -179,7 +179,7 @@ func GetVoiceV3(ctx Context) error {
return err

}
err = SendVoiceV3(m.Chat, data)
err = SendVoiceV3(m.Chat, &data)
return err
}

Expand Down Expand Up @@ -235,7 +235,7 @@ func GetVoiceV3Pro(ctx Context) error {
return err

}
err = SendVoiceV3(m.Chat, data)
err = SendVoiceV3(m.Chat, &data)
return err
}

Expand Down Expand Up @@ -313,15 +313,15 @@ func SendErrVoice(chat *Chat, errStr string) error {
}

// SendVoice 发音频消息
func SendVoice(chat *Chat, v genShinVoiceV2) error {
func SendVoice(chat *Chat, v *genShinVoiceV2) error {
audioCaption := fmt.Sprintf("%s \n\n #%s %s", v.Text, v.NpcNameLocal, v.Topic)
voice := Voice{File: FromURL(v.AudioURL), Caption: audioCaption}
_, err := voice.Send(config.BotConfig.Bot, chat, nil)
return err
}

// SendVoiceV3 发音频消息 - 适用于v3版api(合成语音)
func SendVoiceV3(chat *Chat, v genShinVoiceV3) error {
func SendVoiceV3(chat *Chat, v *genShinVoiceV3) error {
audioCaption := fmt.Sprintf("%s \n\n #%s", v.Text, v.Character)
voice := Voice{File: FromURL(v.VoiceUrl), Caption: audioCaption}
_, err := voice.Send(config.BotConfig.Bot, chat, nil)
Expand Down
Loading

0 comments on commit ec63604

Please sign in to comment.