add dc img proxy and enable it for welcome bot

This commit is contained in:
Seraphim Strub 2023-10-13 20:25:03 +02:00
parent 717f3d1bf5
commit f9268f31e9
3 changed files with 83 additions and 1 deletions

View file

@ -63,6 +63,12 @@ domaincheckbot:
GO_CMD: domaincheckbot GO_CMD: domaincheckbot
<<: *docker_build <<: *docker_build
dcimgproxy:
stage: build
variables:
GO_CMD: dcimgproxy
<<: *docker_build
pages: pages:
stage: build stage: build
script: script:

75
cmd/dcimgproxy/main.go Normal file
View file

@ -0,0 +1,75 @@
package main
import (
"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/cache"
"github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2"
"golang.org/x/exp/slog"
"io"
"net/http"
"os"
"strings"
)
var (
token = os.Getenv("disgo_token")
avatarCache map[snowflake.ID][]byte
)
func init() {
avatarCache = make(map[snowflake.ID][]byte)
}
func main() {
mux := http.NewServeMux()
client, err := disgo.New(token, bot.WithCacheConfigOpts(
cache.WithCaches(
cache.FlagsAll,
),
),
)
if err != nil {
slog.Error(err.Error())
return
}
rest := client.Rest()
mux.HandleFunc("/api/v1/discord/avatar/", func(w http.ResponseWriter, r *http.Request) {
flake, err := snowflake.Parse(strings.TrimPrefix(r.URL.Path, "/api/v1/discord/avatar/"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
avatar, ok := avatarCache[flake]
if !ok {
user, err := rest.GetUser(flake)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
avatarUrl := user.EffectiveAvatarURL()
res, err := http.Get(avatarUrl)
if err != nil || res.StatusCode != http.StatusOK {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// cache entry
avatarCache[flake], err = io.ReadAll(res.Body)
avatar = avatarCache[flake]
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", http.DetectContentType(avatar))
_, _ = w.Write(avatar)
return
})
log.Fatal(http.ListenAndServe(":8080", mux))
}

View file

@ -30,7 +30,8 @@ func JoinEvent(event *events.GuildMemberJoin) {
SetEmbeds(discord.NewEmbedBuilder(). SetEmbeds(discord.NewEmbedBuilder().
SetColor(16068661). SetColor(16068661).
SetTimestamp(time.Now()). SetTimestamp(time.Now()).
SetThumbnail(user.EffectiveAvatarURL(discord.WithSize(1024))). //SetThumbnail(user.EffectiveAvatarURL(discord.WithSize(1024))).
SetThumbnail(fmt.Sprintf("https://app11.rvo.one/api/v1/discord/avatar/%s", user.ID.String())).
AddField( AddField(
fmt.Sprintf("Welcome to the %v Server", guild.Name), fmt.Sprintf("Welcome to the %v Server", guild.Name),
fmt.Sprintf("Welcome %v (`%v`) on %v 🍎", user.Mention(), user.Username, guild.Name), false). fmt.Sprintf("Welcome %v (`%v`) on %v 🍎", user.Mention(), user.Username, guild.Name), false).