This repository has been archived on 2024-05-11. You can view files and clone it, but cannot push or open issues or pull requests.
Corn-Utility/toolbox/google.go

112 lines
2.9 KiB
Go
Raw Normal View History

2023-10-23 03:36:40 -04:00
package toolbox
import (
"context"
"corn-util/bot/loaders"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/rest"
"github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2"
"golang.org/x/oauth2/google"
"gopkg.in/Iwark/spreadsheet.v2"
)
var (
TRUE = true
recordAddedColor = 0x57f287
recordRemovedColor = 0xed4245
uuidCacheFile = "uuidCache.log"
)
type SpreadsheetCells struct {
UUID string
IGN string
}
func RetrieveSheetData() {
// Read the required values from config store
type jsonDataStruct struct {
GoogleSpreadsheetID string `json:"googleSpreadsheetId"`
BanRecords string `json:"banRecords"`
}
readData, _ := loaders.DataLoader.Read(&loaders.JSON{}, "config.json")
jsonData := jsonDataStruct{}
jsonDataBytes, _ := json.Marshal(readData)
json.Unmarshal(jsonDataBytes, &jsonData)
spreadsheetId := jsonData.GoogleSpreadsheetID
ServiceAccount, _ := os.ReadFile("GCP_SERVICE.json")
conf, err := google.JWTConfigFromJSON(ServiceAccount, spreadsheet.Scope)
if err != nil {
log.Errorf("error creating JWT config: %v", err)
return
}
client := conf.Client(context.TODO())
service := spreadsheet.NewServiceWithClient(client)
log.Infof("[BAN-RECORDS] Checking for new data...")
spreadsheet, err := service.FetchSpreadsheet(spreadsheetId)
if err != nil {
log.Errorf("error fetching spreadsheet: %v", err)
return
}
rowData := spreadsheet.Sheets[0].Data.GridData[0].RowData
if len(rowData) < 2 {
fmt.Println("not enough data in spreadsheet")
return
}
cachedUUIDsDATA, err := os.ReadFile(uuidCacheFile)
if err != nil {
log.Errorf("error reading uuidCacheFile: %v", err)
return
}
cachedUUIDs := make(map[string]bool)
for _, line := range strings.Split(string(cachedUUIDsDATA), "\n") {
cachedUUIDs[line] = true
}
discordEmbedData := discord.Embed{Color: recordAddedColor}
for _, row := range rowData {
if len(row.Values) < 2 {
continue
}
uuid := row.Values[0].FormattedValue
ign := row.Values[1].FormattedValue
if uuid == "UUID" && ign == "IGN" {
continue
}
if cachedUUIDs[uuid] {
continue
}
field := discord.EmbedField{Name: ign, Value: fmt.Sprintf("`%v`", uuid)}
discordEmbedData.Title = "Ban record added"
discordEmbedData.Fields = append(discordEmbedData.Fields, field)
cachedUUIDs[uuid] = true
}
var sb strings.Builder
for uuid := range cachedUUIDs {
sb.WriteString(uuid)
sb.WriteString("\n")
}
err = os.WriteFile(uuidCacheFile, []byte(sb.String()), 0644)
if err != nil {
log.Errorf("error writing uuidCacheFile: %v", err)
return
}
channel, _ := rest.Channels.GetChannel(rest.NewChannels(rest.NewClient(loaders.TokenLoader("bot"))), snowflake.MustParse(jsonData.BanRecords))
rest.Channels.CreateMessage(rest.NewChannels(rest.NewClient(loaders.TokenLoader("bot"))), channel.ID(), discord.MessageCreate{Embeds: []discord.Embed{discordEmbedData}})
}