Skip to content

Commit

Permalink
Failsafe added in case someone use "/send" before downloading and loa…
Browse files Browse the repository at this point in the history
…ding the csv
  • Loading branch information
outerwinnie committed Sep 2, 2024
1 parent ab6db9a commit 1f49185
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Program
private static string _credentialsPath;
private static TimeSpan _postTimeSpain;
private static TimeZoneInfo _spainTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
private static bool _isImageUrlsLoaded = false; // Flag to track if image URLs are loaded

static async Task Main(string[] args)
{
Expand Down Expand Up @@ -92,6 +93,8 @@ private static async Task OnReady()
.Where(record => !string.IsNullOrWhiteSpace(record.image_url) && record.has_spoilers != "yes")
.Select(record => record.image_url.Trim())
.ToList();

_isImageUrlsLoaded = true; // Set flag to true when URLs are loaded
}

Console.WriteLine("Filtered URLs read from CSV:");
Expand All @@ -106,13 +109,6 @@ private static async Task OnReady()
return;
}

// Check if imageUrls is empty
if (_imageUrls.Count == 0)
{
Console.WriteLine("No valid URLs available. Exiting...");
return;
}

// Register commands
await RegisterCommandsAsync();

Expand Down Expand Up @@ -150,15 +146,22 @@ private static async Task HandleInteractionAsync(SocketInteraction interaction)

private static async Task HandleSendCommandAsync(SocketSlashCommand command)
{
if (_imageUrls.Count > 0)
if (_isImageUrlsLoaded)
{
int index = _random.Next(_imageUrls.Count);
string randomUrl = _imageUrls[index];
await command.RespondAsync(randomUrl);
if (_imageUrls.Count > 0)
{
int index = _random.Next(_imageUrls.Count);
string randomUrl = _imageUrls[index];
await command.RespondAsync(randomUrl);
}
else
{
await command.RespondAsync("No URLs available.");
}
}
else
{
await command.RespondAsync("No URLs available.");
await command.RespondAsync("The bot is still loading data. Please try again later.");
}
}

Expand Down

0 comments on commit 1f49185

Please sign in to comment.