Skip to content

Commit

Permalink
add reroll option
Browse files Browse the repository at this point in the history
  • Loading branch information
th0mk committed Apr 7, 2024
1 parent 7967203 commit 0b40e71
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 17 deletions.
17 changes: 12 additions & 5 deletions src/FMBot.Bot/Builders/AlbumBuilders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,18 @@ public class AlbumBuilders

response.Embed.WithDescription(description.ToString());

response.Embed.WithFooter(response.EmbedFooter);
response.Components = new ComponentBuilder()
.WithButton("Album", $"{InteractionConstants.Album.Info}-{databaseAlbum.Id}-{userSettings.DiscordUserId}-{context.ContextUser.DiscordUserId}", style: ButtonStyle.Secondary, emote: new Emoji("💽"))
.WithButton($"For {await this._userService.GetUserTitleAsync(context.DiscordGuild, context.DiscordUser)}", style: ButtonStyle.Secondary, disabled: true, customId: "0");

if (albumSearch.IsRandom)
{
response.Embed.WithFooter($"Random album #{albumSearch.RandomAlbumPosition} ({albumSearch.RandomAlbumPlaycount} {StringExtensions.GetPlaysString(albumSearch.RandomAlbumPlaycount)})");
response.Components.WithButton("Reroll",
$"{InteractionConstants.Album.RandomCover}-{userSettings.DiscordUserId}-{context.ContextUser.DiscordUserId}",
style: ButtonStyle.Secondary, emote: new Emoji("🎲"));
}

response.Stream = image;
response.FileName =
$"cover-{StringExtensions.ReplaceInvalidChars($"{albumSearch.Album.ArtistName}_{albumSearch.Album.AlbumName}")}";
Expand All @@ -1057,10 +1068,6 @@ public class AlbumBuilders

await cacheStream.DisposeAsync();

response.Components = new ComponentBuilder()
.WithButton("Album", $"{InteractionConstants.Album.Info}-{databaseAlbum.Id}-{userSettings.DiscordUserId}-{context.ContextUser.DiscordUserId}", style: ButtonStyle.Secondary, emote: new Emoji("💽"))
.WithButton($"For {await this._userService.GetUserTitleAsync(context.DiscordGuild, context.DiscordUser)}", style: ButtonStyle.Secondary, disabled: true, customId: "0");

return response;
}

Expand Down
10 changes: 9 additions & 1 deletion src/FMBot.Bot/Builders/PlayBuilders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,8 @@ public class PlayBuilder
ContextModel context,
UserSettingsModel userSettings,
int mileStoneAmount,
long userTotalPlaycount)
long userTotalPlaycount,
bool isRandom = false)
{
var response = new ResponseModel
{
Expand Down Expand Up @@ -889,6 +890,13 @@ public class PlayBuilder
}
}

if (isRandom)
{
response.Components = new ComponentBuilder().WithButton("Reroll",
$"{InteractionConstants.RandomMilestone}-{userSettings.DiscordUserId}-{context.ContextUser.DiscordUserId}",
style: ButtonStyle.Secondary, emote: new Emoji("🎲"));
}

response.Embed.WithDescription(reply.ToString());

return response;
Expand Down
3 changes: 3 additions & 0 deletions src/FMBot.Bot/Resources/InteractionConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ public static class Album
public const string Info = "album-info";
public const string Tracks = "album-tracks";
public const string Cover = "album-cover";
public const string RandomCover = "randomalbum-cover";
}

public const string RandomMilestone = "random-milestone";

public static class ModerationCommands
{
public const string CensorTypes = "admin-censor-*";
Expand Down
2 changes: 1 addition & 1 deletion src/FMBot.Bot/Services/MusicBotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public async Task Scrobble(MusicBot musicBot, SocketUserMessage msg, ICommandCon
embed.WithFooter($"Use '{prfx}botscrobbling' for more information.");

_ = this.Interactivity.DelayedDeleteMessageAsync(
await context.Channel.SendMessageAsync(embed: embed.Build()),
await context.Channel.SendMessageAsync(embed: embed.Build(), flags: MessageFlags.SuppressNotification),
TimeSpan.FromSeconds(120));

Log.Debug("BotScrobbling: Scrobbled {trackName} by {artistName} for {listenerCount} users in {guildName} / {guildId}", trackResult.TrackName, trackResult.ArtistName, listenerCount, context.Guild.Name, context.Guild.Id);
Expand Down
6 changes: 4 additions & 2 deletions src/FMBot.Bot/Services/SettingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,12 +1030,13 @@ private async Task<User> DiscordIdToUser(string value)
return goalAmount;
}

public static int GetMilestoneAmount(
public static (int amount, bool isRandom) GetMilestoneAmount(
string extraOptions,
long currentPlaycount)
{
var goalAmount = 100;
var ownGoalSet = false;
var isRandom = false;

if (extraOptions != null)
{
Expand Down Expand Up @@ -1072,6 +1073,7 @@ private async Task<User> DiscordIdToUser(string value)
{
goalAmount = RandomNumberGenerator.GetInt32(1, (int)currentPlaycount);
ownGoalSet = true;
isRandom = true;
break;
}
}
Expand All @@ -1094,7 +1096,7 @@ private async Task<User> DiscordIdToUser(string value)
goalAmount = 1;
}

return goalAmount;
return (goalAmount, isRandom);
}

public static GuildRankingSettings SetGuildRankingSettings(GuildRankingSettings guildRankingSettings, string extraOptions)
Expand Down
2 changes: 1 addition & 1 deletion src/FMBot.Bot/Services/TimerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public async Task UpdateStatus()

try
{
if (this.CurrentFeatured.Status != null)
if (this.CurrentFeatured?.Status != null)
{
await this._client.SetCustomStatusAsync(this.CurrentFeatured.Status);
return;
Expand Down
33 changes: 33 additions & 0 deletions src/FMBot.Bot/SlashCommands/AlbumSlashCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,39 @@ public async Task AlbumCoverAsync(string album, string discordUser, string reque
}
}

[ComponentInteraction($"{InteractionConstants.Album.RandomCover}-*-*")]
[UsernameSetRequired]
public async Task RandomAlbumCoverAsync(string discordUser, string requesterDiscordUser)
{
var discordUserId = ulong.Parse(discordUser);
var requesterDiscordUserId = ulong.Parse(requesterDiscordUser);

if (this.Context.User.Id != requesterDiscordUserId)
{
await RespondAsync("🎲 Sorry, only the user that requested the random cover can reroll.", ephemeral: true);
return;
}

_ = DeferAsync();
await this.Context.DisableInteractionButtons();

var contextUser = await this._userService.GetUserWithDiscogs(requesterDiscordUserId);
var discordContextUser = await this.Context.Client.GetUserAsync(requesterDiscordUserId);
var userSettings = await this._settingService.GetOriginalContextUser(discordUserId, requesterDiscordUserId, this.Context.Guild, this.Context.User);

try
{
var response = await this._albumBuilders.CoverAsync(new ContextModel(this.Context, contextUser, discordContextUser), userSettings, "random");

await this.Context.UpdateInteractionEmbed(response, this.Interactivity, false);
this.Context.LogCommandUsed(response.CommandResponse);
}
catch (Exception e)
{
await this.Context.HandleCommandException(e);
}
}

[SlashCommand("albumtracks", "Shows album info for the album you're currently listening to or searching for")]
[UsernameSetRequired]
[CommandContextType(InteractionContextType.BotDm, InteractionContextType.PrivateChannel, InteractionContextType.Guild)]
Expand Down
42 changes: 40 additions & 2 deletions src/FMBot.Bot/SlashCommands/PlaySlashCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using FMBot.Bot.Services.Guild;
using FMBot.Domain.Interfaces;
using FMBot.Domain.Models;
using FMBot.Subscriptions.Models;
using MetaBrainz.MusicBrainz.Interfaces;
using SummaryAttribute = Discord.Interactions.SummaryAttribute;

namespace FMBot.Bot.SlashCommands;
Expand Down Expand Up @@ -101,7 +103,7 @@ public async Task NowPlayingAsync([Summary("user", "The user to show (defaults t

try
{
if (message != null && response.CommandResponse == CommandResponse.Ok)
if (message != null && this.Context.Channel != null && response.CommandResponse == CommandResponse.Ok)
{
if (contextUser.EmoteReactions != null && contextUser.EmoteReactions.Any() && SupporterService.IsSupporter(contextUser.UserType))
{
Expand Down Expand Up @@ -311,7 +313,7 @@ public async Task StreakDeleteButton(DeleteStreakModal modal)
var mileStoneAmount = SettingService.GetMilestoneAmount(amount.ToString(), userInfo.Playcount);

var response = await this._playBuilder.MileStoneAsync(new ContextModel(this.Context, contextUser),
userSettings, mileStoneAmount, userInfo.Playcount);
userSettings, mileStoneAmount.amount, userInfo.Playcount);

await this.Context.SendFollowUpResponse(this.Interactivity, response);
this.Context.LogCommandUsed(response.CommandResponse);
Expand All @@ -322,6 +324,42 @@ public async Task StreakDeleteButton(DeleteStreakModal modal)
}
}

[ComponentInteraction($"{InteractionConstants.RandomMilestone}-*-*")]
[UsernameSetRequired]
public async Task RandomMilestoneAsync(string discordUser, string requesterDiscordUser)
{
var discordUserId = ulong.Parse(discordUser);
var requesterDiscordUserId = ulong.Parse(requesterDiscordUser);

if (this.Context.User.Id != requesterDiscordUserId)
{
await RespondAsync("🎲 Sorry, only the user that requested the random milestone can reroll.", ephemeral: true);
return;
}

_ = DeferAsync();
await this.Context.DisableInteractionButtons();

var contextUser = await this._userService.GetUserWithDiscogs(requesterDiscordUserId);
var userSettings = await this._settingService.GetOriginalContextUser(discordUserId, requesterDiscordUserId, this.Context.Guild, this.Context.User);
var targetUser = await this._userService.GetUserWithDiscogs(requesterDiscordUserId);

try
{
var mileStoneAmount = SettingService.GetMilestoneAmount("random", targetUser.TotalPlaycount.GetValueOrDefault());

var response = await this._playBuilder.MileStoneAsync(new ContextModel(this.Context, contextUser),
userSettings, mileStoneAmount.amount, targetUser.TotalPlaycount.GetValueOrDefault(), mileStoneAmount.isRandom);

await this.Context.UpdateInteractionEmbed(response, this.Interactivity, false);
this.Context.LogCommandUsed(response.CommandResponse);
}
catch (Exception e)
{
await this.Context.HandleCommandException(e);
}
}

[SlashCommand("year", "Shows an overview of your year")]
[UsernameSetRequired]
public async Task YearAsync(
Expand Down
8 changes: 4 additions & 4 deletions src/FMBot.Bot/TextCommands/GameCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public GameCommands(IOptions<BotSettings> botSettings, UserService userService,
this.Interactivity = interactivity;
}

[Command("jumble", RunMode = RunMode.Async)]
[Summary("Play the Jumble game.")]
[UsernameSetRequired]
[CommandCategories(CommandCategory.Friends)]
//[Command("jumble", RunMode = RunMode.Async)]
//[Summary("Play the Jumble game.")]
//[UsernameSetRequired]
//[CommandCategories(CommandCategory.Friends)]
public async Task JumbleAsync()
{
_ = this.Context.Channel.TriggerTypingAsync();
Expand Down
2 changes: 1 addition & 1 deletion src/FMBot.Bot/TextCommands/LastFM/PlayCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public async Task MilestoneAsync([Remainder] string extraOptions = null)
mileStoneAmount = SettingService.GetMilestoneAmount(this.Context.Message.ReferencedMessage.Content, userInfo.Playcount);
}

var response = await this._playBuilder.MileStoneAsync(new ContextModel(this.Context, prfx, contextUser), userSettings, mileStoneAmount, userInfo.Playcount);
var response = await this._playBuilder.MileStoneAsync(new ContextModel(this.Context, prfx, contextUser), userSettings, mileStoneAmount.amount, userInfo.Playcount, mileStoneAmount.isRandom);

await this.Context.SendResponse(this.Interactivity, response);
this.Context.LogCommandUsed(response.CommandResponse);
Expand Down

0 comments on commit 0b40e71

Please sign in to comment.