Browse Source

Add all the bot logic

main
kolaente 5 months ago
parent
commit
5e9f3d2c75
Signed by: konrad GPG Key ID: F40E70337AB24C9B
  1. 5
      go.mod
  2. 8
      go.sum
  3. 159
      main.go

5
go.mod

@ -1,3 +1,8 @@
module kolaente.dev/konrad/discord-kaenguru
go 1.16
require (
github.com/bwmarrin/discordgo v0.23.2 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
)

8
go.sum

@ -0,0 +1,8 @@
github.com/bwmarrin/discordgo v0.23.2 h1:BzrtTktixGHIu9Tt7dEE6diysEF9HWnXeHuoJEt2fH4=
github.com/bwmarrin/discordgo v0.23.2/go.mod h1:c1WtWUGN6nREDmzIpyTp/iD3VYt4Fpx+bVyfBG7JE+M=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA=
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=

159
main.go

@ -0,0 +1,159 @@
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 {
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)
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))
}
Loading…
Cancel
Save