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
@@ -24,31 +24,58 @@ namespace StarsiegeBot
|
||||
{
|
||||
private readonly List<Quickchats> Quickchats;
|
||||
private readonly Random rnd = new Random();
|
||||
private readonly bool QuickChatsEnabled;
|
||||
|
||||
public Quickchat()
|
||||
{
|
||||
Console.WriteLine("Quick Chat Commands Loaded");
|
||||
// Load all the Quick Chat texts and sound wav file info.
|
||||
var json = "";
|
||||
using (var fs = File.OpenRead("quickchats.json"))
|
||||
using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
|
||||
json = sr.ReadToEnd();
|
||||
Dictionary<string, Quickchats> tempQC = JsonConvert.DeserializeObject<Dictionary<string, Quickchats>>(json);
|
||||
Quickchats = Enumerable.ToList(tempQC.Values);
|
||||
// Check to see if the file exists... Otherwise disable all commands.
|
||||
if (File.Exists("quickchats.json"))
|
||||
{
|
||||
// Load the JSON file.
|
||||
var json = "";
|
||||
using (var fs = File.OpenRead("quickchats.json"))
|
||||
using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
|
||||
json = sr.ReadToEnd();
|
||||
Dictionary<string, Quickchats> tempQC = JsonConvert.DeserializeObject<Dictionary<string, Quickchats>>(json);
|
||||
// set the Quick Chat object.
|
||||
Quickchats = Enumerable.ToList(tempQC.Values);
|
||||
// enable the commands.
|
||||
QuickChatsEnabled = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// file not found. disable commands, leave a comment in console.
|
||||
Console.WriteLine(" -- --- --- Quick Chat JSON file not found.");
|
||||
QuickChatsEnabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
[Command("script")]
|
||||
[Description("Gives the `say()` for use in the Quickchat.cs file.")]
|
||||
public async Task GetQuickChatScript(CommandContext ctx, [Description("ID of Quick chat to get script for.")] int index)
|
||||
{
|
||||
// trigger some typing on discord's side.
|
||||
await ctx.TriggerTypingAsync();
|
||||
if (index < Quickchats.Count || index >= 0)
|
||||
// If we're disabled, let them know. And exit out.
|
||||
if (!QuickChatsEnabled)
|
||||
{
|
||||
await ctx.RespondAsync("Quick Chat Commands have been disabled. Please contact the bot owners.");
|
||||
return;
|
||||
}
|
||||
// We're good to go... If we have an index, pull it up.
|
||||
index = Math.Abs(index);
|
||||
// is our index within bounds?
|
||||
if (index < Quickchats.Count)
|
||||
{
|
||||
// yes it is, give the script to them.
|
||||
Quickchats chat = Quickchats[index];
|
||||
// why does this look broken, yet works? maybe its the ToString() function. I unno.
|
||||
await ctx.RespondAsync("```" + chat.ToString() + "```");
|
||||
}
|
||||
else
|
||||
{
|
||||
// we couldn't find it... Oh well, let the end user know.
|
||||
await ctx.RespondAsync("The ID specified is out of bounds. 0-" + (Quickchats.Count - 1));
|
||||
}
|
||||
}
|
||||
@@ -57,15 +84,25 @@ namespace StarsiegeBot
|
||||
[Description("Runs a check to see how many WAV files are missing from all QC's.")]
|
||||
public async Task QuickChatCheck(CommandContext ctx)
|
||||
{
|
||||
await ctx.TriggerTypingAsync();
|
||||
// If we're disabled, let them know. And exit out.
|
||||
if (!QuickChatsEnabled)
|
||||
{
|
||||
await ctx.RespondAsync("Quick Chat Commands have been disabled. Please contact the bot owners.");
|
||||
return;
|
||||
}
|
||||
// Start a count
|
||||
int missingFiles = 0;
|
||||
for (int i = 0; i < Quickchats.Count; i++)
|
||||
{
|
||||
// If this file doesnt exist, log the ID in the console, and add to the count.
|
||||
if (!File.Exists($"./qc/{Quickchats[i].soundFile}"))
|
||||
{
|
||||
missingFiles++;
|
||||
Console.Write($"{i}, ");
|
||||
}
|
||||
}
|
||||
// Reply to the message and let the user know we're missing X files.
|
||||
await ctx.RespondAsync($"Missing files: {missingFiles}");
|
||||
}
|
||||
|
||||
@@ -74,10 +111,18 @@ namespace StarsiegeBot
|
||||
public async Task QuickChat(CommandContext ctx, [Description("The ID of the quick Chat.")] int id = -1)
|
||||
{
|
||||
await ctx.TriggerTypingAsync();
|
||||
// If we're disabled, let them know. And exit out.
|
||||
if (!QuickChatsEnabled)
|
||||
{
|
||||
await ctx.RespondAsync("Quick Chat Commands have been disabled. Please contact the bot owners.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Start a new Message Build.
|
||||
DiscordMessageBuilder msg = new DiscordMessageBuilder();
|
||||
|
||||
Quickchats chat;
|
||||
|
||||
// Make an absolute value out of what was given to us.
|
||||
// -1 lets us know that we're doing random. A number out of bounds will also give a random number.
|
||||
if (id != -1)
|
||||
id = Math.Abs(id);
|
||||
if (id == -1 || id > Quickchats.Count)
|
||||
@@ -88,19 +133,27 @@ namespace StarsiegeBot
|
||||
}
|
||||
else
|
||||
{
|
||||
// we're given a number within bounds. Give them that QC.
|
||||
chat = Quickchats[id];
|
||||
msg.Content = id + ": " + chat.text;
|
||||
|
||||
}
|
||||
|
||||
// If the sound file to the select QC exists, upload it as well.
|
||||
if (File.Exists($"./qc/{chat.soundFile}"))
|
||||
{
|
||||
// Open the file.
|
||||
FileStream sound = new FileStream($"./qc/{chat.soundFile}", FileMode.Open);
|
||||
// feed it to the Message Builder.
|
||||
msg.WithFile(chat.soundFile, sound);
|
||||
// Send the message.
|
||||
await ctx.RespondAsync(msg);
|
||||
// Close the file.
|
||||
sound.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
// No file to upload, just send the the text QC.
|
||||
await ctx.RespondAsync(msg);
|
||||
}
|
||||
}
|
||||
@@ -109,19 +162,34 @@ namespace StarsiegeBot
|
||||
public async Task SearchQuickChats(CommandContext ctx, [RemainingText] string toSearch)
|
||||
{
|
||||
await ctx.TriggerTypingAsync();
|
||||
// If we're disabled, let them know. And exit out.
|
||||
if (!QuickChatsEnabled)
|
||||
{
|
||||
await ctx.RespondAsync("Quick Chat Commands have been disabled. Please contact the bot owners.");
|
||||
return;
|
||||
}
|
||||
|
||||
// We're going to build a message.
|
||||
DiscordMessageBuilder msg = new DiscordMessageBuilder();
|
||||
msg.Content = "";
|
||||
// We're going to search each QC for the stuff in toSearch.
|
||||
for (int i = 0; i < Quickchats.Count; i++)
|
||||
{
|
||||
// do we have a match?
|
||||
if (Quickchats[i].text.ToLower().Contains(toSearch.ToLower()))
|
||||
{
|
||||
// add it to the list!
|
||||
msg.Content += $"[{i}] {Quickchats[i].text}\r\n";
|
||||
}
|
||||
}
|
||||
|
||||
// If message content is still nothing, report we found nothing.
|
||||
if (msg.Content == "")
|
||||
{
|
||||
msg.Content = "No results found.";
|
||||
}
|
||||
|
||||
// Tell the end user our results, or lack of.
|
||||
await ctx.RespondAsync(msg);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user