tweaks
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
22
internal/discord/bot.go
Normal file
22
internal/discord/bot.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package discord
|
||||
|
||||
import (
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type BotSession struct {
|
||||
*discordgo.Session
|
||||
}
|
||||
|
||||
func newBotSession(cfg *Config) (*BotSession, error) {
|
||||
session, err := discordgo.New("Bot " + cfg.BotToken)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "discordgo.New")
|
||||
}
|
||||
return &BotSession{Session: session}, nil
|
||||
}
|
||||
|
||||
func (api *APIClient) Bot() *BotSession {
|
||||
return api.bot
|
||||
}
|
||||
@@ -12,6 +12,7 @@ type Config struct {
|
||||
ClientSecret string // ENV DISCORD_CLIENT_SECRET: Discord application client secret (required)
|
||||
OAuthScopes string // Authorisation scopes for OAuth
|
||||
RedirectPath string // ENV DISCORD_REDIRECT_PATH: Path for the OAuth redirect handler (required)
|
||||
BotToken string // ENV DISCORD_BOT_TOKEN: Token for the discord bot (required)
|
||||
}
|
||||
|
||||
func ConfigFromEnv() (any, error) {
|
||||
@@ -20,6 +21,7 @@ func ConfigFromEnv() (any, error) {
|
||||
ClientSecret: env.String("DISCORD_CLIENT_SECRET", ""),
|
||||
OAuthScopes: getOAuthScopes(),
|
||||
RedirectPath: env.String("DISCORD_REDIRECT_PATH", ""),
|
||||
BotToken: env.String("DISCORD_BOT_TOKEN", ""),
|
||||
}
|
||||
|
||||
// Check required fields
|
||||
@@ -32,6 +34,9 @@ func ConfigFromEnv() (any, error) {
|
||||
if cfg.RedirectPath == "" {
|
||||
return nil, errors.New("Envar not set: DISCORD_REDIRECT_PATH")
|
||||
}
|
||||
if cfg.BotToken == "" {
|
||||
return nil, errors.New("Envar not set: DISCORD_BOT_TOKEN")
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
@@ -8,9 +8,30 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"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
|
||||
}
|
||||
|
||||
// Token represents a response from the Discord OAuth API after a successful authorization request
|
||||
type Token struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
|
||||
Reference in New Issue
Block a user