Compare commits
7 Commits
master
...
333bc3b26c
Author | SHA1 | Date | |
---|---|---|---|
333bc3b26c
|
|||
a3137e5276
|
|||
7b3368eea4
|
|||
5a141be534
|
|||
49000133d8
|
|||
756aaf2379
|
|||
6bd4744745
|
12
bot/bot.go
12
bot/bot.go
@ -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.")
|
||||||
|
@ -5,7 +5,10 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
defaultReactions []string = []string{"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩", "🔥", "🍒", "🎉", "🥳", "🎊"}
|
defaultReactions []string = []string{
|
||||||
|
"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩",
|
||||||
|
"🔥", "🍒", "🎉", "🥳", "🎊", "📉", "📈", "💀", "☠️",
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
84
bot/rps.go
Normal file
84
bot/rps.go
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
package bot
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.kill0.net/chill9/beepboop/lib"
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Rock = iota
|
||||||
|
Paper
|
||||||
|
Scissors
|
||||||
|
Lizard
|
||||||
|
Spock
|
||||||
|
)
|
||||||
|
|
||||||
|
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 (b *Bot) RpsCommand() CommandFunc {
|
||||||
|
return func(args []string, m *discordgo.MessageCreate) error {
|
||||||
|
var (
|
||||||
|
bc, pc int
|
||||||
|
be, pe string
|
||||||
|
c string
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(args) != 1 {
|
||||||
|
b.Session.ChannelMessageSend(
|
||||||
|
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
|
||||||
|
)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
c = strings.ToLower(args[0])
|
||||||
|
|
||||||
|
pc, ok := rpsChoiceMap[c] // player's choice
|
||||||
|
if !ok {
|
||||||
|
b.Session.ChannelMessageSend(
|
||||||
|
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
bc = lib.MapRand(rpsChoiceMap) // bot's choice
|
||||||
|
pe = rpsEmojiMap[pc] // player's emoji
|
||||||
|
be = rpsEmojiMap[bc] // bot's emoji
|
||||||
|
|
||||||
|
if bc == pc {
|
||||||
|
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||||
|
"%s v %s: draw", be, pe,
|
||||||
|
))
|
||||||
|
return nil
|
||||||
|
} else if lib.Contains(rpsVictoryMap[bc], pc) {
|
||||||
|
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||||
|
"%s v %s: %s wins", be, pe, lib.MapKey(rpsChoiceMap, bc),
|
||||||
|
))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||||
|
"%s v %s: %s wins", be, pe, lib.MapKey(rpsChoiceMap, pc),
|
||||||
|
))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
82
bot/rpsls.go
Normal file
82
bot/rpsls.go
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
package bot
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.kill0.net/chill9/beepboop/lib"
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
rpslsVictoryMap map[int][]int = map[int][]int{
|
||||||
|
Rock: {Scissors, Lizard},
|
||||||
|
Paper: {Rock, Spock},
|
||||||
|
Scissors: {Paper, Lizard},
|
||||||
|
Lizard: {Paper, Spock},
|
||||||
|
Spock: {Scissors, Rock},
|
||||||
|
}
|
||||||
|
|
||||||
|
rpslsChoiceMap map[string]int = map[string]int{
|
||||||
|
"rock": Rock,
|
||||||
|
"paper": Paper,
|
||||||
|
"scissors": Scissors,
|
||||||
|
"lizard": Lizard,
|
||||||
|
"spock": Spock,
|
||||||
|
}
|
||||||
|
|
||||||
|
rpslsEmojiMap map[int]string = map[int]string{
|
||||||
|
Rock: "🪨️",
|
||||||
|
Paper: "📝",
|
||||||
|
Scissors: "✂️",
|
||||||
|
Lizard: "🦎",
|
||||||
|
Spock: "🖖",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func (b *Bot) RpslsCommand() CommandFunc {
|
||||||
|
return func(args []string, m *discordgo.MessageCreate) error {
|
||||||
|
var (
|
||||||
|
bc, pc int
|
||||||
|
be, pe string
|
||||||
|
c string
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(args) != 1 {
|
||||||
|
b.Session.ChannelMessageSend(
|
||||||
|
m.ChannelID, "help: `!rpsls (rock | paper | scissors | lizard | spock )`",
|
||||||
|
)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
c = strings.ToLower(args[0])
|
||||||
|
|
||||||
|
pc, ok := rpslsChoiceMap[c] // player's choice
|
||||||
|
if !ok {
|
||||||
|
b.Session.ChannelMessageSend(
|
||||||
|
m.ChannelID, "help: `!rpsls (rock | paper | scissors | lizard | spock)`",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
bc = lib.MapRand(rpslsChoiceMap) // bot's choice
|
||||||
|
pe = rpslsEmojiMap[pc] // player's emoji
|
||||||
|
be = rpslsEmojiMap[bc] // bot's emoji
|
||||||
|
|
||||||
|
if bc == pc {
|
||||||
|
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||||
|
"%s v %s: draw", be, pe,
|
||||||
|
))
|
||||||
|
return nil
|
||||||
|
} else if lib.Contains(rpslsVictoryMap[bc], pc) {
|
||||||
|
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||||
|
"%s v %s: %s wins", be, pe, lib.MapKey(rpslsChoiceMap, bc),
|
||||||
|
))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||||
|
"%s v %s: %s wins", be, pe, lib.MapKey(rpslsChoiceMap, pc),
|
||||||
|
))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
@ -133,3 +141,21 @@ func SplitArgs(s string, n int) (args []string) {
|
|||||||
}
|
}
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
@ -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) {
|
||||||
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
12
lib/rand.go
12
lib/rand.go
@ -37,3 +37,15 @@ func SeedMathRand() error {
|
|||||||
func RandInt(min int, max int) int {
|
func RandInt(min int, max int) int {
|
||||||
return rand.Intn(max-min+1) + min
|
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")
|
||||||
|
}
|
||||||
|
@ -22,6 +22,12 @@ type (
|
|||||||
H1 float32 `json:"1h"`
|
H1 float32 `json:"1h"`
|
||||||
H3 float32 `json:"3h"`
|
H3 float32 `json:"3h"`
|
||||||
} `json:"rain"`
|
} `json:"rain"`
|
||||||
|
|
||||||
|
Weather []struct {
|
||||||
|
Main string `json:"main"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Icon string `json:"icon"`
|
||||||
|
} `json:"weather"`
|
||||||
}
|
}
|
||||||
|
|
||||||
WeatherError struct {
|
WeatherError struct {
|
||||||
|
Reference in New Issue
Block a user