Skip to content

Commit

Permalink
Autocomplete and series billboards
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamH18 committed Oct 29, 2023
1 parent d378a09 commit 44dccb5
Show file tree
Hide file tree
Showing 11 changed files with 814 additions and 120 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ docker compose up -d
| /remove_series_channel | admin only | Deregister a channel with a given series, channel is not deleted |
| /add_user | admin only | Register a user as a member of the group |
| /remove_user | admin only | Remove a user from the group, deletes all related settings. User is not kicked |
| /remove_user_by_id | admin only | Remove a user from the group by manually input ID, deletes all related settings. User is not kicked |
| /server_users | admin only | See all registered users on the server |
| /add_job | admin only | Register a new job type for the group |
| /add_global_job | owner only | Register a new job type for all users |
Expand Down
79 changes: 73 additions & 6 deletions bot/billboards.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,87 @@
package bot

import (
"fmt"
"log"
"scanlation-discord-bot/database"

"github.com/bwmarrin/discordgo"
)

func UpdateSeriesBillboard(series string, guild string) {
log.Printf("Updating series billboard for series %s and guild %s\n", series, guild)
bill, channel, err := database.Repo.GetSeriesBillboard(series, guild)
if err != nil {
log.Println("Error getting billboard message: " + err.Error())
return
} else if bill == "" {
log.Println("Server does not have a billboard for this series")
return
}

//Billboard should be edited, so gather data
serData, err := database.Repo.GetAllSeriesInfo(series, guild)
if err != nil {
log.Println("Error getting series info for billboard: " + err.Error())
return
}
notes, _, err := database.Repo.GetSeriesNotes(series, guild)
if err != nil {
log.Println("Error getting notes info for billboard: " + err.Error())
notes = []string{}
}
assMap, err := database.Repo.GetSeriesAssignments(series, guild)
if err != nil {
log.Println("Error getting series assignment info: " + err.Error())
return
}

//Build embeds
serInfoEmb := BuildSeriesInfoEmbed(serData, notes)
serAssEmb, err := BuildSeriesAssignmentsEmbed(assMap, series, guild)
if err != nil {
log.Println("Error building assignments embed: " + err.Error())
return
}

message := discordgo.MessageEdit{
Content: &emptyStr,
Embeds: []*discordgo.MessageEmbed{serInfoEmb, serAssEmb},
ID: bill,
Channel: channel,
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.Button{
Label: "Add Note",
Style: discordgo.PrimaryButton,
CustomID: fmt.Sprintf("note_add_button %s", series),
},
discordgo.Button{
Label: "Remove Note",
Style: discordgo.DangerButton,
CustomID: fmt.Sprintf("note_remove_button %s", series),
},
},
},
},
}
_, err = goBot.ChannelMessageEditComplex(&message)
if err != nil {
log.Println("Error editing message: " + err.Error())
}
}

func UpdateAllSeriesBillboards(guild string) {

log.Printf("Updating all series billboards in guild %s\n", guild)
allBill, err := database.Repo.GetAllSeriesBillboards(guild)
if err != nil {
log.Println("Error getting all billboards: " + err.Error())
return
}
for _, bill := range allBill {
go UpdateSeriesBillboard(bill, guild)
}
}

// Edit existing assignments billboard message to reflect new data
Expand Down Expand Up @@ -41,6 +110,7 @@ func UpdateAssignmentsBillboard(guild string) {
}

message := discordgo.MessageEdit{
Content: &emptyStr,
Embeds: []*discordgo.MessageEmbed{embed},
ID: bill,
Channel: channel,
Expand Down Expand Up @@ -70,13 +140,10 @@ func UpdateColorsBillboard(guild string) {
return
}

embed, err := BuildColorsEmbed(assMap, guild)
if err != nil {
log.Println("Error building embed: " + err.Error())
return
}
embed := BuildColorsEmbed(assMap, guild)

message := discordgo.MessageEdit{
Content: &emptyStr,
Embeds: []*discordgo.MessageEmbed{embed},
ID: bill,
Channel: channel,
Expand Down
61 changes: 49 additions & 12 deletions bot/buildtables.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,21 @@ import (
"github.com/bwmarrin/discordgo"
)

// TODO: Fix tables to not error on user retrieval errors, just don't list that user

// Returns table with all values in reminders DB included
func BuildVerboseRemindersTable(rems []database.Reminder) (string, error) {
func BuildVerboseRemindersTable(rems []database.Reminder) string {
var buf strings.Builder
w := tabwriter.NewWriter(&buf, 1, 1, 1, ' ', 0)
fmt.Fprintln(w, "Reminders for all users:\n\nID\tGuild\tChannel\tUser\tDays\tMessage\tRepeat\tTime\t")
for _, rem := range rems {
usr, err := GetUserName(rem.Guild, rem.User)
if err != nil {
return "", err
log.Printf("Error retrieving username for user %s from guild %s: %s\n", rem.User, rem.Guild, err.Error())
usr = ""
}
fmt.Fprintf(w, "%d\t%s\t%s\t%s\t%d\t%s\t%t\t%s\t\n", rem.ID, rem.Guild, rem.Channel, usr, rem.Days, rem.Message, rem.Repeat, rem.Time)
}
w.Flush()
return "```\n" + buf.String() + "```", nil
return "```\n" + buf.String() + "```"
}

// Returns table with useful values in reminders DB included
Expand Down Expand Up @@ -132,7 +131,9 @@ func BuildSeriesAssignmentsEmbed(assMap map[string][]string, series string, guil
for _, user := range assMap[job] {
name, err := GetUserPing(guild, user)
if err != nil {
return nil, err
//Can occur if user leaves server without being removed from DB
log.Printf("Error retrieving user ping for user %s from guild %s: %s\n", user, guild, err.Error())
name = user
}
usersStr += name
color, err := database.Repo.GetUserColor(user, guild)
Expand Down Expand Up @@ -218,7 +219,8 @@ func BuildJobAssignmentsEmbed(assMap map[string][]string, job string, guild stri
userF := new(discordgo.MessageEmbedField)
userF.Name, err = GetUserName(guild, user)
if err != nil {
return nil, err
log.Printf("Error retrieving username for user %s from guild %s: %s\n", user, guild, err.Error())
userF.Name = user
}
//Build string for each set of series
seriesStr := ""
Expand All @@ -237,6 +239,38 @@ func BuildJobAssignmentsEmbed(assMap map[string][]string, job string, guild stri
return &embed, nil
}

// Builds embed for basic series info plus notes
func BuildSeriesInfoEmbed(series database.Series, notes []string) *discordgo.MessageEmbed {
//Initialize embed
embed := discordgo.MessageEmbed{
Type: discordgo.EmbedTypeRich,
Title: series.NameFull,
Description: "Alias for commands - " + series.NameSh,
}
fields := []*discordgo.MessageEmbedField{}

//Add repo link field
if series.RepoLink != "" {
fields = append(fields, &discordgo.MessageEmbedField{Name: "Repo Link:", Value: series.RepoLink})
}

//Add notes field
if len(notes) != 0 {
ind := 1
text := ""
for _, n := range notes {
text += fmt.Sprintf("%d. %s\n", ind, n)
ind++
}
text = text[:len(text)-1]
fields = append(fields, &discordgo.MessageEmbedField{Name: "Series Notes:", Value: text})
}

embed.Fields = fields

return &embed
}

// Builds the embed for showing all assignments. Hierarchy is series-job-user
func BuildFullAssignmentsEmbed(assMap map[string]map[string][]string, guild string) (*discordgo.MessageEmbed, error) {
//Initialize embed
Expand Down Expand Up @@ -289,7 +323,8 @@ func BuildFullAssignmentsEmbed(assMap map[string]map[string][]string, guild stri
for _, user := range assMap[ser][job] {
userN, err := GetUserPing(guild, user)
if err != nil {
return nil, err
log.Printf("Error retrieving user ping for user %s from guild %s: %s\n", user, guild, err.Error())
userN = user
}
jobStr += userN + ", "
}
Expand All @@ -305,7 +340,7 @@ func BuildFullAssignmentsEmbed(assMap map[string]map[string][]string, guild stri
}

// Builds embed for showing user color prefs
func BuildColorsEmbed(assMap map[string]string, guild string) (*discordgo.MessageEmbed, error) {
func BuildColorsEmbed(assMap map[string]string, guild string) *discordgo.MessageEmbed {
//Initialize embed
embed := discordgo.MessageEmbed{
Type: discordgo.EmbedTypeRich,
Expand All @@ -331,7 +366,8 @@ func BuildColorsEmbed(assMap map[string]string, guild string) (*discordgo.Messag
for key := range assMap {
nm, err := GetUserName(guild, key)
if err != nil {
return nil, err
log.Printf("Error retrieving username for user %s from guild %s: %s\n", key, guild, err.Error())
nm = key
}
namesToId[nm] = key
names = append(names, nm)
Expand All @@ -343,15 +379,16 @@ func BuildColorsEmbed(assMap map[string]string, guild string) (*discordgo.Messag
for _, name := range names {
ping, err := GetUserPing(guild, namesToId[name])
if err != nil {
return nil, err
log.Printf("Error retrieving user ping for user %s from guild %s: %s\n", name, guild, err.Error())
ping = name
}
text += ping + " - " + assMap[namesToId[name]] + "\n"
}
text = text[:len(text)-1]
fields[0].Name = "Colors:"
fields[0].Value = text
embed.Fields = fields
return &embed, nil
return &embed
}

// Builds the embed for showing all server series
Expand Down
Loading

0 comments on commit 44dccb5

Please sign in to comment.