Refactor commands to use the router
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing

This commit is contained in:
2022-09-06 00:00:47 -05:00
parent 0345b1cba1
commit c59c95c47a
13 changed files with 161 additions and 324 deletions

View File

@ -1,52 +0,0 @@
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
Name string
}
)
func (c *Coin) Flip() bool {
*c = Coin(lib.Itob(lib.RandInt(0, 1)))
return bool(*c)
}
func NewCoinHandler(s string) *CoinHandler {
return &CoinHandler{Name: s}
}
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
}
if !lib.ContainsCommand(m.Content, h.config.Prefix, h.Name) {
return
}
if c.Flip() {
msg = "heads"
} else {
msg = "tails"
}
s.ChannelMessageSend(m.ChannelID, msg)
}

View File

@ -1,111 +0,0 @@
package handler
import (
"errors"
"fmt"
"math/rand"
"strconv"
"strings"
"git.kill0.net/chill9/beepboop/bot"
"git.kill0.net/chill9/beepboop/lib"
"github.com/bwmarrin/discordgo"
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 NewDealHandler(s string) *DealHandler {
h := new(DealHandler)
h.Name = s
return h
}
func (h *DealHandler) SetConfig(config bot.Config) {
h.config = config
}
func (h *DealHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
if !lib.ContainsCommand(m.Content, h.config.Prefix, h.Name) {
return
}
rand.Shuffle(len(deck), func(i, j int) {
deck[i], deck[j] = deck[j], deck[i]
})
log.Debugf("%+v", deck)
_, args := lib.SplitCommandAndArgs(m.Content, h.config.Prefix)
if len(args) != 1 {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("help: `!%s <n>`", h.Name))
return
}
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 {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("error: %s\n", err))
return
}
s.ChannelMessageSend(m.ChannelID, JoinCards(hand, " "))
}
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)
}

View File

@ -1,131 +0,0 @@
package handler
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"git.kill0.net/chill9/beepboop/bot"
"git.kill0.net/chill9/beepboop/lib"
"github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
)
const (
MaxDice = 100
MaxSides = 100
)
type (
Roll struct {
N, D, Sum int
Rolls []int
S string
}
RollHandler struct {
config bot.Config
Name 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 NewRollHandler(s string) *RollHandler {
return &RollHandler{Name: s}
}
func (h *RollHandler) SetConfig(config bot.Config) {
h.config = config
}
func (h *RollHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
var (
err error
msg, roll string
r *Roll
)
if m.Author.ID == s.State.User.ID {
return
}
if !lib.ContainsCommand(m.Content, h.config.Prefix, h.Name) {
return
}
x := strings.Split(m.Content, " ")
if len(x) != 2 {
s.ChannelMessageSend(m.ChannelID, "help: `!roll <n>d<s>`")
return
}
roll = x[1]
r, err = ParseRoll(roll)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
r.RollDice()
log.Debugf("rolled dice: %+v", r)
msg = fmt.Sprintf("🎲 %s = %d", lib.JoinInt(r.Rolls, " + "), r.Sum)
s.ChannelMessageSend(m.ChannelID, msg)
}

View File

@ -1,37 +0,0 @@
package handler
import (
"git.kill0.net/chill9/beepboop/bot"
"git.kill0.net/chill9/beepboop/lib"
"github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
)
type (
PingHandler struct {
config bot.Config
Name string
}
)
func NewPingHandler(s string) *PingHandler {
return &PingHandler{Name: s}
}
func (h *PingHandler) SetConfig(config bot.Config) {
h.config = config
}
func (h *PingHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
if !lib.ContainsCommand(m.Content, h.config.Prefix, h.Name) {
return
}
log.Debug("received ping")
s.ChannelMessageSend(m.ChannelID, "pong")
}

View File

@ -1,105 +0,0 @@
package handler
import (
"strings"
"git.kill0.net/chill9/beepboop/bot"
"git.kill0.net/chill9/beepboop/lib"
"github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
)
const (
Bullets = 1
GunFireMessage = "💀🔫"
GunClickMessage = "😌🔫"
)
type (
Gun struct {
C [6]bool
N int
}
RouletteHandler struct {
config bot.Config
Name string
}
)
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 NewRouletteHandler(s string) *RouletteHandler {
return &RouletteHandler{Name: s}
}
func (h *RouletteHandler) SetConfig(config bot.Config) {
h.config = config
}
func (h *RouletteHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
if !strings.HasPrefix(m.Content, "!roulette") {
return
}
if gun.IsEmpty() {
gun.Load(Bullets)
log.Debugf("reloading gun: %+v\n", gun)
}
log.Debugf("firing gun: %+v\n", gun)
if gun.Fire() {
s.ChannelMessageSend(m.ChannelID, GunFireMessage)
} else {
s.ChannelMessageSend(m.ChannelID, GunClickMessage)
}
}

View File

@ -1,65 +0,0 @@
package handler
import (
"fmt"
"strings"
"time"
"git.kill0.net/chill9/beepboop/bot"
"github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
)
type (
TimeHandler struct {
config bot.Config
Name string
}
)
func NewTimeHandler(s string) *TimeHandler {
return &TimeHandler{Name: s}
}
func (h *TimeHandler) SetConfig(config bot.Config) {
h.config = config
}
func (h *TimeHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
var (
t time.Time
tz string
)
if m.Author.ID == s.State.User.ID {
return
}
if !strings.HasPrefix(m.Content, "!time") {
return
}
x := strings.SplitN(m.Content, " ", 2)
if len(x) > 2 {
s.ChannelMessageSend(m.ChannelID, "help: `!time TIMEZONE`")
return
}
now := time.Now()
if len(x) == 2 {
tz = x[1]
loc, err := time.LoadLocation(tz)
if err != nil {
log.Warnf("failed to load location: %s", err)
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
t = now.In(loc)
} else {
t = now
}
s.ChannelMessageSend(m.ChannelID, fmt.Sprint(t))
}

View File

@ -1,47 +0,0 @@
package handler
import (
"fmt"
"runtime"
"git.kill0.net/chill9/beepboop/bot"
"git.kill0.net/chill9/beepboop/lib"
"github.com/bwmarrin/discordgo"
)
const (
SourceURI = "https://git.kill0.net/chill9/bb"
)
type (
VersionHandler struct {
config bot.Config
Name string
}
)
func NewVersionHandler(s string) *VersionHandler {
return &VersionHandler{Name: s}
}
func (h *VersionHandler) SetConfig(config bot.Config) {
h.config = config
}
func (h *VersionHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
if !lib.ContainsCommand(m.Content, h.config.Prefix, h.Name) {
return
}
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
"go version: %s\nplatform: %s\nos: %s\nsource: %s\n",
runtime.Version(),
runtime.GOARCH,
runtime.GOOS,
SourceURI,
))
}

View File

@ -1,77 +0,0 @@
package handler
import (
"fmt"
"strings"
"git.kill0.net/chill9/beepboop/bot"
"git.kill0.net/chill9/beepboop/lib/weather"
"github.com/bwmarrin/discordgo"
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 (h *WeatherHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
var (
err error
loc string
w weather.Weather
)
if m.Author.ID == s.State.User.ID {
return
}
if !strings.HasPrefix(m.Content, "!weather") {
return
}
x := strings.SplitN(m.Content, " ", 2)
if len(x) != 2 {
s.ChannelMessageSend(m.ChannelID, "help: `!weather <CITY>,<STATE>,<COUNTRY>`")
return
}
loc = x[1]
if h.Config.OpenWeatherMapToken == "" {
log.Error("OpenWeather token is not set")
return
}
wc := weather.NewClient(h.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
}
log.Debugf("weather returned for '%s': %+v", loc, w)
s.ChannelMessageSend(m.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(),
))
}