Skip to content

Commit

Permalink
Merge pull request #5 from outerwinnie/test
Browse files Browse the repository at this point in the history
Added timer
  • Loading branch information
outerwinnie authored Sep 5, 2024
2 parents 351b6c5 + d15419d commit 2c4ee90
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 38 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ENV GOOGLE_DRIVE_FILE_ID=""
ENV GOOGLE_CREDENTIALS_PATH="/app/credentials.json"
ENV REWARDS_CSV_PATH="/app/data/rewards.csv"
ENV POST_TIME="20:00:00"
ENV REWARDS_INTERVAL_MINUTES="5"

# Entry point for the application
ENTRYPOINT ["dotnet", "Recuerdense-Bot.dll"]
91 changes: 53 additions & 38 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
Expand Down Expand Up @@ -30,6 +31,10 @@ class Program
// Path to the local rewards CSV file
private static string _rewardsCsvPath;

// Timer for periodic rewards processing
private static Timer _rewardsTimer;
private static TimeSpan _rewardsInterval;

static async Task Main(string[] args)
{
// Read environment variables
Expand All @@ -39,9 +44,10 @@ static async Task Main(string[] args)
_credentialsPath = Environment.GetEnvironmentVariable("GOOGLE_CREDENTIALS_PATH");
var postTimeStr = Environment.GetEnvironmentVariable("POST_TIME");
_rewardsCsvPath = Environment.GetEnvironmentVariable("REWARDS_CSV_PATH");
var rewardsIntervalStr = Environment.GetEnvironmentVariable("REWARDS_INTERVAL_MINUTES");

// Check if token, channelId, fileId, credentialsPath, postTime, or rewardsCsvPath is null or empty
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(channelIdStr) || string.IsNullOrEmpty(_fileId) || string.IsNullOrEmpty(_credentialsPath) || string.IsNullOrEmpty(postTimeStr) || string.IsNullOrEmpty(_rewardsCsvPath))
// Check if token, channelId, fileId, credentialsPath, postTime, rewardsCsvPath, or rewardsInterval is null or empty
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(channelIdStr) || string.IsNullOrEmpty(_fileId) || string.IsNullOrEmpty(_credentialsPath) || string.IsNullOrEmpty(postTimeStr) || string.IsNullOrEmpty(_rewardsCsvPath) || string.IsNullOrEmpty(rewardsIntervalStr))
{
Console.WriteLine("Environment variables are not set correctly.");
return;
Expand All @@ -61,6 +67,15 @@ static async Task Main(string[] args)
return;
}

// Parse rewards interval
if (!int.TryParse(rewardsIntervalStr, out int rewardsIntervalMinutes))
{
Console.WriteLine("Invalid REWARDS_INTERVAL_MINUTES format. It must be an integer.");
return;
}

_rewardsInterval = TimeSpan.FromMinutes(rewardsIntervalMinutes);

// Initialize the Discord client
_client = new DiscordSocketClient();
_client.Log += Log;
Expand All @@ -71,6 +86,12 @@ static async Task Main(string[] args)
await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync();

// Set up the timer for rewards processing
_rewardsTimer = new Timer(async _ =>
{
await ProcessRewards();
}, null, _rewardsInterval, _rewardsInterval);

// Block the application until it is closed
await Task.Delay(-1);
}
Expand All @@ -91,9 +112,9 @@ private static async Task OnReady()
if (csvData != null)
{
using (var reader = new StringReader(csvData))
using (var csv = new CsvReader(reader, new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)))
using (var csvReader = new CsvReader(reader, new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)))
{
_imageUrls = csv.GetRecords<YourRecordClass>()
_imageUrls = csvReader.GetRecords<YourRecordClass>()
.Where(record => !string.IsNullOrWhiteSpace(record.image_url) && record.has_spoilers != "yes")
.Select(record => record.image_url.Trim())
.ToList();
Expand All @@ -113,7 +134,7 @@ private static async Task OnReady()
return;
}

// Process rewards
// Process rewards initially
await ProcessRewards();

// Register commands
Expand Down Expand Up @@ -260,51 +281,45 @@ private static async Task PostRandomImageUrl()

private static async Task ProcessRewards()
{
if (string.IsNullOrEmpty(_rewardsCsvPath) || !File.Exists(_rewardsCsvPath))
{
Console.WriteLine("Rewards CSV file not found.");
return;
}

try
{
var records = new List<RewardRecordClass>();
// Read the rewards CSV from local storage
var rewardsCsvData = File.ReadAllText(_rewardsCsvPath);

// Read the existing rewards data
using (var reader = new StreamReader(_rewardsCsvPath))
using (var csv = new CsvReader(reader, new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)))
// Parse the CSV data
using (var reader = new StringReader(rewardsCsvData))
using (var csvReader = new CsvReader(reader, new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)))
{
records = csv.GetRecords<RewardRecordClass>().ToList();
}
var records = csvReader.GetRecords<RewardRecordClass>().ToList();

foreach (var record in records)
{
if (record.RewardName == "recuerdate")
foreach (var record in records)
{
if (int.TryParse(record.Quantity, out int quantity))
if (record.RewardName.Equals("recuerdate", StringComparison.OrdinalIgnoreCase))
{
var imagesSent = 0;
for (int i = 0; i < quantity; i++)
if (int.TryParse(record.Quantity, out int quantity) && quantity > 0)
{
await PostRandomImageUrl();
imagesSent++;
Console.WriteLine($"Processing reward '{record.RewardName}' with quantity {quantity}.");
for (int i = 0; i < quantity; i++)
{
await PostRandomImageUrl();
}

// Decrease the quantity after sending the images
record.Quantity = "0"; // Set quantity to 0 after processing
}
else
{
Console.WriteLine($"Invalid Quantity value for record with RewardName '{record.RewardName}'.");
}

// Update the Quantity field
record.Quantity = (quantity - imagesSent).ToString();
}
else
{
Console.WriteLine($"Invalid Quantity value for record with RewardName '{record.RewardName}'.");
}
}
}

// Write the updated rewards data back to the CSV file
using (var writer = new StreamWriter(_rewardsCsvPath))
using (var csv = new CsvWriter(writer, new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)))
{
csv.WriteRecords(records);
// Write the updated rewards data back to the CSV file
using (var writer = new StreamWriter(_rewardsCsvPath))
using (var csvWriter = new CsvWriter(writer, new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)))
{
csvWriter.WriteRecords(records);
}
}
}
catch (Exception ex)
Expand Down

0 comments on commit 2c4ee90

Please sign in to comment.