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.

115 lines
2.8 KiB

4 years ago
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"},
":fries:": {"pommes"},
":hamburger:": {"burger"},
":fish:": {"lachs", "filet", "fisch", "kabeljau"},
":apple:": {"apfel", "äpfel"},
":poultry_leg:": {"hänchen"},
":meat_on_bone:": {"schnitzel"},
":spaghetti:": {"nudel", "spaghetti", "pasta"},
}
)
4 years ago
func main() {
if len(os.Args) <= 0 {
println("Please add token as cli argument.")
return
}
dg, err := discordgo.New(fmt.Sprintf("Bot %s", os.Args[1]))
canteens, err := openmensa.GetCanteens(175)
if err != nil {
fmt.Println("error creating discord bot: ", 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" {
sendMealsForDate(s, time.Now(), 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")
4 years ago
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 += " "
}
4 years ago
messages[i] = &discordgo.MessageEmbedField{
Name: prefix + meal.Name,
4 years ago
Value: fmt.Sprintf("%.2f€", *meal.Prices.Students),
}
if len(meal.Notes) > 0 {
footer += fmt.Sprintf("[%d] %s\n", i + 1, strings.Join(meal.Notes, ", "))
}
}
s.ChannelMessageSendComplex(channel, &discordgo.MessageSend{
4 years ago
Embed: &discordgo.MessageEmbed{
Title: fmt.Sprintf("** :spaghetti: 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,
},
},
})
}