Skip to content

Commit

Permalink
Add RuleService.CreateRuleNotFoundEmbed
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Mar 6, 2022
1 parent 753d18f commit ff798c3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 43 deletions.
23 changes: 10 additions & 13 deletions Hammer/CommandModules/Rules/RulesModule.DeleteRuleCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
29 changes: 13 additions & 16 deletions Hammer/CommandModules/Rules/RulesModule.RuleCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion Hammer/CommandModules/Rules/RulesModule.RulesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 10 additions & 13 deletions Hammer/CommandModules/Rules/RulesModule.SetRuleCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
16 changes: 16 additions & 0 deletions Hammer/Services/RuleService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DisCatSharp.Entities;
using Hammer.Data;
using Hammer.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -47,6 +48,21 @@ public async Task<Rule> AddRuleAsync(DiscordGuild guild, string ruleContent)
return rule;
}

/// <summary>
/// Creates a "Rule Not Found" embed.
/// </summary>
/// <param name="guild">The guild whose branding to display.</param>
/// <param name="ruleId">The ID of the rule which wasn't found.</param>
/// <returns>A <see cref="DiscordEmbed" /> stating the rule cannot be found.</returns>
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;
}

/// <summary>
/// Deletes a rule from the database.
/// </summary>
Expand Down

0 comments on commit ff798c3

Please sign in to comment.