mirror of
https://github.com/idanoo/NZCovidBot
synced 2025-07-02 11:42:15 +00:00
Rework to use API instead of CSVs
This commit is contained in:
parent
b73a6b7cae
commit
4937ec7253
13 changed files with 290 additions and 675 deletions
|
@ -1,8 +1,10 @@
|
|||
package nzcovidbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/DisgoOrg/disgohook"
|
||||
"github.com/DisgoOrg/disgohook/api"
|
||||
|
@ -11,29 +13,66 @@ import (
|
|||
// Slice of discord webhooks
|
||||
var DiscordWebhooks []string
|
||||
|
||||
func postToDiscord(webhookString string, msg string) {
|
||||
if webhookString == "" {
|
||||
func postToDiscord() {
|
||||
postableDiscordData := getPostableDiscordData()
|
||||
if len(postableDiscordData) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
tokenParts := strings.Split(webhookString, "/")
|
||||
len := len(tokenParts)
|
||||
webhook, err := disgohook.NewWebhookClientByToken(nil, nil, tokenParts[len-2]+"/"+tokenParts[len-1])
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
for _, discordWebhook := range DiscordWebhooks {
|
||||
for _, postableData := range postableDiscordData {
|
||||
if discordWebhook != "" {
|
||||
tokenParts := strings.Split(discordWebhook, "/")
|
||||
len := len(tokenParts)
|
||||
|
||||
_, err = webhook.SendEmbeds(api.NewEmbedBuilder().
|
||||
SetDescription(msg).
|
||||
Build(),
|
||||
)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
// Build discord request
|
||||
webhook, err := disgohook.NewWebhookClientByToken(nil, nil, tokenParts[len-2]+"/"+tokenParts[len-1])
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
// Send discord message
|
||||
_, err = webhook.SendEmbeds(api.NewEmbedBuilder().
|
||||
SetDescription(postableData).
|
||||
Build(),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// getPostableDiscordData - Returns slices containing 20~ locations each
|
||||
// to send as separate messages
|
||||
func getPostableDiscordData() []string {
|
||||
// Create our slice of groups
|
||||
groups := make([]string, 0)
|
||||
if len(newLocations.Items) == 0 {
|
||||
return groups
|
||||
}
|
||||
|
||||
rows := make([]string, 0)
|
||||
for _, item := range newLocations.Items {
|
||||
rows = append(rows, getDiscordRow(item))
|
||||
|
||||
if len(rows) > 20 {
|
||||
groups = append(groups, strings.Join(rows, "\n"))
|
||||
rows = make([]string, 0)
|
||||
}
|
||||
}
|
||||
|
||||
return append(groups, strings.Join(rows, "\n"))
|
||||
}
|
||||
|
||||
// formatCsvDiscordRow Format the string to a tidy string for the interwebs
|
||||
func getDiscordRow(item ApiItem) string {
|
||||
return fmt.Sprintf("**%s** %s on _%s_",
|
||||
item.EventName, item.Location.Address, item.getDateString())
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue