Skip to content

Commit

Permalink
Merge pull request #3 from outerwinnie/testing
Browse files Browse the repository at this point in the history
Added /send to force a random picture
  • Loading branch information
outerwinnie committed Sep 2, 2024
2 parents b4bfde1 + 6903374 commit bd98cf8
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 30 deletions.
79 changes: 62 additions & 17 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
using Discord.Commands;
using Discord.Rest;
using CsvHelper;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
Expand All @@ -23,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 All @@ -37,11 +40,6 @@ static async Task Main(string[] args)
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(channelIdStr) || string.IsNullOrEmpty(_fileId) || string.IsNullOrEmpty(_credentialsPath) || string.IsNullOrEmpty(postTimeStr))
{
Console.WriteLine("Environment variables are not set correctly.");
Console.WriteLine($"DISCORD_BOT_TOKEN: {(string.IsNullOrEmpty(token) ? "Not set" : "Set")}");
Console.WriteLine($"DISCORD_CHANNEL_ID: {(string.IsNullOrEmpty(channelIdStr) ? "Not set" : "Set")}");
Console.WriteLine($"GOOGLE_DRIVE_FILE_ID: {(string.IsNullOrEmpty(_fileId) ? "Not set" : "Set")}");
Console.WriteLine($"GOOGLE_CREDENTIALS_PATH: {(string.IsNullOrEmpty(_credentialsPath) ? "Not set" : "Set")}");
Console.WriteLine($"POST_TIME: {(string.IsNullOrEmpty(postTimeStr) ? "Not set" : "Set")}");
return;
}

Expand All @@ -63,6 +61,7 @@ static async Task Main(string[] args)
_client = new DiscordSocketClient();
_client.Log += Log;
_client.Ready += OnReady;
_client.InteractionCreated += HandleInteractionAsync;

// Start the bot
await _client.LoginAsync(TokenType.Bot, token);
Expand Down Expand Up @@ -94,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 @@ -108,17 +109,62 @@ 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();

// Schedule the first post
await ScheduleNextPost();
}

private static async Task RegisterCommandsAsync()
{
var sendCommand = new SlashCommandBuilder()
.WithName("send")
.WithDescription("Send a random image from the list");

// Replace 'your_guild_id_here' with your actual guild ID
var guildId = ulong.Parse(Environment.GetEnvironmentVariable("GUILD_ID")); // Example: 123456789012345678
var guild = _client.GetGuild(guildId);

await guild.DeleteApplicationCommandsAsync(); // Clear existing commands in the guild
await _client.Rest.DeleteAllGlobalCommandsAsync(); // Optionally clear global commands
await guild.CreateApplicationCommandAsync(sendCommand.Build());

Console.WriteLine("Slash command /send registered for guild");
}

private static async Task HandleInteractionAsync(SocketInteraction interaction)
{
if (interaction is SocketSlashCommand command)
{
if (command.Data.Name == "send")
{
await HandleSendCommandAsync(command);
}
}
}

private static async Task HandleSendCommandAsync(SocketSlashCommand command)
{
if (_isImageUrlsLoaded)
{
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("The bot is still loading data. Please try again later.");
}
}

private static async Task ScheduleNextPost()
{
var nowUtc = DateTime.UtcNow;
Expand Down Expand Up @@ -204,12 +250,11 @@ private static async Task PostRandomImageUrl()
Console.WriteLine("No URLs available.");
}
}
}

// Define a class that matches the CSV structure
public class YourRecordClass
{
public string image_url { get; set; }
public string has_spoilers { get; set; }
public class YourRecordClass
{
public string image_url { get; set; }
public string has_spoilers { get; set; }
}
}
}
1 change: 1 addition & 0 deletions Recuerdense-Bot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="Discord.Net" Version="3.15.3" />
<PackageReference Include="Google.Apis.Drive.v3" Version="1.68.0.3508" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="System.Threading.Tasks" Version="4.3.0" />
</ItemGroup>

Expand Down
Binary file modified obj/Debug/net7.0/Recuerdense-Bot.assets.cache
Binary file not shown.
Binary file modified obj/Debug/net7.0/Recuerdense-Bot.csproj.AssemblyReference.cache
Binary file not shown.
4 changes: 4 additions & 0 deletions obj/Recuerdense-Bot.csproj.nuget.dgspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
"target": "Package",
"version": "[1.68.0.3508, )"
},
"Microsoft.Extensions.DependencyInjection": {
"target": "Package",
"version": "[8.0.0, )"
},
"System.Threading.Tasks": {
"target": "Package",
"version": "[4.3.0, )"
Expand Down
85 changes: 74 additions & 11 deletions obj/project.assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,39 @@
}
}
},
"Microsoft.Extensions.DependencyInjection.Abstractions/6.0.0": {
"Microsoft.Extensions.DependencyInjection/8.0.0": {
"type": "package",
"dependencies": {
"Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0"
},
"compile": {
"lib/net6.0/Microsoft.Extensions.DependencyInjection.Abstractions.dll": {
"lib/net7.0/Microsoft.Extensions.DependencyInjection.dll": {
"related": ".xml"
}
},
"runtime": {
"lib/net6.0/Microsoft.Extensions.DependencyInjection.Abstractions.dll": {
"lib/net7.0/Microsoft.Extensions.DependencyInjection.dll": {
"related": ".xml"
}
},
"build": {
"buildTransitive/netcoreapp3.1/_._": {}
"buildTransitive/net6.0/_._": {}
}
},
"Microsoft.Extensions.DependencyInjection.Abstractions/8.0.0": {
"type": "package",
"compile": {
"lib/net7.0/Microsoft.Extensions.DependencyInjection.Abstractions.dll": {
"related": ".xml"
}
},
"runtime": {
"lib/net7.0/Microsoft.Extensions.DependencyInjection.Abstractions.dll": {
"related": ".xml"
}
},
"build": {
"buildTransitive/net6.0/_._": {}
}
},
"Microsoft.NETCore.Platforms/1.1.0": {
Expand Down Expand Up @@ -705,27 +724,66 @@
"useSharedDesignerContext.txt"
]
},
"Microsoft.Extensions.DependencyInjection.Abstractions/6.0.0": {
"sha512": "xlzi2IYREJH3/m6+lUrQlujzX8wDitm4QGnUu6kUXTQAWPuZY8i+ticFJbzfqaetLA6KR/rO6Ew/HuYD+bxifg==",
"Microsoft.Extensions.DependencyInjection/8.0.0": {
"sha512": "V8S3bsm50ig6JSyrbcJJ8bW2b9QLGouz+G1miK3UTaOWmMtFwNNNzUf4AleyDWUmTrWMLNnFSLEQtxmxgNQnNQ==",
"type": "package",
"path": "microsoft.extensions.dependencyinjection.abstractions/6.0.0",
"path": "microsoft.extensions.dependencyinjection/8.0.0",
"files": [
".nupkg.metadata",
".signature.p7s",
"Icon.png",
"LICENSE.TXT",
"PACKAGE.md",
"THIRD-PARTY-NOTICES.TXT",
"buildTransitive/net461/Microsoft.Extensions.DependencyInjection.targets",
"buildTransitive/net462/_._",
"buildTransitive/net6.0/_._",
"buildTransitive/netcoreapp2.0/Microsoft.Extensions.DependencyInjection.targets",
"lib/net462/Microsoft.Extensions.DependencyInjection.dll",
"lib/net462/Microsoft.Extensions.DependencyInjection.xml",
"lib/net6.0/Microsoft.Extensions.DependencyInjection.dll",
"lib/net6.0/Microsoft.Extensions.DependencyInjection.xml",
"lib/net7.0/Microsoft.Extensions.DependencyInjection.dll",
"lib/net7.0/Microsoft.Extensions.DependencyInjection.xml",
"lib/net8.0/Microsoft.Extensions.DependencyInjection.dll",
"lib/net8.0/Microsoft.Extensions.DependencyInjection.xml",
"lib/netstandard2.0/Microsoft.Extensions.DependencyInjection.dll",
"lib/netstandard2.0/Microsoft.Extensions.DependencyInjection.xml",
"lib/netstandard2.1/Microsoft.Extensions.DependencyInjection.dll",
"lib/netstandard2.1/Microsoft.Extensions.DependencyInjection.xml",
"microsoft.extensions.dependencyinjection.8.0.0.nupkg.sha512",
"microsoft.extensions.dependencyinjection.nuspec",
"useSharedDesignerContext.txt"
]
},
"Microsoft.Extensions.DependencyInjection.Abstractions/8.0.0": {
"sha512": "cjWrLkJXK0rs4zofsK4bSdg+jhDLTaxrkXu4gS6Y7MAlCvRyNNgwY/lJi5RDlQOnSZweHqoyvgvbdvQsRIW+hg==",
"type": "package",
"path": "microsoft.extensions.dependencyinjection.abstractions/8.0.0",
"files": [
".nupkg.metadata",
".signature.p7s",
"Icon.png",
"LICENSE.TXT",
"PACKAGE.md",
"THIRD-PARTY-NOTICES.TXT",
"buildTransitive/net461/Microsoft.Extensions.DependencyInjection.Abstractions.targets",
"buildTransitive/net462/_._",
"buildTransitive/net6.0/_._",
"buildTransitive/netcoreapp2.0/Microsoft.Extensions.DependencyInjection.Abstractions.targets",
"buildTransitive/netcoreapp3.1/_._",
"lib/net461/Microsoft.Extensions.DependencyInjection.Abstractions.dll",
"lib/net461/Microsoft.Extensions.DependencyInjection.Abstractions.xml",
"lib/net462/Microsoft.Extensions.DependencyInjection.Abstractions.dll",
"lib/net462/Microsoft.Extensions.DependencyInjection.Abstractions.xml",
"lib/net6.0/Microsoft.Extensions.DependencyInjection.Abstractions.dll",
"lib/net6.0/Microsoft.Extensions.DependencyInjection.Abstractions.xml",
"lib/net7.0/Microsoft.Extensions.DependencyInjection.Abstractions.dll",
"lib/net7.0/Microsoft.Extensions.DependencyInjection.Abstractions.xml",
"lib/net8.0/Microsoft.Extensions.DependencyInjection.Abstractions.dll",
"lib/net8.0/Microsoft.Extensions.DependencyInjection.Abstractions.xml",
"lib/netstandard2.0/Microsoft.Extensions.DependencyInjection.Abstractions.dll",
"lib/netstandard2.0/Microsoft.Extensions.DependencyInjection.Abstractions.xml",
"lib/netstandard2.1/Microsoft.Extensions.DependencyInjection.Abstractions.dll",
"lib/netstandard2.1/Microsoft.Extensions.DependencyInjection.Abstractions.xml",
"microsoft.extensions.dependencyinjection.abstractions.6.0.0.nupkg.sha512",
"microsoft.extensions.dependencyinjection.abstractions.8.0.0.nupkg.sha512",
"microsoft.extensions.dependencyinjection.abstractions.nuspec",
"useSharedDesignerContext.txt"
]
Expand Down Expand Up @@ -1179,6 +1237,7 @@
"CsvHelper >= 33.0.1",
"Discord.Net >= 3.15.3",
"Google.Apis.Drive.v3 >= 1.68.0.3508",
"Microsoft.Extensions.DependencyInjection >= 8.0.0",
"System.Threading.Tasks >= 4.3.0"
]
},
Expand Down Expand Up @@ -1231,6 +1290,10 @@
"target": "Package",
"version": "[1.68.0.3508, )"
},
"Microsoft.Extensions.DependencyInjection": {
"target": "Package",
"version": "[8.0.0, )"
},
"System.Threading.Tasks": {
"target": "Package",
"version": "[4.3.0, )"
Expand Down
5 changes: 3 additions & 2 deletions obj/project.nuget.cache
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": 2,
"dgSpecHash": "8L4McY806g6gaWyJ3N7rwakc1O/2wbVwtmjPlu8A5GWQBuYsZdNi3tecXUHRcjJgzaDu7Yn+CZcgGsaN4aqxeg==",
"dgSpecHash": "OqRsFgHRl7cY56bSI1IQL7iCdqXvdVoikKRr6yUyu6ZULVMINRj33//CN4Vf3HCgadJw8B+pv+bzRlgGvMU/YA==",
"success": true,
"projectFilePath": "D:\\Winnie\\Documentos\\recuerdate\\Recuerdense-Bot.csproj",
"expectedPackageFiles": [
Expand All @@ -17,7 +17,8 @@
"C:\\Users\\Winnie\\.nuget\\packages\\google.apis.core\\1.68.0\\google.apis.core.1.68.0.nupkg.sha512",
"C:\\Users\\Winnie\\.nuget\\packages\\google.apis.drive.v3\\1.68.0.3508\\google.apis.drive.v3.1.68.0.3508.nupkg.sha512",
"C:\\Users\\Winnie\\.nuget\\packages\\microsoft.bcl.asyncinterfaces\\6.0.0\\microsoft.bcl.asyncinterfaces.6.0.0.nupkg.sha512",
"C:\\Users\\Winnie\\.nuget\\packages\\microsoft.extensions.dependencyinjection.abstractions\\6.0.0\\microsoft.extensions.dependencyinjection.abstractions.6.0.0.nupkg.sha512",
"C:\\Users\\Winnie\\.nuget\\packages\\microsoft.extensions.dependencyinjection\\8.0.0\\microsoft.extensions.dependencyinjection.8.0.0.nupkg.sha512",
"C:\\Users\\Winnie\\.nuget\\packages\\microsoft.extensions.dependencyinjection.abstractions\\8.0.0\\microsoft.extensions.dependencyinjection.abstractions.8.0.0.nupkg.sha512",
"C:\\Users\\Winnie\\.nuget\\packages\\microsoft.netcore.platforms\\1.1.0\\microsoft.netcore.platforms.1.1.0.nupkg.sha512",
"C:\\Users\\Winnie\\.nuget\\packages\\microsoft.netcore.targets\\1.1.0\\microsoft.netcore.targets.1.1.0.nupkg.sha512",
"C:\\Users\\Winnie\\.nuget\\packages\\newtonsoft.json\\13.0.3\\newtonsoft.json.13.0.3.nupkg.sha512",
Expand Down

0 comments on commit bd98cf8

Please sign in to comment.