Skip to content

Commit

Permalink
update: toggle notifications
Browse files Browse the repository at this point in the history
- Separate live and post notifications
- Allow enabling/disabling live or post notifications
- Update /list command to show notification status
  • Loading branch information
fvckgrimm committed Nov 29, 2024
1 parent 7062b38 commit fdfbb0f
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 17 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

## TODO:

- [ ] Hack together way to provide roles based on sub/follow per server
- [x] Fix following logic to still add if account has no pfp and just let it be empty in the embed
- [ ] Possibly separate live and post notification
- [ ] Allow enabling and disabling one or the other
- [x] Possibly separate live and post notification
- [x] Allow enabling and disabling one or the other

## Running The Bot Yourself

Expand Down
10 changes: 6 additions & 4 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, last_post_id, last_stream_start, mention_role, avatar_location, avatar_location_updated_at, live_image_url FROM monitored_users")
rows, err := tx.Query("SELECT guild_id, user_id, username, 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")
if err != nil {
return err
}
Expand All @@ -102,9 +102,11 @@ func (b *Bot) monitorUsers() {
AvatarLocation string
AvatarLocationUpdatedAt int64
LiveImageURL string
PostsEnabled bool
LiveEnabled bool
}

err := rows.Scan(&user.GuildID, &user.UserID, &user.Username, &user.NotificationChannel, &user.LastPostID, &user.LastStreamStart, &user.MentionRole, &user.AvatarLocation, &user.AvatarLocationUpdatedAt, &user.LiveImageURL)
err := rows.Scan(&user.GuildID, &user.UserID, &user.Username, &user.NotificationChannel, &user.LastPostID, &user.LastStreamStart, &user.MentionRole, &user.AvatarLocation, &user.AvatarLocationUpdatedAt, &user.LiveImageURL, &user.PostsEnabled, &user.LiveEnabled)
if err != nil {
log.Printf("Error scanning row: %v", err)
continue
Expand Down Expand Up @@ -136,7 +138,7 @@ func (b *Bot) monitorUsers() {
}
}

if streamInfo.Response.Stream.Status == 2 && streamInfo.Response.Stream.StartedAt > user.LastStreamStart {
if user.LiveEnabled && streamInfo.Response.Stream.Status == 2 && streamInfo.Response.Stream.StartedAt > user.LastStreamStart {
_, err = tx.Exec(`
UPDATE monitored_users
SET last_stream_start = ?
Expand Down Expand Up @@ -168,7 +170,7 @@ func (b *Bot) monitorUsers() {
continue
}

if len(postInfo) > 0 && postInfo[0].ID != user.LastPostID {
if user.PostsEnabled && len(postInfo) > 0 && postInfo[0].ID != user.LastPostID {
_, err = tx.Exec(`
UPDATE monitored_users
SET last_post_id = ?
Expand Down
34 changes: 34 additions & 0 deletions internal/bot/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,40 @@ func (b *Bot) registerCommands() {
},
},
},
{
Name: "toggle",
Description: "Toggle notifications for a model",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "username",
Description: "Fansly username",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionString,
Name: "type",
Description: "Notification type to toggle",
Required: true,
Choices: []*discordgo.ApplicationCommandOptionChoice{
{
Name: "Posts",
Value: "posts",
},
{
Name: "Live",
Value: "live",
},
},
},
{
Type: discordgo.ApplicationCommandOptionBoolean,
Name: "enabled",
Description: "Enable or disable notifications",
Required: true,
},
},
},
}

_, err := b.Session.ApplicationCommandBulkOverwrite(b.Session.State.User.ID, "", commands)
Expand Down
72 changes: 68 additions & 4 deletions internal/bot/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func (b *Bot) interactionCreate(s *discordgo.Session, i *discordgo.InteractionCr
b.handleListCommand(s, i)
case "setliveimage":
b.handleSetLiveImageCommand(s, i)
case "toggle":
b.handleToggleCommand(s, i)
}
}

Expand Down Expand Up @@ -143,7 +145,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
SELECT username, notification_channel, mention_role, posts_enabled, live_enabled
FROM monitored_users
WHERE guild_id = ?
`, i.GuildID)
Expand All @@ -155,8 +157,11 @@ func (b *Bot) handleListCommand(s *discordgo.Session, i *discordgo.InteractionCr

var monitoredUsers []string
for rows.Next() {
var username, channelID, roleID string
err := rows.Scan(&username, &channelID, &roleID)
var (
username, channelID, roleID string
postsEnabled, liveEnabled bool
)
err := rows.Scan(&username, &channelID, &roleID, &postsEnabled, &liveEnabled)
if err != nil {
log.Printf("Error scanning row: %v", err)
continue
Expand All @@ -174,7 +179,23 @@ func (b *Bot) handleListCommand(s *discordgo.Session, i *discordgo.InteractionCr
}
}

userInfo := fmt.Sprintf("- %s (Channel: %s, Role: %s)", username, channelInfo, roleInfo)
// Create status indicators
postStatus := "✅"
if !postsEnabled {
postStatus = "❌"
}
liveStatus := "✅"
if !liveEnabled {
liveStatus = "❌"
}

userInfo := fmt.Sprintf("- %s\n • Channel: %s\n • Role: %s\n • Posts: %s\n • Live: %s",
username,
channelInfo,
roleInfo,
postStatus,
liveStatus,
)
monitoredUsers = append(monitoredUsers, userInfo)
}

Expand Down Expand Up @@ -235,6 +256,49 @@ func (b *Bot) handleSetLiveImageCommand(s *discordgo.Session, i *discordgo.Inter
b.editInteractionResponse(s, i, fmt.Sprintf("Live image for %s has been set successfully.", username))
}

func (b *Bot) handleToggleCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
options := i.ApplicationCommandData().Options
username := options[0].StringValue()
notifiType := options[1].StringValue()
enabled := options[2].BoolValue()

var column string
switch notifiType {
case "posts":
column = "posts_enabled"
case "live":
column = "live_enabled"
default:
b.respondToInteraction(s, i, "Invalid notification type")
return
}

query := fmt.Sprintf(`
UPDATE monitored_users
SET %s = ?
WHERE guild_id = ? AND username = ?
`, column)

result, err := b.DB.Exec(query, enabled, i.GuildID, username)
if err != nil {
b.respondToInteraction(s, i, fmt.Sprintf("Error updating settings: %v", err))
return
}

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

status := "enabled"
if !enabled {
status = "disabled"
}

b.respondToInteraction(s, i, fmt.Sprintf("%s notifications %s for %s", notifiType, status, username))
}

// 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
2 changes: 2 additions & 0 deletions internal/database/databse.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func createTables() {
avatar_location TEXT,
avatar_location_updated_at INTEGER,
live_image_url TEXT,
posts_enabled BOOLEAN DEFAULT 1,
live_enabled BOOLEAN DEFAULT 1,
PRIMARY KEY (guild_id, user_id)
)
`)
Expand Down
12 changes: 5 additions & 7 deletions migrate.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sqlite3
# import time

# Connect to the old and new databases
old_conn = sqlite3.connect("bot-old.db")
Expand All @@ -20,6 +19,8 @@
avatar_location TEXT,
avatar_location_updated_at INTEGER,
live_image_url TEXT,
posts_enabled BOOLEAN DEFAULT 1,
live_enabled BOOLEAN DEFAULT 1,
PRIMARY KEY (guild_id, user_id)
)
""")
Expand All @@ -33,13 +34,10 @@
new_cursor.execute(
"""
INSERT INTO monitored_users
(guild_id, user_id, username, notification_channel, last_post_id, last_stream_start, mention_role, avatar_location, avatar_location_updated_at, live_image_url)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
(guild_id, user_id, username, notification_channel, last_post_id, last_stream_start, mention_role, avatar_location, avatar_location_updated_at, live_image_url, posts_enabled, live_enabled)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
*record,
None,
), # Add all existing fields from old record, plus None for live_image_url
(*record[:10], 1, 1), # Take first 10 fields from record, add two boolean flags
)

# Commit the changes and close the connections
Expand Down

0 comments on commit fdfbb0f

Please sign in to comment.