Improve ban records system
All checks were successful
Build and push container image / build (push) Successful in 39s

This commit is contained in:
toast 2023-10-25 12:08:44 +11:00
parent bfe787b632
commit 9d9704baa4
3 changed files with 50 additions and 17 deletions

View File

@ -168,7 +168,25 @@ func ListenForCommand(e *events.ApplicationCommandInteractionCreate) {
Embeds: []discord.Embed{ Embeds: []discord.Embed{
{ {
Description: "Ban records on Google Sheets will now be sent in this channel.\n" + 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, Color: mainEmbedColor,
}, },
}, },

View File

@ -3,7 +3,7 @@ package main
import ( import (
botEvents "corn-util/bot/events" botEvents "corn-util/bot/events"
"corn-util/bot/loaders" "corn-util/bot/loaders"
"corn-util/bot/toolbox" "corn-util/bot/utils"
"os" "os"
"os/signal" "os/signal"
"runtime" "runtime"
@ -55,7 +55,7 @@ func main() {
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
toolbox.RetrieveSheetData() utils.BanRecordsSheet()
} }
} }
}() }()

View File

@ -1,4 +1,4 @@
package toolbox package utils
import ( import (
"context" "context"
@ -26,9 +26,10 @@ var (
type SpreadsheetCells struct { type SpreadsheetCells struct {
UUID string UUID string
IGN string IGN string
Reason string
} }
func RetrieveSheetData() { func BanRecordsSheet() {
// Read the required values from config store // Read the required values from config store
type jsonDataStruct struct { type jsonDataStruct struct {
GoogleSpreadsheetID string `json:"googleSpreadsheetId"` GoogleSpreadsheetID string `json:"googleSpreadsheetId"`
@ -64,41 +65,53 @@ func RetrieveSheetData() {
return return
} }
cachedUUIDsDATA, err := os.ReadFile(uuidCacheFile) cachedUUIDsData, err := os.ReadFile(uuidCacheFile)
if err != nil { if err != nil {
log.Errorf("error reading uuidCacheFile: %v", err) log.Errorf("error reading uuidCacheFile: %v", err)
return return
} }
cachedUUIDs := make(map[string]bool) cachedUUIDs := make(map[string]bool)
for _, line := range strings.Split(string(cachedUUIDsDATA), "\n") { for _, line := range strings.Split(string(cachedUUIDsData), "\n") {
cachedUUIDs[line] = true cachedUUIDs[line] = true
} }
discordEmbedData := discord.Embed{Color: recordAddedColor} var discordEmbedSlice []discord.Embed
for _, row := range rowData { for _, row := range rowData {
if len(row.Values) < 2 { if len(row.Values) < 3 {
continue continue
} }
uuid := row.Values[0].FormattedValue uuid := row.Values[0].FormattedValue
ign := row.Values[1].FormattedValue ign := row.Values[1].FormattedValue
if uuid == "UUID" && ign == "IGN" { reason := row.Values[2].FormattedValue
if uuid == "UUID" && ign == "IGN" && reason == "Reason" {
continue continue
} }
if cachedUUIDs[uuid] && (ign == "" || reason == "") {
os.WriteFile(uuidCacheFile, []byte(strings.ReplaceAll(string(cachedUUIDsData), fmt.Sprintf("%v", uuid), "")), 0644)
}
if cachedUUIDs[uuid] { if cachedUUIDs[uuid] {
continue continue
} }
field := discord.EmbedField{Name: ign, Value: fmt.Sprintf("`%v`", uuid)} discordEmbedData := discord.Embed{Color: recordAddedColor}
discordEmbedData.Title = "Ban record added" 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 cachedUUIDs[uuid] = true
} }
var sb strings.Builder var sb strings.Builder
for uuid := range cachedUUIDs { for uuid := range cachedUUIDs {
sb.WriteString(uuid) sb.WriteString(fmt.Sprintf("%v\n", uuid))
sb.WriteString("\n")
} }
err = os.WriteFile(uuidCacheFile, []byte(sb.String()), 0644) err = os.WriteFile(uuidCacheFile, []byte(sb.String()), 0644)
if err != nil { if err != nil {
@ -107,5 +120,7 @@ func RetrieveSheetData() {
} }
channel, _ := rest.Channels.GetChannel(rest.NewChannels(rest.NewClient(loaders.TokenLoader("bot"))), snowflake.MustParse(jsonData.BanRecords)) 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}}) rest.Channels.CreateMessage(rest.NewChannels(rest.NewClient(loaders.TokenLoader("bot"))), channel.ID(), discord.MessageCreate{Embeds: []discord.Embed{discordEmbedData}})
}
} }