Skip to content

Commit

Permalink
update(cmds): set post and live notification rols
Browse files Browse the repository at this point in the history
  • Loading branch information
fvckgrimm committed Jan 25, 2025
1 parent 0335573 commit a8bb61f
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 25 deletions.
14 changes: 8 additions & 6 deletions internal/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (b *Bot) monitorUsers() {
}
defer tx.Rollback()

rows, err := tx.Query("SELECT guild_id, user_id, username, notification_channel, post_notification_channel, live_notification_channel, last_post_id, last_stream_start, mention_role, avatar_location, avatar_location_updated_at, live_image_url, posts_enabled, live_enabled FROM monitored_users")
rows, err := tx.Query("SELECT guild_id, user_id, username, notification_channel, post_notification_channel, live_notification_channel, last_post_id, last_stream_start, mention_role, avatar_location, avatar_location_updated_at, live_image_url, posts_enabled, live_enabled, live_mention_role, post_mention_role FROM monitored_users")
if err != nil {
return err
}
Expand All @@ -106,9 +106,11 @@ func (b *Bot) monitorUsers() {
LiveImageURL string
PostsEnabled bool
LiveEnabled bool
LiveMentionRole string
PostMentionRole string
}

err := rows.Scan(&user.GuildID, &user.UserID, &user.Username, &user.NotificationChannel, &user.PostNotificationChannel, &user.LiveNotificationChannel, &user.LastPostID, &user.LastStreamStart, &user.MentionRole, &user.AvatarLocation, &user.AvatarLocationUpdatedAt, &user.LiveImageURL, &user.PostsEnabled, &user.LiveEnabled)
err := rows.Scan(&user.GuildID, &user.UserID, &user.Username, &user.NotificationChannel, &user.PostNotificationChannel, &user.LiveNotificationChannel, &user.LastPostID, &user.LastStreamStart, &user.MentionRole, &user.AvatarLocation, &user.AvatarLocationUpdatedAt, &user.LiveImageURL, &user.PostsEnabled, &user.LiveEnabled, &user.LiveMentionRole, &user.PostMentionRole)
if err != nil {
log.Printf("Error scanning row: %v", err)
continue
Expand Down Expand Up @@ -153,8 +155,8 @@ func (b *Bot) monitorUsers() {
embedMsg := embed.CreateLiveStreamEmbed(user.Username, streamInfo, user.AvatarLocation, user.LiveImageURL)

mention := "@everyone"
if user.MentionRole != "" {
mention = fmt.Sprintf("<@&%s>", user.MentionRole)
if user.LiveMentionRole != "" {
mention = fmt.Sprintf("<@&%s>", user.LiveMentionRole)
}

targetChannel := user.LiveNotificationChannel
Expand Down Expand Up @@ -215,8 +217,8 @@ func (b *Bot) monitorUsers() {
embedMsg := embed.CreatePostEmbed(user.Username, postInfo[0], user.AvatarLocation, postMedia)

mention := "@everyone"
if user.MentionRole != "" {
mention = fmt.Sprintf("<@&%s>", user.MentionRole)
if user.PostMentionRole != "" {
mention = fmt.Sprintf("<@&%s>", user.PostMentionRole)
}

targetChannel := user.PostNotificationChannel
Expand Down
36 changes: 36 additions & 0 deletions internal/bot/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,42 @@ func (b *Bot) registerCommands() {
},
},
},
{
Name: "setpostmention",
Description: "Set role to mention for post notifications",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "username",
Description: "Fansly username",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionRole,
Name: "role",
Description: "Role to mention (optional)",
Required: false,
},
},
},
{
Name: "setlivemention",
Description: "Set role to mention for live notifications",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "username",
Description: "Fansly username",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionRole,
Name: "role",
Description: "Role to mention (optional)",
Required: false,
},
},
},
}

_, err := b.Session.ApplicationCommandBulkOverwrite(b.Session.State.User.ID, "", commands)
Expand Down
110 changes: 92 additions & 18 deletions internal/bot/handlers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bot

import (
"database/sql"
"fmt"
"log"
"strings"
Expand Down Expand Up @@ -35,6 +36,10 @@ func (b *Bot) interactionCreate(s *discordgo.Session, i *discordgo.InteractionCr
b.handleToggleCommand(s, i)
case "setchannel":
b.handleSetChannelCommand(s, i)
case "setpostmention":
b.handleSetPostMentionCommand(s, i)
case "setlivemention":
b.handleSetLiveMentionCommand(s, i)
}
}

Expand Down Expand Up @@ -125,9 +130,9 @@ func (b *Bot) handleAddCommand(s *discordgo.Session, i *discordgo.InteractionCre
err = b.retryDbOperation(func() error {
_, err = b.DB.Exec(`
INSERT OR REPLACE INTO monitored_users
(guild_id, user_id, username, notification_channel, post_notification_channel, live_notification_channel, last_post_id, last_stream_start, mention_role, avatar_location, avatar_location_updated_at, live_image_url, posts_enabled, live_enabled)
VALUES (?, ?, ?, ?, ?, ?, '', 0, ?, ?, ?, ?, 1, 1)
`, i.GuildID, accountInfo.ID, username, channel.ID, channel.ID, channel.ID, mentionRole, avatarLocation, time.Now().Unix(), "")
(guild_id, user_id, username, notification_channel, post_notification_channel, live_notification_channel, last_post_id, last_stream_start, mention_role, avatar_location, avatar_location_updated_at, live_image_url, posts_enabled, live_enabled, live_mention_role, post_mention_role)
VALUES (?, ?, ?, ?, ?, ?, '', 0, ?, ?, ?, ?, 1, 1, ?, ?)
`, i.GuildID, accountInfo.ID, username, channel.ID, channel.ID, channel.ID, mentionRole, avatarLocation, time.Now().Unix(), "", mentionRole, mentionRole)
return err
})

Expand Down Expand Up @@ -180,7 +185,7 @@ func (b *Bot) respondToInteraction(s *discordgo.Session, i *discordgo.Interactio
func (b *Bot) handleListCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
// Fetch monitored users for the current guild
rows, err := b.DB.Query(`
SELECT username, notification_channel, mention_role, posts_enabled, live_enabled
SELECT username, notification_channel, posts_enabled, live_enabled, live_mention_role, post_mention_role
FROM monitored_users
WHERE guild_id = ?
`, i.GuildID)
Expand All @@ -193,26 +198,19 @@ func (b *Bot) handleListCommand(s *discordgo.Session, i *discordgo.InteractionCr
var monitoredUsers []string
for rows.Next() {
var (
username, channelID, roleID string
postsEnabled, liveEnabled bool
username, channelID, postMentionRole, liveMentionRole string
postsEnabled, liveEnabled bool
)
err := rows.Scan(&username, &channelID, &roleID, &postsEnabled, &liveEnabled)
err := rows.Scan(&username, &channelID, &postsEnabled, &liveEnabled, &liveMentionRole, &postMentionRole)
if err != nil {
log.Printf("Error scanning row: %v", err)
continue
}

channelInfo := fmt.Sprintf("<#%s>", channelID)

roleInfo := "No role"
if roleID != "" {
role, err := s.State.Role(i.GuildID, roleID)
if err != nil {
log.Printf("Error fetching role: %v", err)
} else {
roleInfo = role.Name
}
}
roleInfoPost := getRoleName(s, i.GuildID, postMentionRole)
roleInfoLive := getRoleName(s, i.GuildID, liveMentionRole)

// Create status indicators
postStatus := "✅"
Expand All @@ -224,10 +222,11 @@ func (b *Bot) handleListCommand(s *discordgo.Session, i *discordgo.InteractionCr
liveStatus = "❌"
}

userInfo := fmt.Sprintf("- %s\n • Channel: %s\n • Role: %s\n • Posts: %s\n • Live: %s",
userInfo := fmt.Sprintf("- %s\n • Channel: %s\nLive Role: %s\n • Post Role: %s\n • Posts: %s\n • Live: %s",
username,
channelInfo,
roleInfo,
roleInfoPost,
roleInfoLive,
postStatus,
liveStatus,
)
Expand Down Expand Up @@ -372,6 +371,81 @@ func (b *Bot) handleSetChannelCommand(s *discordgo.Session, i *discordgo.Interac
b.respondToInteraction(s, i, fmt.Sprintf("Successfully set %s notification channel for %s to %s", notifType, username, channel.Mention()))
}

func (b *Bot) handleSetPostMentionCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
options := i.ApplicationCommandData().Options
username := options[0].StringValue()
var roleID string
if len(options) > 1 {
role := options[1].RoleValue(s, i.GuildID)
if role != nil {
roleID = role.ID
}
}

result, err := b.DB.Exec(`
UPDATE monitored_users
SET post_mention_role = ?
WHERE guild_id = ? AND username = ?
`, roleID, i.GuildID, username)

if handleUpdateResponse(b, s, i, err, result, "post mention role", username, roleID) {
return
}
}

func (b *Bot) handleSetLiveMentionCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
options := i.ApplicationCommandData().Options
username := options[0].StringValue()
var roleID string
if len(options) > 1 {
role := options[1].RoleValue(s, i.GuildID)
if role != nil {
roleID = role.ID
}
}

result, err := b.DB.Exec(`
UPDATE monitored_users
SET live_mention_role = ?
WHERE guild_id = ? AND username = ?
`, roleID, i.GuildID, username)

if handleUpdateResponse(b, s, i, err, result, "live mention role", username, roleID) {
return
}
}

func handleUpdateResponse(b *Bot, s *discordgo.Session, i *discordgo.InteractionCreate, err error, result sql.Result, roleType, username, roleID string) bool {
if err != nil {
b.respondToInteraction(s, i, fmt.Sprintf("Error updating %s: %v", roleType, err))
return true
}

rowsAffected, _ := result.RowsAffected()
if rowsAffected == 0 {
b.respondToInteraction(s, i, fmt.Sprintf("User %s not found", username))
return true
}

message := fmt.Sprintf("%s for %s has been cleared.", roleType, username)
if roleID != "" {
message = fmt.Sprintf("%s for %s set to <@&%s>", roleType, username, roleID)
}
b.respondToInteraction(s, i, message)
return false
}

func getRoleName(s *discordgo.Session, guildID, roleID string) string {
if roleID == "" {
return "No role"
}
role, err := s.State.Role(guildID, roleID)
if err != nil {
return "Unknown role"
}
return role.Name
}

// Add this new helper function
func (b *Bot) editInteractionResponse(s *discordgo.Session, i *discordgo.InteractionCreate, content string) {
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Expand Down
38 changes: 37 additions & 1 deletion internal/database/databse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

var DB *sql.DB

const currentVersion = 2
const currentVersion = 3

func Init() {
var err error
Expand Down Expand Up @@ -47,6 +47,7 @@ func runMigrations(currentDBVersion int) {
migrations := []func(*sql.DB) error{
migrateToV1,
migrateToV2,
migrateToV3,
// Add new migrations here
}

Expand Down Expand Up @@ -130,6 +131,41 @@ func migrateToV2(db *sql.DB) error {
return tx.Commit()
}

func migrateToV3(db *sql.DB) error {
tx, err := db.Begin()
if err != nil {
return err
}
defer tx.Rollback()

_, err = tx.Exec(`
ALTER TABLE monitored_users
ADD COLUMN post_mention_role TEXT;
`)
if err != nil {
return err
}

_, err = tx.Exec(`
ALTER TABLE monitored_users
ADD COLUMN live_mention_role TEXT;
`)
if err != nil {
return err
}

_, err = tx.Exec(`
UPDATE monitored_users
SET post_mention_role = mention_role,
live_mention_role = mention_role
`)
if err != nil {
return err
}

return tx.Commit()
}

func Close() {
DB.Close()
}
Expand Down

0 comments on commit a8bb61f

Please sign in to comment.