This commit is contained in:
parent
9c78ea2d48
commit
7c684af8c3
79 changed files with 3594 additions and 3257 deletions
50
internal/plugin/fun/coin.go
Normal file
50
internal/plugin/fun/coin.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
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}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue