commit 70b506036a08d9bc1ae803d758b0de8b2c041e2c Author: royalcat Date: Thu Jun 27 16:45:32 2024 +0300 init diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..0cfc43c --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,10 @@ +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + - package-ecosystem: "gomod" + directory: "/" + schedule: + interval: "weekly" diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml new file mode 100644 index 0000000..09a849a --- /dev/null +++ b/.github/workflows/docker.yaml @@ -0,0 +1,64 @@ +name: docker + +on: + push: + branches: + - master + tags: + - "v*" + +jobs: + build-docker: + permissions: + contents: read + packages: write + strategy: + fail-fast: false + matrix: + platform: + - linux/amd64 + - linux/arm64 + runs-on: ubuntu-latest + steps: + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Container Registry + uses: docker/login-action@v3 + with: + registry: git.kmsign.ru + username: ${{ github.actor }} + password: ${{ secrets.PACKAGE_TOKEN }} + + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Docker meta + id: meta + uses: https://github.com/docker/metadata-action@v5 + with: + # list of Docker images to use as base name for tags + images: git.kmsign.ru/${{ github.repository }} + # generate Docker tags based on the following events/attributes + tags: | + type=schedule + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=sha + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + push: true + pull: true + platforms: ${{ matrix.platform }} + labels: ${{ steps.meta.outputs.labels }} + sbom: true + provenance: true + # cache-from: type=gha + # cache-to: type=gha,mode=max + tags: ${{ steps.meta.outputs.tags }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cc7f039 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +bin \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..6e239ef --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,25 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Bot", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "cmd/bot/main.go", + "cwd": "bin", + "envFile": "${workspaceFolder}/.env" + }, + { + "name": "DBViewer", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "cmd/dbviewer/main.go", + "cwd": "bin", + } + ] +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..123cf17 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM golang:1.22 as builder + +WORKDIR /app + +COPY go.mod ./ +COPY go.sum ./ +RUN --mount=type=cache,mode=0777,target=/go/pkg/mod go mod download all + +COPY ./cmd ./cmd +COPY ./src ./src +RUN --mount=type=cache,mode=0777,target=/go/pkg/mod CGO_ENABLED=0 go build -tags timetzdata -o /konfachcloud-discord-bot ./cmd/bot/main.go + + +FROM scratch + +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt +COPY --from=builder /konfachcloud-discord-bot /konfachcloud-discord-bot + +ENTRYPOINT ["/konfachcloud-discord-bot"] diff --git a/cmd/bot/main.go b/cmd/bot/main.go new file mode 100644 index 0000000..86e262e --- /dev/null +++ b/cmd/bot/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "context" + "os" + "os/signal" + "syscall" + + "git.kmsign.ru/royalcat/konfachcloud-discord-bot/src/bot" +) + +func main() { + botToken, ok := os.LookupEnv("DISCORD_BOT_TOKEN") + if !ok { + panic("DISCORD_BOT_TOKEN env var is not set") + } + b, err := bot.Run(bot.Settings{ + BotToken: botToken, + HashLength: 1, + DistanceThreshold: 4, + }) + if err != nil { + panic(err) + } + defer b.Close(context.Background()) + + sc := make(chan os.Signal, 1) + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) + <-sc +} diff --git a/cmd/dbviewer/main.go b/cmd/dbviewer/main.go new file mode 100644 index 0000000..120e7e4 --- /dev/null +++ b/cmd/dbviewer/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + + "github.com/dgraph-io/badger/v4" +) + +func main() { + opts := badger.DefaultOptions("./db/hash") + db, err := badger.Open(opts) + if err != nil { + panic(err) + } + defer db.Close() + + err = db.View(func(txn *badger.Txn) error { + it := txn.NewIterator(badger.DefaultIteratorOptions) + defer it.Close() + + for it.Rewind(); it.Valid(); it.Next() { + item := it.Item() + val, err := item.ValueCopy(nil) + if err != nil { + return err + } + fmt.Println(string(item.Key()) + " " + string(val)) + } + + return nil + }) + if err != nil { + panic(err) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..123a677 --- /dev/null +++ b/go.mod @@ -0,0 +1,34 @@ +module git.kmsign.ru/royalcat/konfachcloud-discord-bot + +go 1.22.4 + +require ( + github.com/bwmarrin/discordgo v0.28.1 + github.com/corona10/goimagehash v1.1.0 + github.com/dgraph-io/badger/v4 v4.2.0 + github.com/royalcat/kv v0.0.0-20240617101007-c9c746b3916f + github.com/royalcat/kv/kvbadger v0.0.0-20240617101007-c9c746b3916f + golang.org/x/image v0.18.0 +) + +require ( + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/dgraph-io/ristretto v0.1.1 // indirect + github.com/dustin/go-humanize v1.0.0 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/golang/glog v1.0.0 // indirect + github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect + github.com/golang/protobuf v1.5.2 // indirect + github.com/golang/snappy v0.0.3 // indirect + github.com/google/flatbuffers v1.12.1 // indirect + github.com/gorilla/websocket v1.4.2 // indirect + github.com/klauspost/compress v1.12.3 // indirect + github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect + github.com/pkg/errors v0.9.1 // indirect + go.opencensus.io v0.22.5 // indirect + golang.org/x/crypto v0.15.0 // indirect + golang.org/x/net v0.18.0 // indirect + golang.org/x/sys v0.14.0 // indirect + google.golang.org/protobuf v1.28.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..49ef7e0 --- /dev/null +++ b/go.sum @@ -0,0 +1,138 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4= +github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= +github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/corona10/goimagehash v1.1.0 h1:teNMX/1e+Wn/AYSbLHX8mj+mF9r60R1kBeqE9MkoYwI= +github.com/corona10/goimagehash v1.1.0/go.mod h1:VkvE0mLn84L4aF8vCb6mafVajEb6QYMHl2ZJLn0mOGI= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgraph-io/badger/v4 v4.2.0 h1:kJrlajbXXL9DFTNuhhu9yCx7JJa4qpYWxtE8BzuWsEs= +github.com/dgraph-io/badger/v4 v4.2.0/go.mod h1:qfCqhPoWDFJRx1gp5QwwyGo8xk1lbHUxvK9nK0OGAak= +github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8= +github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA= +github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA= +github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= +github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= +github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/glog v1.0.0 h1:nfP3RFugxnNRyKgeWd4oI1nYvXpxrx8ck8ZrcizshdQ= +github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4= +github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 h1:ZgQEtGgCBiWRM39fZuwSd1LwSqqSW0hOdXCYYDX0R3I= +github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA= +github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/flatbuffers v1.12.1 h1:MVlul7pQNoDzWRLTw5imwYsl+usrS1TXG2H4jg6ImGw= +github.com/google/flatbuffers v1.12.1/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/compress v1.12.3 h1:G5AfA94pHPysR56qqrkO2pxEexdDzrpFJ6yt/VqWxVU= +github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/royalcat/kv v0.0.0-20240617101007-c9c746b3916f h1:bG8Pp/YXkpC2eFI7psTiTAL7QTBqHQcP/lEpiqmBXn4= +github.com/royalcat/kv v0.0.0-20240617101007-c9c746b3916f/go.mod h1:UB/VwpTut8c3IXLJFvYWFxAAZymk9eBuJRMJmpSpwYU= +github.com/royalcat/kv/kvbadger v0.0.0-20240617101007-c9c746b3916f h1:wz3pvg7YJdibZXQRV6B5pVPeDK8bgnuJVnBf7OFtCWI= +github.com/royalcat/kv/kvbadger v0.0.0-20240617101007-c9c746b3916f/go.mod h1:JxgA1VGwbqu+WqdmjmjT0v6KeWoWlN6Y5lesjmphExM= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +go.opencensus.io v0.22.5 h1:dntmOdLpSpHlVqbW5Eay97DelsZHe+55D+xC6i0dDS0= +go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA= +golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ= +golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= +golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= +google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/src/bot/bot.go b/src/bot/bot.go new file mode 100644 index 0000000..4e6adc5 --- /dev/null +++ b/src/bot/bot.go @@ -0,0 +1,236 @@ +package bot + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "log/slog" + "os" + "slices" + "sync" + "time" + + "github.com/bwmarrin/discordgo" + "github.com/corona10/goimagehash" + "github.com/royalcat/kv" +) + +type State struct { + InitialScanLastID map[string]string `json:"initial_scan_last_id"` // channelID -> messageID +} + +type Settings struct { + HashLength int + DistanceThreshold int + BotToken string +} + +type Bot struct { + session *discordgo.Session + state State + saveStateMu sync.Mutex + settings Settings + hashDB kv.Store[attachmentKey, goimagehash.ExtImageHash] + log *slog.Logger +} + +func (b *Bot) Close(ctx context.Context) error { + return errors.Join( + b.hashDB.Close(ctx), + b.session.Close(), + ) +} + +const stateFileName = "state.json" + +func (b *Bot) saveState() error { + b.saveStateMu.Lock() + defer b.saveStateMu.Unlock() + + f, err := os.OpenFile(stateFileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) + if err != nil { + return err + } + defer f.Close() + return json.NewEncoder(f).Encode(b.state) +} + +func Run(settings Settings) (*Bot, error) { + state := State{} + if _, err := os.Stat(stateFileName); err == nil { + f, err := os.Open(stateFileName) + if err != nil { + return nil, err + } + defer f.Close() + + err = json.NewDecoder(f).Decode(&state) + if err != nil { + return nil, fmt.Errorf("state decode error: %w", err) + } + } + + hashDB, err := newHashDb(settings.HashLength) + if err != nil { + return nil, err + } + + b := &Bot{ + state: state, + hashDB: hashDB, + settings: settings, + log: slog.Default(), + } + + b.session, err = discordgo.New("Bot " + settings.BotToken) + if err != nil { + return nil, err + } + + b.session.Identify.Intents |= discordgo.IntentsGuildMessages + + b.session.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) { + b.log.Info("bot is ready") + }) + + enabledChannels := []string{"576665679449686017", "871355568659496960"} + + b.session.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) { + if len(m.Attachments) == 0 || !slices.Contains(enabledChannels, m.ChannelID) { + return + } + ctx := context.Background() + + for _, att := range m.Attachments { + ih, err := b.attachmentHash(ctx, att) + if err != nil { + b.log.Error("attachment hash error", slog.String("error", err.Error())) + continue + } + + if ih == nil { + continue + } + + simularKeys, err := b.searchSimular(ctx, m.ChannelID, ih) + if err != nil { + b.log.Error("search simular error", slog.String("error", err.Error())) + continue + } + + if len(simularKeys) > 0 { + b.log.Info("found simular", slog.Any("simularKey", simularKeys)) + err := b.sendBayanMessage(ctx, m.ChannelID, + &discordgo.MessageReference{ + GuildID: m.GuildID, + ChannelID: m.ChannelID, + MessageID: m.ID, + }, + len(simularKeys), + &discordgo.MessageReference{ + GuildID: m.GuildID, + ChannelID: simularKeys[0].ChannelID, + MessageID: simularKeys[0].MessageID, + }, + ) + if err != nil { + b.log.Error("send bayan message error", slog.String("error", err.Error())) + } + } + + err = b.saveHash(ctx, m.ChannelID, m.ID, att.ID, ih) + if err != nil { + b.log.Error("scan attachment error", slog.String("error", err.Error())) + } + } + + return + }) + + err = b.session.Open() + if err != nil { + return nil, err + } + + go func() { + for _, channelID := range enabledChannels { + err = b.initalMessagesScan(context.Background(), channelID) + if err != nil { + b.log.Error("inital scan error", slog.String("error", err.Error()), slog.String("channelID", channelID)) + } + b.log.Info("inital scan done", slog.String("channelID", channelID)) + } + }() + + return b, nil +} +func (b *Bot) sendBayanMessage(ctx context.Context, channelID string, bayan *discordgo.MessageReference, orginalCount int, firstOriginal *discordgo.MessageReference) error { + originalUrl := fmt.Sprintf("https://discord.com/channels/%s/%s/%s", firstOriginal.GuildID, firstOriginal.ChannelID, firstOriginal.MessageID) + _, err := b.session.ChannelMessageSendReply(channelID, + fmt.Sprintf("Баян обнаружен\nУже было скинуто %d раз\n%s", orginalCount, originalUrl), + bayan, + discordgo.WithContext(ctx), + ) + if err != nil { + return fmt.Errorf("send message error: %w", err) + } + return nil +} + +func (b *Bot) initalMessagesScan(ctx context.Context, channelID string) error { + log := b.log.With(slog.String("channelID", channelID)) + + lastId, _ := b.state.InitialScanLastID[channelID] + + for { + messages, err := b.session.ChannelMessages(channelID, 100, lastId, "", "") + if err != nil { + return err + } + + for i, msg := range messages { + log := log.With(slog.String("messageID", msg.ID)) + for _, att := range msg.Attachments { + log := log.With(slog.String("attachmentID", att.ID)) + + _, err := b.hashDB.Get(ctx, attachmentKey{ChannelID: channelID, MessageID: msg.ID, AttachmentID: att.ID}) + if err == nil { + continue + } + + ih, err := b.attachmentHash(ctx, att) + if err != nil { + log.Error("attachment hash error", slog.String("error", err.Error())) + continue + } + + if ih == nil { + continue + } + + err = b.saveHash(ctx, channelID, msg.ID, att.ID, ih) + if err != nil { + log.Error("scan attachment error", slog.String("error", err.Error())) + continue + } + } + lastId = msg.ID + + log.Info("inital scan message added to db", + slog.String("message_date", msg.Timestamp.Format(time.DateTime)), + slog.Int("batch_index", i), + slog.Int("batch_size", len(messages)), + ) + } + + b.state.InitialScanLastID[channelID] = lastId + b.saveState() + + if len(messages) < 100 { + break + } + } + + return nil +} diff --git a/src/bot/hashdb.go b/src/bot/hashdb.go new file mode 100644 index 0000000..a9f3ae0 --- /dev/null +++ b/src/bot/hashdb.go @@ -0,0 +1,115 @@ +package bot + +import ( + "bytes" + "encoding/binary" + "fmt" + "strings" + + "github.com/corona10/goimagehash" + "github.com/royalcat/kv" + "github.com/royalcat/kv/kvbadger" +) + +type attachmentKey struct { + ChannelID string + MessageID string + AttachmentID string +} + +func (a attachmentKey) IsEmpty() bool { + return a.ChannelID == "" && a.MessageID == "" && a.AttachmentID == "" +} + +func (a attachmentKey) String() string { + out := strings.Builder{} + + if a.ChannelID == "" { + return out.String() + } + out.WriteString(a.ChannelID + "/") + + if a.MessageID == "" { + return out.String() + } + out.WriteString(a.MessageID + "/") + + if a.AttachmentID == "" { + return out.String() + } + out.WriteString(a.AttachmentID) + + return out.String() +} + +var _ kv.Binary = (*attachmentKey)(nil) + +// MarshalBinary implements kv.Binary. +func (a attachmentKey) MarshalBinary() ([]byte, error) { + if a.ChannelID == "" { + return nil, fmt.Errorf("must be at least channel id") + } + + return []byte(a.String()), nil +} + +// UnmarshalBinary implements kv.Binary. +func (a *attachmentKey) UnmarshalBinary(data []byte) error { + s := string(data) + + parts := strings.Split(s, "/") + if len(parts) != 3 { + return fmt.Errorf("not enough parts in key: %s", s) + } + a.ChannelID = parts[0] + a.MessageID = parts[1] + a.AttachmentID = parts[2] + + return nil +} + +type extimagehashCodec struct { + hashLength int +} + +var _ kv.Codec = extimagehashCodec{} + +// Marshal implements kv.Codec. +func (u extimagehashCodec) Marshal(v any) ([]byte, error) { + val, ok := v.(goimagehash.ExtImageHash) + if !ok { + return nil, fmt.Errorf("value is not image hash") + } + if val.Bits() != u.hashLength*64 { + return nil, fmt.Errorf("hash length mismatch") + } + + buf := bytes.NewBuffer(make([]byte, 0, u.hashLength*8)) + err := binary.Write(buf, binary.BigEndian, val.GetHash()) + + return buf.Bytes(), err +} + +// Unmarshal implements kv.Codec. +func (u extimagehashCodec) Unmarshal(data []byte, v any) error { + hash := make([]uint64, u.hashLength) + err := binary.Read(bytes.NewReader(data), binary.BigEndian, hash) + if err != nil { + return err + } + + if _, ok := v.(*goimagehash.ExtImageHash); !ok { + return fmt.Errorf("value is not image hash") + } + + h := goimagehash.NewExtImageHash(hash, goimagehash.PHash, u.hashLength*64) + *v.(*goimagehash.ExtImageHash) = *h + + return nil +} + +func newHashDb(hashLength int) (kv.Store[attachmentKey, goimagehash.ExtImageHash], error) { + opts := kvbadger.DefaultOptions("./db/hash") + opts.Codec = extimagehashCodec{hashLength: hashLength} + return kvbadger.NewBagerKVBinaryKey[attachmentKey, goimagehash.ExtImageHash](opts) +} diff --git a/src/bot/image_hash.go b/src/bot/image_hash.go new file mode 100644 index 0000000..4d05216 --- /dev/null +++ b/src/bot/image_hash.go @@ -0,0 +1,57 @@ +package bot + +import ( + "context" + "fmt" + "image" + _ "image/jpeg" + _ "image/png" + "log/slog" + "net/http" + "slices" + "strings" + + _ "golang.org/x/image/webp" + + "github.com/bwmarrin/discordgo" + "github.com/corona10/goimagehash" +) + +var knownContentTypes = []string{"image/jpeg", "image/png", "image/webp"} + +func (b *Bot) attachmentHash(ctx context.Context, att *discordgo.MessageAttachment) (*goimagehash.ExtImageHash, error) { + if !slices.Contains(knownContentTypes, att.ContentType) { + if strings.HasPrefix(att.ContentType, "image/") { + b.log.Warn("unknown image content type", slog.String("content_type", att.ContentType)) + } + return nil, nil + } + + r, err := http.NewRequestWithContext(ctx, "GET", att.URL, nil) + if err != nil { + return nil, err + } + + resp, err := http.DefaultClient.Do(r) + if err != nil { + return nil, fmt.Errorf("http get error: %w", err) + } + + img, _, err := image.Decode(resp.Body) + if err != nil { + return nil, fmt.Errorf("image decode error: %w", err) + } + ih, err := goimagehash.ExtPerceptionHash(img, 8, 8) + if err != nil { + return nil, fmt.Errorf("hash error: %w", err) + } + return ih, nil +} + +func (b *Bot) saveHash(ctx context.Context, channelID, messageID, attachmentID string, ih *goimagehash.ExtImageHash) error { + err := b.hashDB.Set(ctx, attachmentKey{ChannelID: channelID, MessageID: messageID, AttachmentID: attachmentID}, *ih) + if err != nil { + return fmt.Errorf("hash db set error: %w", err) + } + return nil +} diff --git a/src/bot/search.go b/src/bot/search.go new file mode 100644 index 0000000..e11fe85 --- /dev/null +++ b/src/bot/search.go @@ -0,0 +1,31 @@ +package bot + +import ( + "context" + "io" + "log/slog" + + "github.com/corona10/goimagehash" +) + +func (b *Bot) searchSimular(ctx context.Context, channelID string, ih *goimagehash.ExtImageHash) ([]attachmentKey, error) { + log := b.log.With("channelID", channelID) + var simularKeys []attachmentKey + err := b.hashDB.RangeWithPrefix(ctx, attachmentKey{ChannelID: channelID}, func(key attachmentKey, ih2 goimagehash.ExtImageHash) error { + dist, err := ih.Distance(&ih2) + if err != nil { + log.Error("distance calculating error", slog.String("error", err.Error())) + return nil + } + + if dist <= b.settings.DistanceThreshold { + simularKeys = append(simularKeys, key) + } + return nil + }) + if err == io.EOF { + err = nil + } + + return simularKeys, err +}