reddit

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParsePostID

func ParsePostID(s string) (string, error)

func ParseThingID

func ParseThingID(s string) (string, error)

ParseThingID resolves what a reply should attach to. A comment permalink wins over the post it sits under, since that is what the link points at.

Types

type APIError

type APIError struct {
	Status int
	Body   string
}

func (*APIError) Error

func (e *APIError) Error() string

type Client

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

func New

func New(sess *session.Session, reauth Reauth) (*Client, error)

func (*Client) Comments

func (c *Client) Comments(ctx context.Context, idOrURL string, o CommentsOptions) (Result[Thread], error)

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, target string) (Result[Deletion], error)

Delete removes your own post or comment. Reddit reports success even when the item was never yours, so this confirms the outcome before returning.

func (*Client) Edit

func (c *Client) Edit(ctx context.Context, target, text string) (Result[Edited], error)

Edit rewrites the body of your own text post or comment. Link posts have no body to edit, and Reddit refuses those.

func (*Client) FetchHome

func (c *Client) FetchHome(ctx context.Context) (int, error)

func (*Client) Get

func (c *Client) Get(ctx context.Context, path string, query url.Values) ([]byte, error)

func (*Client) GetJSON

func (c *Client) GetJSON(ctx context.Context, path string, query url.Values, v any) (json.RawMessage, error)

func (*Client) Logout

func (c *Client) Logout(ctx context.Context) error

func (*Client) Me

func (c *Client) Me(ctx context.Context) (Result[User], error)

func (*Client) MoreChildren

func (c *Client) MoreChildren(ctx context.Context, postIDOrURL string, ids []string, sort string) (Result[[]Comment], error)

func (*Client) Post

func (c *Client) Post(ctx context.Context, idOrURL string) (Result[Post], error)

func (*Client) PostForm

func (c *Client) PostForm(ctx context.Context, path string, form url.Values) ([]byte, error)

PostForm submits a form-encoded body. Reddit answers write endpoints with a json envelope that carries its own error list, so callers check that too.

func (*Client) Reply

func (c *Client) Reply(ctx context.Context, parent, text string) (Result[Comment], error)

Reply comments on a post or replies to another comment. parent accepts a fullname, a post id, or a post or comment URL.

func (*Client) Saved

func (c *Client) Saved(ctx context.Context, o ListOptions) (Result[Page[Post]], error)

func (*Client) Search

func (c *Client) Search(ctx context.Context, query string, o SearchOptions) (Result[Page[Post]], error)

func (*Client) Session

func (c *Client) Session() *session.Session

func (*Client) Submit

func (c *Client) Submit(ctx context.Context, o SubmitOptions) (Result[Submission], error)

Submit creates a post. Exactly one of Text or URL must be set.

func (*Client) SubredditInfo

func (c *Client) SubredditInfo(ctx context.Context, sub string) (Result[Subreddit], error)

func (*Client) SubredditPosts

func (c *Client) SubredditPosts(ctx context.Context, sub string, o ListOptions) (Result[Page[Post]], error)

func (*Client) User

func (c *Client) User(ctx context.Context, name string) (Result[User], error)

func (*Client) UserComments

func (c *Client) UserComments(ctx context.Context, name string, o ListOptions) (Result[Page[Comment]], error)

func (*Client) UserPosts

func (c *Client) UserPosts(ctx context.Context, name string, o ListOptions) (Result[Page[Post]], error)

func (*Client) Username

func (c *Client) Username(ctx context.Context) (string, error)

type Comment

type Comment struct {
	ID            string    `json:"id"`
	Fullname      string    `json:"fullname"`
	Author        string    `json:"author"`
	Body          string    `json:"body"`
	Score         int       `json:"score"`
	ScoreHidden   bool      `json:"score_hidden,omitempty"`
	Created       string    `json:"created"`
	Depth         int       `json:"depth"`
	ParentID      string    `json:"parent_id"`
	LinkID        string    `json:"link_id,omitempty"`
	Subreddit     string    `json:"subreddit,omitempty"`
	LinkTitle     string    `json:"link_title,omitempty"`
	Permalink     string    `json:"permalink"`
	IsOP          bool      `json:"is_op,omitempty"`
	Stickied      bool      `json:"stickied,omitempty"`
	Distinguished string    `json:"distinguished,omitempty"`
	Edited        bool      `json:"edited,omitempty"`
	AuthorFlair   string    `json:"author_flair,omitempty"`
	MyVote        string    `json:"my_vote,omitempty"`
	Saved         bool      `json:"saved,omitempty"`
	Replies       []Comment `json:"replies,omitempty"`
	MoreReplies   int       `json:"more_replies,omitempty"`
	MoreIDs       []string  `json:"more_ids,omitempty"`
}

type CommentsOptions

type CommentsOptions struct {
	Sort    string
	Limit   int
	Depth   int
	Comment string
	Context int
}

type Deletion

type Deletion struct {
	Fullname string `json:"fullname"`
	Deleted  bool   `json:"deleted"`
	Note     string `json:"note,omitempty"`
}

Deletion reports what happened, including whether the item actually went away. Reddit answers a delete the same way whether or not the item was yours, so this is checked separately.

type Edited

type Edited struct {
	Kind    string   `json:"kind"`
	Post    *Post    `json:"post,omitempty"`
	Comment *Comment `json:"comment,omitempty"`
}

Edited is whatever the edit changed. Reddit returns the updated thing, and which field is set depends on whether it was a post or a comment.

type ListOptions

type ListOptions struct {
	Sort  string
	Time  string
	Limit int
	After string
}

type More

type More struct {
	ParentID string   `json:"parent_id"`
	Count    int      `json:"count"`
	IDs      []string `json:"ids"`
}

type Page

type Page[T any] struct {
	Items []T    `json:"items"`
	After string `json:"after,omitempty"`
}

type Post

type Post struct {
	ID            string  `json:"id"`
	Fullname      string  `json:"fullname"`
	Title         string  `json:"title"`
	Author        string  `json:"author"`
	Subreddit     string  `json:"subreddit"`
	Score         int     `json:"score"`
	UpvoteRatio   float64 `json:"upvote_ratio"`
	NumComments   int     `json:"num_comments"`
	Created       string  `json:"created"`
	URL           string  `json:"url"`
	Permalink     string  `json:"permalink"`
	Selftext      string  `json:"selftext,omitempty"`
	IsSelf        bool    `json:"is_self"`
	NSFW          bool    `json:"nsfw"`
	Spoiler       bool    `json:"spoiler,omitempty"`
	Stickied      bool    `json:"stickied,omitempty"`
	Locked        bool    `json:"locked,omitempty"`
	Archived      bool    `json:"archived,omitempty"`
	Domain        string  `json:"domain,omitempty"`
	Flair         string  `json:"flair,omitempty"`
	AuthorFlair   string  `json:"author_flair,omitempty"`
	Distinguished string  `json:"distinguished,omitempty"`
	PostHint      string  `json:"post_hint,omitempty"`
	Edited        bool    `json:"edited,omitempty"`
	MyVote        string  `json:"my_vote,omitempty"`
	Saved         bool    `json:"saved,omitempty"`
	Crosspost     string  `json:"crosspost_parent,omitempty"`
}

type Reauth

type Reauth func(ctx context.Context) (*session.Session, error)

type Result

type Result[T any] struct {
	Data T
	Raw  json.RawMessage
}

type SearchOptions

type SearchOptions struct {
	Subreddit string
	Sort      string
	Time      string
	Limit     int
	After     string
	NSFW      bool
}

type Submission

type Submission struct {
	ID        string `json:"id"`
	Fullname  string `json:"fullname"`
	URL       string `json:"url"`
	Subreddit string `json:"subreddit"`
	Title     string `json:"title"`
}

Submission is a newly created post.

type SubmitOptions

type SubmitOptions struct {
	Subreddit string
	Title     string
	Text      string
	URL       string
	NSFW      bool
	Spoiler   bool
	FlairID   string
	FlairText string
	// NoReplyNotifications turns off inbox replies for the new post.
	NoReplyNotifications bool
}

type Subreddit

type Subreddit struct {
	ID                string `json:"id"`
	Name              string `json:"name"`
	Title             string `json:"title"`
	PublicDescription string `json:"public_description,omitempty"`
	Description       string `json:"description,omitempty"`
	Subscribers       int    `json:"subscribers"`
	ActiveUsers       int    `json:"active_users"`
	Created           string `json:"created"`
	NSFW              bool   `json:"nsfw"`
	Type              string `json:"type"`
	URL               string `json:"url"`
	Lang              string `json:"lang,omitempty"`
	Subscribed        bool   `json:"subscribed"`
	Moderator         bool   `json:"moderator,omitempty"`
	Banned            bool   `json:"banned,omitempty"`
	Icon              string `json:"icon,omitempty"`
}

type Thread

type Thread struct {
	Post     Post      `json:"post"`
	Comments []Comment `json:"comments"`
	More     *More     `json:"more,omitempty"`
}

type User

type User struct {
	ID               string `json:"id"`
	Name             string `json:"name"`
	Created          string `json:"created"`
	LinkKarma        int    `json:"link_karma"`
	CommentKarma     int    `json:"comment_karma"`
	TotalKarma       int    `json:"total_karma"`
	AwardeeKarma     int    `json:"awardee_karma,omitempty"`
	AwarderKarma     int    `json:"awarder_karma,omitempty"`
	Description      string `json:"description,omitempty"`
	DisplayTitle     string `json:"display_title,omitempty"`
	Followers        int    `json:"followers,omitempty"`
	IconImg          string `json:"icon_img,omitempty"`
	IsGold           bool   `json:"is_gold,omitempty"`
	IsMod            bool   `json:"is_mod,omitempty"`
	IsEmployee       bool   `json:"is_employee,omitempty"`
	Verified         bool   `json:"verified,omitempty"`
	HasVerifiedEmail bool   `json:"has_verified_email,omitempty"`
	NSFW             bool   `json:"nsfw,omitempty"`
	IsFriend         bool   `json:"is_friend,omitempty"`
	IsBlocked        bool   `json:"is_blocked,omitempty"`
	IsSuspended      bool   `json:"is_suspended,omitempty"`
	InboxCount       int    `json:"inbox_count,omitempty"`
	HasMail          bool   `json:"has_mail,omitempty"`
}

type WriteError

type WriteError struct {
	Errors [][]string
}

WriteError is Reddit rejecting a write on its own terms, such as a rate limit or a missing subreddit. The HTTP status is still 200 in that case.

func (*WriteError) Code

func (e *WriteError) Code() string

Code returns the first error code Reddit gave, such as RATELIMIT.

func (*WriteError) Error

func (e *WriteError) Error() string

Jump to

Keyboard shortcuts

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