50 lines
1,016 B
Go
50 lines
1,016 B
Go
package fun
|
|
|
|
import (
|
|
"math/rand"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.nakama.town/fmartingr/butterrobot/internal/model"
|
|
"git.nakama.town/fmartingr/butterrobot/internal/plugin"
|
|
)
|
|
|
|
// CoinPlugin flips a coin
|
|
type CoinPlugin struct {
|
|
plugin.BasePlugin
|
|
rand *rand.Rand
|
|
}
|
|
|
|
// NewCoin creates a new CoinPlugin instance
|
|
func NewCoin() *CoinPlugin {
|
|
source := rand.NewSource(time.Now().UnixNano())
|
|
return &CoinPlugin{
|
|
BasePlugin: plugin.BasePlugin{
|
|
ID: "fun.coin",
|
|
Name: "Coin Flip",
|
|
Help: "Flips a coin when you type 'flip a coin'",
|
|
},
|
|
rand: rand.New(source),
|
|
}
|
|
}
|
|
|
|
// OnMessage handles incoming messages
|
|
func (p *CoinPlugin) OnMessage(msg *model.Message, config map[string]interface{}) []*model.Message {
|
|
if !strings.Contains(strings.ToLower(msg.Text), "flip a coin") {
|
|
return nil
|
|
}
|
|
|
|
result := "Heads"
|
|
if p.rand.Intn(2) == 0 {
|
|
result = "Tails"
|
|
}
|
|
|
|
response := &model.Message{
|
|
Text: result,
|
|
Chat: msg.Chat,
|
|
ReplyTo: msg.ID,
|
|
Channel: msg.Channel,
|
|
}
|
|
|
|
return []*model.Message{response}
|
|
}
|