github.com/diamondburned/arikawa/v2@v2.1.0/gateway/ready.go (about)

     1  package gateway
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/diamondburned/arikawa/v2/discord"
     8  )
     9  
    10  type (
    11  	// ReadyEvent is the struct for a READY event.
    12  	ReadyEvent struct {
    13  		Version int `json:"version"`
    14  
    15  		User      discord.User `json:"user"`
    16  		SessionID string       `json:"session_id"`
    17  
    18  		PrivateChannels []discord.Channel  `json:"private_channels"`
    19  		Guilds          []GuildCreateEvent `json:"guilds"`
    20  
    21  		Shard *Shard `json:"shard,omitempty"`
    22  
    23  		// Undocumented fields
    24  
    25  		UserSettings      *UserSettings          `json:"user_settings,omitempty"`
    26  		ReadStates        []ReadState            `json:"read_state,omitempty"`
    27  		UserGuildSettings []UserGuildSetting     `json:"user_guild_settings,omitempty"`
    28  		Relationships     []discord.Relationship `json:"relationships,omitempty"`
    29  		Presences         []Presence             `json:"presences,omitempty"`
    30  
    31  		FriendSuggestionCount int      `json:"friend_suggestion_count,omitempty"`
    32  		GeoOrderedRTCRegions  []string `json:"geo_ordered_rtc_regions,omitempty"`
    33  	}
    34  
    35  	// ReadState is a single ReadState entry. It is undocumented.
    36  	ReadState struct {
    37  		ChannelID        discord.ChannelID `json:"id"`
    38  		LastMessageID    discord.MessageID `json:"last_message_id"`
    39  		LastPinTimestamp discord.Timestamp `json:"last_pin_timestamp"`
    40  		MentionCount     int               `json:"mention_count"`
    41  	}
    42  
    43  	// UserSettings is the struct for (almost) all user settings. It is
    44  	// undocumented.
    45  	UserSettings struct {
    46  		ShowCurrentGame         bool  `json:"show_current_game"`
    47  		DefaultGuildsRestricted bool  `json:"default_guilds_restricted"`
    48  		InlineAttachmentMedia   bool  `json:"inline_attachment_media"`
    49  		InlineEmbedMedia        bool  `json:"inline_embed_media"`
    50  		GIFAutoPlay             bool  `json:"gif_auto_play"`
    51  		RenderEmbeds            bool  `json:"render_embeds"`
    52  		RenderReactions         bool  `json:"render_reactions"`
    53  		AnimateEmoji            bool  `json:"animate_emoji"`
    54  		AnimateStickers         int   `json:"animate_stickers"`
    55  		EnableTTSCommand        bool  `json:"enable_tts_command"`
    56  		MessageDisplayCompact   bool  `json:"message_display_compact"`
    57  		ConvertEmoticons        bool  `json:"convert_emoticons"`
    58  		ExplicitContentFilter   uint8 `json:"explicit_content_filter"` // ???
    59  		DisableGamesTab         bool  `json:"disable_games_tab"`
    60  		DeveloperMode           bool  `json:"developer_mode"`
    61  		DetectPlatformAccounts  bool  `json:"detect_platform_accounts"`
    62  		StreamNotification      bool  `json:"stream_notification_enabled"`
    63  		AccessibilityDetection  bool  `json:"allow_accessibility_detection"`
    64  		ContactSync             bool  `json:"contact_sync_enabled"`
    65  		NativePhoneIntegration  bool  `json:"native_phone_integration_enabled"`
    66  
    67  		TimezoneOffset int `json:"timezone_offset"`
    68  
    69  		Locale string `json:"locale"`
    70  		Theme  string `json:"theme"`
    71  
    72  		GuildPositions   []discord.GuildID `json:"guild_positions"`
    73  		GuildFolders     []GuildFolder     `json:"guild_folders"`
    74  		RestrictedGuilds []discord.GuildID `json:"restricted_guilds"`
    75  
    76  		FriendSourceFlags FriendSourceFlags `json:"friend_source_flags"`
    77  
    78  		Status       Status            `json:"status"`
    79  		CustomStatus *CustomUserStatus `json:"custom_status"`
    80  	}
    81  
    82  	// CustomUserStatus is the custom user status that allows setting an emoji
    83  	// and a piece of text on each user.
    84  	CustomUserStatus struct {
    85  		Text      string            `json:"text"`
    86  		ExpiresAt discord.Timestamp `json:"expires_at,omitempty"`
    87  		EmojiID   discord.EmojiID   `json:"emoji_id,string"`
    88  		EmojiName string            `json:"emoji_name"`
    89  	}
    90  
    91  	// UserGuildSetting stores the settings for a single guild. It is
    92  	// undocumented.
    93  	UserGuildSetting struct {
    94  		GuildID discord.GuildID `json:"guild_id"`
    95  
    96  		SuppressRoles    bool            `json:"suppress_roles"`
    97  		SuppressEveryone bool            `json:"suppress_everyone"`
    98  		Muted            bool            `json:"muted"`
    99  		MuteConfig       *UserMuteConfig `json:"mute_config"`
   100  
   101  		MobilePush    bool             `json:"mobile_push"`
   102  		Notifications UserNotification `json:"message_notifications"`
   103  
   104  		ChannelOverrides []UserChannelOverride `json:"channel_overrides"`
   105  	}
   106  
   107  	// A UserChannelOverride struct describes a channel settings override for a
   108  	// users guild settings.
   109  	UserChannelOverride struct {
   110  		Muted         bool              `json:"muted"`
   111  		MuteConfig    *UserMuteConfig   `json:"mute_config"`
   112  		Notifications UserNotification  `json:"message_notifications"`
   113  		ChannelID     discord.ChannelID `json:"channel_id"`
   114  	}
   115  
   116  	// UserMuteConfig seems to describe the mute settings. It belongs to the
   117  	// UserGuildSettingEntry and UserChannelOverride structs and is
   118  	// undocumented.
   119  	UserMuteConfig struct {
   120  		SelectedTimeWindow int               `json:"selected_time_window"`
   121  		EndTime            discord.Timestamp `json:"end_time"`
   122  	}
   123  
   124  	// GuildFolder holds a single folder that you see in the left guild panel.
   125  	GuildFolder struct {
   126  		Name     string            `json:"name"`
   127  		ID       GuildFolderID     `json:"id"`
   128  		GuildIDs []discord.GuildID `json:"guild_ids"`
   129  		Color    discord.Color     `json:"color"`
   130  	}
   131  
   132  	// FriendSourceFlags describes sources that friend requests could be sent
   133  	// from. It belongs to the UserSettings struct and is undocumented.
   134  	FriendSourceFlags struct {
   135  		All           bool `json:"all,omitempty"`
   136  		MutualGuilds  bool `json:"mutual_guilds,omitempty"`
   137  		MutualFriends bool `json:"mutual_friends,omitempty"`
   138  	}
   139  )
   140  
   141  // UserNotification is the notification setting for a channel or guild.
   142  type UserNotification uint8
   143  
   144  const (
   145  	AllNotifications UserNotification = iota
   146  	OnlyMentions
   147  	NoNotifications
   148  	GuildDefaults
   149  )
   150  
   151  // GuildFolderID is possibly a snowflake. It can also be 0 (null) or a low
   152  // number of unknown significance.
   153  type GuildFolderID int64
   154  
   155  func (g *GuildFolderID) UnmarshalJSON(b []byte) error {
   156  	var body = string(b)
   157  	if body == "null" {
   158  		return nil
   159  	}
   160  
   161  	body = strings.Trim(body, `"`)
   162  
   163  	u, err := strconv.ParseInt(body, 10, 64)
   164  	if err != nil {
   165  		return err
   166  	}
   167  
   168  	*g = GuildFolderID(u)
   169  	return nil
   170  }
   171  
   172  func (g GuildFolderID) MarshalJSON() ([]byte, error) {
   173  	if g == 0 {
   174  		return []byte("null"), nil
   175  	}
   176  
   177  	return []byte(strconv.FormatInt(int64(g), 10)), nil
   178  }
   179  
   180  // ReadySupplemental event structs. For now, this event is never used, and its
   181  // usage have yet been discovered.
   182  type (
   183  	// ReadySupplementalEvent is the struct for a READY_SUPPLEMENTAL event,
   184  	// which is an undocumented event.
   185  	ReadySupplementalEvent struct {
   186  		Guilds          []GuildCreateEvent     `json:"guilds"` // only have ID and VoiceStates
   187  		MergedMembers   [][]SupplementalMember `json:"merged_members"`
   188  		MergedPresences MergedPresences        `json:"merged_presences"`
   189  	}
   190  
   191  	// SupplementalMember is the struct for a member in the MergedMembers field
   192  	// of ReadySupplementalEvent. It has slight differences to discord.Member.
   193  	SupplementalMember struct {
   194  		UserID  discord.UserID   `json:"user_id"`
   195  		Nick    string           `json:"nick,omitempty"`
   196  		RoleIDs []discord.RoleID `json:"roles"`
   197  
   198  		GuildID     discord.GuildID `json:"guild_id,omitempty"`
   199  		IsPending   bool            `json:"is_pending,omitempty"`
   200  		HoistedRole discord.RoleID  `json:"hoisted_role"`
   201  
   202  		Mute bool `json:"mute"`
   203  		Deaf bool `json:"deaf"`
   204  
   205  		// Joined specifies when the user joined the guild.
   206  		Joined discord.Timestamp `json:"joined_at"`
   207  
   208  		// BoostedSince specifies when the user started boosting the guild.
   209  		BoostedSince discord.Timestamp `json:"premium_since,omitempty"`
   210  	}
   211  
   212  	// MergedPresences is the struct for presences of guilds' members and
   213  	// friends. It is undocumented.
   214  	MergedPresences struct {
   215  		Guilds  [][]SupplementalPresence `json:"guilds"`
   216  		Friends []SupplementalPresence   `json:"friends"`
   217  	}
   218  
   219  	// SupplementalPresence is a single presence for either a guild member or
   220  	// friend. It is used in MergedPresences and is undocumented.
   221  	SupplementalPresence struct {
   222  		UserID discord.UserID `json:"user_id"`
   223  
   224  		// Status is either "idle", "dnd", "online", or "offline".
   225  		Status Status `json:"status"`
   226  		// Activities are the user's current activities.
   227  		Activities []discord.Activity `json:"activities"`
   228  		// ClientStaus is the user's platform-dependent status.
   229  		ClientStatus ClientStatus `json:"client_status"`
   230  
   231  		// LastModified is only present in Friends.
   232  		LastModified discord.UnixMsTimestamp `json:"last_modified,omitempty"`
   233  	}
   234  )
   235  
   236  // ConvertSupplementalMember converts a SupplementalMember to a regular Member.
   237  func ConvertSupplementalMember(sm SupplementalMember) discord.Member {
   238  	return discord.Member{
   239  		User:         discord.User{ID: sm.UserID},
   240  		Nick:         sm.Nick,
   241  		RoleIDs:      sm.RoleIDs,
   242  		Joined:       sm.Joined,
   243  		BoostedSince: sm.BoostedSince,
   244  		Deaf:         sm.Deaf,
   245  		Mute:         sm.Mute,
   246  	}
   247  }
   248  
   249  // ConvertSupplementalPresence converts a SupplementalPresence to a regular
   250  // Presence with an empty GuildID.
   251  func ConvertSupplementalPresence(sp SupplementalPresence) Presence {
   252  	return Presence{
   253  		User:         discord.User{ID: sp.UserID},
   254  		Status:       sp.Status,
   255  		Activities:   sp.Activities,
   256  		ClientStatus: sp.ClientStatus,
   257  	}
   258  }