Skip to content

Commit

Permalink
more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
outerwinnie committed Aug 31, 2024
1 parent 6be1d7e commit b571f09
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace DiscordBotExample
class Program
{
private static List<string> _imageUrls;
private static List<string> _messages;
private static Random _random = new Random();
private static DiscordSocketClient _client;
private static ulong _channelId;
Expand Down Expand Up @@ -90,18 +91,23 @@ private static async Task OnReady()
using (var reader = new StringReader(csvData))
using (var csv = new CsvReader(reader, new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)))
{
_imageUrls = csv.GetRecords<YourRecordClass>()
.Where(record => !string.IsNullOrWhiteSpace(record.image_url)
&& record.has_spoilers != "yes"
&& record.total_reactions >= 8)
.Select(record => record.image_url.Trim())
.ToList();
var records = csv.GetRecords<YourRecordClass>().ToList();

// Filter records with at least 8 reactions, including those without images
_messages = records.Where(record => record.total_reactions >= 8).Select(record =>
{
if (!string.IsNullOrWhiteSpace(record.image_url))
{
return $"{record.message_text}\n{record.image_url.Trim()}";
}
return record.message_text;
}).ToList();
}

Console.WriteLine("Filtered URLs read from CSV:");
foreach (var url in _imageUrls)
Console.WriteLine("Filtered messages read from CSV:");
foreach (var message in _messages)
{
Console.WriteLine(url);
Console.WriteLine(message);
}
}
else
Expand All @@ -110,10 +116,10 @@ private static async Task OnReady()
return;
}

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

Expand All @@ -125,8 +131,7 @@ private static async Task ScheduleNextPost()
{
var nowUtc = DateTime.UtcNow;
var spainTime = TimeZoneInfo.ConvertTimeFromUtc(nowUtc, _spainTimeZone);

// Specify that nextPostTimeSpain is unspecified in terms of kind because we will convert it to a specific time zone

var nextPostTimeSpain = DateTime.SpecifyKind(DateTime.Today.Add(_postTimeSpain), DateTimeKind.Unspecified);

if (nextPostTimeSpain <= spainTime)
Expand All @@ -135,17 +140,15 @@ private static async Task ScheduleNextPost()
nextPostTimeSpain = nextPostTimeSpain.AddDays(1);
}

// Convert the unspecified time to Spain time zone and then to UTC
nextPostTimeSpain = TimeZoneInfo.ConvertTimeToUtc(nextPostTimeSpain, _spainTimeZone);

// Calculate the delay
var delay = nextPostTimeSpain - nowUtc;

Console.WriteLine($"Scheduling next post in {delay.TotalMinutes} minutes.");

await Task.Delay(delay);

await PostRandomImageUrl();
await PostRandomMessage();

// Schedule the next post
await ScheduleNextPost();
Expand All @@ -155,7 +158,6 @@ private static async Task<string> DownloadCsvFromGoogleDrive()
{
try
{
// Set up Google Drive API service
var credential = GoogleCredential.FromFile(_credentialsPath)
.CreateScoped(DriveService.Scope.DriveReadonly);

Expand All @@ -165,7 +167,6 @@ private static async Task<string> DownloadCsvFromGoogleDrive()
ApplicationName = "DiscordBotExample",
});

// Download the file
var request = service.Files.Get(_fileId);
var stream = new MemoryStream();
request.MediaDownloader.ProgressChanged += progress =>
Expand All @@ -191,26 +192,27 @@ private static async Task<string> DownloadCsvFromGoogleDrive()
}
}

private static async Task PostRandomImageUrl()
private static async Task PostRandomMessage()
{
var channel = _client.GetChannel(_channelId) as IMessageChannel;

if (channel != null && _imageUrls.Count > 0)
if (channel != null && _messages.Count > 0)
{
int index = _random.Next(_imageUrls.Count);
string randomUrl = _imageUrls[index];
await channel.SendMessageAsync(randomUrl);
int index = _random.Next(_messages.Count);
string randomMessage = _messages[index];
await channel.SendMessageAsync(randomMessage);
}
else
{
Console.WriteLine("No URLs available.");
Console.WriteLine("No messages available.");
}
}
}

// Define a class that matches the CSV structure
public class YourRecordClass
{
public string message_text { get; set; }
public string image_url { get; set; }
public string has_spoilers { get; set; }
public int total_reactions { get; set; }
Expand Down

0 comments on commit b571f09

Please sign in to comment.