165 lines
3.9 KiB
Go
165 lines
3.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/bwmarrin/discordgo"
|
|
"github.com/dustin/go-humanize"
|
|
"math/rand"
|
|
"os"
|
|
"os/signal"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
tokenEnvName = `DISCORD_TOKEN`
|
|
numberRegExConst = `([0-9](\.|,)?)+`
|
|
dmFactor = 2
|
|
omFactor = 2
|
|
omBlackMarketFactor = 4
|
|
davonHätteManDieDDREntschuldenKönnen = 172_000_000_000 * dmFactor * omFactor
|
|
)
|
|
|
|
var (
|
|
moneyRegEx = regexp.MustCompile(`((kostet ?` + numberRegExConst + `)|(` + numberRegExConst + ` ?(€|euro)))`)
|
|
numberRegEx = regexp.MustCompile(numberRegExConst)
|
|
)
|
|
|
|
func init() {
|
|
rand.Seed(time.Now().Unix())
|
|
}
|
|
|
|
func main() {
|
|
bot, err := createBot()
|
|
if err != nil {
|
|
fmt.Println("Could not create bot:", err)
|
|
return
|
|
}
|
|
|
|
bot.AddHandler(messageOstmarkHandler)
|
|
err = bot.Open()
|
|
|
|
if err != nil {
|
|
fmt.Println("Error opening Discord session: ", err)
|
|
return
|
|
}
|
|
|
|
// Wait here until CTRL-C or other term signal is received.
|
|
fmt.Println("Now running. Press CTRL-C to exit.")
|
|
sc := make(chan os.Signal, 1)
|
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
|
|
<-sc
|
|
|
|
// Cleanly close down the Discord session.
|
|
_ = bot.Close()
|
|
|
|
fmt.Println("Bye!")
|
|
}
|
|
|
|
func createBot() (bot *discordgo.Session, err error) {
|
|
token := os.Getenv(tokenEnvName)
|
|
if token == "" {
|
|
return nil, fmt.Errorf("token cannot be empty. Please set the " + tokenEnvName + " env variable")
|
|
}
|
|
|
|
return discordgo.New("Bot " + token)
|
|
}
|
|
|
|
func messageOstmarkHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
|
|
if m.Author.ID == s.State.User.ID || m.Author.Bot {
|
|
return
|
|
}
|
|
|
|
match := moneyRegEx.FindString(m.Message.Content)
|
|
|
|
if len(match) == 0 {
|
|
return
|
|
}
|
|
|
|
amountMatch := numberRegEx.FindString(match)
|
|
amountMatch = strings.ReplaceAll(amountMatch, ",", ".")
|
|
amount, err := strconv.ParseFloat(amountMatch, 64)
|
|
if err != nil {
|
|
fmt.Println("Error parsing amount:", err)
|
|
return
|
|
}
|
|
|
|
fmt.Println("Found amount: ", amount)
|
|
|
|
if amount <= 0 {
|
|
s.ChannelMessageSend(m.ChannelID, "Nice try")
|
|
return
|
|
}
|
|
|
|
messages := []string{
|
|
"Ganze " + fmtCommaf(amount) + " €!",
|
|
}
|
|
|
|
currentMax := amount
|
|
stuffNum := 5 + rand.Int63n(95)
|
|
iter := 0
|
|
for {
|
|
messages = append(messages,
|
|
fmt.Sprintf("%s D-Mark", fmtCommaf(currentMax*dmFactor)),
|
|
fmt.Sprintf("%s Ostmark", fmtCommaf(currentMax*dmFactor*omFactor)),
|
|
fmt.Sprintf("%s Ostmark auf dem Schwarzmarkt", fmtCommaf(currentMax*dmFactor*omBlackMarketFactor)),
|
|
)
|
|
|
|
if currentMax > davonHätteManDieDDREntschuldenKönnen {
|
|
currentMax = currentMax * dmFactor * omBlackMarketFactor
|
|
break
|
|
}
|
|
|
|
currentMax = float64(stuffNum) * currentMax
|
|
|
|
messages = append(messages,
|
|
fmt.Sprintf("Dafür hätte man das gleiche Ding damals in der DDR bestimmt %s mal bekommen", fmtComma(stuffNum)),
|
|
fmt.Sprintf("Wenn man das heute %s mal kaufen will, müsste man %s € dafür ausgeben", fmtComma(stuffNum), fmtCommaf(currentMax)),
|
|
)
|
|
stuffNum = rand.Int63n(30) * stuffNum
|
|
|
|
if iter == 2 {
|
|
messages = append(messages, "Ich weiß was ihr denkt:")
|
|
}
|
|
|
|
iter++
|
|
}
|
|
|
|
messages = append(messages,
|
|
fmt.Sprintf("%s Ostmark!", fmtCommaf(currentMax)),
|
|
"_Davon hätte man die DDR entschulden können!_",
|
|
"So teuer ist das alles geworden!",
|
|
)
|
|
|
|
err = sendLoop(s, m.ChannelID, messages)
|
|
if err != nil {
|
|
fmt.Println("Could not send message: ", err)
|
|
}
|
|
}
|
|
|
|
func sendLoop(s *discordgo.Session, channelID string, messages []string) error {
|
|
for _, message := range messages {
|
|
//time.Sleep(time.Duration(100+rand.Intn(1500)) * time.Millisecond)
|
|
if _, err := s.ChannelMessageSend(channelID, message); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func fmtCommaf(num float64) string {
|
|
if (num - float64(int64(num))) > 0 {
|
|
return humanize.FormatFloat("#.###,##", num)
|
|
}
|
|
|
|
return humanize.FormatFloat("#.###,", num)
|
|
}
|
|
|
|
func fmtComma(num int64) string {
|
|
return fmtCommaf(float64(num))
|
|
}
|