darchive

package
v0.0.0-...-addf1bc Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2021 License: Unlicense Imports: 12 Imported by: 0

Documentation

Overview

Package darchive contains type definitions for the darchive format as well as an encoder and decoder for the format. darchive is a gzip or zstd-compressed tar file containing JSON data.

Many of the Discord-related structs, types, and methods are taken from Arikawa's `discord` package, specifically v3.0.0-rc.2: https://pkg.go.dev/github.com/diamondburned/arikawa/v3@v3.0.0-rc.2/discord.

Index

Constants

View Source
const CurrentArchiveVersion = 4

CurrentArchiveVersion is the current archive version. Other packages in this repository will output this version.

View Source
const Epoch = 1420070400000 * time.Millisecond

Epoch is the Discord epoch constant in time.Duration (nanoseconds) since Unix epoch.

View Source
const NullSnowflake = ^Snowflake(0)

NullSnowflake gets encoded into a null. This is used for optional and nullable snowflake fields.

Variables

This section is empty.

Functions

func DurationSinceEpoch

func DurationSinceEpoch(t time.Time) time.Duration

DurationSinceEpoch returns the duration from the Discord epoch to current.

Types

type Archive

type Archive struct {
	Meta  Meta
	Guild Guild

	Users    map[uint64]User
	Messages map[Snowflake][]Message
}

Archive is a complete archive file.

func Decode

func Decode(r io.Reader, compressed bool) (d Archive, err error)

Decode decodes r into a darchive struct. If compressed is true, r is treated as a gzip-compressed tarball. If false, r is treated as an uncompressed tarball.

Decode returns (d, nil) on an io.EOF error, (d, err) on any other error.

- Files unknown to the darchive format are silently ignored. - Channels not listed in d.Meta.Channels are silently ignored.

type Attachment

type Attachment struct {
	// ID is the attachment id.
	ID Snowflake `json:"id"`
	// Filename is the name of file attached.
	Filename string `json:"filename"`
	// ContentType is the media type of file.
	ContentType string `json:"content_type,omitempty"`
	// Size is the size of file in bytes.
	Size uint64 `json:"size"`

	// URL is the source url of file.
	URL string `json:"url"`
	// Proxy is the a proxied url of file.
	Proxy string `json:"proxy_url"`

	// Height is the height of the file, if it is an image.
	Height uint `json:"height,omitempty"`
	// Width is the width of the file, if it is an image.
	Width uint `json:"width,omitempty"`
}

Attachment is a message attachment.

type Channel

type Channel struct {
	ID    Snowflake   `json:"id"`
	Name  string      `json:"name"`
	Topic string      `json:"topic"`
	Type  ChannelType `json:"type"`
	NSFW  bool        `json:"nsfw"`
}

Channel is a guild text channel.

type ChannelType

type ChannelType uint8

ChannelType ...

const (
	// GuildText is a text channel within a server.
	GuildText ChannelType = iota
	// DirectMessage is a direct message between users.
	DirectMessage
	// GuildVoice is a voice channel within a server.
	GuildVoice
	// GroupDM is a direct message between multiple users.
	GroupDM
	// GuildCategory is an organizational category that contains up to 50
	// channels.
	GuildCategory
	// GuildNews is a channel that users can follow and crosspost into their
	// own server.
	GuildNews
	// GuildStore is a channel in which game developers can sell their game on
	// Discord.
	GuildStore

	// GuildNewsThread is a temporary sub-channel within a GUILD_NEWS channel
	GuildNewsThread
	// GuildPublicThread is a temporary sub-channel within a GUILD_TEXT
	// channel.
	GuildPublicThread
	// GuildPrivateThread isa temporary sub-channel within a GUILD_TEXT channel
	// that is only viewable by those invited and those with the MANAGE_THREADS
	// permission.
	GuildPrivateThread
	// GuildStageVoice is a voice channel for hosting events with an audience.
	GuildStageVoice
)

https://discord.com/developers/docs/resources/channel#channel-object-channel-types

type CompressionType

type CompressionType uint8

CompressionType is the compression type used for encoding/decoding.

const (
	NoCompression   CompressionType = 0
	GzipCompression CompressionType = 1
)

Compression type constants

const DefaultCompression CompressionType = NoCompression

DefaultCompression is the default compression type.

type Embed

type Embed struct {
	Title       string    `json:"title,omitempty"`
	Type        EmbedType `json:"type,omitempty"`
	Description string    `json:"description,omitempty"`

	URL string `json:"url,omitempty"`

	Timestamp time.Time `json:"timestamp,omitempty"`
	Color     uint32    `json:"color,omitempty"`

	Footer    *EmbedFooter    `json:"footer,omitempty"`
	Image     *EmbedImage     `json:"image,omitempty"`
	Thumbnail *EmbedThumbnail `json:"thumbnail,omitempty"`
	Video     *EmbedVideo     `json:"video,omitempty"`
	Provider  *EmbedProvider  `json:"provider,omitempty"`
	Author    *EmbedAuthor    `json:"author,omitempty"`
	Fields    []EmbedField    `json:"fields,omitempty"`
}

Embed ...

type EmbedAuthor

type EmbedAuthor struct {
	Name      string `json:"name,omitempty"`
	URL       string `json:"url,omitempty"`
	Icon      string `json:"icon_url,omitempty"`
	ProxyIcon string `json:"proxy_icon_url,omitempty"`
}

EmbedAuthor ...

type EmbedField

type EmbedField struct {
	Name   string `json:"name"`
	Value  string `json:"value"`
	Inline bool   `json:"inline,omitempty"`
}

EmbedField ...

type EmbedFooter

type EmbedFooter struct {
	Text      string `json:"text"`
	Icon      string `json:"icon_url,omitempty"`
	ProxyIcon string `json:"proxy_icon_url,omitempty"`
}

EmbedFooter ...

type EmbedImage

type EmbedImage struct {
	URL    string `json:"url"`
	Proxy  string `json:"proxy_url"`
	Height uint   `json:"height,omitempty"`
	Width  uint   `json:"width,omitempty"`
}

EmbedImage ...

type EmbedProvider

type EmbedProvider struct {
	Name string `json:"name"`
	URL  string `json:"url"`
}

EmbedProvider ...

type EmbedThumbnail

type EmbedThumbnail struct {
	URL    string `json:"url,omitempty"`
	Proxy  string `json:"proxy_url,omitempty"`
	Height uint   `json:"height,omitempty"`
	Width  uint   `json:"width,omitempty"`
}

EmbedThumbnail ...

type EmbedType

type EmbedType string

EmbedType ...

const (
	NormalEmbed  EmbedType = "rich"
	ImageEmbed   EmbedType = "image"
	VideoEmbed   EmbedType = "video"
	GIFVEmbed    EmbedType = "gifv"
	ArticleEmbed EmbedType = "article"
	LinkEmbed    EmbedType = "link"
)

EmbedType constants

type EmbedVideo

type EmbedVideo struct {
	URL    string `json:"url"`
	Height uint   `json:"height"`
	Width  uint   `json:"width"`
}

EmbedVideo ...

type Emoji

type Emoji struct {
	// ID is the ID of the Emoji.
	// The ID will be NullSnowflake, if the Emoji is a Unicode emoji.
	ID Snowflake `json:"id"`
	// Name is the name of the emoji.
	Name string `json:"name"`

	// RoleIDs are the roles the emoji is whitelisted to.
	//
	// This field is only available for custom emojis.
	RoleIDs []Snowflake `json:"roles,omitempty"`
	// User is the user that created the emoji.
	//
	// This field is only available for custom emojis.
	User User `json:"user,omitempty"`

	// RequireColons specifies whether the emoji must be wrapped in colons.
	//
	// This field is only available for custom emojis.
	RequireColons bool `json:"require_colons,omitempty"`
	// Managed specifies whether the emoji is managed.
	//
	// This field is only available for custom emojis.
	Managed bool `json:"managed,omitempty"`
	// Animated specifies whether the emoji is animated.
	//
	// This field is only available for custom emojis.
	Animated bool `json:"animated,omitempty"`
	// Available specifies whether the emoji can be used.
	// This may be false due to loss of Server Boosts.
	//
	// This field is only available for custom emojis.
	Available bool `json:"available,omitempty"`
}

Emoji ...

func (Emoji) EmojiURL

func (e Emoji) EmojiURL() string

EmojiURL returns the URL of the emoji and auto-detects a suitable type.

This will only work for custom emojis.

func (Emoji) EmojiURLWithType

func (e Emoji) EmojiURLWithType(t ImageType) string

EmojiURLWithType returns the URL to the emoji's image.

This will only work for custom emojis.

Supported ImageTypes: PNG, GIF

type Encoder

type Encoder struct {
	sync.RWMutex

	CompressionType CompressionType
	// contains filtered or unexported fields
}

Encoder creates darchive files.

func NewEncoder

func NewEncoder() *Encoder

NewEncoder ...

func (*Encoder) AddMessages

func (enc *Encoder) AddMessages(channelID Snowflake, msgs []Message) error

AddMessages adds the given messages to the encoder. Messages should be in reverse order, i.e. oldest first.

func (*Encoder) AddUsers

func (enc *Encoder) AddUsers(users map[uint64]User) *Encoder

AddUsers adds the given users to the encoder. Existing values will be overwritten if they have the same key, so be careful! Ensuring unique keys, and ensuring that they match messages, is left to the caller.

func (*Encoder) Encode

func (enc *Encoder) Encode() (io.Reader, error)

Encode encodes the data in enc to an io.Reader.

func (*Encoder) Guild

func (enc *Encoder) Guild(g Guild) *Encoder

Guild sets the guild for this encoder.

func (*Encoder) User

func (enc *Encoder) User(u User) *Encoder

User sets the user who invoked the archiver.

type Guild

type Guild struct {
	Name     string    `json:"name"`
	ID       Snowflake `json:"id"`
	Channels []Channel `json:"channels"`
	Roles    []Role    `json:"roles"`
}

Guild is stored in guild.json

type ImageType

type ImageType string

ImageType ...

const (
	// AutoImage chooses automatically between a PNG and GIF.
	AutoImage ImageType = "auto"

	// JPEGImage is the JPEG image type.
	JPEGImage ImageType = ".jpeg"
	// PNGImage is the PNG image type.
	PNGImage ImageType = ".png"
	// WebPImage is the WebP image type.
	WebPImage ImageType = ".webp"
	// GIFImage is the GIF image type.
	GIFImage ImageType = ".gif"
)

type InvalidMessageError

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

InvalidMessageError can be returned by Encoder.AddMessages

func (*InvalidMessageError) Error

func (e *InvalidMessageError) Error() string

type Message

type Message struct {
	ID      Snowflake `json:"id,omitempty"`
	Content string    `json:"content,omitempty"`

	Timestamp       time.Time `json:"timestamp,omitempty"`
	EditedTimestamp time.Time `json:"edited_timestamp,omitempty"`

	Pinned bool `json:"pinned"`

	MentionRoles    []Snowflake `json:"mention_roles,omitempty"`
	MentionEveryone bool        `json:"mention_everyone"`
	// Reference to users.json/Archive.Users
	MentionUsers []uint64 `json:"mention_users,omitempty"`

	// Reference to users.json/Archive.Users
	Author uint64 `json:"author"`

	Attachments []Attachment `json:"attachments,omitempty"`
	Embeds      []Embed      `json:"embeds,omitempty"`
	Reactions   []Reaction   `json:"reactions,omitempty"`

	Type MessageType `json:"type"`

	WebhookID Snowflake `json:"webhook_id,omitempty"`

	Reference *MessageReference `json:"message_reference,omitempty"`
}

Message is a single message.

type MessageReference

type MessageReference struct {
	// MessageID is the id of the originating message.
	MessageID Snowflake `json:"message_id,omitempty"`
	// ChannelID is the id of the originating message's channel.
	ChannelID Snowflake `json:"channel_id,omitempty"`
	// GuildID is the id of the originating message's guild.
	GuildID Snowflake `json:"guild_id,omitempty"`
}

MessageReference ...

type MessageType

type MessageType int

MessageType ...

const (
	MessageTypeDefault                               MessageType = 0
	MessageTypeRecipientAdd                          MessageType = 1
	MessageTypeRecipientRemove                       MessageType = 2
	MessageTypeCall                                  MessageType = 3
	MessageTypeChannelNameChange                     MessageType = 4
	MessageTypeChannelIconChange                     MessageType = 5
	MessageTypeChannelPinnedMessage                  MessageType = 6
	MessageTypeGuildMemberJoin                       MessageType = 7
	MessageTypeUserPremiumGuildSubscription          MessageType = 8
	MessageTypeUserPremiumGuildSubscriptionTierOne   MessageType = 9
	MessageTypeUserPremiumGuildSubscriptionTierTwo   MessageType = 10
	MessageTypeUserPremiumGuildSubscriptionTierThree MessageType = 11
	MessageTypeChannelFollowAdd                      MessageType = 12
	MessageTypeGuildDiscoveryDisqualified            MessageType = 14
	MessageTypeGuildDiscoveryRequalified             MessageType = 15
	MessageTypeReply                                 MessageType = 19
	MessageTypeChatInputCommand                      MessageType = 20
	MessageTypeContextMenuCommand                    MessageType = 23
)

MessageType constants

type Meta

type Meta struct {
	ArchiveVersion int         `json:"archive_version"`
	Timestamp      time.Time   `json:"timestamp"`
	Channels       []Snowflake `json:"channels"`
	User           *User       `json:"user,omitempty"`
}

Meta is the structure of the meta file.

type Reaction

type Reaction struct {
	// Count is the amount of times the emoji has been used to react.
	Count int `json:"count"`
	// Me specifies whether the current user reacted using this emoji.
	Me bool `json:"me"`
	// Emoji contains emoji information.
	Emoji Emoji `json:"emoji"`
}

Reaction ...

type Role

type Role struct {
	// ID is the role id.
	ID Snowflake `json:"id"`
	// Name is the role name.
	Name string `json:"name"`
	// Position is the position of this role.
	Position int `json:"position"`
	// Color is the integer representation of hexadecimal color code.
	Color uint32 `json:"color"`
}

Role is a guild role.

type Snowflake

type Snowflake uint64

Snowflake is a snowflake ID

func NewSnowflake

func NewSnowflake(t time.Time) Snowflake

NewSnowflake creates a new Snowflake with just the time component set.

func ParseSnowflake

func ParseSnowflake(sf string) (Snowflake, error)

ParseSnowflake parses a Snowflake from a string.

func (Snowflake) IsValid

func (s Snowflake) IsValid() bool

IsValid returns whether or not the snowflake is valid.

func (Snowflake) MarshalJSON

func (s Snowflake) MarshalJSON() ([]byte, error)

MarshalJSON ...

func (Snowflake) String

func (s Snowflake) String() string

String returns the ID, or nothing if the snowflake isn't valid.

func (*Snowflake) UnmarshalJSON

func (s *Snowflake) UnmarshalJSON(v []byte) error

UnmarshalJSON ...

type User

type User struct {
	ID            Snowflake `json:"id"`
	Username      string    `json:"username"`
	Avatar        string    `json:"avatar"`
	Discriminator string    `json:"discriminator"`
	Bot           bool      `json:"bot"`

	// Empty if the user doesn't have a nickname in the guild, or if the message was sent by a webhook, or if the archive creator didn't fill it.
	Nick string `json:"nick,omitempty"`
	// List of role IDs the user has
	Roles []Snowflake `json:"roles,omitempty"`
}

User is the author of a message. Also borrows two fields from guild members.

Jump to

Keyboard shortcuts

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