cache

package
v1.2.5-0...-67eddf4 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2025 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Telegram = "telegram"
	YouTube  = "youtube"
	Spotify  = "spotify"
	JioSaavn = "jiosaavn"
	Apple    = "apple_music"
)
View Source
const (
	Admins   = "admins"
	Everyone = "everyone"
	Auth     = "auth"
)

Variables

AdminCache is a cache for chat administrators.

View Source
var ChatCache = NewChatCacher()

ChatCache is the global chat cacher.

Functions

func ClearAdminCache

func ClearAdminCache(chatID int64)

ClearAdminCache removes cached administrator lists. If the chatID is 0, it clears the entire admin cache. Otherwise, it clears the cache for a specific chat.

func GetAdmins

func GetAdmins(client *telegram.Client, chatID int64, forceReload bool) ([]*telegram.Participant, error)

GetAdmins fetches a list of administrators from the cache or, if not present, from the Telegram API. It accepts a Telegram client, a chat ID, and a boolean to force a reload from the API, bypassing the cache. It returns a slice of telegram.Participant objects and any error encountered.

func GetChatAdmins

func GetChatAdmins(chatID int64) ([]int64, error)

GetChatAdmins retrieves the list of admin IDs for a given chat from the cache. It takes a chat ID and returns a slice of admin IDs, or an error if the admins are not found in the cache.

func GetFileDur

func GetFileDur(m *tg.NewMessage) int

GetFileDur extracts the duration of a media file from a Telegram message. It returns the duration in seconds or 0 if the media type is unsupported or has no duration.

func GetFileDuration

func GetFileDuration(filePath string) int

GetFileDuration uses ffprobe to determine the duration of a media file. It takes a file path and returns the duration in seconds, or 0 if an error occurs.

func GetUserAdmin

func GetUserAdmin(client *telegram.Client, chatID, userID int64, forceReload bool) (*telegram.Participant, error)

GetUserAdmin retrieves the participant information for a single administrator in a chat. It accepts a Telegram client, a chat ID, a user ID, and a boolean to force a reload from the API. It returns a telegram.Participant object or an error if the user is not an admin.

func SecToMin

func SecToMin(seconds int) string

SecToMin converts a duration in seconds to a formatted string (MM:SS or HH:MM:SS). It returns "0:00" for negative inputs and logs a warning.

Types

type Cache

type Cache[T any] struct {
	// contains filtered or unexported fields
}

Cache is a generic, thread-safe TTL cache that stores values with string keys.

func NewCache

func NewCache[T any](ttl time.Duration) *Cache[T]

NewCache initializes and returns a new Cache with a specified default TTL. The ttl parameter sets the default time-to-live duration for cache items.

func (*Cache[T]) Clear

func (c *Cache[T]) Clear()

Clear purges all items from the cache, making it empty.

func (*Cache[T]) Delete

func (c *Cache[T]) Delete(key string)

Delete removes an item from the cache by its key.

func (*Cache[T]) Get

func (c *Cache[T]) Get(key string) (T, bool)

Get retrieves a value from the cache by its key. It returns the cached value and true if the key exists and has not expired; otherwise, it returns the zero value and false.

func (*Cache[T]) Set

func (c *Cache[T]) Set(key string, value T)

Set adds or updates a value in the cache with the default TTL. It takes a key and a value to store.

func (*Cache[T]) SetWithTTL

func (c *Cache[T]) SetWithTTL(key string, value T, ttl time.Duration)

SetWithTTL adds or updates a value in the cache with a custom TTL, overriding the default. It takes a key, a value, and a custom TTL duration.

type CachedTrack

type CachedTrack struct {
	URL       string `json:"url"`
	Name      string `json:"name"`
	Loop      int    `json:"loop"`
	User      string `json:"user"`
	FilePath  string `json:"file_path"`
	Thumbnail string `json:"thumbnail"`
	TrackID   string `json:"track_id"`
	Duration  int    `json:"duration"`
	Lyrics    string `json:"lyrics"`
	IsVideo   bool   `json:"is_video"`
	Platform  string `json:"platform"`
}

CachedTrack defines the structure for a track that is stored in the queue. It includes metadata such as the track's URL, name, duration, and the user who requested it.

type ChatCacher

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

ChatCacher is a thread-safe cache that manages music queues for multiple chats.

func NewChatCacher

func NewChatCacher() *ChatCacher

NewChatCacher initializes and returns a new ChatCacher.

func (*ChatCacher) AddSong

func (c *ChatCacher) AddSong(chatID int64, song *CachedTrack) *CachedTrack

AddSong adds a new song to a chat's queue. If the chat does not exist, it creates a new one. It takes a chat ID and a CachedTrack to add, and returns the added track.

func (*ChatCacher) ClearChat

func (c *ChatCacher) ClearChat(chatID int64, diskClear bool)

ClearChat removes all tracks from a chat's queue and optionally deletes the files from disk.

func (*ChatCacher) GetActiveChats

func (c *ChatCacher) GetActiveChats() []int64

GetActiveChats returns a list of all chat IDs where the music player is currently active.

func (*ChatCacher) GetLoopCount

func (c *ChatCacher) GetLoopCount(chatID int64) int

GetLoopCount retrieves the loop count for the currently playing song in a chat.

func (*ChatCacher) GetPlayingTrack

func (c *ChatCacher) GetPlayingTrack(chatID int64) *CachedTrack

GetPlayingTrack retrieves the currently playing song for a given chat. It returns the current track or nil if the queue is empty.

func (*ChatCacher) GetQueue

func (c *ChatCacher) GetQueue(chatID int64) []*CachedTrack

GetQueue returns a copy of the current song queue for a chat.

func (*ChatCacher) GetQueueLength

func (c *ChatCacher) GetQueueLength(chatID int64) int

GetQueueLength returns the total number of songs in a chat's queue.

func (*ChatCacher) GetTrackIfExists

func (c *ChatCacher) GetTrackIfExists(chatID int64, trackID string) *CachedTrack

GetTrackIfExists searches for a track in the queue by its ID and returns it if found. It returns the track or nil if it does not exist in the queue.

func (*ChatCacher) GetUpcomingTrack

func (c *ChatCacher) GetUpcomingTrack(chatID int64) *CachedTrack

GetUpcomingTrack retrieves the next song in the queue for a given chat. It returns the upcoming track or nil if the queue is empty or has only one song.

func (*ChatCacher) IsActive

func (c *ChatCacher) IsActive(chatID int64) bool

IsActive checks if the music player is currently active in a specific chat. It returns true if active, otherwise false.

func (*ChatCacher) RemoveCurrentSong

func (c *ChatCacher) RemoveCurrentSong(chatID int64, diskClear bool) *CachedTrack

RemoveCurrentSong removes the currently playing song from the queue. It can also optionally clear the associated file from the disk. It returns the removed track or nil if the queue was empty.

func (*ChatCacher) RemoveTrack

func (c *ChatCacher) RemoveTrack(chatID int64, index int) bool

RemoveTrack removes a specific song from the queue by its index. It returns true if the track was successfully removed, otherwise false.

func (*ChatCacher) SetActive

func (c *ChatCacher) SetActive(chatID int64, active bool)

SetActive updates the active state of the music player for a chat.

func (*ChatCacher) SetLoopCount

func (c *ChatCacher) SetLoopCount(chatID int64, loop int) bool

SetLoopCount sets the loop count for the currently playing song. It returns true if the loop count was successfully set, otherwise false.

type ChatData

type ChatData struct {
	IsActive bool
	Queue    []*CachedTrack
}

ChatData holds the state of a chat's music queue, including whether it is active and the list of tracks.

type FFProbeFormat

type FFProbeFormat struct {
	Format struct {
		Duration string `json:"duration"`
	} `json:"format"`
}

FFProbeFormat defines the structure for parsing the format information from ffprobe's JSON output.

type Item

type Item[T any] struct {
	Value      T
	Expiration time.Time
}

Item represents an item stored in the cache, containing a value and its expiration time.

type MusicTrack

type MusicTrack struct {
	URL      string `json:"url"`
	Name     string `json:"name"`
	ID       string `json:"id"`
	Cover    string `json:"cover"`
	Duration int    `json:"duration"`
	Platform string `json:"platform"`
}

MusicTrack represents a single music track returned from a search query. It contains essential details like the track's name, ID, and cover art URL.

type PlatformTracks

type PlatformTracks struct {
	Results []MusicTrack `json:"results"`
}

PlatformTracks is a collection of music tracks, typically returned from a search operation.

type TrackInfo

type TrackInfo struct {
	URL      string `json:"url"`
	CdnURL   string `json:"cdnurl"`
	Key      string `json:"key"`
	Name     string `json:"name"`
	TC       string `json:"tc"`
	Cover    string `json:"cover"`
	Duration int    `json:"duration"`
	Lyrics   string `json:"lyrics"`
	Platform string `json:"platform"`
}

TrackInfo holds detailed information about a specific track, including its CDN URL, cover art, and lyrics.

Jump to

Keyboard shortcuts

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