Skip to content
This repository was archived by the owner on Oct 12, 2021. It is now read-only.

Commit 6e629d0

Browse files
committed
Fixed commented out code that crashed on certain listings with ghost packages. Improved logging.
1 parent f3f7790 commit 6e629d0

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

StoreBot/Program.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ static async Task Main(string[] args)
2929
Console.WriteLine($"Connected to Discord services");
3030
await Utilities.Log($"Logged into Discord at {System.DateTime.Now}");
3131
await client.SetGameAsync("DisplayCatalog", null, ActivityType.Watching);
32-
dcat = new DisplayCatalogHandler(DCatEndpoint.Production, new Locale(Market.US, Lang.en, true));
3332
await Task.Delay(-1);
3433

3534
}
@@ -39,6 +38,7 @@ static async Task MessageReceived(SocketMessage message)
3938
{
4039
if (message.Content.StartsWith("$"))
4140
{
41+
dcat = DisplayCatalogHandler.ProductionConfig();
4242
#if DEBUG
4343
await message.Channel.SendMessageAsync($"StoreBot is running in DEBUG mode. Output will be verbose.");
4444
#endif
@@ -60,6 +60,13 @@ static async Task MessageReceived(SocketMessage message)
6060
dcat = new DisplayCatalogHandler(DCatEndpoint.Xbox, new Locale(Market.US, Lang.en, true));
6161
await message.Channel.SendMessageAsync($"DCAT Endpoint was changed to {message.Content}");
6262
break;
63+
#if DEBUG
64+
case "$STOP":
65+
await message.Channel.SendMessageAsync($"StoreBot is shutting down. Thanks for playing FE3.");
66+
await Utilities.Log(new LogMessage(LogSeverity.Info, message.Channel.Name, "StoreBot is shutting down. Thanks for playing FE3."));
67+
System.Environment.Exit(0);
68+
break;
69+
#endif
6370

6471
}
6572
if (message.Content.Length != 13)
@@ -148,23 +155,25 @@ private static async Task BuildReply(DisplayCatalogModel displayCatalogModel, So
148155
await message.Channel.SendMessageAsync(MoreDetailsHelper.ToString());
149156
StringBuilder packages = new StringBuilder();
150157
List<string> packagelist = new List<string>(Regex.Split(packages.ToString(), @"(?<=\G.{1999})", RegexOptions.Singleline));
151-
/*
158+
152159
if (displayCatalogModel.Product.DisplaySkuAvailabilities[0].Sku.Properties.Packages.Count > 0)
153160
{
154161
Debug.WriteLine(displayCatalogModel.Product.DisplaySkuAvailabilities[0].Sku.Properties.Packages.Count);
155-
try
162+
if (displayCatalogModel.Product.DisplaySkuAvailabilities[0].Sku.Properties.Packages.Count != 0)
156163
{
157-
foreach (var Package in displayCatalogModel.Product.DisplaySkuAvailabilities[0].Sku.Properties.Packages[0].PackageDownloadUris)
164+
//For some weird reason, some listings report having packages when really they don't have one hosted. This checks the child to see if the package is really null or not.
165+
if (!object.ReferenceEquals(displayCatalogModel.Product.DisplaySkuAvailabilities[0].Sku.Properties.Packages[0].PackageDownloadUris, null))
158166
{
159-
packages.AppendLine($"Xbox Live Package: {Package.Uri}");
167+
foreach (var Package in displayCatalogModel.Product.DisplaySkuAvailabilities[0].Sku.Properties.Packages[0].PackageDownloadUris)
168+
{
169+
packages.AppendLine($"Xbox Live Package: {Package.Uri}");
170+
}
160171
}
161172
}
162-
catch { }
173+
174+
163175
}
164-
*/
165-
/*
166176

167-
*/
168177
packages.AppendLine($"(2/3)");
169178
await message.Channel.SendMessageAsync(packages.ToString());
170179
foreach (PackageInstance package in packageInstances)

StoreBot/Utilities.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Discord;
22
using System;
33
using System.Collections.Generic;
4+
using System.Diagnostics;
45
using System.IO;
56
using System.Text;
67
using System.Threading.Tasks;
@@ -11,8 +12,33 @@ public static class Utilities
1112
{
1213
public static async Task Log(LogMessage message)
1314
{
14-
Console.WriteLine($"Exception: {message.Message} at {message.Source}");
15-
await File.AppendAllTextAsync("log.txt", $"Exception: {message.Message} at {message.Source}");
15+
switch (message.Severity)
16+
{
17+
case LogSeverity.Error:
18+
Console.WriteLine($"Exception: {message.Message}:{message.Exception.StackTrace}. Caller: {message.Source}");
19+
await File.AppendAllTextAsync("log.txt", $"[{DateTime.Now.ToString()}]Exception: {message.Message}:{message.Exception.StackTrace}. Caller: {message.Source}");
20+
return;
21+
case LogSeverity.Warning:
22+
Console.WriteLine($"Warning: {message.Message}. Caller: {message.Source}");
23+
await File.AppendAllTextAsync("log.txt", $"[{DateTime.Now.ToString()}]Warning: {message.Message}. Caller: {message.Source}");
24+
return;
25+
case LogSeverity.Critical:
26+
Console.WriteLine($"CRITICAL FAILURE: {message.Message}:{message.Exception.StackTrace}. StoreBot will now exit.");
27+
await File.AppendAllTextAsync("log.txt", $"[{DateTime.Now.ToString()}]CRITICAL FAILURE: {message.Message}:{message.Exception.StackTrace}. StoreBot will now exit.");
28+
throw message.Exception;
29+
#if DEBUG
30+
case LogSeverity.Verbose:
31+
Console.WriteLine($"Verbose Output: {message.Message}. Caller: {message.Source}");
32+
await File.AppendAllTextAsync("log.txt", $"Verbose Output: {message.Message}. Caller: {message.Source}");
33+
return;
34+
case LogSeverity.Debug:
35+
Console.WriteLine($"Debug Output: {message.Message}. Caller: {message.Source}");
36+
await File.AppendAllTextAsync("log.txt", $"Debug Output: {message.Message}. Caller: {message.Source}");
37+
return;
38+
#endif
39+
default:
40+
return;
41+
}
1642
}
1743

1844
public static async Task Log(string message)
@@ -37,7 +63,18 @@ public Settings()
3763
string[] NumberOfSearchResultsRaw = rawsettings[0].Split("=");
3864
string[] AuthTokenRaw = rawsettings[1].Split("=");
3965
this.NumberOfSearchResults = int.Parse(NumberOfSearchResultsRaw[1]);
40-
this.AuthToken = AuthTokenRaw[1];
66+
if(AuthTokenRaw[1] == "REPLACEME")
67+
{
68+
Debug.WriteLine("Using Environment Variable Token");
69+
this.AuthToken = System.Environment.GetEnvironmentVariable("STOREBOTTOKEN", EnvironmentVariableTarget.User);
70+
Debug.WriteLine($"Got Environment Variable Token: {this.AuthToken}");
71+
72+
}
73+
else
74+
{
75+
this.AuthToken = AuthTokenRaw[1];
76+
77+
}
4178
}
4279

4380

0 commit comments

Comments
 (0)