Added in fail checks on files existing for all the JSON files in the project. Minus questions.json since that file doesn't exist quite yet.
This commit is contained in:
1 parent
9af84c3e62
commit
f1ac028c35
4 files changed
+216
-26
No files matched your search
@@ -31,16 +31,28 @@ namespace StarsiegeBot
|
||||
DiscordColor.Violet, DiscordColor.Wheat, DiscordColor.White, DiscordColor.Yellow };
|
||||
private readonly Random rnd = new Random();
|
||||
private readonly Dictionary<string, SSFunction> ssFunctions;
|
||||
private readonly bool FunctionsEnabled;
|
||||
|
||||
public Functions()
|
||||
{
|
||||
Console.WriteLine("Starsiege Function Commands Loaded");
|
||||
// Load the Functions JSON file. Has all information regarding Starsiege in-game Functions.
|
||||
var json = "";
|
||||
using (var fs = File.OpenRead("functions.json"))
|
||||
using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
|
||||
json = sr.ReadToEnd();
|
||||
ssFunctions = JsonConvert.DeserializeObject<Dictionary<string, SSFunction>>(json);
|
||||
|
||||
// Check for file, if not there, disable commands.
|
||||
if (File.Exists("functions.json"))
|
||||
{
|
||||
// Load the Functions JSON file. Has all information regarding Starsiege in-game Functions.
|
||||
var json = "";
|
||||
using (var fs = File.OpenRead("functions.json"))
|
||||
using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
|
||||
json = sr.ReadToEnd();
|
||||
ssFunctions = JsonConvert.DeserializeObject<Dictionary<string, SSFunction>>(json);
|
||||
FunctionsEnabled = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
FunctionsEnabled = false;
|
||||
Console.WriteLine(" --- --- --- Functions JSON not found.");
|
||||
}
|
||||
}
|
||||
|
||||
[GroupCommand]
|
||||
@@ -48,39 +60,61 @@ namespace StarsiegeBot
|
||||
public async Task StarsiegeFunctions(CommandContext ctx, [RemainingText, Description("The function to attempt to look up.")] string command = "")
|
||||
{
|
||||
await ctx.TriggerTypingAsync();
|
||||
if (!FunctionsEnabled)
|
||||
{
|
||||
await ctx.RespondAsync("Function Commands have been disabled. Please contact the bot owners.");
|
||||
return;
|
||||
}
|
||||
|
||||
// If the function command is empty make a list of commands in Starsiege.
|
||||
if (command.Equals(string.Empty))
|
||||
{
|
||||
// Get a list of the Keys.
|
||||
List<string> keys = new List<string>(ssFunctions.Keys);
|
||||
// get a new output.
|
||||
string output = string.Empty;
|
||||
// loop over the keys.
|
||||
foreach (string item in keys)
|
||||
{
|
||||
// if our output is empty, add the first time.
|
||||
if (output.Equals(string.Empty))
|
||||
{
|
||||
output = $"`{item}`";
|
||||
}
|
||||
// output is nto empty, add second or further item with a comma.
|
||||
else
|
||||
{
|
||||
output += $", `{item}`";
|
||||
}
|
||||
}
|
||||
// send the output.
|
||||
await ctx.RespondAsync("List of known Functions in Starsiege: " + output);
|
||||
}
|
||||
|
||||
|
||||
// we have a command the want to try to find.
|
||||
else if (ssFunctions.TryGetValue(command.ToLower(), out SSFunction outItem))
|
||||
{
|
||||
// New Message Embed!
|
||||
DiscordEmbedBuilder embed = new DiscordEmbedBuilder();
|
||||
// Set the title to the name of this function.
|
||||
embed.WithTitle(outItem.title);
|
||||
// set the timestamp to now... why, idk, its not really needed actually.
|
||||
embed.WithTimestamp(DateTime.Now);
|
||||
// add a random color to the left side bar of the embed.
|
||||
embed.WithColor(colours[rnd.Next(0, colours.Length)]);
|
||||
// Message Description, I dont remember what it is any more, sorry!
|
||||
embed.WithDescription(outItem.description);
|
||||
// iterate over each field item, and add them to the embed.
|
||||
foreach (Fields item in outItem.fields)
|
||||
{
|
||||
embed.AddField(item.name, item.value, true);
|
||||
}
|
||||
// Build the Embed.
|
||||
DiscordEmbed em = embed.Build();
|
||||
// send the embed.
|
||||
await ctx.RespondAsync(embed: em);
|
||||
}
|
||||
// we couldn't find that exact command... let them know.
|
||||
else
|
||||
{
|
||||
await ctx.RespondAsync("That command wasn't food, pot-head.");
|
||||
@@ -91,6 +125,14 @@ namespace StarsiegeBot
|
||||
public async Task FunctionCount(CommandContext ctx)
|
||||
{
|
||||
await ctx.TriggerTypingAsync();
|
||||
|
||||
// check ot make sure these commands are enabled. if disabled, report it.
|
||||
if (!FunctionsEnabled)
|
||||
{
|
||||
await ctx.RespondAsync("Function Commands have been disabled. Please contact the bot owners.");
|
||||
return;
|
||||
}
|
||||
// they wanted to know how many functions were documented... so tell them!
|
||||
await ctx.RespondAsync($"There are {ssFunctions.Count} functions in the known list.");
|
||||
}
|
||||
|
||||
@@ -124,6 +166,4 @@ namespace StarsiegeBot
|
||||
return $"Name: {name} || Value: {value}";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user