Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the option to fuction as a reward for the social credit bot #4

Merged
merged 8 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ENV DISCORD_BOT_TOKEN=""
ENV DISCORD_CHANNEL_ID=""
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"

# Entry point for the application
Expand Down
72 changes: 70 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class Program
private static TimeZoneInfo _spainTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
private static bool _isImageUrlsLoaded = false; // Flag to track if image URLs are loaded

// Path to the local rewards CSV file
private static string _rewardsCsvPath;

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

// Check if token, channelId, fileId, credentialsPath, or postTime is null or empty
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(channelIdStr) || string.IsNullOrEmpty(_fileId) || string.IsNullOrEmpty(_credentialsPath) || string.IsNullOrEmpty(postTimeStr))
// 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))
{
Console.WriteLine("Environment variables are not set correctly.");
return;
Expand Down Expand Up @@ -109,6 +113,9 @@ private static async Task OnReady()
return;
}

// Process rewards
await ProcessRewards();

// Register commands
await RegisterCommandsAsync();

Expand Down Expand Up @@ -251,10 +258,71 @@ 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 existing rewards data
using (var reader = new StreamReader(_rewardsCsvPath))
using (var csv = new CsvReader(reader, new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)))
{
records = csv.GetRecords<RewardRecordClass>().ToList();
}

foreach (var record in records)
{
if (record.RewardName == "recuerdate")
{
if (int.TryParse(record.Quantity, out int quantity))
{
var imagesSent = 0;
for (int i = 0; i < quantity; i++)
{
await PostRandomImageUrl();
imagesSent++;
}

// 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);
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred while processing the rewards file: {ex.Message}");
}
}

public class YourRecordClass
{
public string image_url { get; set; }
public string has_spoilers { get; set; }
}

public class RewardRecordClass
{
public string RewardName { get; set; }
public string Quantity { get; set; }
}
}
}