vote

package
v0.0.0-...-d931726 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var VoteEmotes = strings.Fields("\u0031\u20E3 \u0032\u20E3 \u0033\u20E3 \u0034\u20E3 \u0035\u20E3 \u0036\u20E3 \u0037\u20E3 \u0038\u20E3 \u0039\u20E3 \u0030\u20E3")

VoteEmotes contains the emotes used to tick a vote.

View Source
var VotesRunning = map[string]Vote{}

VotesRunning maps running vote IDs to their vote instances.

Functions

func HashUserID

func HashUserID(userID string, salt []byte) (hash string, err error)

HashUserID takes a userID as well as a salt value to generate a unique hash.

Therefore, the first 12 bits are cut of to obscure the ID while keeping the risk of collissions low.

Types

type Tick

type Tick struct {
	UserID string
	Tick   int
}

Tick wraps a user ID and the index of the selection ticked.

type Vote

type Vote struct {
	ID            string
	MsgID         string
	CreatorID     string
	GuildID       string
	ChannelID     string
	Description   string
	ImageURL      string
	Expires       time.Time
	Possibilities []string
	Ticks         map[string]*Tick
}

Vote wraps the information and current state of a vote and its ticks.

func Unmarshal

func Unmarshal(data string) (Vote, error)

Unmarshal tries to deserialize a raw data string to a Vote object. Errors occured during deserialization are returned as well.

func (*Vote) AddReactions

func (v *Vote) AddReactions(s *discordgo.Session) error

AddReactions adds the reactions to the votes message for each selection possibility.

Vote emotes are used from VoteEmotes.

func (*Vote) AsEmbed

func (v *Vote) AsEmbed(s *discordgo.Session, voteState ...VoteState) (*discordgo.MessageEmbed, error)

AsEmbed creates a discordgo.MessageEmbed from the vote. If voteState is passed, the state will be displayed as well. Otherwise, it will be assumed that the vote is open.

If voteState is VoteStateClosed or VoteStateExpired, a pie chart will be generated representing the distribution of vote ticks and sent as image to the channel.

func (*Vote) AsField

func (v *Vote) AsField() *discordgo.MessageEmbedField

AsField creates a discordgo.MessageEmbedField from the vote information.

func (*Vote) Close

func (v *Vote) Close(s *discordgo.Session, voteState VoteState) error

Close closes the vote and removes it from the VotesRunning map.

func (*Vote) Marshal

func (v *Vote) Marshal() (string, error)

Marshal serializes the vote to a raw data string.

The vote object is encoded to a byte array using the gob encoder and then encoded to a base64 string.

func (*Vote) SetExpire

func (v *Vote) SetExpire(s *discordgo.Session, d time.Duration, tp timeprovider.Provider) error

SetExpire sets the expiration for a vote.

func (*Vote) Tick

func (v *Vote) Tick(s *discordgo.Session, userID string, tick int) (err error)

Tick sets the tick for the specified user to the vote.

type VoteState

type VoteState int

VoteState defines the lifecycle state of a Vote.

const (
	VoteStateOpen VoteState = iota
	VoteStateClosed
	VoteStateClosedNC
	VoteStateExpired
)

Jump to

Keyboard shortcuts

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