Refactored handlers and command handlers
This commit is contained in:
122
bot/bot.go
122
bot/bot.go
@ -7,7 +7,9 @@ import (
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/command"
|
||||
"git.kill0.net/chill9/beepboop/config"
|
||||
"git.kill0.net/chill9/beepboop/handler"
|
||||
"git.kill0.net/chill9/beepboop/lib"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@ -19,10 +21,19 @@ var C *config.Config
|
||||
|
||||
type (
|
||||
Bot struct {
|
||||
Session *discordgo.Session
|
||||
Config *config.Config
|
||||
config *config.Config
|
||||
session *discordgo.Session
|
||||
commands map[string]*Command
|
||||
}
|
||||
|
||||
Command struct {
|
||||
Name string
|
||||
Func CommandFunc
|
||||
NArgs int
|
||||
}
|
||||
|
||||
CommandFunc func(args []string, s *discordgo.Session, m *discordgo.MessageCreate) error
|
||||
|
||||
MessageHandler func(s *discordgo.Session, m *discordgo.MessageCreate)
|
||||
)
|
||||
|
||||
@ -33,60 +44,110 @@ func init() {
|
||||
viper.BindPFlags(pflag.CommandLine)
|
||||
}
|
||||
|
||||
func NewBot(s *discordgo.Session, config *config.Config) *Bot {
|
||||
return &Bot{Session: s, Config: config}
|
||||
func NewBot(config *config.Config, s *discordgo.Session) *Bot {
|
||||
return &Bot{
|
||||
session: s,
|
||||
commands: make(map[string]*Command),
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) RegisterCommands() {
|
||||
AddCommand(&Command{
|
||||
func (b *Bot) AddHandler(handler interface{}) func() {
|
||||
return b.session.AddHandler(handler)
|
||||
}
|
||||
|
||||
func (b *Bot) AddCommand(cmd *Command) {
|
||||
b.commands[cmd.Name] = cmd
|
||||
}
|
||||
|
||||
func (b *Bot) GetCommand(name string) (*Command, bool) {
|
||||
cmd, ok := b.commands[name]
|
||||
return cmd, ok
|
||||
}
|
||||
|
||||
func (b *Bot) CommandHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
if m.Author.ID == s.State.User.ID {
|
||||
return
|
||||
}
|
||||
|
||||
if !lib.HasCommand(m.Content, b.config.Prefix) {
|
||||
return
|
||||
}
|
||||
|
||||
cmdName, arg := lib.SplitCommandAndArg(m.Content, b.config.Prefix)
|
||||
|
||||
cmd, ok := b.GetCommand(cmdName)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
args := lib.SplitArgs(arg, cmd.NArgs)
|
||||
|
||||
if ok {
|
||||
log.Debugf("command: %v, args: %v, nargs: %d", cmd.Name, args, len(args))
|
||||
if err := cmd.Func(args, s, m); err != nil {
|
||||
log.Errorf("failed to execute command: %s", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
log.Warnf("unknown command: %v, args: %v, nargs: %d", cmdName, args, len(args))
|
||||
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("unknown command: %s", cmdName))
|
||||
}
|
||||
|
||||
func (b *Bot) Init(h *handler.Handlers, ch *command.Handlers) {
|
||||
// Register handlers
|
||||
b.AddHandler(h.Reaction)
|
||||
|
||||
// Register commands
|
||||
b.AddCommand(&Command{
|
||||
Name: "coin",
|
||||
Func: b.CoinCommand(),
|
||||
Func: ch.Coin,
|
||||
})
|
||||
AddCommand(&Command{
|
||||
b.AddCommand(&Command{
|
||||
Name: "deal",
|
||||
Func: b.DealCommand(),
|
||||
Func: ch.Deal,
|
||||
NArgs: 1,
|
||||
})
|
||||
AddCommand(&Command{
|
||||
b.AddCommand(&Command{
|
||||
Name: "ping",
|
||||
Func: b.PingCommand(),
|
||||
Func: ch.Ping,
|
||||
})
|
||||
AddCommand(&Command{
|
||||
b.AddCommand(&Command{
|
||||
Name: "roll",
|
||||
Func: b.RollCommand(),
|
||||
Func: ch.Roll,
|
||||
NArgs: 1,
|
||||
})
|
||||
AddCommand(&Command{
|
||||
b.AddCommand(&Command{
|
||||
Name: "roulette",
|
||||
Func: ch.Roulette,
|
||||
})
|
||||
b.AddCommand(&Command{
|
||||
Name: "rps",
|
||||
Func: b.RpsCommand(),
|
||||
Func: ch.Rps,
|
||||
NArgs: 1,
|
||||
})
|
||||
AddCommand(&Command{
|
||||
b.AddCommand(&Command{
|
||||
Name: "rpsls",
|
||||
Func: b.RpslsCommand(),
|
||||
Func: ch.Rpsls,
|
||||
NArgs: 1,
|
||||
})
|
||||
AddCommand(&Command{
|
||||
b.AddCommand(&Command{
|
||||
Name: "time",
|
||||
Func: b.TimeCommand(),
|
||||
Func: ch.Time,
|
||||
NArgs: 1,
|
||||
})
|
||||
AddCommand(&Command{
|
||||
b.AddCommand(&Command{
|
||||
Name: "version",
|
||||
Func: b.VersionCommand(),
|
||||
Func: ch.Version,
|
||||
})
|
||||
AddCommand(&Command{
|
||||
b.AddCommand(&Command{
|
||||
Name: "weather",
|
||||
Func: b.WeatherCommand(),
|
||||
Func: ch.Weather,
|
||||
NArgs: 1,
|
||||
})
|
||||
}
|
||||
|
||||
func (b *Bot) RegisterHandlers() {
|
||||
b.Session.AddHandler(b.CommandHandler())
|
||||
b.Session.AddHandler(b.ReactionHandler())
|
||||
}
|
||||
|
||||
func Run() error {
|
||||
initConfig()
|
||||
go reloadConfig()
|
||||
@ -104,9 +165,8 @@ func Run() error {
|
||||
return fmt.Errorf("error creating discord session: %v", err)
|
||||
}
|
||||
|
||||
b := NewBot(dg, C)
|
||||
b.RegisterHandlers()
|
||||
b.RegisterCommands()
|
||||
b := NewBot(C, dg)
|
||||
b.Init(handler.NewHandlers(C), command.NewHandlers(C))
|
||||
|
||||
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
|
||||
|
||||
|
31
bot/coin.go
31
bot/coin.go
@ -1,31 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"git.kill0.net/chill9/beepboop/lib"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
type Coin bool
|
||||
|
||||
func (c *Coin) Flip() bool {
|
||||
*c = Coin(lib.Itob(lib.RandInt(0, 1)))
|
||||
return bool(*c)
|
||||
}
|
||||
|
||||
func (b *Bot) CoinCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
var (
|
||||
c Coin
|
||||
msg string
|
||||
)
|
||||
|
||||
if c.Flip() {
|
||||
msg = "heads"
|
||||
} else {
|
||||
msg = "tails"
|
||||
}
|
||||
|
||||
b.Session.ChannelMessageSend(m.ChannelID, msg)
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,91 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/config"
|
||||
"git.kill0.net/chill9/beepboop/lib"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultCommander *Commander
|
||||
)
|
||||
|
||||
type (
|
||||
Commander struct {
|
||||
commands map[string]*Command
|
||||
}
|
||||
|
||||
Command struct {
|
||||
Name string
|
||||
Config *config.Config
|
||||
Func CommandFunc
|
||||
NArgs int
|
||||
}
|
||||
|
||||
CommandFunc func(args []string, m *discordgo.MessageCreate) error
|
||||
)
|
||||
|
||||
func init() {
|
||||
DefaultCommander = NewCommander()
|
||||
}
|
||||
|
||||
func NewCommander() *Commander {
|
||||
cmdr := new(Commander)
|
||||
cmdr.commands = make(map[string]*Command)
|
||||
return cmdr
|
||||
}
|
||||
|
||||
func (cmdr *Commander) AddCommand(cmd *Command) {
|
||||
cmdr.commands[cmd.Name] = cmd
|
||||
}
|
||||
|
||||
func (cmdr *Commander) GetCommand(name string) (*Command, bool) {
|
||||
cmd, ok := cmdr.commands[name]
|
||||
return cmd, ok
|
||||
}
|
||||
|
||||
func AddCommand(cmd *Command) {
|
||||
DefaultCommander.AddCommand(cmd)
|
||||
}
|
||||
|
||||
func GetCommand(name string) (*Command, bool) {
|
||||
cmd, ok := DefaultCommander.GetCommand(name)
|
||||
return cmd, ok
|
||||
}
|
||||
|
||||
func (b *Bot) CommandHandler() func(*discordgo.Session, *discordgo.MessageCreate) {
|
||||
return func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
var cmd *Command
|
||||
|
||||
if m.Author.ID == s.State.User.ID {
|
||||
return
|
||||
}
|
||||
|
||||
if !lib.HasCommand(m.Content, b.Config.Prefix) {
|
||||
return
|
||||
}
|
||||
|
||||
cmdName, arg := lib.SplitCommandAndArg(m.Content, b.Config.Prefix)
|
||||
|
||||
cmd, ok := GetCommand(cmdName)
|
||||
|
||||
args := lib.SplitArgs(arg, cmd.NArgs)
|
||||
|
||||
if ok {
|
||||
cmd.Config = b.Config
|
||||
|
||||
log.Debugf("command: %v, args: %v, nargs: %d", cmd.Name, args, len(args))
|
||||
if err := cmd.Func(args, m); err != nil {
|
||||
log.Errorf("failed to execute command: %s", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
log.Warnf("unknown command: %v, args: %v, nargs: %d", cmdName, args, len(args))
|
||||
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("unknown command: %s", cmdName))
|
||||
}
|
||||
}
|
85
bot/deal.go
85
bot/deal.go
@ -1,85 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type (
|
||||
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 (b *Bot) DealCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) 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 {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, "help: `!deal <n>`")
|
||||
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 {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf("error: %s\n", err))
|
||||
return nil
|
||||
}
|
||||
|
||||
b.Session.ChannelMessageSend(m.ChannelID, JoinCards(hand, " "))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func JoinCards(h []Card, sep string) string {
|
||||
b := make([]string, len(h))
|
||||
|
||||
for i, v := range h {
|
||||
b[i] = string(v)
|
||||
}
|
||||
|
||||
return strings.Join(b, sep)
|
||||
}
|
105
bot/dice.go
105
bot/dice.go
@ -1,105 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"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
|
||||
}
|
||||
)
|
||||
|
||||
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 (b *Bot) RollCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
var (
|
||||
err error
|
||||
msg, roll string
|
||||
r *Roll
|
||||
)
|
||||
|
||||
roll = args[0]
|
||||
|
||||
r, err = ParseRoll(roll)
|
||||
if err != nil {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
r.RollDice()
|
||||
log.Debugf("rolled dice: %+v", r)
|
||||
|
||||
msg = fmt.Sprintf("🎲 %s = %d", lib.JoinInt(r.Rolls, " + "), r.Sum)
|
||||
|
||||
b.Session.ChannelMessageSend(m.ChannelID, msg)
|
||||
return nil
|
||||
}
|
||||
}
|
12
bot/ping.go
12
bot/ping.go
@ -1,12 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func (b *Bot) PingCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, "pong")
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"strings"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/lib"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (b *Bot) ReactionHandler() func(*discordgo.Session, *discordgo.MessageCreate) {
|
||||
return func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
if m.Author.ID == s.State.User.ID {
|
||||
return
|
||||
}
|
||||
|
||||
emojis := b.Config.Handler.Reaction.Emojis
|
||||
channels := b.Config.Handler.Reaction.Channels
|
||||
|
||||
if len(emojis) == 0 {
|
||||
log.Warning("emoji list is empty")
|
||||
return
|
||||
}
|
||||
|
||||
channel, err := s.Channel(m.ChannelID)
|
||||
if err != nil {
|
||||
log.Fatalf("unable to get channel name: %v", err)
|
||||
}
|
||||
|
||||
if len(channels) > 0 && !lib.Contains(channels, channel.Name) {
|
||||
return
|
||||
}
|
||||
|
||||
for _, a := range m.Attachments {
|
||||
if strings.HasPrefix(a.ContentType, "image/") {
|
||||
for i := 1; i <= lib.RandInt(1, len(emojis)); i++ {
|
||||
r := emojis[rand.Intn(len(emojis))]
|
||||
s.MessageReactionAdd(m.ChannelID, m.ID, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for range m.Embeds {
|
||||
for i := 1; i <= lib.RandInt(1, len(emojis)); i++ {
|
||||
r := emojis[rand.Intn(len(emojis))]
|
||||
s.MessageReactionAdd(m.ChannelID, m.ID, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,84 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"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
|
||||
}
|
||||
)
|
||||
|
||||
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] {
|
||||
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 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (b *Bot) RouletteCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
if gun.IsEmpty() {
|
||||
gun.Load(Bullets)
|
||||
log.Debugf("reloading gun: %+v\n", gun)
|
||||
}
|
||||
|
||||
log.Debugf("firing gun: %+v\n", gun)
|
||||
if gun.Fire() {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, GunFireMessage)
|
||||
} else {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, GunClickMessage)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
36
bot/rps.go
36
bot/rps.go
@ -1,36 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/lib/rps"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func (b *Bot) RpsCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
if len(args) != 1 {
|
||||
b.Session.ChannelMessageSend(
|
||||
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
pc := strings.ToLower(args[0]) // player's choice
|
||||
|
||||
g := rps.NewGame(rps.RulesRps, rps.EmojiMapRps)
|
||||
|
||||
bc := g.Rand() // bot's choice
|
||||
|
||||
s, err := g.Play(bc, pc)
|
||||
if _, ok := err.(rps.InvalidChoiceError); ok {
|
||||
b.Session.ChannelMessageSend(
|
||||
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
|
||||
)
|
||||
}
|
||||
|
||||
b.Session.ChannelMessageSend(m.ChannelID, s)
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
36
bot/rpsls.go
36
bot/rpsls.go
@ -1,36 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/lib/rps"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func (b *Bot) RpslsCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
if len(args) != 1 {
|
||||
b.Session.ChannelMessageSend(
|
||||
m.ChannelID, "help: `!rps (rock | paper | scissors | lizard | spock)`",
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
pc := strings.ToLower(args[0]) // player's choice
|
||||
|
||||
g := rps.NewGame(rps.RulesRpsls, rps.EmojiMapRpsls)
|
||||
|
||||
bc := g.Rand() // bot's choice
|
||||
|
||||
s, err := g.Play(bc, pc)
|
||||
if _, ok := err.(rps.InvalidChoiceError); ok {
|
||||
b.Session.ChannelMessageSend(
|
||||
m.ChannelID, "help: `!rps (rock | paper | scissors | lizard | spock)`",
|
||||
)
|
||||
}
|
||||
|
||||
b.Session.ChannelMessageSend(m.ChannelID, s)
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
36
bot/time.go
36
bot/time.go
@ -1,36 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (b *Bot) TimeCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) 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)
|
||||
b.Session.ChannelMessageSend(m.ChannelID, err.Error())
|
||||
return nil
|
||||
}
|
||||
t = now.In(loc)
|
||||
} else {
|
||||
t = now
|
||||
}
|
||||
|
||||
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprint(t))
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
const (
|
||||
SourceURI = "https://git.kill0.net/chill9/bb"
|
||||
)
|
||||
|
||||
func (b *Bot) VersionCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||
"go version: %s\nplatform: %s\nos: %s\nsource: %s\n",
|
||||
runtime.Version(),
|
||||
runtime.GOARCH,
|
||||
runtime.GOOS,
|
||||
SourceURI,
|
||||
))
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/lib/weather"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (b *Bot) WeatherCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
var (
|
||||
err error
|
||||
loc string
|
||||
w weather.Weather
|
||||
)
|
||||
|
||||
if len(args) != 1 {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, "help: `!weather <CITY>,<STATE>,<COUNTRY>`")
|
||||
return nil
|
||||
}
|
||||
|
||||
loc = args[0]
|
||||
|
||||
if b.Config.OpenWeatherMapToken == "" {
|
||||
log.Error("OpenWeather token is not set")
|
||||
return nil
|
||||
}
|
||||
|
||||
wc := weather.NewClient(b.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)
|
||||
|
||||
b.Session.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(),
|
||||
))
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user