diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 15e523b..850e782 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -63,6 +63,12 @@ domaincheckbot: GO_CMD: domaincheckbot <<: *docker_build +dcimgproxy: + stage: build + variables: + GO_CMD: dcimgproxy + <<: *docker_build + pages: stage: build script: diff --git a/cmd/dcimgproxy/main.go b/cmd/dcimgproxy/main.go new file mode 100644 index 0000000..c8cad91 --- /dev/null +++ b/cmd/dcimgproxy/main.go @@ -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)) +} diff --git a/cmd/welcomebot/event/listener.go b/cmd/welcomebot/event/listener.go index 2941514..fb85a34 100644 --- a/cmd/welcomebot/event/listener.go +++ b/cmd/welcomebot/event/listener.go @@ -30,7 +30,8 @@ func JoinEvent(event *events.GuildMemberJoin) { SetEmbeds(discord.NewEmbedBuilder(). SetColor(16068661). 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( fmt.Sprintf("Welcome to the %v Server", guild.Name), fmt.Sprintf("Welcome %v (`%v`) on %v 🍎", user.Mention(), user.Username, guild.Name), false).