You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

141 lines
3.6 KiB

package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/kiliankoe/openmensa"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
var (
canteen *openmensa.Canteen
emojis = map[string][]string{
":pizza:": {"pizza"},
":hotdog:": {"wurst", "würstchen", "hotdog"},
":fries:": {"pommes"},
":hamburger:": {"burger"},
":fish:": {"lachs", "filet", "fisch", "kabeljau", "hering", "makrele", "sardine", "schellfisch", "seezunge"},
":apple:": {"apfel", "äpfel"},
":poultry_leg:": {"hähnchen"},
":meat_on_bone:": {"schnitzel", "steak", "schwein", "huhn", "geflügel"},
":spaghetti:": {"nudel", "spaghetti", "pasta"},
":bread:": {"brot", "brötchen"},
":rice:": {"reis"},
}
)
func main() {
token := os.Getenv("MENSABOT_TOKEN")
if (len(token) == 0 || token == "token") && len(os.Args) <= 0 {
fmt.Println("Please add token as environment variable (MENSABOT_TOKEN) or cli argument.")
return
}
if len(os.Args) > 1 {
token = os.Args[1]
}
dg, err := discordgo.New(fmt.Sprintf("Bot %s", token))
if err != nil {
fmt.Println("error creating discord bot: ", err)
return
}
canteens, err := openmensa.GetCanteens(175)
if err != nil {
fmt.Println("error getting mensa: ", err)
return
}
dg.AddHandler(messageCreate)
canteen = canteens[0]
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
// Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is 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.
dg.Close()
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
// This isn't required in this specific example but it's a good practice.
if m.Author.ID == s.State.User.ID {
return
}
if m.Content == "!mensa" {
date := time.Now()
switch date.Weekday() {
case time.Saturday:
date = date.AddDate(0, 0, -1)
break
case time.Sunday:
date = date.AddDate(0, 0, -2)
break
}
sendMealsForDate(s, date, m.ChannelID)
}
}
func sendMealsForDate(s *discordgo.Session, t time.Time, channel string) {
meals, err := canteen.GetMeals(t)
if err != nil {
s.ChannelMessageSend(channel, ":angry: an error occured")
fmt.Println("an error occured while getting the meals: ", err)
return
}
var messages = make([]*discordgo.MessageEmbedField, len(meals))
var footer = ""
for i, meal := range meals {
prefix := ""
outer:
for emoji, keywords := range emojis {
for _, keyword := range keywords {
if strings.Contains(strings.ToLower(meal.Name), keyword) {
prefix += emoji
continue outer
}
}
}
if len(prefix) > 0 {
prefix += " "
}
messages[i] = &discordgo.MessageEmbedField{
Name: prefix + meal.Name,
Value: fmt.Sprintf("%.2f€", *meal.Prices.Students),
}
if len(meal.Notes) > 0 {
footer += fmt.Sprintf("[%d] %s\n", i+1, strings.Join(meal.Notes, ", "))
}
}
_, err = s.ChannelMessageSendComplex(channel, &discordgo.MessageSend{
Embed: &discordgo.MessageEmbed{
Title: fmt.Sprintf("**Menü vom %s**", t.Format("02.01.2006")),
Fields: messages,
Footer: &discordgo.MessageEmbedFooter{
IconURL: "https://pbs.twimg.com/profile_images/643755515118505984/xzZMK7fU_400x400.png",
Text: footer,
},
},
})
if err != nil {
fmt.Println("an error occured while sending the meals: ", err)
}
}