Reworked NotFacts.cs to match Snapple.cs. Small refactor on GameCommands.cs
This commit is contained in:
1 parent
965b972e0c
commit
cf036c534f
4 files changed
+143
-84
No files matched your search
+106
-10
@@ -1,28 +1,34 @@
|
||||
using DSharpPlus.CommandsNext;
|
||||
using DSharpPlus.CommandsNext.Attributes;
|
||||
using DSharpPlus.Entities;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace StarsiegeBot
|
||||
{
|
||||
[Group("notfact")]
|
||||
class NotFacts : BaseCommandModule
|
||||
{
|
||||
private readonly List<string> notfacts;
|
||||
private List<string> notfacts;
|
||||
private bool IsEnabled;
|
||||
private readonly string fileName = "notfacts.json";
|
||||
|
||||
public NotFacts()
|
||||
{
|
||||
Console.WriteLine("NotFacts Command Loaded");
|
||||
string json = File.ReadAllText("notfacts.json");
|
||||
notfacts = JsonConvert.DeserializeObject<List<string>>(json);
|
||||
IsEnabled = Load();
|
||||
}
|
||||
|
||||
[Command("notfact"), Description("Get a NotFact")]
|
||||
public async Task Snapplefact(CommandContext ctx, [Description("[Optional] A NotFact ID")] int notFactID = 0)
|
||||
[GroupCommand, Description("Get a NotFact")]
|
||||
public async Task NotFact(CommandContext ctx, [Description("[Optional] A NotFact ID")] int notFactID = 0)
|
||||
{
|
||||
if (!IsEnabled)
|
||||
return;
|
||||
await ctx.TriggerTypingAsync();
|
||||
|
||||
notFactID--;
|
||||
@@ -35,16 +41,106 @@ namespace StarsiegeBot
|
||||
{
|
||||
await ctx.RespondAsync(notfacts[Program.rnd.Next(0, notfacts.Count)]);
|
||||
}
|
||||
|
||||
// await ctx.RespondAsync("**Warning:** The Id's used here are arbitrary");
|
||||
}
|
||||
|
||||
[Command("notfactcount"), Description("Get a count of the NotFacts")]
|
||||
public async Task SnappleCount(CommandContext ctx)
|
||||
[Command("count"), Aliases("c"), Description("Get a count of the NotFacts")]
|
||||
public async Task NotFactCount(CommandContext ctx)
|
||||
{
|
||||
if (!IsEnabled)
|
||||
return;
|
||||
await ctx.TriggerTypingAsync();
|
||||
await ctx.RespondAsync(notfacts.Count + " NotFacts in the NotFacts Bank");
|
||||
await ctx.RespondAsync(StartEmbed(notfacts.Count + " NotFacts in the NotFacts Bank"));
|
||||
}
|
||||
|
||||
[Command("load"), RequireOwner, Aliases("l")]
|
||||
public async Task Load(CommandContext ctx)
|
||||
{
|
||||
// trigger the typing.
|
||||
await ctx.TriggerTypingAsync();
|
||||
// start the response.
|
||||
DiscordEmbedBuilder embed = StartEmbed("Reloading NotFacts");
|
||||
// our old setting...
|
||||
embed.AddField("Old", (IsEnabled ? "Enabled" : "Disabled"));
|
||||
// run this, we dont care about its actual results.
|
||||
_ = Load();
|
||||
// report our new setting.
|
||||
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(fileName))
|
||||
embed.WithFooter($":warning: {fileName} file is missing.");
|
||||
// send our results.
|
||||
await ctx.RespondAsync(embed);
|
||||
}
|
||||
|
||||
[Command("toggle"), RequireOwner, Aliases("t")]
|
||||
public async Task Enable(CommandContext ctx, [RemainingText] string isEnabled = null)
|
||||
{
|
||||
// trigger the typing.
|
||||
await ctx.TriggerTypingAsync();
|
||||
// lower case teh input.
|
||||
isEnabled = isEnabled.ToLower();
|
||||
string[] turnOn = { "on", "true", "1" };
|
||||
string[] turnOff = { "off", "false", "0" };
|
||||
//start our response.
|
||||
DiscordEmbedBuilder embed = StartEmbed("NotFact Toggle");
|
||||
embed.AddField("Old", (IsEnabled ? "Enabled" : "Disabled"));
|
||||
// check for file.
|
||||
if (File.Exists(fileName))
|
||||
{
|
||||
// compare desired input against on or off values.
|
||||
if (turnOn.Contains(isEnabled))
|
||||
{
|
||||
IsEnabled = true;
|
||||
embed.AddField("New", (IsEnabled ? "Enabled" : "Disabled"));
|
||||
}
|
||||
else if (turnOff.Contains(isEnabled))
|
||||
{
|
||||
IsEnabled = false;
|
||||
embed.AddField("New", (IsEnabled ? "Enabled" : "Disabled"));
|
||||
}
|
||||
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.WithFooter(":warning: Invalid command given?");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// file is missing, disable this, regardless of their desires. Report the file is missing in the footer with a :warning:
|
||||
IsEnabled = false;
|
||||
embed.AddField("New", (IsEnabled ? "Enabled" : "Disabled"));
|
||||
embed.WithFooter($":warning: {fileName} file is missing, and it can not be enabled.");
|
||||
}
|
||||
await ctx.RespondAsync(embed);
|
||||
}
|
||||
|
||||
|
||||
private DiscordEmbedBuilder StartEmbed(string desc)
|
||||
{
|
||||
DiscordEmbedBuilder embed = new DiscordEmbedBuilder
|
||||
{
|
||||
Description = desc,
|
||||
Color = Program.colours[Program.rnd.Next(0, Program.colours.Length)]
|
||||
};
|
||||
return embed;
|
||||
}
|
||||
|
||||
private bool Load()
|
||||
{
|
||||
bool ret;
|
||||
if (File.Exists(fileName))
|
||||
{
|
||||
string json = File.ReadAllText(fileName);
|
||||
notfacts = JsonConvert.DeserializeObject<List<string>>(json);
|
||||
ret = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = false;
|
||||
}
|
||||
IsEnabled = ret;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user