utils

package
v0.0.0-...-4b039c5 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: ISC Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckBalance

func CheckBalance(ctx context.Context, dbManager *database.DBManager, userID []byte, costUSD float64, debug bool, billingEnabled bool) (requiredDCR float64, currentBalanceDCR float64, err error)

CheckBalance checks if a user has sufficient balance for a given cost in USD, without deducting. It returns the required DCR amount, the current balance in DCR, and potentially an ErrInsufficientBalance or other critical error. If billingEnabled is false, it returns success (nil error).

func DeductBalance

func DeductBalance(ctx context.Context, dbManager *database.DBManager, userID []byte, costUSD float64, debug bool, billingEnabled bool) (chargedDCR float64, newBalanceDCR float64, err error)

DeductBalance deducts the specified cost in USD from the user's balance. It assumes the balance check has already passed IF billing is enabled. Returns the amount charged in DCR, the new balance in DCR, and any error encountered. If billingEnabled is false, it returns zero charged and the current balance without hitting the DB.

func ExtractAudioNoteData

func ExtractAudioNoteData(message string) (string, error)

ExtractAudioNoteData extracts the base64 audio data from an audio note message

func FormatBalanceMessage

func FormatBalanceMessage(balanceDCR float64, dcrPrice float64) string

FormatBalanceMessage formats a balance message with DCR and USD values

func FormatBillingConfirmation

func FormatBillingConfirmation(taskName string, billingEnabled bool, billingAttempted bool, billingSucceeded bool, chargedDCR float64, chargedUSD float64, finalBalanceDCR float64) string

FormatBillingConfirmation builds the complete billing confirmation string appended to a task-completion message. It handles all billing outcome cases:

  • billing succeeded: show charged DCR, USD, and new balance
  • billing attempted but failed: show warning with balance and content type
  • billing not attempted (but enabled): show "no charge" with balance
  • billing disabled: show "billing is disabled"

taskName is the content type shown in the failure message (e.g. "video", "results", "audio").

func FormatBillingMessage

func FormatBillingMessage(chargedDCR float64, chargedUSD float64, remainingBalance float64) string

FormatBillingMessage formats a billing message with charged amount and remaining balance

func FormatCommandHelpHeader

func FormatCommandHelpHeader(commandName string, model faladapter.AppModel, userID zkidentity.ShortID, dbManager braibottypes.DBManagerInterface) string

FormatCommandHelpHeader generates the standard header for command help messages.

func FormatDebugAfterDeduction

func FormatDebugAfterDeduction(newBalanceAtoms int64) string

FormatDebugAfterDeduction formats debug information after balance deduction

func FormatDebugBalanceInfo

func FormatDebugBalanceInfo(userID string, balanceAtoms int64, costUSD float64, costDCR float64, costAtoms int64) string

FormatDebugBalanceInfo formats debug information about a user's balance

func FormatDebugCommandInfo

func FormatDebugCommandInfo(commandName string, userID string, balanceAtoms int64, costUSD float64, costDCR float64, costAtoms int64) string

FormatDebugCommandInfo formats debug information for a command

func FormatEmbeddedAudioMessage

func FormatEmbeddedAudioMessage(modelName string, base64Data string) string

FormatEmbeddedAudioMessage formats a message with embedded audio

func FormatEmbeddedImageMessage

func FormatEmbeddedImageMessage(altText string, contentType string, base64Data string) string

FormatEmbeddedImageMessage formats a message with an embedded image

func FormatInsufficientBalanceMessage

func FormatInsufficientBalanceMessage(requiredDCR float64, currentDCR float64) string

FormatInsufficientBalanceMessage formats a message for insufficient balance

func FormatInsufficientBalanceMessageWithUSD

func FormatInsufficientBalanceMessageWithUSD(requiredDCR float64, currentDCR float64, usdAmount float64) string

FormatInsufficientBalanceMessageWithUSD formats a message for insufficient balance with USD value

func FormatModelHelpMessage

func FormatModelHelpMessage(modelName string, description string, priceUSD float64, priceDCR float64, balanceDCR float64, helpDoc string) string

FormatModelHelpMessage formats a help message for a model

func FormatModelListMessage

func FormatModelListMessage(commandName string, models map[string]interface{}) string

FormatModelListMessage formats a message listing available models

func FormatThousands

func FormatThousands(n float64) string

FormatThousands formats a float64 with commas as thousands separators, rounded to the nearest integer.

func FormatUSDThousands

func FormatUSDThousands(n float64) string

FormatUSDThousands formats a float64 as USD with thousands separators and 2 decimal places.

func GetBTCPrice

func GetBTCPrice() (float64, error)

GetBTCPrice gets the current BTC price in USD from CoinGecko

func GetDCRPrice

func GetDCRPrice() (float64, float64, error)

GetDCRPrice gets the current DCR price in USD and BTC from CoinGecko

func GetUserIDString

func GetUserIDString(uid []byte) string

GetUserIDString converts a user's UID bytes to a string ID

func HandleServiceResultOrError

func HandleServiceResultOrError(ctx context.Context, bot *kit.Bot, msgCtx braibottypes.MessageContext, commandName string, result interface{}, err error) error

HandleServiceResultOrError encapsulates common error handling for service calls. It checks for direct errors (like context cancellation, insufficient balance) and then checks the success status within the result. `result` is expected to be a pointer to a struct with `Success bool` and `Error error` fields. Returns nil if the error was handled (PM sent/logged appropriately), otherwise returns the error to propagate.

func IsAudioNote

func IsAudioNote(message string) bool

IsAudioNote checks if a message contains an audio note embed

func SendFileToUser

func SendFileToUser(ctx context.Context, bot *kit.Bot, userNick string, fileURL string, filePrefix string, contentType string) error

SendFileToUser downloads a file from a URL and sends it to a user. It creates a temporary file, downloads the content, and sends it using the bot. The temporary file is automatically cleaned up after sending.

func SendToUser

func SendToUser(ctx context.Context, bot *kit.Bot, isPM bool, nick, gc, msg string) error

SendToUser sends a message to either a PM or a group chat based on isPM.

func USDToDCR

func USDToDCR(usdAmount float64) (float64, error)

USDToDCR converts a USD amount to DCR using current exchange rate

Types

type ErrInsufficientBalance

type ErrInsufficientBalance struct {
	Message string
}

ErrInsufficientBalance is a custom error type for insufficient funds.

func (*ErrInsufficientBalance) Error

func (e *ErrInsufficientBalance) Error() string

Error implements the error interface.

type ServiceResult

type ServiceResult interface {
	IsSuccess() bool
	GetError() error
}

ServiceResult defines an interface for common fields in service results. Assumes result structs have `Success bool` and `Error error` fields or methods.

Jump to

Keyboard shortcuts

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