Skip to content

Commit c8b0c72

Browse files
committed
perf: reduce duplication, refactor out base embed creation
1 parent 102a4b4 commit c8b0c72

1 file changed

Lines changed: 39 additions & 63 deletions

File tree

SuggestionBot/Services/SuggestionService.cs

Lines changed: 39 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -64,37 +64,8 @@ public DiscordEmbed CreatePrivateEmbed(Suggestion suggestion)
6464
return new DiscordEmbedBuilder();
6565
}
6666

67-
if (!_configurationService.TryGetGuildConfiguration(guild, out GuildConfiguration? configuration))
68-
{
69-
configuration = new GuildConfiguration();
70-
}
71-
72-
string emoji = suggestion.Status switch
73-
{
74-
SuggestionStatus.Suggested => "🗳️",
75-
SuggestionStatus.Rejected => "❌",
76-
SuggestionStatus.Implemented => "✅",
77-
SuggestionStatus.Accepted => "✅",
78-
_ => throw new ArgumentOutOfRangeException(nameof(suggestion), suggestion.Status, null)
79-
};
80-
8167
DiscordUser author = GetAuthor(suggestion);
82-
var embed = new DiscordEmbedBuilder();
83-
string authorName = author.GetUsernameWithDiscriminator();
84-
embed.WithAuthor($"Suggestion from {authorName}", iconUrl: author.GetAvatarUrl(ImageFormat.Png));
85-
embed.WithThumbnail(guild.GetIconUrl(ImageFormat.Png));
86-
embed.WithDescription(suggestion.Content);
87-
embed.WithFooter($"Suggestion {suggestion.Id}");
88-
embed.WithColor(suggestion.Status switch
89-
{
90-
SuggestionStatus.Suggested => configuration.SuggestedColor,
91-
SuggestionStatus.Rejected => configuration.RejectedColor,
92-
SuggestionStatus.Implemented => configuration.ImplementedColor,
93-
SuggestionStatus.Accepted => configuration.AcceptedColor,
94-
_ => throw new ArgumentOutOfRangeException(nameof(suggestion), suggestion.Status, null)
95-
});
96-
97-
embed.AddField("Status", $"{emoji} **{suggestion.Status.Humanize(LetterCasing.AllCaps)}**", true);
68+
DiscordEmbedBuilder embed = CreateDefaultEmbed(suggestion, guild);
9869
embed.AddField("Author", author.Mention, true);
9970
embed.AddField("Submitted", Formatter.Timestamp(suggestion.Timestamp), true);
10071
embed.AddField("View Suggestion", GetSuggestionLink(suggestion), true);
@@ -138,39 +109,7 @@ public DiscordEmbed CreatePublicEmbed(Suggestion suggestion)
138109
return new DiscordEmbedBuilder();
139110
}
140111

141-
if (!_configurationService.TryGetGuildConfiguration(guild, out GuildConfiguration? configuration))
142-
{
143-
configuration = new GuildConfiguration();
144-
}
145-
146-
string emoji = suggestion.Status switch
147-
{
148-
SuggestionStatus.Suggested => "🗳️",
149-
SuggestionStatus.Rejected => "❌",
150-
SuggestionStatus.Implemented => "✅",
151-
SuggestionStatus.Accepted => "✅",
152-
_ => throw new ArgumentOutOfRangeException(nameof(suggestion), suggestion.Status, null)
153-
};
154-
155-
DiscordUser author = GetAuthor(suggestion);
156-
var embed = new DiscordEmbedBuilder();
157-
string authorName = author.GetUsernameWithDiscriminator();
158-
embed.WithAuthor($"Suggestion from {authorName}", iconUrl: author.GetAvatarUrl(ImageFormat.Png));
159-
embed.WithThumbnail(guild.GetIconUrl(ImageFormat.Png));
160-
embed.WithDescription(suggestion.Content);
161-
embed.WithFooter($"Suggestion {suggestion.Id}");
162-
embed.WithTimestamp(suggestion.Timestamp);
163-
embed.WithColor(suggestion.Status switch
164-
{
165-
SuggestionStatus.Suggested => configuration.SuggestedColor,
166-
SuggestionStatus.Rejected => configuration.RejectedColor,
167-
SuggestionStatus.Implemented => configuration.ImplementedColor,
168-
SuggestionStatus.Accepted => configuration.AcceptedColor,
169-
_ => throw new ArgumentOutOfRangeException(nameof(suggestion), suggestion.Status, null)
170-
});
171-
172-
embed.AddField("Status", $"{emoji} **{suggestion.Status.Humanize(LetterCasing.AllCaps)}**", true);
173-
112+
DiscordEmbedBuilder embed = CreateDefaultEmbed(suggestion, guild);
174113
if (!string.IsNullOrWhiteSpace(suggestion.Remarks))
175114
{
176115
embed.AddField("Staff Remarks", suggestion.Remarks);
@@ -695,6 +634,43 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken)
695634
return Task.CompletedTask;
696635
}
697636

637+
private DiscordEmbedBuilder CreateDefaultEmbed(Suggestion suggestion, DiscordGuild guild)
638+
{
639+
if (!_configurationService.TryGetGuildConfiguration(guild, out GuildConfiguration? configuration))
640+
{
641+
configuration = new GuildConfiguration();
642+
}
643+
644+
string emoji = suggestion.Status switch
645+
{
646+
SuggestionStatus.Suggested => "🗳️",
647+
SuggestionStatus.Rejected => "❌",
648+
SuggestionStatus.Implemented => "✅",
649+
SuggestionStatus.Accepted => "✅",
650+
_ => throw new ArgumentOutOfRangeException(nameof(suggestion), suggestion.Status, null)
651+
};
652+
653+
DiscordUser author = GetAuthor(suggestion);
654+
var embed = new DiscordEmbedBuilder();
655+
string authorName = author.GetUsernameWithDiscriminator();
656+
embed.WithAuthor($"Suggestion from {authorName}", iconUrl: author.GetAvatarUrl(ImageFormat.Png));
657+
embed.WithThumbnail(guild.GetIconUrl(ImageFormat.Png));
658+
embed.WithDescription(suggestion.Content);
659+
embed.WithFooter($"Suggestion {suggestion.Id}");
660+
embed.WithTimestamp(suggestion.Timestamp);
661+
embed.WithColor(suggestion.Status switch
662+
{
663+
SuggestionStatus.Suggested => configuration.SuggestedColor,
664+
SuggestionStatus.Rejected => configuration.RejectedColor,
665+
SuggestionStatus.Implemented => configuration.ImplementedColor,
666+
SuggestionStatus.Accepted => configuration.AcceptedColor,
667+
_ => throw new ArgumentOutOfRangeException(nameof(suggestion), suggestion.Status, null)
668+
});
669+
670+
embed.AddField("Status", $"{emoji} **{suggestion.Status.Humanize(LetterCasing.AllCaps)}**", true);
671+
return embed;
672+
}
673+
698674
private Task OnGuildAvailable(DiscordClient sender, GuildCreateEventArgs args)
699675
{
700676
DiscordGuild guild = args.Guild;

0 commit comments

Comments
 (0)