This commit is contained in:
2026-01-27 19:14:12 +11:00
parent fb701bf205
commit 96d534f045
15 changed files with 138 additions and 314 deletions

View File

@@ -10,26 +10,6 @@ import (
"github.com/pkg/errors"
)
type OAuthSession struct {
*discordgo.Session
}
func NewOAuthSession(token *Token) (*OAuthSession, error) {
session, err := discordgo.New("Bearer " + token.AccessToken)
if err != nil {
return nil, errors.Wrap(err, "discordgo.New")
}
return &OAuthSession{Session: session}, nil
}
func (s *OAuthSession) GetUser() (*discordgo.User, error) {
user, err := s.User("@me")
if err != nil {
return nil, errors.Wrap(err, "s.User")
}
return user, nil
}
// APIClient is an HTTP client wrapper that handles Discord API rate limits
type APIClient struct {
cfg *Config
@@ -38,6 +18,7 @@ type APIClient struct {
mu sync.RWMutex
buckets map[string]*RateLimitState
trustedHost string
bot *BotSession
}
// NewAPIClient creates a new Discord API client with rate limit handling
@@ -51,11 +32,20 @@ func NewAPIClient(cfg *Config, logger *hlog.Logger, trustedhost string) (*APICli
if trustedhost == "" {
return nil, errors.New("trustedhost cannot be empty")
}
bot, err := newBotSession(cfg)
if err != nil {
return nil, errors.Wrap(err, "newBotSession")
}
return &APIClient{
client: &http.Client{Timeout: 30 * time.Second},
logger: logger,
buckets: make(map[string]*RateLimitState),
cfg: cfg,
trustedHost: trustedhost,
bot: bot,
}, nil
}
func (api *APIClient) Ping() (*discordgo.Application, error) {
return api.bot.Application("@me")
}