Skip to content

Commit

Permalink
added functality to remove starboard message if star count is zero
Browse files Browse the repository at this point in the history
  • Loading branch information
quantumburrito committed Sep 9, 2024
1 parent 3e53449 commit c3d2159
Showing 1 changed file with 51 additions and 23 deletions.
74 changes: 51 additions & 23 deletions uncord-bot-go/handlers/starboard_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,11 @@ func OnReactionAdd(event *events.GuildMessageReactionAdd) {
}
}

// OnReactionRemove handles the removal of a star reaction and updates the star count in the database.
func OnReactionRemove(event *events.GuildMessageReactionRemove) {
// Check if the removed reaction is a star emoji
if event.Emoji.Name == "⭐" {
// Fetch the message that was reacted to
message, err := event.Client().Rest().GetMessage(event.ChannelID, event.MessageID)
if err != nil {
log.Printf("Error fetching message: %v", err)
return
}

if event.Emoji.Name != nil && *event.Emoji.Name == "⭐" {
// Decrement the star count in the database
err = RemoveStarFromMessage(event.MessageID.String())
err := RemoveStarFromMessage(event.MessageID.String())
if err != nil {
log.Printf("Error removing star from message: %v", err)
return
Expand All @@ -84,16 +76,43 @@ func OnReactionRemove(event *events.GuildMessageReactionRemove) {
return
}

// Optional: if the star count drops to 0, remove it from the starboard
// If the star count is 0, remove the message from the starboard
if starCount <= 0 {
err = RemoveFromStarboard(event.MessageID.String())
// Fetch the starboard message ID from the database
starboardMessageID, err := GetStarboardMessageID(event.MessageID.String())
if err != nil {
log.Printf("Error removing message from starboard: %v", err)
log.Printf("Error fetching starboard message ID: %v", err)
return
}

// Delete the message from the starboard channel
err = DeleteStarboardMessage(event.Client, starboardMessageID)
if err != nil {
log.Printf("Error deleting message from starboard: %v", err)
}

// Optionally, you could delete the row from the database
// err = RemoveFromStarboard(event.MessageID.String())
// if err != nil {
// log.Printf("Error removing message from starboard: %v", err)
// }
}
}
}

func GetStarboardMessageID(messageID string) (string, error) {
var starboardMessageID string
query := `SELECT starboard_message_id FROM starboard WHERE message_id = $1`
err := config.DB.QueryRow(query, messageID).Scan(&starboardMessageID)
return starboardMessageID, err
}

func DeleteStarboardMessage(client *discord.Client, starboardMessageID string) error {
starboardMessageIDSnowflake := discord.Snowflake(starboardMessageID)
err := client.Rest().DeleteMessage(config.AppConfig.StarboardChannelID, starboardMessageIDSnowflake)
return err
}

func RemoveStarFromMessage(messageID string) error {
query := `UPDATE starboard SET star_count = star_count - 1 WHERE message_id = $1 AND star_count > 0`
_, err := config.DB.Exec(query, messageID)
Expand All @@ -106,34 +125,43 @@ func RemoveFromStarboard(messageID string) error {
return err
}

func PostToStarboard(event *events.GuildMessageReactionAdd, message *discord.Message, starCount int) {
// Safely handle the author's avatar URL
avatarURL := ""
if message.Author.AvatarURL() != nil {
avatarURL = *message.Author.AvatarURL()
}
func UpdateStarboardMessageID(messageID, starboardMessageID string) error {
query := `UPDATE starboard SET starboard_message_id = $1 WHERE message_id = $2`
_, err := config.DB.Exec(query, starboardMessageID, messageID)
return err
}

func PostToStarboard(event *events.GuildMessageReactionAdd, message *discord.Message, starCount int) error {
// Create the embed for the starred message
embedBuilder := discord.NewEmbedBuilder().
SetTitle(fmt.Sprintf("⭐ %d", starCount)). // Add star count in title
SetTitle(fmt.Sprintf("⭐ %d # %s", starCount, event.ChannelID.String())). // Add star count in title
SetDescription(message.Content). // Add message content
AddField("Source", fmt.Sprintf("[Jump!](https://discord.com/channels/%s/%s/%s)", event.GuildID.String(), event.ChannelID.String(), event.MessageID.String()), false). // Jump link to the message
SetAuthorName(message.Author.Username). // Add the author's username
SetAuthorIcon(avatarURL). // Add the author's avatar URL
SetTimestamp(message.CreatedAt). // Timestamp of the original message
SetFooterText("From #" + event.ChannelID.String()) // Channel name in the footer

// If there are any attachments (e.g., an image), add them to the embed
if len(message.Attachments) > 0 {
embedBuilder.SetImage(message.Attachments[0].URL) // Add the first attachment as an image
}

embed := embedBuilder.Build()

// Send the embed to the starboard channel
_, err := event.Client().Rest().CreateMessage(config.AppConfig.StarboardChannelID, discord.NewMessageCreateBuilder().AddEmbeds(embed).Build())
// Send the embed to the starboard channel and capture the message ID
starboardMessage, err := event.Client().Rest().CreateMessage(config.AppConfig.StarboardChannelID, discord.NewMessageCreateBuilder().AddEmbeds(embed).Build())
if err != nil {
log.Printf("Error sending message to starboard: %v", err)
return err
}

// Update the database with the starboard message ID
err = UpdateStarboardMessageID(event.MessageID.String(), starboardMessage.ID.String())
if err != nil {
log.Printf("Error updating starboard message ID in database: %v", err)
return err
}

return nil
}

0 comments on commit c3d2159

Please sign in to comment.