getguildbase.go
· 3.7 KiB · Go
Raw
package get_user_guild_base_info
import (
"errors"
"net/http"
"time"
"github.com/anti-raid/splashtail/animusmagic"
"github.com/anti-raid/splashtail/types"
"github.com/anti-raid/splashtail/utils"
"github.com/anti-raid/splashtail/utils/mewext"
"github.com/anti-raid/splashtail/webserver/state"
"github.com/go-chi/chi/v5"
"go.uber.org/zap"
docs "github.com/infinitybotlist/eureka/doclib"
"github.com/infinitybotlist/eureka/ratelimit"
"github.com/infinitybotlist/eureka/uapi"
)
func Docs() *docs.Doc {
return &docs.Doc{
Summary: "Get User Guild Base Info",
Description: "This endpoint will return basic user and guild information given their IDs",
Resp: types.UserGuildBaseData{},
Params: []docs.Parameter{
{
Name: "user_id",
Description: "The ID of the user to get information about",
In: "path",
Required: true,
Schema: docs.IdSchema,
},
{
Name: "guild_id",
Description: "Whether to refresh the user's guilds from discord",
In: "path",
Required: true,
Schema: docs.IdSchema,
},
},
}
}
func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse {
limit, err := ratelimit.Ratelimit{
Expiry: 5 * time.Minute,
MaxRequests: 5,
Bucket: "get_user_guild_base_info",
}.Limit(d.Context, r)
if err != nil {
state.Logger.Error("Error while ratelimiting", zap.Error(err), zap.String("bucket", "get_user_guild_base_info"))
return uapi.DefaultResponse(http.StatusInternalServerError)
}
if limit.Exceeded {
return uapi.HttpResponse{
Json: types.ApiError{
Message: "You are being ratelimited. Please try again in " + limit.TimeToReset.String(),
},
Headers: limit.Headers(),
Status: http.StatusTooManyRequests,
}
}
guildId := chi.URLParam(r, "guild_id")
userId := chi.URLParam(r, "user_id")
if guildId == "" || userId == "" {
return uapi.DefaultResponse(http.StatusBadRequest)
}
// Fetch from animus magic
clusterId, err := mewext.GetClusterIDFromGuildID(guildId, state.MewldInstanceList.Map, int(state.MewldInstanceList.ShardCount))
if err != nil {
state.Logger.Error("Error getting cluster ID", zap.Error(err))
return uapi.HttpResponse{
Status: http.StatusInternalServerError,
Json: types.ApiError{
Message: "Error getting cluster ID: " + err.Error(),
},
}
}
for _, v := range state.MewldInstanceList.Instances {
if v.ClusterID == clusterId {
if !v.Active || v.CurrentlyKilling || len(v.ClusterHealth) == 0 {
return uapi.HttpResponse{
Status: http.StatusInternalServerError,
Json: types.ApiError{
Message: "Cluster is not healthy",
},
}
}
}
}
resps, err := state.AnimusMagicClient.Request(d.Context, state.Rueidis, &animusmagic.RequestData{
ClusterID: utils.Pointer(uint16(clusterId)),
Message: &animusmagic.AnimusMessage{
GetBaseGuildAndUserInfo: &struct {
GuildID string `json:"guild_id"`
UserID string `json:"user_id"`
}{
GuildID: guildId,
UserID: userId,
},
},
})
if err != nil {
state.Logger.Error("Error sending request to animus magic", zap.Error(err))
if errors.Is(err, animusmagic.ErrOpError) && len(resps) > 0 {
return uapi.HttpResponse{
Status: http.StatusInternalServerError,
Json: resps[0].Error,
}
}
return uapi.HttpResponse{
Status: http.StatusInternalServerError,
Json: types.ApiError{
Message: "Error sending request to animus magic: " + err.Error(),
},
}
}
if len(resps) != 1 {
return uapi.HttpResponse{
Status: http.StatusInternalServerError,
Json: types.ApiError{
Message: "Unexpected number of responses",
},
}
}
resp := resps[0]
return uapi.HttpResponse{
Json: resp.Resp.GetBaseGuildAndUserInfo,
}
}
1 | package get_user_guild_base_info |
2 | |
3 | import ( |
4 | "errors" |
5 | "net/http" |
6 | "time" |
7 | |
8 | "github.com/anti-raid/splashtail/animusmagic" |
9 | "github.com/anti-raid/splashtail/types" |
10 | "github.com/anti-raid/splashtail/utils" |
11 | "github.com/anti-raid/splashtail/utils/mewext" |
12 | "github.com/anti-raid/splashtail/webserver/state" |
13 | "github.com/go-chi/chi/v5" |
14 | "go.uber.org/zap" |
15 | |
16 | docs "github.com/infinitybotlist/eureka/doclib" |
17 | "github.com/infinitybotlist/eureka/ratelimit" |
18 | "github.com/infinitybotlist/eureka/uapi" |
19 | ) |
20 | |
21 | func Docs() *docs.Doc { |
22 | return &docs.Doc{ |
23 | Summary: "Get User Guild Base Info", |
24 | Description: "This endpoint will return basic user and guild information given their IDs", |
25 | Resp: types.UserGuildBaseData{}, |
26 | Params: []docs.Parameter{ |
27 | { |
28 | Name: "user_id", |
29 | Description: "The ID of the user to get information about", |
30 | In: "path", |
31 | Required: true, |
32 | Schema: docs.IdSchema, |
33 | }, |
34 | { |
35 | Name: "guild_id", |
36 | Description: "Whether to refresh the user's guilds from discord", |
37 | In: "path", |
38 | Required: true, |
39 | Schema: docs.IdSchema, |
40 | }, |
41 | }, |
42 | } |
43 | } |
44 | |
45 | func Route(d uapi.RouteData, r *http.Request) uapi.HttpResponse { |
46 | limit, err := ratelimit.Ratelimit{ |
47 | Expiry: 5 * time.Minute, |
48 | MaxRequests: 5, |
49 | Bucket: "get_user_guild_base_info", |
50 | }.Limit(d.Context, r) |
51 | |
52 | if err != nil { |
53 | state.Logger.Error("Error while ratelimiting", zap.Error(err), zap.String("bucket", "get_user_guild_base_info")) |
54 | return uapi.DefaultResponse(http.StatusInternalServerError) |
55 | } |
56 | |
57 | if limit.Exceeded { |
58 | return uapi.HttpResponse{ |
59 | Json: types.ApiError{ |
60 | Message: "You are being ratelimited. Please try again in " + limit.TimeToReset.String(), |
61 | }, |
62 | Headers: limit.Headers(), |
63 | Status: http.StatusTooManyRequests, |
64 | } |
65 | } |
66 | |
67 | guildId := chi.URLParam(r, "guild_id") |
68 | userId := chi.URLParam(r, "user_id") |
69 | |
70 | if guildId == "" || userId == "" { |
71 | return uapi.DefaultResponse(http.StatusBadRequest) |
72 | } |
73 | |
74 | // Fetch from animus magic |
75 | clusterId, err := mewext.GetClusterIDFromGuildID(guildId, state.MewldInstanceList.Map, int(state.MewldInstanceList.ShardCount)) |
76 | |
77 | if err != nil { |
78 | state.Logger.Error("Error getting cluster ID", zap.Error(err)) |
79 | return uapi.HttpResponse{ |
80 | Status: http.StatusInternalServerError, |
81 | Json: types.ApiError{ |
82 | Message: "Error getting cluster ID: " + err.Error(), |
83 | }, |
84 | } |
85 | } |
86 | |
87 | for _, v := range state.MewldInstanceList.Instances { |
88 | if v.ClusterID == clusterId { |
89 | if !v.Active || v.CurrentlyKilling || len(v.ClusterHealth) == 0 { |
90 | return uapi.HttpResponse{ |
91 | Status: http.StatusInternalServerError, |
92 | Json: types.ApiError{ |
93 | Message: "Cluster is not healthy", |
94 | }, |
95 | } |
96 | } |
97 | } |
98 | } |
99 | |
100 | resps, err := state.AnimusMagicClient.Request(d.Context, state.Rueidis, &animusmagic.RequestData{ |
101 | ClusterID: utils.Pointer(uint16(clusterId)), |
102 | Message: &animusmagic.AnimusMessage{ |
103 | GetBaseGuildAndUserInfo: &struct { |
104 | GuildID string `json:"guild_id"` |
105 | UserID string `json:"user_id"` |
106 | }{ |
107 | GuildID: guildId, |
108 | UserID: userId, |
109 | }, |
110 | }, |
111 | }) |
112 | |
113 | if err != nil { |
114 | state.Logger.Error("Error sending request to animus magic", zap.Error(err)) |
115 | |
116 | if errors.Is(err, animusmagic.ErrOpError) && len(resps) > 0 { |
117 | return uapi.HttpResponse{ |
118 | Status: http.StatusInternalServerError, |
119 | Json: resps[0].Error, |
120 | } |
121 | } |
122 | |
123 | return uapi.HttpResponse{ |
124 | Status: http.StatusInternalServerError, |
125 | Json: types.ApiError{ |
126 | Message: "Error sending request to animus magic: " + err.Error(), |
127 | }, |
128 | } |
129 | } |
130 | |
131 | if len(resps) != 1 { |
132 | return uapi.HttpResponse{ |
133 | Status: http.StatusInternalServerError, |
134 | Json: types.ApiError{ |
135 | Message: "Unexpected number of responses", |
136 | }, |
137 | } |
138 | } |
139 | |
140 | resp := resps[0] |
141 | |
142 | return uapi.HttpResponse{ |
143 | Json: resp.Resp.GetBaseGuildAndUserInfo, |
144 | } |
145 | } |
146 |