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{
{
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,
},
},

View File

@ -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()
}
}
}()

View File

@ -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}})
}
}