bb/bot/handler/coin.go

53 lines
806 B
Go
Raw Normal View History

2022-08-25 15:00:43 +00:00
package handler
import (
"git.kill0.net/chill9/beepboop/bot"
"git.kill0.net/chill9/beepboop/lib"
"github.com/bwmarrin/discordgo"
)
type (
Coin bool
CoinHandler struct {
config bot.Config
2022-08-27 14:00:35 +00:00
Name string
2022-08-25 15:00:43 +00:00
}
)
func (c *Coin) Flip() bool {
*c = Coin(lib.Itob(lib.RandInt(0, 1)))
return bool(*c)
}
2022-08-27 14:00:35 +00:00
func NewCoinHandler(s string) *CoinHandler {
2022-09-05 03:36:14 +00:00
return &CoinHandler{Name: s}
2022-08-25 15:00:43 +00:00
}
func (h *CoinHandler) SetConfig(config bot.Config) {
h.config = config
}
func (h *CoinHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
var (
c Coin
msg string
)
if m.Author.ID == s.State.User.ID {
return
}
2022-09-05 17:54:56 +00:00
if !lib.ContainsCommand(m.Content, h.config.Prefix, h.Name) {
2022-08-25 15:00:43 +00:00
return
}
if c.Flip() {
msg = "heads"
} else {
msg = "tails"
}
s.ChannelMessageSend(m.ChannelID, msg)
}