redditbot

package
v2.0.0-beta.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2023 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const RedditColor = 0xff581a

Variables

View Source
var (
	ErrSubredditNotFound  = errors.New("subreddit not found")
	ErrSubredditForbidden = errors.New("subreddit forbidden")
)
View Source
var Commands = []discord.ApplicationCommandCreate{
	discord.SlashCommandCreate{
		Name:        "reddit",
		Description: "manage your subscribed subreddits",
		Options: []discord.ApplicationCommandOption{
			discord.ApplicationCommandOptionSubCommand{
				Name:        "add",
				Description: "add a subreddit to your subscriptions",
				Options: []discord.ApplicationCommandOption{
					discord.ApplicationCommandOptionString{
						Name:        "subreddit",
						Description: "the subreddit to add",
						Required:    true,
					},
					discord.ApplicationCommandOptionString{
						Name:        "type",
						Description: "the type of posts to send",
						Required:    false,
						Choices:     typeChoices,
					},
					discord.ApplicationCommandOptionString{
						Name:        "format-type",
						Description: "how to format the subreddit posts",
						Required:    false,
						Choices:     formatTypeChoices,
					},
				},
			},
			discord.ApplicationCommandOptionSubCommand{
				Name:        "update",
				Description: "update a subreddit in your subscriptions",
				Options: []discord.ApplicationCommandOption{
					discord.ApplicationCommandOptionString{
						Name:        "subreddit",
						Description: "the subreddit to update",
						Required:    true,
					},
					discord.ApplicationCommandOptionString{
						Name:        "type",
						Description: "the type of posts to send",
						Required:    false,
						Choices:     typeChoices,
					},
					discord.ApplicationCommandOptionString{
						Name:        "format-type",
						Description: "how to format the subreddit posts",
						Required:    false,
						Choices:     formatTypeChoices,
					},
				},
			},
			discord.ApplicationCommandOptionSubCommand{
				Name:        "remove",
				Description: "remove a subreddit from your subscriptions",
				Options: []discord.ApplicationCommandOption{
					discord.ApplicationCommandOptionString{
						Name:        "subreddit",
						Description: "the subreddit to remove",
						Required:    true,
					},
				},
			},
			discord.ApplicationCommandOptionSubCommand{
				Name:        "list",
				Description: "list your subscribed subreddits",
				Options: []discord.ApplicationCommandOption{
					discord.ApplicationCommandOptionChannel{
						Name:        "channel",
						Description: "the channel to list the subreddits for",
						Required:    false,
					},
				},
			},
		},
		DefaultMemberPermissions: json.NewNullablePtr(discord.PermissionManageGuild),
	},
	discord.SlashCommandCreate{
		Name:        "info",
		Description: "get info about me",
	},
}
View Source
var (
	ErrSubscriptionNotFound = errors.New("subscription not found")
)

Functions

This section is empty.

Types

type Bot

type Bot struct {
	Cfg           Config
	RedditIcon    []byte
	Client        bot.Client
	Reddit        *Reddit
	DB            *DB
	Server        *http.Server
	MetricsServer *http.Server
	DiscordConfig *oauth2.Config
	Rand          *rand.Rand

	States map[string]SetupState
}

func (*Bot) AddSubscription

func (b *Bot) AddSubscription(sub Subscription) error

func (*Bot) Close

func (b *Bot) Close()

func (*Bot) ListenAndServe

func (b *Bot) ListenAndServe()

func (*Bot) ListenAndServeMetrics

func (b *Bot) ListenAndServeMetrics()

func (*Bot) ListenSubreddits

func (b *Bot) ListenSubreddits()

func (*Bot) OnApplicationCommand

func (b *Bot) OnApplicationCommand(event *events.ApplicationCommandInteractionCreate)

func (*Bot) OnDiscordCallback

func (b *Bot) OnDiscordCallback(w http.ResponseWriter, r *http.Request)

func (*Bot) OnInfo

func (*Bot) RemoveSubscription

func (b *Bot) RemoveSubscription(webhookID snowflake.ID, webhookToken string, err error) error

func (*Bot) RemoveSubscriptionByGuildSubreddit

func (b *Bot) RemoveSubscriptionByGuildSubreddit(guildID snowflake.ID, subreddit string, reason string) error

type Config

type Config struct {
	TestMode bool           `koanf:"test_mode"`
	Log      LogConfig      `koanf:"log"`
	Server   ServerConfig   `koanf:"server"`
	Discord  DiscordConfig  `koanf:"discord"`
	Reddit   RedditConfig   `koanf:"reddit"`
	Database DatabaseConfig `koanf:"database"`
	Metrics  MetricsConfig  `koanf:"metrics"`
}

func ReadConfig

func ReadConfig() (Config, error)

func (Config) String

func (c Config) String() string

func (Config) Validate

func (c Config) Validate() error

type DB

type DB struct {
	// contains filtered or unexported fields
}

func NewDB

func NewDB(cfg DatabaseConfig, schema string) (*DB, error)

func (*DB) AddSubscription

func (d *DB) AddSubscription(sub Subscription) error

func (*DB) Close

func (d *DB) Close() error

func (*DB) GetAllSubscriptionIDs

func (d *DB) GetAllSubscriptionIDs() ([]snowflake.ID, error)

func (*DB) GetSubscription

func (d *DB) GetSubscription(webhookID snowflake.ID) (*Subscription, error)

func (*DB) GetSubscriptionsByChannel

func (d *DB) GetSubscriptionsByChannel(channelID snowflake.ID) ([]Subscription, error)

func (*DB) GetSubscriptionsByGuild

func (d *DB) GetSubscriptionsByGuild(guildID snowflake.ID) ([]Subscription, error)

func (*DB) GetSubscriptionsByGuildSubreddit

func (d *DB) GetSubscriptionsByGuildSubreddit(guildID snowflake.ID, subreddit string) (*Subscription, error)

func (*DB) HasSubscription

func (d *DB) HasSubscription(webhookID snowflake.ID) (bool, error)

func (*DB) HasSubscriptionByGuildSubreddit

func (d *DB) HasSubscriptionByGuildSubreddit(guildID snowflake.ID, subreddit string) (bool, error)

func (*DB) RemoveSubscription

func (d *DB) RemoveSubscription(webhookID snowflake.ID) (*Subscription, error)

func (*DB) RemoveSubscriptionByGuildSubreddit

func (d *DB) RemoveSubscriptionByGuildSubreddit(guildID snowflake.ID, subreddit string) (*Subscription, error)

func (*DB) UpdateSubscription

func (d *DB) UpdateSubscription(webhookID snowflake.ID, postType string, formatType FormatType) error

func (*DB) UpdateSubscriptionLastPost

func (d *DB) UpdateSubscriptionLastPost(webhookID snowflake.ID, lastPost time.Time) error

type DatabaseConfig

type DatabaseConfig struct {
	Type           DatabaseType   `cfg:"type"`
	PostgresConfig PostgresConfig `koanf:"postgres"`
	SQLite         SQLiteConfig   `koanf:"sqlite"`
}

func (DatabaseConfig) String

func (c DatabaseConfig) String() string

func (DatabaseConfig) Validate

func (c DatabaseConfig) Validate() error

type DatabaseType

type DatabaseType string
const (
	DatabaseTypePostgres DatabaseType = "postgres"
	DatabaseTypeSQLite   DatabaseType = "sqlite"
)

type DiscordConfig

type DiscordConfig struct {
	Token        string `koanf:"token"`
	ClientSecret string `koanf:"client_secret"`
	SyncCommands bool   `koanf:"sync_commands"`
}

func (DiscordConfig) String

func (c DiscordConfig) String() string

func (DiscordConfig) Validate

func (c DiscordConfig) Validate() error

type FormatType

type FormatType string
const (
	FormatTypeEmbed FormatType = "embed"
	FormatTypeText  FormatType = "text"
)

type LogConfig

type LogConfig struct {
	Level     log.Level `koanf:"level"`
	AddSource bool      `koanf:"add_source"`
}

func (LogConfig) Flags

func (c LogConfig) Flags() int

func (LogConfig) String

func (c LogConfig) String() string

func (LogConfig) Validate

func (c LogConfig) Validate() error

type MetricsConfig

type MetricsConfig struct {
	Enabled    bool   `koanf:"enabled"`
	ListenAddr string `koanf:"listen_addr"`
	Endpoint   string `koanf:"endpoint"`
}

func (MetricsConfig) String

func (c MetricsConfig) String() string

func (MetricsConfig) Validate

func (c MetricsConfig) Validate() error

type PostgresConfig

type PostgresConfig struct {
	Host     string `koanf:"host"`
	Port     int    `koanf:"port"`
	Username string `koanf:"username"`
	Password string `koanf:"password"`
	Database string `koanf:"database"`
	SSLMode  string `koanf:"ssl_mode"`
}

func (PostgresConfig) DataSourceName

func (c PostgresConfig) DataSourceName() string

func (PostgresConfig) String

func (c PostgresConfig) String() string

func (PostgresConfig) Validate

func (c PostgresConfig) Validate() error

type Reddit

type Reddit struct {
	// contains filtered or unexported fields
}

func NewReddit

func NewReddit(cfg RedditConfig) (*Reddit, error)

func (*Reddit) GetPostsUntil

func (r *Reddit) GetPostsUntil(subreddit string, fetchType string, until time.Time) ([]RedditPost, error)

func (*Reddit) GetSubredditIcon

func (r *Reddit) GetSubredditIcon(subreddit string) (string, error)

type RedditAbout

type RedditAbout struct {
	CommunityIcon string `json:"community_icon"`
}

type RedditConfig

type RedditConfig struct {
	ClientID          string `koanf:"client_id"`
	ClientSecret      string `koanf:"client_secret"`
	RequestsPerMinute int    `koanf:"requests_per_minute"`
}

func (RedditConfig) String

func (c RedditConfig) String() string

func (RedditConfig) Validate

func (c RedditConfig) Validate() error

type RedditListing

type RedditListing[T any] struct {
	Before   string `json:"before"`
	Children []struct {
		Data T `json:"data"`
	} `json:"children"`
}

type RedditPost

type RedditPost struct {
	Selftext              string          `json:"selftext"`
	AuthorFullname        string          `json:"author_fullname"`
	Title                 string          `json:"title"`
	SubredditNamePrefixed string          `json:"subreddit_name_prefixed"`
	ID                    string          `json:"id"`
	Name                  string          `json:"name"`
	Author                string          `json:"author"`
	URL                   string          `json:"url"`
	Permalink             string          `json:"permalink"`
	CreatedUtc            float64         `json:"created_utc"`
	SrDetail              SubredditDetail `json:"sr_detail"`
}

type RedditResponse

type RedditResponse[T any] struct {
	Kind string `json:"kind"`
	Data T      `json:"data"`
}

type SQLiteConfig

type SQLiteConfig struct {
	Path string `koanf:"path"`
}

func (SQLiteConfig) DataSourceName

func (c SQLiteConfig) DataSourceName() string

func (SQLiteConfig) String

func (c SQLiteConfig) String() string

func (SQLiteConfig) Validate

func (c SQLiteConfig) Validate() error

type ServerConfig

type ServerConfig struct {
	Enabled     bool   `koanf:"enabled"`
	ListenAddr  string `koanf:"listen_addr"`
	Endpoint    string `koanf:"endpoint"`
	RedirectURL string `koanf:"redirect_url"`
}

func (ServerConfig) String

func (c ServerConfig) String() string

func (ServerConfig) Validate

func (c ServerConfig) Validate() error

type SetupState

type SetupState struct {
	Subreddit   string
	PostType    string
	FormatType  FormatType
	Interaction discord.ApplicationCommandInteraction
}

type SubredditDetail

type SubredditDetail struct {
	CommunityIcon string `json:"community_icon"`
}

type Subscription

type Subscription struct {
	Subreddit    string       `db:"subreddit"`
	Type         string       `db:"type"`
	FormatType   FormatType   `db:"format_type"`
	GuildID      snowflake.ID `db:"guild_id"`
	ChannelID    snowflake.ID `db:"channel_id"`
	WebhookID    snowflake.ID `db:"webhook_id"`
	WebhookToken string       `db:"webhook_token"`
	LastPost     time.Time    `db:"last_post"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL