Skip to content

Commit

Permalink
add /loved slash command
Browse files Browse the repository at this point in the history
  • Loading branch information
th0mk committed Jan 19, 2025
1 parent 46ad91a commit 8a6d74b
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 133 deletions.
99 changes: 99 additions & 0 deletions src/FMBot.Bot/Builders/TrackBuilders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,105 @@ public async Task<ResponseModel> UnLoveTrackAsync(
return response;
}

public async Task<ResponseModel> LovedTracksAsync(
ContextModel context,
UserSettingsModel userSettings)
{
var response = new ResponseModel
{
ResponseType = ResponseType.Paginator,
};

var pages = new List<PageBuilder>();
string sessionKey = null;
if (!userSettings.DifferentUser && !string.IsNullOrEmpty(context.ContextUser.SessionKeyLastFm))
{
sessionKey = context.ContextUser.SessionKeyLastFm;
}

const int amount = 200;

var lovedTracks =
await this._dataSourceFactory.GetLovedTracksAsync(userSettings.UserNameLastFm, amount,
sessionKey: sessionKey);

if (!lovedTracks.Content.RecentTracks.Any())
{
response.Embed.WithDescription(
$"The Last.fm user `{userSettings.UserNameLastFm}` has no loved tracks yet! \n" +
$"Use `{context.Prefix}love` to add tracks to your list.");
response.CommandResponse = CommandResponse.NoScrobbles;
response.ResponseType = ResponseType.Embed;
return response;
}

if (GenericEmbedService.RecentScrobbleCallFailed(lovedTracks))
{
var errorResponse =
GenericEmbedService.RecentScrobbleCallFailedResponse(lovedTracks, userSettings.UserNameLastFm);
return errorResponse;
}

var userTitle = await this._userService.GetUserTitleAsync(context.DiscordGuild, context.DiscordUser);
var title = !userSettings.DifferentUser
? userTitle
: $"{userSettings.UserNameLastFm}, requested by {userTitle}";

response.EmbedAuthor.WithName($"Last loved tracks for {title}");
response.EmbedAuthor.WithUrl(lovedTracks.Content.UserRecentTracksUrl);

if (!userSettings.DifferentUser)
{
response.EmbedAuthor.WithIconUrl(context.DiscordUser.GetAvatarUrl());
}

var firstTrack = lovedTracks.Content.RecentTracks[0];

var footer = $"{userSettings.UserNameLastFm} has {lovedTracks.Content.TotalAmount} loved tracks";
DateTime? timePlaying = null;

if (!firstTrack.NowPlaying && firstTrack.TimePlayed.HasValue)
{
timePlaying = firstTrack.TimePlayed.Value;
}

if (timePlaying.HasValue)
{
footer += " | Last loved track:";
}

var lovedTrackPages = lovedTracks.Content.RecentTracks.ChunkBy(10);

var counter = lovedTracks.Content.TotalAmount;
foreach (var lovedTrackPage in lovedTrackPages)
{
var lovedPageString = new StringBuilder();
foreach (var lovedTrack in lovedTrackPage)
{
var trackString = LastFmRepository.TrackToOneLinedLinkedString(lovedTrack);

lovedPageString.AppendLine($"`{counter}` - {trackString}");
counter--;
}

var page = new PageBuilder()
.WithDescription(lovedPageString.ToString())
.WithColor(DiscordConstants.LastFmColorRed)
.WithAuthor(response.EmbedAuthor)
.WithFooter(footer);

if (timePlaying.HasValue)
{
page.WithTimestamp(timePlaying);
}

pages.Add(page);
}

response.StaticPaginator = StringService.BuildStaticPaginator(pages);
return response;
}

public async Task<ResponseModel> GuildTracksAsync(
ContextModel context,
Guild guild,
Expand Down
8 changes: 4 additions & 4 deletions src/FMBot.Bot/SlashCommands/TopSlashCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public async Task TopAlbumsAsync(
[Summary("Private", "Only show response to you")] bool privateResponse = false)
{
_ = DeferAsync(privateResponse);

var contextUser = await this._userService.GetUserSettingsAsync(this.Context.User);
var userSettings = await this._settingService.GetUser(user, contextUser, this.Context.Guild, this.Context.User, true);
mode ??= contextUser.Mode ?? ResponseMode.Embed;
Expand All @@ -121,7 +121,7 @@ public async Task TopTracksAsync(
[Summary("Private", "Only show response to you")] bool privateResponse = false)
{
_ = DeferAsync(privateResponse);

var contextUser = await this._userService.GetUserSettingsAsync(this.Context.User);
var userSettings = await this._settingService.GetUser(user, contextUser, this.Context.Guild, this.Context.User, true);
mode ??= contextUser.Mode ?? ResponseMode.Embed;
Expand All @@ -148,7 +148,7 @@ public async Task TopGenresAsync(
[Summary("Private", "Only show response to you")] bool privateResponse = false)
{
_ = DeferAsync(privateResponse);

var contextUser = await this._userService.GetUserSettingsAsync(this.Context.User);
var userSettings = await this._settingService.GetUser(user, contextUser, this.Context.Guild, this.Context.User, true);
mode ??= contextUser.Mode ?? ResponseMode.Embed;
Expand All @@ -175,7 +175,7 @@ public async Task TopCountriesAsync(
[Summary("Private", "Only show response to you")] bool privateResponse = false)
{
_ = DeferAsync(privateResponse);

var contextUser = await this._userService.GetUserSettingsAsync(this.Context.User);
var userSettings = await this._settingService.GetUser(user, contextUser, this.Context.Guild, this.Context.User, true);
mode ??= contextUser.Mode ?? ResponseMode.Embed;
Expand Down
19 changes: 19 additions & 0 deletions src/FMBot.Bot/SlashCommands/TrackSlashCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,23 @@ public async Task ReceiptAsync(
await this.Context.SendFollowUpResponse(this.Interactivity, response, privateResponse);
this.Context.LogCommandUsed(response.CommandResponse);
}

[SlashCommand("loved", "Shows your Last.fm loved tracks")]
[UsernameSetRequired]
[CommandContextType(InteractionContextType.BotDm, InteractionContextType.PrivateChannel, InteractionContextType.Guild)]
[IntegrationType(ApplicationIntegrationType.UserInstall, ApplicationIntegrationType.GuildInstall)]
public async Task LovedTracksAsync(
[Summary("User", "The user to show (defaults to self)")] string user = null,
[Summary("Private", "Only show response to you")] bool privateResponse = false)
{
_ = DeferAsync(privateResponse);

var contextUser = await this._userService.GetUserSettingsAsync(this.Context.User);
var userSettings = await this._settingService.GetUser(user, contextUser, this.Context.Guild, this.Context.User, true);

var response = await this._trackBuilders.LovedTracksAsync(new ContextModel(this.Context, contextUser), userSettings);

await this.Context.SendFollowUpResponse(this.Interactivity, response, privateResponse);
this.Context.LogCommandUsed(response.CommandResponse);
}
}
Loading

0 comments on commit 8a6d74b

Please sign in to comment.