Further enhancement of Snapple.cs.

This commit is contained in:
Chris Darnell committed 2021-04-30 23:42:27 -07:00
1 parent 1e26a5f56c
commit 965b972e0c
1 file changed
+30 -9
+30 -9
View File
@@ -11,7 +11,7 @@ using System.Threading.Tasks;
namespace StarsiegeBot namespace StarsiegeBot
{ {
[Group("snapple")] [Group("snapple"), Aliases("snap")]
class SnappleFacts : BaseCommandModule class SnappleFacts : BaseCommandModule
{ {
private List<string> facts; private List<string> facts;
@@ -26,41 +26,61 @@ namespace StarsiegeBot
[GroupCommand, Description("Get a Snapple \"Real Fact\".")] [GroupCommand, Description("Get a Snapple \"Real Fact\".")]
public async Task Snapplefact(CommandContext ctx, [Description("[Optional] A Snapple Fact ID")] int snapId = -1) public async Task Snapplefact(CommandContext ctx, [Description("[Optional] A Snapple Fact ID")] int snapId = -1)
{ {
// if we're not enabled, exit out.
if (!IsEnabled)
return;
// trigger typing.
await ctx.TriggerTypingAsync(); await ctx.TriggerTypingAsync();
// store our output.
string output; string output;
// @neo, why is this here?
snapId--; snapId--;
try try
{ {
output = facts[snapId]); // attempt to get the desired fact.
output = facts[snapId];
return; return;
} }
catch (Exception) catch (Exception)
{ {
// we couldn't find the exact one, give them a random one.
output = facts[Program.rnd.Next(0, facts.Count)]; output = facts[Program.rnd.Next(0, facts.Count)];
} }
// give them what they want.
await ctx.RespondAsync(StartEmbed(output)); await ctx.RespondAsync(StartEmbed(output));
} }
[Command("count"), Description("Get a Snapple \"Real Fact\".")] [Command("count"), Description("Get a Snapple \"Real Fact\"."), Aliases("c")]
public async Task SnappleCount(CommandContext ctx) public async Task SnappleCount(CommandContext ctx)
{ {
if (!IsEnabled)
return;
await ctx.TriggerTypingAsync(); await ctx.TriggerTypingAsync();
DiscordEmbedBuilder embed = StartEmbed(facts.Count + " facts in the Snapple \"Real Facts\" Bank"); // trigger the typing, and report how many facts are in teh bank.
await ctx.RespondAsync(embed); await ctx.RespondAsync(StartEmbed(facts.Count + " facts in the Snapple \"Real Facts\" Bank"));
} }
[Command("load"), RequireOwner] [Command("load"), RequireOwner, Aliases("l")]
public async Task Load(CommandContext ctx) public async Task Load(CommandContext ctx)
{ {
// trigger the typing.
await ctx.TriggerTypingAsync(); await ctx.TriggerTypingAsync();
// start the response.
DiscordEmbedBuilder embed = StartEmbed("Reloading Snapple Facts"); DiscordEmbedBuilder embed = StartEmbed("Reloading Snapple Facts");
// our old setting...
embed.AddField("Old", (IsEnabled ? "Enabled" : "Disabled")); embed.AddField("Old", (IsEnabled ? "Enabled" : "Disabled"));
// run this, we dont care about its actual results.
_ = Load(); _ = Load();
// report our new setting.
embed.AddField("New", (IsEnabled ? "Enabled" : "Disabled")); embed.AddField("New", (IsEnabled ? "Enabled" : "Disabled"));
// since the only thing that *should* go wrong is a missing file, report it if thats the case.
if (!File.Exists("snapple.json"))
embed.WithFooter(":warning: snapple.json file is missing.");
// send our results.
await ctx.RespondAsync(embed); await ctx.RespondAsync(embed);
} }
[Command("toggle")] [Command("toggle"), RequireOwner, Aliases("t")]
public async Task Enable (CommandContext ctx, [RemainingText]string isEnabled = null) public async Task Enable (CommandContext ctx, [RemainingText]string isEnabled = null)
{ {
// trigger the typing. // trigger the typing.
@@ -88,12 +108,14 @@ namespace StarsiegeBot
} }
else else
{ {
// we dont know what they were trying to do, throw an error in the footer with a :warning: emote.
embed.AddField("New", (IsEnabled ? "Enabled" : "Disabled")); embed.AddField("New", (IsEnabled ? "Enabled" : "Disabled"));
embed.WithFooter(":warning: Invalid command given."); embed.WithFooter(":warning: Invalid command given?");
} }
} }
else else
{ {
// file is missing, disable this, regardless of their desires. Report the file is missing in the footer with a :warning:
IsEnabled = false; IsEnabled = false;
embed.AddField("New", (IsEnabled ? "Enabled" : "Disabled")); embed.AddField("New", (IsEnabled ? "Enabled" : "Disabled"));
embed.WithFooter(":warning: snapple.json file is missing, and it can not be enabled."); embed.WithFooter(":warning: snapple.json file is missing, and it can not be enabled.");
@@ -101,7 +123,6 @@ namespace StarsiegeBot
await ctx.RespondAsync(embed); await ctx.RespondAsync(embed);
} }
private DiscordEmbedBuilder StartEmbed(string desc) private DiscordEmbedBuilder StartEmbed(string desc)
{ {
DiscordEmbedBuilder embed = new DiscordEmbedBuilder DiscordEmbedBuilder embed = new DiscordEmbedBuilder