Skip to content

Commit

Permalink
Merge pull request #62 from Zechiax/Feature/ChangelogStyle
Browse files Browse the repository at this point in the history
Add option to change changelog style
  • Loading branch information
Zechiax authored Feb 23, 2023
2 parents 87b166e + ddd0fd7 commit 0c9034d
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 17 deletions.
31 changes: 30 additions & 1 deletion Asterion/ComponentBuilders/SettingsComponentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static class SettingsComponentBuilder
public const string MoreButtonId = "settings-module-more:*";
public const string MainScreenButtonId = "settings-main:*";
public const string ChangeMessageStyleSelectionId = "change-message-style:*";
public const string ChangeChangelogStyleSelectionId = "change-changelog-style:*";
public static ComponentBuilder GetIntroButtons(string userId)
{
var components =
Expand Down Expand Up @@ -89,6 +90,7 @@ public static ComponentBuilder GetMessageStyleSelectionComponents(Guild guild, s
{
return new ComponentBuilder()
.WithSelectMenu(GetMessageStyleSelection(guild, userId))
.WithSelectMenu(GetChangelogStyleSelection(guild, userId))
.WithButton(GetBackButton(userId), 1);
}
public static SelectMenuBuilder GetMessageStyleSelection(Guild guild, string userId)
Expand All @@ -97,7 +99,8 @@ public static SelectMenuBuilder GetMessageStyleSelection(Guild guild, string use
{
MaxValues = 1,
MinValues = 1,
CustomId = ChangeMessageStyleSelectionId.Replace("*", userId)
CustomId = ChangeMessageStyleSelectionId.Replace("*", userId),
Placeholder = "Select a message style"
};

var options = new List<SelectMenuOptionBuilder>();
Expand All @@ -115,4 +118,30 @@ public static SelectMenuBuilder GetMessageStyleSelection(Guild guild, string use

return menu;
}

public static SelectMenuBuilder GetChangelogStyleSelection(Guild guild, string userId)
{
var menu = new SelectMenuBuilder
{
MaxValues = 1,
MinValues = 1,
CustomId = ChangeChangelogStyleSelectionId.Replace("*", userId),
Placeholder = "Select a changelog style"
};

var options = new List<SelectMenuOptionBuilder>();
foreach (var style in Enum.GetValues(typeof(ChangelogStyle)).Cast<ChangelogStyle>())
{
options.Add(new SelectMenuOptionBuilder()
{
Label = Enum.GetName(typeof(ChangelogStyle), style),
Value = style.ToString(),
IsDefault = style == guild.GuildSettings.ChangelogStyle
});
}

menu.Options = options;

return menu;
}
}
3 changes: 3 additions & 0 deletions Asterion/Database/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<GuildSettings>().Property(p => p.ShowChannelSelection).HasDefaultValue(true);
modelBuilder.Entity<GuildSettings>().Property(p => p.CheckMessagesForModrinthLink).HasDefaultValue(false);
modelBuilder.Entity<GuildSettings>().Property(p => p.ShowSubscribeButton).HasDefaultValue(true);
modelBuilder.Entity<GuildSettings>().Property(p => p.MessageStyle).HasDefaultValue(MessageStyle.Full);
modelBuilder.Entity<GuildSettings>().Property(p => p.ChangelogStyle).HasDefaultValue(ChangelogStyle.PlainText);
modelBuilder.Entity<GuildSettings>().Property(p => p.ChangeLogMaxLength).HasDefaultValue(2000);
}
}
}
8 changes: 8 additions & 0 deletions Asterion/Database/Models/ChangelogStyle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Asterion.Database.Models;

public enum ChangelogStyle
{
PlainText,
CodeBlock,
NoChangelog
}
4 changes: 4 additions & 0 deletions Asterion/Database/Models/GuildSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ public class GuildSettings

public bool? CheckMessagesForModrinthLink { get; set; } = false;
public MessageStyle MessageStyle { get; set; } = MessageStyle.Full;

public ChangelogStyle ChangelogStyle { get; set; } = ChangelogStyle.PlainText;

public long ChangeLogMaxLength { get; set; } = 2000;
}
31 changes: 24 additions & 7 deletions Asterion/EmbedBuilders/ModrinthEmbedBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ public static EmbedBuilder GetProjectEmbed(Project project, IEnumerable<TeamMemb
return embed;
}

public static EmbedBuilder VersionUpdateEmbed(MessageStyle style, Project project, Version version,
public static EmbedBuilder VersionUpdateEmbed(GuildSettings settings, Project project, Version version,
IEnumerable<TeamMember>? teamMembers = null)
{
return style switch
return settings.MessageStyle switch
{
MessageStyle.Full => GetFullVersionUpdateEmbed(project, version, teamMembers),
MessageStyle.Full => GetFullVersionUpdateEmbed(project, version, settings, teamMembers),
MessageStyle.Compact => GetCompactVersionUpdateEmbed(project, version, teamMembers),
_ => throw new ArgumentOutOfRangeException(nameof(style), style, null)
_ => throw new ArgumentOutOfRangeException(nameof(settings.MessageStyle), settings.MessageStyle, null)
};
}

Expand All @@ -171,7 +171,25 @@ private static EmbedBuilder GetCompactVersionUpdateEmbed(Project project, Versio
return embed;
}

private static EmbedBuilder GetFullVersionUpdateEmbed(Project project, Version version, IEnumerable<TeamMember>? teamMembers = null)
private static string FormatChangelog(string? changelog, GuildSettings settings)
{
// var changelog = string.IsNullOrEmpty(version.Changelog) ? "\n\n*No changelog provided*" : $"\n\n{Format.Underline(Format.Bold("Changelog:"))}\n" + $"{version.Changelog}".Truncate(DescriptionLimit);
switch (settings.ChangelogStyle)
{
case ChangelogStyle.PlainText:
return string.IsNullOrEmpty(changelog) ? "\n\n*No changelog provided*" : $"\n\n{Format.Underline(Format.Bold("Changelog:"))}\n" + $"{changelog}".Truncate((int)settings.ChangeLogMaxLength);

case ChangelogStyle.CodeBlock:
return string.IsNullOrEmpty(changelog) ? Format.Code("\n\n*No changelog provided*") : $"\n\n{Format.Underline(Format.Bold("Changelog:"))}\n" + Format.Code($"{changelog}".Truncate((int)settings.ChangeLogMaxLength));

case ChangelogStyle.NoChangelog:
return string.Empty;
}

throw new ArgumentOutOfRangeException(nameof(settings.ChangelogStyle), settings.ChangelogStyle, null);
}

private static EmbedBuilder GetFullVersionUpdateEmbed(Project project, Version version, GuildSettings guildSettings, IEnumerable<TeamMember>? teamMembers = null)
{
var sbFiles = new StringBuilder();

Expand All @@ -180,8 +198,7 @@ private static EmbedBuilder GetFullVersionUpdateEmbed(Project project, Version v
sbFiles.AppendLine($"[{file.FileName}]({file.Url}) | {ByteSize.FromBytes(file.Size).Humanize()}");
}

var changelog = string.IsNullOrEmpty(version.Changelog) ? "\n\n*No changelog provided*" : $"\n\n{Format.Underline(Format.Bold("Changelog:"))}\n" +
$"{version.Changelog}".Truncate(DescriptionLimit);
var changelog = FormatChangelog(version.Changelog, guildSettings);

var embedAuthor = GetEmbedAuthor(project, teamMembers, version);

Expand Down
6 changes: 4 additions & 2 deletions Asterion/EmbedBuilders/SettingsEmbedBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public static EmbedBuilder GetViewSettingsEmbed(Guild guild)

var dummyVersion = new Version()
{
Changelog = "This would be the new version's changelog",
Changelog = "This is a changelog \n\n with multiple lines\n and a Markdown link [here](https://modrinth.com/)\n" +
"and a style test: **bold**, *italic*, ~~strikethrough~~, `code`, __underline__\n" +
"So that you can see how it looks like in the embed. 🐱",
Id = "12456789",
DatePublished = DateTime.Now,
GameVersions = new[] {"1.19.2"},
Expand All @@ -74,7 +76,7 @@ public static EmbedBuilder GetViewSettingsEmbed(Guild guild)
VersionNumber = "Version-3.5"
};

var embed = ModrinthEmbedBuilder.VersionUpdateEmbed(guild.GuildSettings.MessageStyle, dummyProject, dummyVersion);
var embed = ModrinthEmbedBuilder.VersionUpdateEmbed(guild.GuildSettings, dummyProject, dummyVersion);

return embed;
}
Expand Down
58 changes: 58 additions & 0 deletions Asterion/Migrations/20230223141738_addChangelogSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Asterion.Migrations
{
/// <inheritdoc />
public partial class addChangelogSettings : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<int>(
name: "MessageStyle",
table: "GuildSettings",
type: "INTEGER",
nullable: false,
defaultValue: 0,
oldClrType: typeof(int),
oldType: "INTEGER");

migrationBuilder.AddColumn<long>(
name: "ChangeLogMaxLength",
table: "GuildSettings",
type: "INTEGER",
nullable: false,
defaultValue: 2000L);

migrationBuilder.AddColumn<int>(
name: "ChangelogStyle",
table: "GuildSettings",
type: "INTEGER",
nullable: false,
defaultValue: 0);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ChangeLogMaxLength",
table: "GuildSettings");

migrationBuilder.DropColumn(
name: "ChangelogStyle",
table: "GuildSettings");

migrationBuilder.AlterColumn<int>(
name: "MessageStyle",
table: "GuildSettings",
type: "INTEGER",
nullable: false,
oldClrType: typeof(int),
oldType: "INTEGER",
oldDefaultValue: 0);
}
}
}
16 changes: 14 additions & 2 deletions Asterion/Migrations/DataContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ partial class DataContextModelSnapshot : ModelSnapshot
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.1");
modelBuilder.HasAnnotation("ProductVersion", "7.0.3");

modelBuilder.Entity("Asterion.Database.Models.Array", b =>
{
Expand Down Expand Up @@ -73,6 +73,16 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");

b.Property<long>("ChangeLogMaxLength")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasDefaultValue(2000L);

b.Property<int>("ChangelogStyle")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasDefaultValue(0);

b.Property<bool?>("CheckMessagesForModrinthLink")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
Expand All @@ -82,7 +92,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasColumnType("INTEGER");

b.Property<int>("MessageStyle")
.HasColumnType("INTEGER");
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasDefaultValue(0);

b.Property<bool?>("RemoveOnLeave")
.ValueGeneratedOnAdd()
Expand Down
4 changes: 3 additions & 1 deletion Asterion/Modules/ModrinthModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,9 @@ await ModifyOriginalResponseAsync(x =>

var team = await _modrinthService.GetProjectsTeamMembersAsync(project.Id);

var embed = ModrinthEmbedBuilder.VersionUpdateEmbed(style, project, latestVersion, team);
var guild = await _dataService.GetGuildByIdAsync(Context.Guild.Id);

var embed = ModrinthEmbedBuilder.VersionUpdateEmbed(guild?.GuildSettings, project, latestVersion, team);

var buttons =
new ComponentBuilder().WithButton(
Expand Down
36 changes: 36 additions & 0 deletions Asterion/Modules/SettingsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,42 @@ await ModifyOriginalResponseAsync(x =>
});
}

[DoUserCheck]
[ComponentInteraction(SettingsComponentBuilder.ChangeChangelogStyleSelectionId)]
public async Task ChangeChangelogStyle(string userId, string[] selectedStyle)
{
await DeferAsync();
var style = (ChangelogStyle) Enum.Parse(typeof(ChangelogStyle), selectedStyle.First());

var guild = await _dataService.GetGuildByIdAsync(Context.Guild.Id);

if (guild is null)
{
await FollowupAsync("Something went wrong, please try again later", ephemeral: true);
return;
}

guild.GuildSettings.ChangelogStyle = style;

var success = await _dataService.UpdateGuildAsync(guild);

if (!success)
{
await FollowupAsync("Something went wrong, please try again later", ephemeral: true);
return;
}

var embed = SettingsEmbedBuilder.GetViewSettingsEmbed(guild);


await ModifyOriginalResponseAsync(x =>
{
x.Embed = embed.Build();
x.Components = SettingsComponentBuilder.GetMessageStyleSelectionComponents(guild, Context.User.Id.ToString())
.Build();
});
}

[DoUserCheck]
[ComponentInteraction("settings-show-subscribe-button:*;*", runMode: RunMode.Async)]
public async Task ShowSubscribeButton(string userId, bool showSubscribeButton)
Expand Down
2 changes: 2 additions & 0 deletions Asterion/Services/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ public async Task<bool> UpdateGuildAsync(Guild updatedGuild)
guild.GuildSettings.ShowChannelSelection = updatedGuild.GuildSettings.ShowChannelSelection;
guild.GuildSettings.CheckMessagesForModrinthLink = updatedGuild.GuildSettings.CheckMessagesForModrinthLink;
guild.GuildSettings.ShowSubscribeButton = updatedGuild.GuildSettings.ShowSubscribeButton;
guild.GuildSettings.ChangelogStyle = updatedGuild.GuildSettings.ChangelogStyle;
guild.GuildSettings.ChangeLogMaxLength = updatedGuild.GuildSettings.ChangeLogMaxLength;

await db.SaveChangesAsync();

Expand Down
8 changes: 4 additions & 4 deletions Asterion/Services/Modrinth/ModrinthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private async Task CheckGuilds(Version[] versions, Project project, IEnumerable<
_logger.LogInformation("Sending updates to guild ID {Id} and channel ID {Channel}", guild.GuildId, channel.Id);

// None of these can be null, everything is checked beforehand
await SendUpdatesToChannel(channel, project, versions, teamMembers, guild.GuildSettings.MessageStyle);
await SendUpdatesToChannel(channel, project, versions, teamMembers, guild.GuildSettings);
}
}

Expand All @@ -214,13 +214,13 @@ private async Task CheckGuilds(Version[] versions, Project project, IEnumerable<
/// <param name="currentProject"></param>
/// <param name="newVersions"></param>
/// <param name="team"></param>
/// <param name="messageStyle"></param>
private async Task SendUpdatesToChannel(SocketTextChannel textChannel, Project currentProject, IEnumerable<Version> newVersions, TeamMember[]? team, MessageStyle messageStyle = MessageStyle.Full)
/// <param name="guildSettings"></param>
private async Task SendUpdatesToChannel(SocketTextChannel textChannel, Project currentProject, IEnumerable<Version> newVersions, TeamMember[]? team, GuildSettings guildSettings)
{
// Iterate versions - they are ordered from latest to oldest, we want to sent them chronologically
foreach (var version in newVersions.Reverse())
{
var embed = ModrinthEmbedBuilder.VersionUpdateEmbed(messageStyle, currentProject, version, team);
var embed = ModrinthEmbedBuilder.VersionUpdateEmbed(guildSettings, currentProject, version, team);
var buttons =
new ComponentBuilder().WithButton(
ModrinthComponentBuilder.GetVersionUrlButton(currentProject, version));
Expand Down

0 comments on commit 0c9034d

Please sign in to comment.