Compare commits
4 Commits
211f963b87
...
5a141be534
Author | SHA1 | Date | |
---|---|---|---|
5a141be534 | |||
49000133d8 | |||
756aaf2379 | |||
6bd4744745 |
12
bot/bot.go
12
bot/bot.go
@ -1,6 +1,7 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
@ -54,6 +55,11 @@ func (b *Bot) RegisterCommands() {
|
||||
Func: b.RollCommand(),
|
||||
NArgs: 1,
|
||||
})
|
||||
AddCommand(&Command{
|
||||
Name: "rps",
|
||||
Func: b.RpsCommand(),
|
||||
NArgs: 1,
|
||||
})
|
||||
AddCommand(&Command{
|
||||
Name: "time",
|
||||
Func: b.TimeCommand(),
|
||||
@ -84,12 +90,12 @@ func Run() error {
|
||||
}
|
||||
|
||||
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))
|
||||
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)
|
||||
@ -99,7 +105,7 @@ func Run() error {
|
||||
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
|
||||
|
||||
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.")
|
||||
|
@ -5,7 +5,10 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
defaultReactions []string = []string{"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩", "🔥", "🍒", "🎉", "🥳", "🎊"}
|
||||
defaultReactions []string = []string{
|
||||
"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩",
|
||||
"🔥", "🍒", "🎉", "🥳", "🎊", "📉", "📈", "💀", "☠️",
|
||||
}
|
||||
)
|
||||
|
||||
type (
|
||||
|
84
bot/rps.go
Normal file
84
bot/rps.go
Normal 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
|
||||
}
|
||||
}
|
@ -1,13 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := bot.Run(); err != nil {
|
||||
os.Exit(1)
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
@ -118,6 +118,10 @@ func SplitCommandAndArg(s, prefix string) (cmd string, arg string) {
|
||||
func SplitCommandAndArgs(s, prefix string, n int) (cmd string, args []string) {
|
||||
cmd, arg := SplitCommandAndArg(s, prefix)
|
||||
|
||||
if arg == "" {
|
||||
return cmd, []string{}
|
||||
}
|
||||
|
||||
if n == 0 {
|
||||
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) {
|
||||
if s == "" {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
if n > 0 {
|
||||
args = strings.SplitN(s, " ", n)
|
||||
} else {
|
||||
|
@ -90,8 +90,8 @@ func TestSplitCommandAndArgs(t *testing.T) {
|
||||
}{
|
||||
{"!command x y", "!", 2, "command", []string{"x", "y"}},
|
||||
{"!command x y z", "!", 2, "command", []string{"x", "y z"}},
|
||||
{"!command", "!", 1, "command", []string{""}},
|
||||
{"hey man", "!", 1, "", []string{""}},
|
||||
{"!command", "!", 1, "command", []string{}},
|
||||
{"hey man", "!", 1, "", []string{}},
|
||||
}
|
||||
for _, table := range tables {
|
||||
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)
|
||||
}
|
||||
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", 3, []string{"a", "b", "c"}},
|
||||
{"a b c", 4, []string{"a", "b", "c"}},
|
||||
{"", 0, []string{}},
|
||||
}
|
||||
for _, table := range tables {
|
||||
if got, want := SplitArgs(table.s, table.n), table.want; !reflect.DeepEqual(got, want) {
|
||||
|
Loading…
Reference in New Issue
Block a user