Compare commits

..

8 Commits

10 changed files with 259 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package bot
import (
"errors"
"fmt"
"os"
"os/signal"
@ -54,6 +55,16 @@ func (b *Bot) RegisterCommands() {
Func: b.RollCommand(),
NArgs: 1,
})
AddCommand(&Command{
Name: "rps",
Func: b.RpsCommand(),
NArgs: 1,
})
AddCommand(&Command{
Name: "rpsls",
Func: b.RpslsCommand(),
NArgs: 1,
})
AddCommand(&Command{
Name: "time",
Func: b.TimeCommand(),
@ -84,12 +95,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 +110,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.")

View File

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

44
bot/rps.go Normal file
View File

@ -0,0 +1,44 @@
package bot
import (
"fmt"
"strings"
"git.kill0.net/chill9/beepboop/lib"
"git.kill0.net/chill9/beepboop/lib/rps"
"github.com/bwmarrin/discordgo"
)
func (b *Bot) RpsCommand() CommandFunc {
return func(args []string, m *discordgo.MessageCreate) error {
var (
bc, pc, be, pe string
)
if len(args) != 1 {
b.Session.ChannelMessageSend(
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
)
return nil
}
pc = strings.ToLower(args[0])
_, ok := rps.EmojiMapRps[pc] // player's choice
if !ok {
b.Session.ChannelMessageSend(
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
)
return nil
}
bc = lib.MapRandKey(rps.EmojiMapRps) // bot's choice
pe = rps.EmojiMapRps[pc] // player's emoji
be = rps.EmojiMapRps[bc] // bot's emoji
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
"%s v %s: %s", pe, be, rps.Play(rps.RulesRps, bc, pc),
))
return nil
}
}

44
bot/rpsls.go Normal file
View File

@ -0,0 +1,44 @@
package bot
import (
"fmt"
"strings"
"git.kill0.net/chill9/beepboop/lib"
"git.kill0.net/chill9/beepboop/lib/rps"
"github.com/bwmarrin/discordgo"
)
func (b *Bot) RpslsCommand() CommandFunc {
return func(args []string, m *discordgo.MessageCreate) error {
var (
bc, pc, be, pe string
)
if len(args) != 1 {
b.Session.ChannelMessageSend(
m.ChannelID, "help: `!rpsls (rock | paper | scissors | lizard | spock)`",
)
return nil
}
pc = strings.ToLower(args[0])
_, ok := rps.EmojiMapRpsls[pc] // player's choice
if !ok {
b.Session.ChannelMessageSend(
m.ChannelID, "help: `!rpsls (rock | paper | scissors | lizard | spock)`",
)
return nil
}
bc = lib.MapRandKey(rps.EmojiMapRpsls) // bot's choice
pe = rps.EmojiMapRpsls[pc] // player's emoji
be = rps.EmojiMapRpsls[bc] // bot's emoji
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
"%s v %s: %s", pe, be, rps.Play(rps.RulesRpsls, bc, pc),
))
return nil
}
}

View File

@ -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)
}
}

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) {
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 {
@ -133,3 +141,21 @@ func SplitArgs(s string, n int) (args []string) {
}
return
}
func MapKeys[K comparable, V any](m map[K]V) []K {
keys := make([]K, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
func MapKey[K comparable, V comparable](m map[K]V, v V) K {
var r K
for k := range m {
if m[k] == v {
return k
}
}
return r
}

View File

@ -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) {
@ -122,3 +123,37 @@ func TestSplitArgs(t *testing.T) {
}
}
}
func TestMapKeys(t *testing.T) {
tables := []struct {
m map[string]int
want []string
}{
{map[string]int{"a": 0, "b": 1, "c": 3}, []string{"a", "b", "c"}},
}
for _, table := range tables {
if got, want := MapKeys(table.m), table.want; !reflect.DeepEqual(got, want) {
t.Errorf("got: %#v, want: %#v", got, want)
}
}
}
func TestMapKey(t *testing.T) {
tables := []struct {
m map[string]int
n int
want string
}{
{map[string]int{"a": 0, "b": 1, "c": 2}, 0, "a"},
{map[string]int{"a": 0, "b": 1, "c": 2}, 1, "b"},
{map[string]int{"a": 0, "b": 1, "c": 2}, 2, "c"},
{map[string]int{"a": 0, "b": 1, "c": 2, "d": 0}, 0, "a"},
}
for _, table := range tables {
if got, want := MapKey(table.m, table.n), table.want; got != want {
t.Errorf("got: %#v, want: %#v", got, want)
}
}
}

View File

@ -37,3 +37,21 @@ func SeedMathRand() error {
func RandInt(min int, max int) int {
return rand.Intn(max-min+1) + min
}
func MapRand[K comparable, V any](m map[K]V) V {
n := rand.Intn(len(m))
i := 0
for _, v := range m {
if i == n {
return v
}
i++
}
panic("unreachable")
}
func MapRandKey[K comparable, V any](m map[K]V) K {
keys := MapKeys(m)
n := rand.Intn(len(m))
return keys[n]
}

62
lib/rps/rps.go Normal file
View File

@ -0,0 +1,62 @@
package rps
import (
"fmt"
)
type (
Game struct {
rules [][]string
emojiMap map[string]string
}
)
var (
RulesRps [][]string = [][]string{
{"rock", "scissors", "crushes"},
{"paper", "rock", "covers"},
{"scissors", "paper", "cuts"},
}
EmojiMapRps map[string]string = map[string]string{
"rock": "🪨️",
"paper": "📝",
"scissors": "✂️",
}
RulesRpsls [][]string = [][]string{
{"rock", "scissors", "crushes"},
{"rock", "lizard", "crushes"},
{"paper", "rock", "covers"},
{"paper", "spock", "disproves"},
{"scissors", "paper", "cuts"},
{"scissors", "lizard", "decapitates"},
{"lizard", "paper", "eats"},
{"lizard", "spock", "poisons"},
{"spock", "scissors", "smashes"},
{"spock", "rock", "vaporizes"},
}
EmojiMapRpsls map[string]string = map[string]string{
"rock": "🪨️",
"paper": "📝",
"scissors": "✂️",
"lizard": "🦎",
"spock": "🖖",
}
)
func Play(rules [][]string, c1, c2 string) string {
for _, rule := range rules {
if c1 == c2 {
return "draw"
}
if c1 == rule[0] && c2 == rule[1] {
return fmt.Sprintf("%s %s %s", c1, rule[2], c2)
} else if c2 == rule[0] && c1 == rule[1] {
return fmt.Sprintf("%s %s %s", c2, rule[2], c1)
}
}
return ""
}

View File

@ -22,6 +22,12 @@ type (
H1 float32 `json:"1h"`
H3 float32 `json:"3h"`
} `json:"rain"`
Weather []struct {
Main string `json:"main"`
Description string `json:"description"`
Icon string `json:"icon"`
} `json:"weather"`
}
WeatherError struct {