utils

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const EPOCH = 1420070400000

EPOCH is Discord's epoch (2015-01-01T00:00:00.000Z)

View Source
const Escaped2Asterisks = "*" + string(8203) + "*"

Escaped2Asterisks is "**" with ZWS characters inbetween

View Source
const Escaped3Asterisks = "*" + string(8203) + "*" + string(8203) + "*"

Escaped3Asterisks is "***" with ZWS characters inbetween

View Source
const Escaped3Backticks = "`" + string(8203) + "`" + string(8203) + "`"

Escaped3Backticks is "````" with ZWS characters inbetween

View Source
const EscapedAtEveryone = EscapedAtSign + "everyone"

EscapedAtEveryone is EscapedAtSign followed by "everyone"

View Source
const EscapedAtHere = EscapedAtSign + "here"

EscapedAtHere is EscapedAtSign followed by "here"

View Source
const EscapedAtSign = "@" + string(8203)

EscapedAtSign is the @ character followed by a ZWS character

View Source
const TwoWeeks = time.Duration(14 * day)

TwoWeeks is two weeks in type time.Duration. This is the maximum age for a purge-able message.

Variables

View Source
var ChannelMentionRegex = regexp.MustCompile(`^<#(\d{17,19})>$`)

ChannelMentionRegex matches channel mentions

View Source
var DiscordIIRegex = regexp.MustCompile("discord[^a-z0-9]*gg")

DiscordIIRegex matches "discord[any symbol but a-z0-9]gg"

View Source
var EmojiFinderRegex = regexp.MustCompile(`<(a)?:([a-zA-Z0-9_-]{2,32}):(\d{17,19})>`)

EmojiFinderRegex matches Discord emojis but without str start/end

View Source
var EmojiRegex = regexp.MustCompile(`^<(a)?:([a-zA-Z0-9_-]{2,32}):(\d{17,19})>$`)

EmojiRegex matches Discord emojis

View Source
var ErrInvalidTime = errors.New("invalid time or out of range")

ErrInvalidTime throws when a bad time string is put into parseTime

View Source
var IDRegex = regexp.MustCompile(`^(\d{17,19})$`)

IDRegex matches Discord IDs (17-19 digit numbers)

View Source
var MentionRegex = regexp.MustCompile(`^<@!?(\d{17,19})>$`)

MentionRegex matches mentions

View Source
var MentionRegexZWS = regexp.MustCompile(`^<@` + string(8203) + `?!?(\d{17,19})>$`)

MentionRegexZWS matches mentions with ZWS

Perms is an array of permission integers, sorted in an almost identical way to the Discord desktop client

View Source
var PermsNames = [...]string{
	"Administrator",

	"View Audit Logs",
	"Manage Server",
	"Manage Roles",
	"Manage Channels",
	"Kick Members",
	"Ban Members",
	"Create Invite",
	"Change Nickname",
	"Manage Nicknames",
	"Manage Emojis",
	"Manage Webhooks",

	"Read Messages",
	"Send Messages",
	"Send TTS Messages",
	"Manage Messages",
	"Embed Links",
	"Attach Files",
	"Read Message History",
	"Mention Everyone",
	"Use External Emojis",
	"Add Reactions",

	"Connect to VC",
	"Speak in VC",
	"Mute Members in VC",
	"Deafen Members in VC",
	"Move Members in VC",
	"Use Voice Activity",
}

PermsNames is an array of permission names, sorted identically to Perms

View Source
var RegionIndicators = [...]string{
	"🇦",
	"🇧",
	"🇨",
	"🇩",
	"🇪",
	"🇫",
	"🇬",
	"🇭",
	"🇮",
	"🇯",
	"🇰",
	"🇱",
	"🇲",
	"🇳",
	"🇴",
	"🇵",
	"🇶",
	"🇷",
	"🇸",
	"🇹",
	"🇺",
	"🇻",
	"🇼",
	"🇽",
	"🇾",
	"🇿",
}

RegionIndicators are all known region indicators in alphabetical order

View Source
var RoleMentionFinderRegex = regexp.MustCompile(`<@&(\d{17,19})>`)

RoleMentionFinderRegex matches role mentions but without str start/end

View Source
var RoleMentionRegex = regexp.MustCompile(`^<@&(\d{17,19})>$`)

RoleMentionRegex matches role mentions

View Source
var Spaces = regexp.MustCompile(`[^\S\n]+`)

Spaces matches whitespace characters and newlines

Functions

func AbsInt

func AbsInt(i int) int

AbsInt is similar to math.Abs but accepts values type int

func BytesToUint

func BytesToUint(a []byte) (x uint)

BytesToUint converts a byte slice to uint

func CapString

func CapString(in string) string

CapString returns in[0:15] + "..." if len(in) > 15, else in

func CapStringLen added in v0.1.1

func CapStringLen(in string, l int) string

CapStringLen returns in[0:l] + "..." if len(in) > l, else in

func Clean

func Clean(unsafe string) string

Clean removes all newlines & backticks and appends a ZWS character after every @

func ClearBold

func ClearBold(uncleared string) string

ClearBold removes every consecutive 2/3 asterisks

func ClearNonspacing added in v0.1.1

func ClearNonspacing(str string) string

ClearNonspacing removes all non-spacing marks from a string

func CommaAnd

func CommaAnd(items []string) string

CommaAnd formats string slices like {"one", "two", "three"} to `one, two and three`

func CommaAndf

func CommaAndf(items []string, formatter func(int, string) string) string

CommaAndf is similar to CommaAnd but calls formatter on each string

func DiffUint32

func DiffUint32(x, y uint32) uint32

DiffUint32 returns abs(x-y)

func DiscordTag

func DiscordTag(user *discordgo.User) string

DiscordTag returns the specified user's Discord tag

func FindPrefix

func FindPrefix(s string, prefixes []string) (prefix string, ok bool)

FindPrefix finds the used prefix in a string

func FindSuffix added in v0.2.3

func FindSuffix(s string, suffixes []string) (prefix string, ok bool)

FindSuffix finds the used suffix in a string

func FormatBytes

func FormatBytes(size uint64) string

FormatBytes humanises byte sizes

func FormatDecimal

func FormatDecimal(in int) string

FormatDecimal formats int values to human-readable strings like `12,345,678`

func FormatDuration

func FormatDuration(duration time.Duration) string

FormatDuration humanises type time.Duration

func FormatHex

func FormatHex(in int64) string

FormatHex formats int64 values to human-readable hex strings like `00B6FF`

func GetEmbedImage

func GetEmbedImage(message *discordgo.Message) (*discordgo.MessageEmbedImage, string)

GetEmbedImage gets an image from the embed of a message and returns nil if none is found

func GetImage

func GetImage(message *discordgo.Message) (*discordgo.MessageAttachment, string)

GetImage gets an image attachment from a message and returns nil if none is found

func HasSuffixes

func HasSuffixes(s string, suffixes []string) bool

HasSuffixes returns whether s has one of suffixes as suffix

func IDtoTimestamp

func IDtoTimestamp(id string) (time.Time, error)

IDtoTimestamp converts Discord snowflakes to type time.Time

func IsValidImageURL added in v0.3.0

func IsValidImageURL(url string) (extension string, isValid bool)

IsValidImageURL checks if the given url is one that leads to an URL

func JoinTo

func JoinTo(b *strings.Builder, a []string, sep string)

JoinTo acts like b.WriteString(strings.Join(a, sep))

func MapIntString

func MapIntString(vs []int, f func(int) string) []string

MapIntString returns a new slice containing the results of calling f on each int in the original slice

func MapSlice

func MapSlice(vs []string, f func(int, string) string) []string

MapSlice returns a new slice containing the results of calling f on each string in the original slice

func Morning

func Morning(t int64) int64

Morning returns the midnight time for the day of the date represented by t.

func Normalise added in v0.6.0

func Normalise(str string) string

Normalise removes all non-spacing marks from a string and replaces all regional indicators with letters

func Ordinal added in v0.1.1

func Ordinal(in int) string

Ordinal formats ints as ordinals (12 -> `12th`)

func ParseDuration

func ParseDuration(in string) (time.Duration, error)

ParseDuration turns strings like `2 hours 3 minutes` to type time.Duration

func PermStringer

func PermStringer(perm int) string

PermStringer is a macro for utils.Perms[perm]

func Printerr

func Printerr(a ...interface{})

Printerr prints to stderr

func Quoter

func Quoter(_ int, s string) string

Quoter wraps the provided string around backticks. Compatible with CommaAndf

func ResolveChannelMention

func ResolveChannelMention(raw string) string

ResolveChannelMention resolves strings like `<#id>` or `id` to `id` and returns a blank string if not resolvable

func ResolveMention

func ResolveMention(raw string) string

ResolveMention resolves strings like `<@id>` or `id` to `id` and returns a blank string if not resolvable

func ResolveMentionZWS

func ResolveMentionZWS(raw string) string

ResolveMentionZWS resolves strings like `<@id>` or `id` to `id` with zero width spaces taken into account and returns a blank string if not resolvable

func ResolveRoleMention

func ResolveRoleMention(raw string) string

ResolveRoleMention resolves strings like `<@&id>` or `id` to `id` and returns a blank string if not resolvable

func Similarity

func Similarity(a, b string) float64

Similarity finds the similarity between a and b Original version at https://github.com/hbakhtiyor/strsim/blob/master/strsim.go

func Sqer

func Sqer(_ int, s string) string

Sqer wraps the provided string around []. Compatible with CommaAndf

func UintToBytes

func UintToBytes(x uint) (out []byte)

UintToBytes converts an uint to a byte slice

func Wait added in v0.1.1

func Wait(seconds time.Duration) <-chan time.Time

Wait waits for the specified number of seconds

Types

type UserError

type UserError struct {
	Err string
}

UserError represents errors that the users can see

func (UserError) Error

func (u UserError) Error() string

Jump to

Keyboard shortcuts

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