From 9d9704baa4cb36d47749eab27f3b611317e654e5 Mon Sep 17 00:00:00 2001 From: toast Date: Wed, 25 Oct 2023 12:08:44 +1100 Subject: [PATCH] Improve ban records system --- events/interaction.go | 20 ++++++++++++++++- main.go | 4 ++-- {toolbox => utils}/google.go | 43 ++++++++++++++++++++++++------------ 3 files changed, 50 insertions(+), 17 deletions(-) rename {toolbox => utils}/google.go (64%) diff --git a/events/interaction.go b/events/interaction.go index 1edde0f..8eb6952 100644 --- a/events/interaction.go +++ b/events/interaction.go @@ -168,7 +168,25 @@ func ListenForCommand(e *events.ApplicationCommandInteractionCreate) { Embeds: []discord.Embed{ { Description: "Ban records on Google Sheets will now be sent in this channel.\n" + - fmt.Sprintf("*Add the bot's service account to the spreadsheet for this to work:*\n`%v`", googleServiceAcc.ClientEmail), + fmt.Sprintf("*Add the bot's service account to the spreadsheet for this to work:*\n`%v`", googleServiceAcc.ClientEmail) + + "\n\nThe format of the spreadsheet needs to be exactly like this:", + Fields: []discord.EmbedField{ + { + Name: "A1", + Value: "UUID", + Inline: &TRUE, + }, + { + Name: "B1", + Value: "IGN", + Inline: &TRUE, + }, + { + Name: "C1", + Value: "Reason", + Inline: &TRUE, + }, + }, Color: mainEmbedColor, }, }, diff --git a/main.go b/main.go index e07284f..8e58469 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ package main import ( botEvents "corn-util/bot/events" "corn-util/bot/loaders" - "corn-util/bot/toolbox" + "corn-util/bot/utils" "os" "os/signal" "runtime" @@ -55,7 +55,7 @@ func main() { for { select { case <-ticker.C: - toolbox.RetrieveSheetData() + utils.BanRecordsSheet() } } }() diff --git a/toolbox/google.go b/utils/google.go similarity index 64% rename from toolbox/google.go rename to utils/google.go index 31a75b1..faade3b 100644 --- a/toolbox/google.go +++ b/utils/google.go @@ -1,4 +1,4 @@ -package toolbox +package utils import ( "context" @@ -24,11 +24,12 @@ var ( ) type SpreadsheetCells struct { - UUID string - IGN string + UUID string + IGN string + Reason string } -func RetrieveSheetData() { +func BanRecordsSheet() { // Read the required values from config store type jsonDataStruct struct { GoogleSpreadsheetID string `json:"googleSpreadsheetId"` @@ -64,41 +65,53 @@ func RetrieveSheetData() { return } - cachedUUIDsDATA, err := os.ReadFile(uuidCacheFile) + 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") { + for _, line := range strings.Split(string(cachedUUIDsData), "\n") { cachedUUIDs[line] = true } - discordEmbedData := discord.Embed{Color: recordAddedColor} + var discordEmbedSlice []discord.Embed for _, row := range rowData { - if len(row.Values) < 2 { + if len(row.Values) < 3 { continue } uuid := row.Values[0].FormattedValue ign := row.Values[1].FormattedValue - if uuid == "UUID" && ign == "IGN" { + 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 } - field := discord.EmbedField{Name: ign, Value: fmt.Sprintf("`%v`", uuid)} + discordEmbedData := discord.Embed{Color: recordAddedColor} discordEmbedData.Title = "Ban record added" - discordEmbedData.Fields = append(discordEmbedData.Fields, field) + 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(uuid) - sb.WriteString("\n") + sb.WriteString(fmt.Sprintf("%v\n", uuid)) } err = os.WriteFile(uuidCacheFile, []byte(sb.String()), 0644) if err != nil { @@ -107,5 +120,7 @@ func RetrieveSheetData() { } 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}}) + for _, discordEmbedData := range discordEmbedSlice { + rest.Channels.CreateMessage(rest.NewChannels(rest.NewClient(loaders.TokenLoader("bot"))), channel.ID(), discord.MessageCreate{Embeds: []discord.Embed{discordEmbedData}}) + } }