Compare commits

..

No commits in common. "5a141be5345abef7b2b1190e4e59e79fc4c5aed9" and "211f963b87e0806386321adb62916e04e47316f1" have entirely different histories.

6 changed files with 10 additions and 112 deletions

View File

@ -1,7 +1,6 @@
package bot package bot
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
@ -55,11 +54,6 @@ 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(),
@ -90,12 +84,12 @@ func Run() error {
} }
if C.DiscordToken == "" { if C.DiscordToken == "" {
return errors.New("discord token not set") log.Fatalf("Discord token is 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 {
return fmt.Errorf("error creating discord session: %v", err) log.Fatalf("error creating Discord session: %v\n", err)
} }
b := NewBot(dg, C) b := NewBot(dg, C)
@ -105,7 +99,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 {
return fmt.Errorf("error opening connection: %v", err) log.Fatalf("error opening connection: %v\n", 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,10 +5,7 @@ const (
) )
var ( var (
defaultReactions []string = []string{ defaultReactions []string = []string{"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩", "🔥", "🍒", "🎉", "🥳", "🎊"}
"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩",
"🔥", "🍒", "🎉", "🥳", "🎊", "📉", "📈", "💀", "☠️",
}
) )
type ( type (

View File

@ -1,84 +0,0 @@
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 (
"git.kill0.net/chill9/beepboop/bot" "os"
log "github.com/sirupsen/logrus" "git.kill0.net/chill9/beepboop/bot"
) )
func main() { func main() {
if err := bot.Run(); err != nil { if err := bot.Run(); err != nil {
log.Fatal(err) os.Exit(1)
} }
} }

View File

@ -118,10 +118,6 @@ 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, " ")
} }
@ -130,10 +126,6 @@ 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,7 +115,6 @@ 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) {