utils

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LongDurationRegex = regexp.MustCompile(`^(?:(?P<years>\d+)y)? *(?:(?P<months>\d+)mo)? *(?:(?P<weeks>\d+)w)? *(?:(?P<days>\d+)d)? *(?:(?P<hours>\d+)h)? *(?:(?P<minutes>\d+)m)? *(?:(?P<seconds>\d+)s)?$`)
View Source
var MessageTemplateInfo = "The following placeholders can be used in join/leave/approval messages " +
	"and will be replaced with the appropriate values." +
	"\n" +
	"\n" +
	"**Username:** `{{User.Username}}` will show as \"Username#1234\" or  \"username\"\n" +
	"**Global name:** `{{User.GlobalName}}` will show the user's global display name\n" +
	"**Server name:** `{{User.ServerName}}` will show the user's server nickname, if any\n" +
	"**Resolved name:** `{{User.ResolvedName}}` will show the user's resolved name, which is" +
	"the server nickname if set, otherwise the global name, or username if neither is set\n" +
	"**Mention:** `{{User.Mention}}` will mention the user if it is used\n" +
	"**User ID:** `{{User.ID}}` will show the user's ID\n" +
	"**Server name:** `{{Server.Name}}` will show the server name\n" +
	"**Server ID:** `{{Server.ID}}` will show the server ID\n"

Functions

func All

func All(vs ...bool) bool

func Any

func Any(vs ...bool) bool

func CalcHalfLife

func CalcHalfLife(timeSince time.Duration, halfLifeTimeDays, weight float64) float64

func FormatFloatUpToPrec

func FormatFloatUpToPrec(num float64, prec int) string

func GetMembersIter

func GetMembersIter(r rest.Rest, guildID snowflake.ID) iter.Seq[IterResult[discord.Member]]

func HasRole

func HasRole(member discord.Member, roleID snowflake.ID) bool

func HasRolesAll

func HasRolesAll(member discord.Member, roleIDs ...snowflake.ID) bool

func HasRolesAny

func HasRolesAny(member discord.Member, roleIDs ...snowflake.ID) bool

func Iif

func Iif[T any](cond bool, t, f T) T

func LogInteraction

func LogInteraction(namespace string, interaction discord.Interaction)

func LogInteractionContext

func LogInteractionContext(namespace string, interaction discord.Interaction, ctx context.Context)

func MentionChannelOrDefault

func MentionChannelOrDefault(id *snowflake.ID, def string) string

MentionChannelOrDefault formats a mention for a channel by its ID. If the ID pointer is nil or zero, use a provided fallback value instead.

func MentionRoleOrDefault

func MentionRoleOrDefault(id *snowflake.ID, def string) string

MentionRoleOrDefault formats a mention for a role by its ID. If the ID pointer is nil or zero, use a provided fallback value instead.

func ParseLongDuration

func ParseLongDuration(s string) (time.Duration, error)

ParseLongDuration parses a string into a time.Duration. It supports the following format:

  • 1y2mo3w4d5h6m3s (year, month, week, day, hour, minute, second)

Note: an empty duration string will be a duration of 0.

func Ref

func Ref[T any](v T) *T

func RefDefault

func RefDefault[T any](v *T, def T) T

func SplitStringToLengthByLine

func SplitStringToLengthByLine(input string, length int) (output []string)

SplitStringToLengthByLine splits a string into multiple parts, so that each part is at or below the length limit. However, the split should only occur on newlines, so that most inline and single-line formatting and such are not affected by it.

Usage notes: Use a length of 2000 for a standard Discord message. If you plan to add any formatting before it, reduce this number.

func WrapRef

func WrapRef[T any](v *T) (T, bool)

Types

type DiscordTime

type DiscordTime struct {
	time.Time
}

func (DiscordTime) ToLongDate

func (t DiscordTime) ToLongDate() string

func (DiscordTime) ToLongDateTime

func (t DiscordTime) ToLongDateTime() string

func (DiscordTime) ToLongTime

func (t DiscordTime) ToLongTime() string

func (DiscordTime) ToRelative

func (t DiscordTime) ToRelative() string

func (DiscordTime) ToShortDate

func (t DiscordTime) ToShortDate() string

func (DiscordTime) ToShortDateTime

func (t DiscordTime) ToShortDateTime() string

func (DiscordTime) ToShortTime

func (t DiscordTime) ToShortTime() string

type IterResult

type IterResult[T any] struct {
	Value T
	Error error
}

type MessageTemplateData

type MessageTemplateData struct {
	User   TemplateUserData
	Server TemplateGuildData
}

func NewMessageTemplateData

func NewMessageTemplateData(user discord.Member, guild discord.Guild) MessageTemplateData

type TemplateGuildData

type TemplateGuildData struct {
	Name string
	ID   snowflake.ID
}

type TemplateUserData

type TemplateUserData struct {
	Username      string
	GlobalName    string
	ServerName    string
	ResolvedName  string
	Mention       string
	Discriminator uint8
	IsBot         bool
	ID            snowflake.ID
}

Jump to

Keyboard shortcuts

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