[+] improve random function
Some checks failed
Build and Push Docker Images / docker (push) Failing after 31s
Some checks failed
Build and Push Docker Images / docker (push) Failing after 31s
Signed-off-by: sunyz <i@sunyz.net>
This commit is contained in:
parent
6e650588c1
commit
b04e1fc72d
19
main.go
19
main.go
@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/realSunyz/lucky-tgbot/plugin/slash"
|
"github.com/realSunyz/lucky-tgbot/plugin/slash"
|
||||||
"github.com/realSunyz/lucky-tgbot/plugin/torf"
|
"github.com/realSunyz/lucky-tgbot/plugin/torf"
|
||||||
"log"
|
"log"
|
||||||
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -18,20 +19,24 @@ func main() {
|
|||||||
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
|
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
|
||||||
}
|
}
|
||||||
|
|
||||||
rebornData, err := reborn.InitRebornList("plugin/reborn/countries.json")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
b, err := tele.NewBot(pref)
|
b, err := tele.NewBot(pref)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error creating bot: ", err)
|
log.Fatal("Error creating bot: ", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rebornData, err := reborn.InitRebornList("plugin/reborn/countries.json")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error initializing rebornData: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
source := rand.NewSource(time.Now().UnixNano())
|
||||||
|
r := rand.New(source)
|
||||||
|
|
||||||
b.Handle("/reborn", func(c tele.Context) error {
|
b.Handle("/reborn", func(c tele.Context) error {
|
||||||
return reborn.Execute(c, rebornData)
|
return reborn.Execute(c, r, rebornData)
|
||||||
})
|
})
|
||||||
|
|
||||||
// b.Handle("/info", info.Execute)
|
// b.Handle("/info", info.Execute)
|
||||||
|
|
||||||
b.Handle(tele.OnText, func(c tele.Context) error {
|
b.Handle(tele.OnText, func(c tele.Context) error {
|
||||||
@ -40,7 +45,7 @@ func main() {
|
|||||||
if strings.HasPrefix(inputText, "/") {
|
if strings.HasPrefix(inputText, "/") {
|
||||||
return slash.Execute(c)
|
return slash.Execute(c)
|
||||||
} else {
|
} else {
|
||||||
return torf.Execute(c)
|
return torf.Execute(c, r)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
tele "gopkg.in/telebot.v3"
|
tele "gopkg.in/telebot.v3"
|
||||||
)
|
)
|
||||||
@ -60,10 +59,7 @@ func randGender(r *rand.Rand) string {
|
|||||||
return genders[r.Intn(len(genders))]
|
return genders[r.Intn(len(genders))]
|
||||||
}
|
}
|
||||||
|
|
||||||
func Execute(c tele.Context, data *RebornData) error {
|
func Execute(c tele.Context, r *rand.Rand, data *RebornData) error {
|
||||||
source := rand.NewSource(time.Now().UnixNano())
|
|
||||||
r := rand.New(source)
|
|
||||||
|
|
||||||
country, err := data.randCountry(r)
|
country, err := data.randCountry(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
tele "gopkg.in/telebot.v3"
|
tele "gopkg.in/telebot.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
func randResponse(saidType int) string {
|
func randResponse(saidType int, r *rand.Rand) string {
|
||||||
responsesMap := map[int][]string{
|
responsesMap := map[int][]string{
|
||||||
1: {"有", "没有"},
|
1: {"有", "没有"},
|
||||||
2: {"好", "不好"},
|
2: {"好", "不好"},
|
||||||
@ -16,24 +16,24 @@ func randResponse(saidType int) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if responses, exists := responsesMap[saidType]; exists {
|
if responses, exists := responsesMap[saidType]; exists {
|
||||||
return responses[rand.Intn(len(responses))]
|
return responses[r.Intn(len(responses))]
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func Execute(c tele.Context) error {
|
func Execute(c tele.Context, r *rand.Rand) error {
|
||||||
inputText := c.Text()
|
inputText := c.Text()
|
||||||
var outputText string
|
var outputText string
|
||||||
|
|
||||||
if strings.Contains(inputText, "有没有") {
|
if strings.Contains(inputText, "有没有") {
|
||||||
outputText = randResponse(1)
|
outputText = randResponse(1, r)
|
||||||
} else if strings.Contains(inputText, "好不好") {
|
} else if strings.Contains(inputText, "好不好") {
|
||||||
outputText = randResponse(2)
|
outputText = randResponse(2, r)
|
||||||
} else if strings.Contains(inputText, "是不是") {
|
} else if strings.Contains(inputText, "是不是") {
|
||||||
outputText = randResponse(3)
|
outputText = randResponse(3, r)
|
||||||
} else if strings.Contains(inputText, "尊嘟假嘟") {
|
} else if strings.Contains(inputText, "尊嘟假嘟") {
|
||||||
outputText = randResponse(4)
|
outputText = randResponse(4, r)
|
||||||
} else {
|
} else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user