Refactor commands to use the router
This commit is contained in:
29
bot/commands/coin.go
Normal file
29
bot/commands/coin.go
Normal file
@ -0,0 +1,29 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
"git.kill0.net/chill9/beepboop/lib"
|
||||
)
|
||||
|
||||
type Coin bool
|
||||
|
||||
func (c *Coin) Flip() bool {
|
||||
*c = Coin(lib.Itob(lib.RandInt(0, 1)))
|
||||
return bool(*c)
|
||||
}
|
||||
|
||||
func CoinCommand(cmd *bot.Command, args []string) error {
|
||||
var (
|
||||
c Coin
|
||||
msg string
|
||||
)
|
||||
|
||||
if c.Flip() {
|
||||
msg = "heads"
|
||||
} else {
|
||||
msg = "tails"
|
||||
}
|
||||
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, msg)
|
||||
return nil
|
||||
}
|
90
bot/commands/deal.go
Normal file
90
bot/commands/deal.go
Normal file
@ -0,0 +1,90 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type (
|
||||
DealHandler struct {
|
||||
config bot.Config
|
||||
Name string
|
||||
}
|
||||
|
||||
Card string
|
||||
|
||||
Deck [52]Card
|
||||
)
|
||||
|
||||
var deck Deck = Deck{
|
||||
"2♣", "3♣", "4♣", "5♣", "6♣", "7♣", "8♣", "9♣", "10♣", "J♣", "Q♣", "K♣", "A♣",
|
||||
"2♦", "3♦", "4♦", "5♦", "6♦", "7♦", "8♦", "9♦", "10♦", "J♦", "Q♦", "K♦", "A♦",
|
||||
"2♥", "3♥", "4♥", "5♥", "6♥", "7♥", "8♥", "9♥", "10♥", "J♥", "Q♥", "K♥", "A♥",
|
||||
"2♠", "3♠", "4♠", "5♠", "6♠", "7♠", "8♠", "9♠", "10♠", "J♠", "Q♠", "K♠", "A♠",
|
||||
}
|
||||
|
||||
func (d *Deck) Deal(n int) ([]Card, error) {
|
||||
var (
|
||||
hand []Card
|
||||
err error
|
||||
)
|
||||
|
||||
if n < 1 {
|
||||
err = errors.New("number cannot be less than 1")
|
||||
return hand, err
|
||||
}
|
||||
|
||||
if n > len(d) {
|
||||
err = errors.New("number is greater than cards in the deck")
|
||||
return hand, err
|
||||
}
|
||||
|
||||
hand = deck[0:n]
|
||||
|
||||
return hand, err
|
||||
}
|
||||
|
||||
func DealCommand(cmd *bot.Command, args []string) error {
|
||||
rand.Shuffle(len(deck), func(i, j int) {
|
||||
deck[i], deck[j] = deck[j], deck[i]
|
||||
})
|
||||
|
||||
log.Debugf("%+v", deck)
|
||||
|
||||
if len(args) != 1 {
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, fmt.Sprintf("help: `!%s <n>`", cmd.Name))
|
||||
return nil
|
||||
}
|
||||
|
||||
n, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
log.Errorf("failed to convert string to int: %s", err)
|
||||
}
|
||||
|
||||
hand, err := deck.Deal(n)
|
||||
if err != nil {
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, fmt.Sprintf("error: %s\n", err))
|
||||
return nil
|
||||
}
|
||||
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, JoinCards(hand, " "))
|
||||
return nil
|
||||
}
|
||||
|
||||
func JoinCards(h []Card, sep string) string {
|
||||
var b []string
|
||||
|
||||
b = make([]string, len(h))
|
||||
|
||||
for i, v := range h {
|
||||
b[i] = string(v)
|
||||
}
|
||||
|
||||
return strings.Join(b, sep)
|
||||
}
|
103
bot/commands/dice.go
Normal file
103
bot/commands/dice.go
Normal file
@ -0,0 +1,103 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
"git.kill0.net/chill9/beepboop/lib"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxDice = 100
|
||||
MaxSides = 100
|
||||
)
|
||||
|
||||
type (
|
||||
Roll struct {
|
||||
N, D, Sum int
|
||||
Rolls []int
|
||||
S string
|
||||
}
|
||||
)
|
||||
|
||||
func NewRoll(n, d int) *Roll {
|
||||
r := new(Roll)
|
||||
r.N = n
|
||||
r.D = d
|
||||
r.S = fmt.Sprintf("%dd%d", r.N, r.D)
|
||||
return r
|
||||
}
|
||||
|
||||
func ParseRoll(roll string) (*Roll, error) {
|
||||
var (
|
||||
dice []string
|
||||
err error
|
||||
n, d int
|
||||
)
|
||||
|
||||
match, _ := regexp.MatchString(`^(?:\d+)?d\d+$`, roll)
|
||||
|
||||
if !match {
|
||||
return nil, errors.New("invalid roll, use `<n>d<sides>` e.g. `4d6`")
|
||||
}
|
||||
|
||||
dice = strings.Split(roll, "d")
|
||||
|
||||
if dice[0] == "" {
|
||||
n = 1
|
||||
} else {
|
||||
n, err = strconv.Atoi(dice[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
d, err = strconv.Atoi(dice[1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if n > MaxDice || d > MaxSides {
|
||||
return nil, fmt.Errorf("invalid roll, n must be <= %d and sides must be <= %d", MaxDice, MaxSides)
|
||||
}
|
||||
|
||||
return NewRoll(n, d), nil
|
||||
}
|
||||
|
||||
func (r *Roll) RollDice() {
|
||||
for i := 1; i <= r.N; i++ {
|
||||
roll := lib.RandInt(1, r.D)
|
||||
r.Rolls = append(r.Rolls, roll)
|
||||
r.Sum += roll
|
||||
}
|
||||
}
|
||||
|
||||
func RollCommand(cmd *bot.Command, args []string) error {
|
||||
var (
|
||||
err error
|
||||
msg, roll string
|
||||
r *Roll
|
||||
)
|
||||
|
||||
roll = args[0]
|
||||
|
||||
r, err = ParseRoll(roll)
|
||||
if err != nil {
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
r.RollDice()
|
||||
log.Debugf("rolled dice: %+v", r)
|
||||
|
||||
msg = fmt.Sprintf("🎲 %s = %d", lib.JoinInt(r.Rolls, " + "), r.Sum)
|
||||
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, msg)
|
||||
return nil
|
||||
}
|
10
bot/commands/ping.go
Normal file
10
bot/commands/ping.go
Normal file
@ -0,0 +1,10 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
)
|
||||
|
||||
func PingCommand(cmd *bot.Command, args []string) error {
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, "pong")
|
||||
return nil
|
||||
}
|
82
bot/commands/roulette.go
Normal file
82
bot/commands/roulette.go
Normal file
@ -0,0 +1,82 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
"git.kill0.net/chill9/beepboop/lib"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
Bullets = 1
|
||||
GunFireMessage = "💀🔫"
|
||||
GunClickMessage = "😌🔫"
|
||||
)
|
||||
|
||||
type (
|
||||
Gun struct {
|
||||
C [6]bool
|
||||
N int
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
gun *Gun
|
||||
)
|
||||
|
||||
func init() {
|
||||
gun = NewGun()
|
||||
}
|
||||
|
||||
func NewGun() *Gun {
|
||||
return new(Gun)
|
||||
}
|
||||
|
||||
func (g *Gun) Load(n int) {
|
||||
g.N = 0
|
||||
for i := 1; i <= n; {
|
||||
x := lib.RandInt(0, len(g.C)-1)
|
||||
if g.C[x] == false {
|
||||
g.C[x] = true
|
||||
i++
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Gun) Fire() bool {
|
||||
if g.C[g.N] {
|
||||
g.C[g.N] = false
|
||||
g.N++
|
||||
return true
|
||||
}
|
||||
|
||||
g.N++
|
||||
return false
|
||||
}
|
||||
|
||||
func (g *Gun) IsEmpty() bool {
|
||||
for _, v := range g.C {
|
||||
if v == true {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func RouletteCommand(cmd *bot.Command, args []string) error {
|
||||
if gun.IsEmpty() {
|
||||
gun.Load(Bullets)
|
||||
log.Debugf("reloading gun: %+v\n", gun)
|
||||
}
|
||||
|
||||
log.Debugf("firing gun: %+v\n", gun)
|
||||
if gun.Fire() {
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, GunFireMessage)
|
||||
} else {
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, GunClickMessage)
|
||||
}
|
||||
return nil
|
||||
}
|
34
bot/commands/time.go
Normal file
34
bot/commands/time.go
Normal file
@ -0,0 +1,34 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func TimeCommand(cmd *bot.Command, args []string) error {
|
||||
var (
|
||||
t time.Time
|
||||
tz string
|
||||
)
|
||||
|
||||
now := time.Now()
|
||||
|
||||
if len(args) == 1 {
|
||||
tz = args[0]
|
||||
loc, err := time.LoadLocation(tz)
|
||||
if err != nil {
|
||||
log.Warnf("failed to load location: %s", err)
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, err.Error())
|
||||
return nil
|
||||
}
|
||||
t = now.In(loc)
|
||||
} else {
|
||||
t = now
|
||||
}
|
||||
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, fmt.Sprint(t))
|
||||
return nil
|
||||
}
|
23
bot/commands/version.go
Normal file
23
bot/commands/version.go
Normal file
@ -0,0 +1,23 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
)
|
||||
|
||||
const (
|
||||
SourceURI = "https://git.kill0.net/chill9/bb"
|
||||
)
|
||||
|
||||
func VersionCommand(cmd *bot.Command, args []string) error {
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, fmt.Sprintf(
|
||||
"go version: %s\nplatform: %s\nos: %s\nsource: %s\n",
|
||||
runtime.Version(),
|
||||
runtime.GOARCH,
|
||||
runtime.GOOS,
|
||||
SourceURI,
|
||||
))
|
||||
return nil
|
||||
}
|
67
bot/commands/weather.go
Normal file
67
bot/commands/weather.go
Normal file
@ -0,0 +1,67 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
"git.kill0.net/chill9/beepboop/lib/weather"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type WeatherHandler struct {
|
||||
Config bot.Config
|
||||
Name string
|
||||
}
|
||||
|
||||
func NewWeatherHandler(s string) *WeatherHandler {
|
||||
return &WeatherHandler{Name: s}
|
||||
}
|
||||
|
||||
func (h *WeatherHandler) SetConfig(config bot.Config) {
|
||||
h.Config = config
|
||||
}
|
||||
|
||||
func WeatherCommand(cmd *bot.Command, args []string) error {
|
||||
var (
|
||||
err error
|
||||
loc string
|
||||
w weather.Weather
|
||||
)
|
||||
|
||||
if len(args) != 1 {
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, "help: `!weather <CITY>,<STATE>,<COUNTRY>`")
|
||||
return nil
|
||||
}
|
||||
|
||||
loc = args[0]
|
||||
|
||||
if cmd.Config.OpenWeatherMapToken == "" {
|
||||
log.Error("OpenWeather token is not set")
|
||||
return nil
|
||||
}
|
||||
|
||||
wc := weather.NewClient(cmd.Config.OpenWeatherMapToken)
|
||||
|
||||
log.Debugf("weather requested for '%s'", loc)
|
||||
|
||||
w, err = wc.Get(loc)
|
||||
if err != nil {
|
||||
log.Errorf("weather client error: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Debugf("weather returned for '%s': %+v", loc, w)
|
||||
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, fmt.Sprintf(
|
||||
"%s (%.1f, %.1f) — C:%.1f F:%.1f K:%.1f",
|
||||
loc,
|
||||
w.Coord.Lat,
|
||||
w.Coord.Lon,
|
||||
w.Main.Temp.Celcius(),
|
||||
w.Main.Temp.Fahrenheit(),
|
||||
w.Main.Temp.Kelvin(),
|
||||
))
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user