Use environment variable instaed of cli parameters

master
Jonas Franz 6 years ago
parent ca54cb4468
commit 6f1f1a6089
  1. 45
      bot.go

@ -2,37 +2,39 @@ package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/kiliankoe/openmensa"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/kiliankoe/openmensa"
)
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"},
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"},
":spaghetti:": {"nudel", "spaghetti", "pasta"},
}
)
func main() {
if len(os.Args) <= 0 {
token := os.Getenv("MENSABOT_TOKEN")
if len(token) == 0 {
println("Please add token as cli argument.")
return
}
dg, err := discordgo.New(fmt.Sprintf("Bot %s", os.Args[1]))
dg, err := discordgo.New(fmt.Sprintf("Bot %s", token))
canteens, err := openmensa.GetCanteens(175)
if err != nil {
fmt.Println("error creating discord bot: ", err)
@ -72,7 +74,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
date = date.AddDate(0, 0, -1)
break
case time.Sunday:
date = date.AddDate(0,0, -2)
date = date.AddDate(0, 0, -2)
break
}
sendMealsForDate(s, date, m.ChannelID)
@ -90,11 +92,12 @@ func sendMealsForDate(s *discordgo.Session, t time.Time, channel string) {
for i, meal := range meals {
prefix := ""
outer: for emoji, keywords := range emojis {
outer:
for emoji, keywords := range emojis {
for _, keyword := range keywords {
if strings.Contains(strings.ToLower(meal.Name), keyword) {
prefix += emoji
continue outer;
continue outer
}
}
}
@ -104,21 +107,21 @@ func sendMealsForDate(s *discordgo.Session, t time.Time, channel string) {
}
messages[i] = &discordgo.MessageEmbedField{
Name: prefix + meal.Name,
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, ", "))
footer += fmt.Sprintf("[%d] %s\n", i+1, strings.Join(meal.Notes, ", "))
}
}
s.ChannelMessageSendComplex(channel, &discordgo.MessageSend{
Embed: &discordgo.MessageEmbed{
Title: fmt.Sprintf("**Menü vom %s**", t.Format("02.01.2006")),
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,
Text: footer,
},
},
})
}
}

Loading…
Cancel
Save