From ff798c32fead2a154f0afee3bce4a1596031e84f Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sun, 6 Mar 2022 20:17:54 +0000 Subject: [PATCH] Add RuleService.CreateRuleNotFoundEmbed --- .../Rules/RulesModule.DeleteRuleCommand.cs | 23 +++++++-------- .../Rules/RulesModule.RuleCommand.cs | 29 +++++++++---------- .../Rules/RulesModule.RulesCommand.cs | 2 +- .../Rules/RulesModule.SetRuleCommand.cs | 23 +++++++-------- Hammer/Services/RuleService.cs | 16 ++++++++++ 5 files changed, 50 insertions(+), 43 deletions(-) diff --git a/Hammer/CommandModules/Rules/RulesModule.DeleteRuleCommand.cs b/Hammer/CommandModules/Rules/RulesModule.DeleteRuleCommand.cs index b65adf9..dd69774 100644 --- a/Hammer/CommandModules/Rules/RulesModule.DeleteRuleCommand.cs +++ b/Hammer/CommandModules/Rules/RulesModule.DeleteRuleCommand.cs @@ -15,24 +15,21 @@ internal sealed partial class RulesModule [RequirePermissionLevel(PermissionLevel.Administrator)] public async Task DeleteRuleCommandAsync(CommandContext context, [Description("The ID of the rule to remove.")] int ruleId) { - await context.Message.CreateReactionAsync(DiscordEmoji.FromUnicode("✅")); - + await context.AcknowledgeAsync(); DiscordGuild guild = context.Guild; - DiscordEmbedBuilder embed = guild.CreateDefaultEmbed(false); if (!_ruleService.GuildHasRule(guild, ruleId)) { - embed.WithColor(0xFF0000); - embed.WithTitle("No such rule"); - embed.WithDescription("A rule by that ID could not be found."); - } - else - { - await _ruleService.DeleteRuleAsync(guild, ruleId); - embed.WithColor(0x4CAF50); - embed.WithTitle($"Rule {ruleId} deleted"); - embed.WithDescription($"To view the new rules, run `{context.Prefix}rules`"); + await context.RespondAsync(_ruleService.CreateRuleNotFoundEmbed(guild, ruleId)); + return; } + + await _ruleService.DeleteRuleAsync(guild, ruleId); + + DiscordEmbedBuilder embed = guild.CreateDefaultEmbed(false); + embed.WithColor(0x4CAF50); + embed.WithTitle($"Rule {ruleId} deleted"); + embed.WithDescription($"To view the new rules, run `{context.Prefix}rules`"); await context.RespondAsync(embed); } diff --git a/Hammer/CommandModules/Rules/RulesModule.RuleCommand.cs b/Hammer/CommandModules/Rules/RulesModule.RuleCommand.cs index 93bde1d..74b515d 100644 --- a/Hammer/CommandModules/Rules/RulesModule.RuleCommand.cs +++ b/Hammer/CommandModules/Rules/RulesModule.RuleCommand.cs @@ -11,27 +11,24 @@ internal sealed partial class RulesModule [Command("rule")] [Description("Displays a specified rule.")] [RequireGuild] - public async Task RuleCommandAsync(CommandContext context, [Description("The ID of the rule to retrieve.")] int id) + public async Task RuleCommandAsync(CommandContext context, [Description("The ID of the rule to retrieve.")] int ruleId) { - await context.Message.CreateReactionAsync(DiscordEmoji.FromUnicode("✅")); - + await context.AcknowledgeAsync(); DiscordGuild guild = context.Guild; - DiscordEmbedBuilder embed = guild.CreateDefaultEmbed(false); - - if (!_ruleService.GuildHasRule(guild, id)) - { - embed.WithColor(0xFF0000); - embed.WithTitle("No such rule"); - embed.WithDescription("A rule by that ID could not be found."); - } - else + + if (!_ruleService.GuildHasRule(guild, ruleId)) { - Rule rule = _ruleService.GetRuleById(guild, id)!; - embed.WithColor(DiscordColor.Orange); - embed.WithTitle(string.IsNullOrWhiteSpace(rule.Brief) ? $"Rule #{rule.Id}" : $"Rule #{rule.Id}. {rule.Brief}"); - embed.WithDescription(rule.Content); + await context.RespondAsync(_ruleService.CreateRuleNotFoundEmbed(guild, ruleId)); + return; } + Rule rule = _ruleService.GetRuleById(guild, ruleId)!; + + DiscordEmbedBuilder embed = guild.CreateDefaultEmbed(false); + embed.WithColor(DiscordColor.Orange); + embed.WithTitle(string.IsNullOrWhiteSpace(rule.Brief) ? $"Rule #{rule.Id}" : $"Rule #{rule.Id}. {rule.Brief}"); + embed.WithDescription(rule.Content); + await context.RespondAsync(embed); } } diff --git a/Hammer/CommandModules/Rules/RulesModule.RulesCommand.cs b/Hammer/CommandModules/Rules/RulesModule.RulesCommand.cs index 836f98e..1a3f0ee 100644 --- a/Hammer/CommandModules/Rules/RulesModule.RulesCommand.cs +++ b/Hammer/CommandModules/Rules/RulesModule.RulesCommand.cs @@ -22,7 +22,7 @@ public async Task RulesCommandAsync(CommandContext context) if (rules.Count == 0) { embed.WithColor(0xFF0000); - embed.WithTitle("No rules found"); + embed.WithTitle("No Rules Found"); embed.WithDescription($"No rules could be found for {Formatter.Bold(guild.Name)}."); } else diff --git a/Hammer/CommandModules/Rules/RulesModule.SetRuleCommand.cs b/Hammer/CommandModules/Rules/RulesModule.SetRuleCommand.cs index dc245e7..bcb83ed 100644 --- a/Hammer/CommandModules/Rules/RulesModule.SetRuleCommand.cs +++ b/Hammer/CommandModules/Rules/RulesModule.SetRuleCommand.cs @@ -17,25 +17,22 @@ public async Task SetRuleCommandAsync(CommandContext context, [Description("The ID of the rule to remove.")] int ruleId, [Description("The new rule text"), RemainingText] string ruleContent) { - await context.Message.CreateReactionAsync(DiscordEmoji.FromUnicode("✅")); - + await context.AcknowledgeAsync(); DiscordGuild guild = context.Guild; - DiscordEmbedBuilder embed = guild.CreateDefaultEmbed(false); if (!_ruleService.GuildHasRule(guild, ruleId)) { - embed.WithColor(0xFF0000); - embed.WithTitle("No such rule"); - embed.WithDescription("A rule by that ID could not be found."); - } - else - { - await _ruleService.SetRuleContentAsync(guild, ruleId, ruleContent); - embed.WithColor(0x4CAF50); - embed.WithTitle($"Rule {ruleId} updated"); - embed.WithDescription(ruleContent); + await context.RespondAsync(_ruleService.CreateRuleNotFoundEmbed(guild, ruleId)); + return; } + await _ruleService.SetRuleContentAsync(guild, ruleId, ruleContent); + + DiscordEmbedBuilder embed = guild.CreateDefaultEmbed(false); + embed.WithColor(0x4CAF50); + embed.WithTitle($"Rule {ruleId} updated"); + embed.WithDescription(ruleContent); + await context.RespondAsync(embed); } } diff --git a/Hammer/Services/RuleService.cs b/Hammer/Services/RuleService.cs index 604b47a..290466f 100644 --- a/Hammer/Services/RuleService.cs +++ b/Hammer/Services/RuleService.cs @@ -1,5 +1,6 @@ using DisCatSharp.Entities; using Hammer.Data; +using Hammer.Extensions; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.Extensions.DependencyInjection; @@ -47,6 +48,21 @@ public async Task AddRuleAsync(DiscordGuild guild, string ruleContent) return rule; } + /// + /// Creates a "Rule Not Found" embed. + /// + /// The guild whose branding to display. + /// The ID of the rule which wasn't found. + /// A stating the rule cannot be found. + public DiscordEmbed CreateRuleNotFoundEmbed(DiscordGuild guild, int ruleId) + { + DiscordEmbedBuilder embed = guild.CreateDefaultEmbed(false); + embed.WithColor(0xFF0000); + embed.WithTitle("Rule Not Found"); + embed.WithDescription($"A rule with ID {ruleId} could not be found."); + return embed; + } + /// /// Deletes a rule from the database. ///