package utils 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 Reason string } func BanRecordsSheet() { // 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 } var discordEmbedSlice []discord.Embed for _, row := range rowData { if len(row.Values) < 3 { continue } uuid := row.Values[0].FormattedValue ign := row.Values[1].FormattedValue reason := row.Values[2].FormattedValue if uuid == "UUID" && ign == "IGN" && reason == "Reason" { continue } if cachedUUIDs[uuid] && (ign == "" || reason == "") { os.WriteFile(uuidCacheFile, []byte(strings.ReplaceAll(string(cachedUUIDsData), fmt.Sprintf("%v", uuid), "")), 0644) } if cachedUUIDs[uuid] { continue } discordEmbedData := discord.Embed{Color: recordAddedColor} discordEmbedData.Title = "Ban record added" 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) cachedUUIDs[uuid] = true } var sb strings.Builder for uuid := range cachedUUIDs { sb.WriteString(fmt.Sprintf("%v\n", uuid)) } 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)) for _, discordEmbedData := range discordEmbedSlice { rest.Channels.CreateMessage(rest.NewChannels(rest.NewClient(loaders.TokenLoader("bot"))), channel.ID(), discord.MessageCreate{Embeds: []discord.Embed{discordEmbedData}}) } }