Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions cmd/baton-discord/config.go

This file was deleted.

11 changes: 3 additions & 8 deletions cmd/baton-discord/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"github.com/conductorone/baton-sdk/pkg/connectorbuilder"
"github.com/conductorone/baton-sdk/pkg/types"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"

cfg "github.com/ConductorOne/baton-discord/pkg/config"
"github.com/ConductorOne/baton-discord/pkg/connector"
configschema "github.com/conductorone/baton-sdk/pkg/config"
)
Expand All @@ -21,14 +21,13 @@ var version = "dev"
func main() {
ctx := context.Background()

_, cmd, err := configschema.DefineConfiguration(ctx, "baton-discord", getConnector, configuration)
_, cmd, err := configschema.DefineConfiguration(ctx, "baton-discord", getConnector, cfg.Configuration)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

cmd.Version = version
cmdFlags(cmd)
err = cmd.Execute()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
Expand All @@ -39,7 +38,7 @@ func main() {
func getConnector(ctx context.Context, v *viper.Viper) (types.ConnectorServer, error) {
l := ctxzap.Extract(ctx)

cb, err := connector.New(ctx, v.GetString("token"))
cb, err := connector.New(ctx, v.GetString("token"), v.GetString("base-url"))
if err != nil {
l.Error("error creating connector", zap.Error(err))
return nil, err
Expand All @@ -53,7 +52,3 @@ func getConnector(ctx context.Context, v *viper.Viper) (types.ConnectorServer, e

return c, nil
}

func cmdFlags(cmd *cobra.Command) {
cmd.PersistentFlags().String("token", "", "The discord bot token. ($BATON_TOKEN)")
}
84 changes: 84 additions & 0 deletions pkg/config/conf.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package config

//go:generate go run ./gen

import (
"github.com/conductorone/baton-sdk/pkg/field"
)

var (
TokenField = field.StringField(
"token",
field.WithDisplayName("Bot Token"),
field.WithDescription("Bot token used to authenticate to Discord."),
field.WithRequired(true),
field.WithIsSecret(true),
)
BaseURLField = field.StringField(
"base-url",
field.WithDisplayName("Base URL"),
field.WithDescription("Override the Discord API URL (for testing)"),
field.WithHidden(true),
field.WithExportTarget(field.ExportTargetCLIOnly),
)

// ConfigurationFields defines the external configuration required for the
// connector to run.
ConfigurationFields = []field.SchemaField{
TokenField,
BaseURLField,
}

Configuration = field.NewConfiguration(
ConfigurationFields,
field.WithConnectorDisplayName("Discord"),
)
)
10 changes: 10 additions & 0 deletions pkg/config/gen/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
cfg "github.com/ConductorOne/baton-discord/pkg/config"
"github.com/conductorone/baton-sdk/pkg/config"
)

func main() {
config.Generate("discord", cfg.Configuration)
}
34 changes: 33 additions & 1 deletion pkg/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"strings"

"github.com/bwmarrin/discordgo"

Expand All @@ -12,6 +13,9 @@ import (
"github.com/conductorone/baton-sdk/pkg/connectorbuilder"
)

// DefaultBaseURL is the default Discord API base URL.
const DefaultBaseURL = "https://discord.com/"

type Connector struct {
conn *discordgo.Session
}
Expand Down Expand Up @@ -47,7 +51,35 @@ func (d *Connector) Validate(ctx context.Context) (annotations.Annotations, erro
}

// New returns a new instance of the connector.
func New(ctx context.Context, token string) (*Connector, error) {
func New(ctx context.Context, token string, baseURL string) (*Connector, error) {
// Set the base URL if provided (must be done before creating the session)
if baseURL != "" {
// Ensure the base URL ends with a slash
if !strings.HasSuffix(baseURL, "/") {
baseURL += "/"
}
discordgo.EndpointDiscord = baseURL
// Rebuild the API endpoint with the new base URL
discordgo.EndpointAPI = discordgo.EndpointDiscord + "api/v" + discordgo.APIVersion + "/"
// Rebuild dependent endpoints
discordgo.EndpointGuilds = discordgo.EndpointAPI + "guilds/"
discordgo.EndpointChannels = discordgo.EndpointAPI + "channels/"
discordgo.EndpointUsers = discordgo.EndpointAPI + "users/"
discordgo.EndpointGateway = discordgo.EndpointAPI + "gateway"
discordgo.EndpointGatewayBot = discordgo.EndpointGateway + "/bot"
discordgo.EndpointWebhooks = discordgo.EndpointAPI + "webhooks/"
discordgo.EndpointStickers = discordgo.EndpointAPI + "stickers/"
discordgo.EndpointStageInstances = discordgo.EndpointAPI + "stage-instances"
discordgo.EndpointSKUs = discordgo.EndpointAPI + "skus"
discordgo.EndpointVoice = discordgo.EndpointAPI + "/voice/"
discordgo.EndpointVoiceRegions = discordgo.EndpointVoice + "regions"
discordgo.EndpointNitroStickersPacks = discordgo.EndpointAPI + "/sticker-packs"
discordgo.EndpointGuildCreate = discordgo.EndpointAPI + "guilds"
discordgo.EndpointApplications = discordgo.EndpointAPI + "applications"
discordgo.EndpointOAuth2 = discordgo.EndpointAPI + "oauth2/"
discordgo.EndpointOAuth2Applications = discordgo.EndpointOAuth2 + "applications"
}

dcConn, err := discordgo.New(fmt.Sprintf("Bot %s", token))
if err != nil {
return nil, err
Expand Down
Loading