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/utils/google.go

127 lines
3.4 KiB
Go
Raw Normal View History

2023-10-24 21:08:44 -04:00
package utils
2023-10-23 03:36:40 -04:00
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 {
2023-10-24 21:08:44 -04:00
UUID string
IGN string
Reason string
2023-10-23 03:36:40 -04:00
}
2023-10-24 21:08:44 -04:00
func BanRecordsSheet() {
2023-10-23 03:36:40 -04:00
// 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
}
2023-10-24 21:08:44 -04:00
cachedUUIDsData, err := os.ReadFile(uuidCacheFile)
2023-10-23 03:36:40 -04:00
if err != nil {
log.Errorf("error reading uuidCacheFile: %v", err)
return
}
cachedUUIDs := make(map[string]bool)
2023-10-24 21:08:44 -04:00
for _, line := range strings.Split(string(cachedUUIDsData), "\n") {
2023-10-23 03:36:40 -04:00
cachedUUIDs[line] = true
}
2023-10-24 21:08:44 -04:00
var discordEmbedSlice []discord.Embed
2023-10-23 03:36:40 -04:00
for _, row := range rowData {
2023-10-24 21:08:44 -04:00
if len(row.Values) < 3 {
2023-10-23 03:36:40 -04:00
continue
}
uuid := row.Values[0].FormattedValue
ign := row.Values[1].FormattedValue
2023-10-24 21:08:44 -04:00
reason := row.Values[2].FormattedValue
if uuid == "UUID" && ign == "IGN" && reason == "Reason" {
2023-10-23 03:36:40 -04:00
continue
}
2023-10-24 21:08:44 -04:00
if cachedUUIDs[uuid] && (ign == "" || reason == "") {
os.WriteFile(uuidCacheFile, []byte(strings.ReplaceAll(string(cachedUUIDsData), fmt.Sprintf("%v", uuid), "")), 0644)
}
2023-10-23 03:36:40 -04:00
if cachedUUIDs[uuid] {
continue
}
2023-10-24 21:08:44 -04:00
discordEmbedData := discord.Embed{Color: recordAddedColor}
2023-10-23 03:36:40 -04:00
discordEmbedData.Title = "Ban record added"
2023-10-24 21:08:44 -04:00
discordEmbedData.Description = fmt.Sprintf("**UUID:**\n`%v`", uuid)
if ign != "" {
discordEmbedData.Description += fmt.Sprintf("\n**Player:**\n`%v`", ign)
} else {
continue
}
if reason != "" {
discordEmbedData.Description += fmt.Sprintf("\n**Reason:**\n`%v`", reason)
}
discordEmbedSlice = append(discordEmbedSlice, discordEmbedData)
2023-10-23 03:36:40 -04:00
cachedUUIDs[uuid] = true
}
var sb strings.Builder
for uuid := range cachedUUIDs {
2023-10-24 21:08:44 -04:00
sb.WriteString(fmt.Sprintf("%v\n", uuid))
2023-10-23 03:36:40 -04:00
}
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))
2023-10-24 21:08:44 -04:00
for _, discordEmbedData := range discordEmbedSlice {
rest.Channels.CreateMessage(rest.NewChannels(rest.NewClient(loaders.TokenLoader("bot"))), channel.ID(), discord.MessageCreate{Embeds: []discord.Embed{discordEmbedData}})
}
2023-10-23 03:36:40 -04:00
}