Compare commits

...

4 Commits

6 changed files with 112 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package bot package bot
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
@ -54,6 +55,11 @@ func (b *Bot) RegisterCommands() {
Func: b.RollCommand(), Func: b.RollCommand(),
NArgs: 1, NArgs: 1,
}) })
AddCommand(&Command{
Name: "rps",
Func: b.RpsCommand(),
NArgs: 1,
})
AddCommand(&Command{ AddCommand(&Command{
Name: "time", Name: "time",
Func: b.TimeCommand(), Func: b.TimeCommand(),
@ -84,12 +90,12 @@ func Run() error {
} }
if C.DiscordToken == "" { if C.DiscordToken == "" {
log.Fatalf("Discord token is not set") return errors.New("discord token not set")
} }
dg, err := discordgo.New(fmt.Sprintf("Bot %s", C.DiscordToken)) dg, err := discordgo.New(fmt.Sprintf("Bot %s", C.DiscordToken))
if err != nil { if err != nil {
log.Fatalf("error creating Discord session: %v\n", err) return fmt.Errorf("error creating discord session: %v", err)
} }
b := NewBot(dg, C) b := NewBot(dg, C)
@ -99,7 +105,7 @@ func Run() error {
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
if err = dg.Open(); err != nil { if err = dg.Open(); err != nil {
log.Fatalf("error opening connection: %v\n", err) return fmt.Errorf("error opening connection: %v", err)
} }
log.Info("The bot is now running. Press CTRL-C to exit.") log.Info("The bot is now running. Press CTRL-C to exit.")

View File

@ -5,7 +5,10 @@ const (
) )
var ( var (
defaultReactions []string = []string{"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩", "🔥", "🍒", "🎉", "🥳", "🎊"} defaultReactions []string = []string{
"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩",
"🔥", "🍒", "🎉", "🥳", "🎊", "📉", "📈", "💀", "☠️",
}
) )
type ( type (

84
bot/rps.go Normal file
View File

@ -0,0 +1,84 @@
package bot
import (
"fmt"
"math/rand"
"strings"
"github.com/bwmarrin/discordgo"
)
const (
Rock = iota
Paper
Scissors
)
var (
rpsVictoryMap map[int]int = map[int]int{
Rock: Scissors,
Paper: Rock,
Scissors: Paper,
}
rpsChoiceMap map[string]int = map[string]int{
"rock": Rock,
"paper": Paper,
"scissors": Scissors,
}
rpsEmojiMap map[int]string = map[int]string{
Rock: "🪨️",
Paper: "📝",
Scissors: "✂️",
}
)
func RpsRand() int {
n := rand.Intn(len(rpsVictoryMap))
i := 0
for _, v := range rpsChoiceMap {
if i == n {
return v
}
i++
}
panic("unreachable")
}
func (b *Bot) RpsCommand() CommandFunc {
return func(args []string, m *discordgo.MessageCreate) error {
var (
botChoice, playerChoice int
botEmoji, playerEmoji string
c string
)
if len(args) != 1 {
b.Session.ChannelMessageSend(m.ChannelID, "help: `!rps (rock | paper | scissors)`")
return nil
}
c = strings.ToLower(args[0])
playerChoice, ok := rpsChoiceMap[c]
if !ok {
b.Session.ChannelMessageSend(m.ChannelID, "help: `!rps (rock | paper | scissors)`")
}
botChoice = RpsRand()
botEmoji = rpsEmojiMap[botChoice]
playerEmoji = rpsEmojiMap[playerChoice]
if botChoice == playerChoice {
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s v %s: draw", botEmoji, playerEmoji))
return nil
} else if rpsVictoryMap[botChoice] == playerChoice {
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s v %s: bot wins", botEmoji, playerEmoji))
return nil
}
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s v %s: you win", botEmoji, playerEmoji))
return nil
}
}

View File

@ -1,13 +1,13 @@
package main package main
import ( import (
"os"
"git.kill0.net/chill9/beepboop/bot" "git.kill0.net/chill9/beepboop/bot"
log "github.com/sirupsen/logrus"
) )
func main() { func main() {
if err := bot.Run(); err != nil { if err := bot.Run(); err != nil {
os.Exit(1) log.Fatal(err)
} }
} }

View File

@ -118,6 +118,10 @@ func SplitCommandAndArg(s, prefix string) (cmd string, arg string) {
func SplitCommandAndArgs(s, prefix string, n int) (cmd string, args []string) { func SplitCommandAndArgs(s, prefix string, n int) (cmd string, args []string) {
cmd, arg := SplitCommandAndArg(s, prefix) cmd, arg := SplitCommandAndArg(s, prefix)
if arg == "" {
return cmd, []string{}
}
if n == 0 { if n == 0 {
return cmd, strings.Split(arg, " ") return cmd, strings.Split(arg, " ")
} }
@ -126,6 +130,10 @@ func SplitCommandAndArgs(s, prefix string, n int) (cmd string, args []string) {
} }
func SplitArgs(s string, n int) (args []string) { func SplitArgs(s string, n int) (args []string) {
if s == "" {
return []string{}
}
if n > 0 { if n > 0 {
args = strings.SplitN(s, " ", n) args = strings.SplitN(s, " ", n)
} else { } else {

View File

@ -90,8 +90,8 @@ func TestSplitCommandAndArgs(t *testing.T) {
}{ }{
{"!command x y", "!", 2, "command", []string{"x", "y"}}, {"!command x y", "!", 2, "command", []string{"x", "y"}},
{"!command x y z", "!", 2, "command", []string{"x", "y z"}}, {"!command x y z", "!", 2, "command", []string{"x", "y z"}},
{"!command", "!", 1, "command", []string{""}}, {"!command", "!", 1, "command", []string{}},
{"hey man", "!", 1, "", []string{""}}, {"hey man", "!", 1, "", []string{}},
} }
for _, table := range tables { for _, table := range tables {
gotCmd, gotArgs := SplitCommandAndArgs(table.s, table.prefix, table.n) gotCmd, gotArgs := SplitCommandAndArgs(table.s, table.prefix, table.n)
@ -99,7 +99,7 @@ func TestSplitCommandAndArgs(t *testing.T) {
t.Errorf("got: %s, want: %s", gotCmd, table.wantCmd) t.Errorf("got: %s, want: %s", gotCmd, table.wantCmd)
} }
if !reflect.DeepEqual(gotArgs, table.wantArgs) { if !reflect.DeepEqual(gotArgs, table.wantArgs) {
t.Errorf("got: %+v, want: %+v", gotArgs, table.wantArgs) t.Errorf("got: %#v, want: %#v", gotArgs, table.wantArgs)
} }
} }
} }
@ -115,6 +115,7 @@ func TestSplitArgs(t *testing.T) {
{"a b c", 2, []string{"a", "b c"}}, {"a b c", 2, []string{"a", "b c"}},
{"a b c", 3, []string{"a", "b", "c"}}, {"a b c", 3, []string{"a", "b", "c"}},
{"a b c", 4, []string{"a", "b", "c"}}, {"a b c", 4, []string{"a", "b", "c"}},
{"", 0, []string{}},
} }
for _, table := range tables { for _, table := range tables {
if got, want := SplitArgs(table.s, table.n), table.want; !reflect.DeepEqual(got, want) { if got, want := SplitArgs(table.s, table.n), table.want; !reflect.DeepEqual(got, want) {